Skip to content

Commit a5dbfab

Browse files
committed
Fix double vendoring issue
1 parent 7a979b2 commit a5dbfab

File tree

4 files changed

+52
-16
lines changed

4 files changed

+52
-16
lines changed

configutils/configutils.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,13 @@ import (
2525
"k8s.io/client-go/tools/clientcmd"
2626
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
2727
"k8s.io/utils/pointer"
28+
ctrl "sigs.k8s.io/controller-runtime"
2829
)
2930

3031
var (
31-
kubeconfig string
32+
log = ctrl.Log.WithName("configutils")
3233
)
3334

34-
const (
35-
RecommendedConfigUsage = "Paths to a kubeconfig. Only required if out-of-cluster."
36-
)
37-
38-
func init() {
39-
// TODO: Fix this to allow double vendoring this library but still register flags on behalf of users
40-
flag.StringVar(&kubeconfig, clientcmd.RecommendedConfigPathFlag, "",
41-
RecommendedConfigUsage)
42-
}
43-
4435
// GetConfigOptions are options to supply for a GetConfig call.
4536
type GetConfigOptions struct {
4637
// Context is the kubeconfig context to load.
@@ -105,6 +96,15 @@ func loadConfigWithContext(apiServerURL string, loader clientcmd.ClientConfigLoa
10596
// test the precedence of loading the config.
10697
var loadInClusterConfig = rest.InClusterConfig
10798

99+
func getKubeconfigFlag() string {
100+
f := flag.CommandLine.Lookup("kubeconfig")
101+
if f == nil {
102+
panic(fmt.Sprintf("--kubeconfig flag is not defined"))
103+
}
104+
105+
return f.Value.String()
106+
}
107+
108108
// GetConfig creates a *rest.Config for talking to a Kubernetes API server.
109109
// Kubeconfig / the '--kubeconfig' flag instruct to use the kubeconfig file at that location.
110110
// Otherwise, will assume running in cluster and use the cluster provided kubeconfig.
@@ -125,9 +125,11 @@ func GetConfig(opts ...GetConfigOption) (*rest.Config, error) {
125125
o := &GetConfigOptions{}
126126
o.ApplyOptions(opts)
127127

128-
kubeconfig := kubeconfig
128+
var kubeconfig string
129129
if o.Kubeconfig != nil {
130130
kubeconfig = *o.Kubeconfig
131+
} else {
132+
kubeconfig = getKubeconfigFlag()
131133
}
132134

133135
// If a flag is specified with the config location, use that
@@ -155,3 +157,17 @@ func GetConfig(opts ...GetConfigOption) (*rest.Config, error) {
155157

156158
return loadConfigWithContext("", loadingRules, o.Context)
157159
}
160+
161+
// GetConfigOrDie creates a *rest.Config for talking to a Kubernetes apiserver.
162+
// If Kubeconfig / --kubeconfig is set, will use the kubeconfig file at that location. Otherwise, will assume running
163+
// in cluster and use the cluster provided kubeconfig.
164+
//
165+
// Will log an error and exit if there is an error creating the rest.Config.
166+
func GetConfigOrDie(opts ...GetConfigOption) *rest.Config {
167+
config, err := GetConfig(opts...)
168+
if err != nil {
169+
log.Error(err, "unable to get kubeconfig")
170+
os.Exit(1)
171+
}
172+
return config
173+
}

configutils/configutils_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package configutils
1616

1717
import (
18+
"flag"
1819
"os"
1920

2021
"github.com/onsi/ginkgo/v2"
@@ -25,6 +26,10 @@ import (
2526
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
2627
)
2728

29+
func setKubeconfigFlag(kubeconfig string) {
30+
ExpectWithOffset(1, flag.CommandLine.Set("kubeconfig", kubeconfig)).To(Succeed())
31+
}
32+
2833
var _ = ginkgo.Describe("Configutils", func() {
2934
ginkgo.Describe("GetConfig", func() {
3035
var (
@@ -76,7 +81,7 @@ var _ = ginkgo.Describe("Configutils", func() {
7681
}()
7782

7883
Expect(clientcmd.WriteToFile(*apiConfig, testFile.Name())).To(Succeed())
79-
kubeconfig = testFile.Name()
84+
setKubeconfigFlag(testFile.Name())
8085

8186
loaded, err := GetConfig()
8287
Expect(err).NotTo(HaveOccurred())
@@ -105,15 +110,15 @@ var _ = ginkgo.Describe("Configutils", func() {
105110
}()
106111

107112
Expect(clientcmd.WriteToFile(*apiConfig, testFile.Name())).To(Succeed())
108-
kubeconfig = testFile.Name()
113+
setKubeconfigFlag(testFile.Name())
109114

110115
loaded, err := GetConfig(Context("other"))
111116
Expect(err).NotTo(HaveOccurred())
112117
Expect(loaded).To(Equal(otherConfig))
113118
})
114119

115120
ginkgo.It("should error if the kubeconfig does not exist", func() {
116-
kubeconfig = "should definitely not exist - ever"
121+
setKubeconfigFlag("should definitely not exist - ever")
117122
_, err := GetConfig()
118123
Expect(err).To(HaveOccurred())
119124
})

go.mod

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.17
55
require (
66
github.com/golang/mock v1.6.0
77
github.com/google/addlicense v1.0.0
8-
github.com/onsi/ginkgo v1.16.5
98
github.com/onsi/ginkgo/v2 v2.1.3
109
github.com/onsi/gomega v1.18.1
1110
github.com/spf13/pflag v1.0.5
@@ -23,29 +22,39 @@ require (
2322
require (
2423
github.com/PuerkitoBio/purell v1.1.1 // indirect
2524
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
25+
github.com/beorn7/perks v1.0.1 // indirect
2626
github.com/bmatcuk/doublestar/v4 v4.0.2 // indirect
27+
github.com/cespare/xxhash/v2 v2.1.1 // indirect
2728
github.com/davecgh/go-spew v1.1.1 // indirect
2829
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
30+
github.com/fsnotify/fsnotify v1.5.1 // indirect
2931
github.com/go-errors/errors v1.0.1 // indirect
3032
github.com/go-logr/logr v1.2.0 // indirect
3133
github.com/go-openapi/jsonpointer v0.19.5 // indirect
3234
github.com/go-openapi/jsonreference v0.19.5 // indirect
3335
github.com/go-openapi/swag v0.19.14 // indirect
3436
github.com/gogo/protobuf v1.3.2 // indirect
37+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
3538
github.com/golang/protobuf v1.5.2 // indirect
3639
github.com/google/go-cmp v0.5.5 // indirect
3740
github.com/google/gofuzz v1.1.0 // indirect
3841
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
42+
github.com/google/uuid v1.1.2 // indirect
3943
github.com/googleapis/gnostic v0.5.5 // indirect
4044
github.com/imdario/mergo v0.3.12 // indirect
4145
github.com/josharian/intern v1.0.0 // indirect
4246
github.com/json-iterator/go v1.1.12 // indirect
4347
github.com/mailru/easyjson v0.7.6 // indirect
48+
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
4449
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4550
github.com/modern-go/reflect2 v1.0.2 // indirect
4651
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
4752
github.com/pkg/errors v0.9.1 // indirect
4853
github.com/pmezard/go-difflib v1.0.0 // indirect
54+
github.com/prometheus/client_golang v1.11.0 // indirect
55+
github.com/prometheus/client_model v0.2.0 // indirect
56+
github.com/prometheus/common v0.28.0 // indirect
57+
github.com/prometheus/procfs v0.6.0 // indirect
4958
github.com/stretchr/objx v0.2.0 // indirect
5059
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
5160
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
@@ -59,11 +68,13 @@ require (
5968
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
6069
golang.org/x/tools v0.1.6 // indirect
6170
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
71+
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
6272
google.golang.org/appengine v1.6.7 // indirect
6373
google.golang.org/protobuf v1.27.1 // indirect
6474
gopkg.in/inf.v0 v0.9.1 // indirect
6575
gopkg.in/yaml.v2 v2.4.0 // indirect
6676
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
77+
k8s.io/component-base v0.23.4 // indirect
6778
k8s.io/klog/v2 v2.30.0 // indirect
6879
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
6980
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4er
179179
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
180180
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
181181
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
182+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
182183
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
183184
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
184185
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
@@ -249,6 +250,7 @@ github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm4
249250
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
250251
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
251252
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
253+
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
252254
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
253255
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
254256
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
@@ -513,6 +515,7 @@ go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
513515
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
514516
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
515517
go.uber.org/goleak v1.1.11-0.20210813005559-691160354723/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
518+
go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA=
516519
go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
517520
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
518521
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
@@ -969,6 +972,7 @@ k8s.io/client-go v0.23.4/go.mod h1:PKnIL4pqLuvYUK1WU7RLTMYKPiIh7MYShLshtRY9cj0=
969972
k8s.io/code-generator v0.23.0/go.mod h1:vQvOhDXhuzqiVfM/YHp+dmg10WDZCchJVObc9MvowsE=
970973
k8s.io/code-generator v0.23.4/go.mod h1:S0Q1JVA+kSzTI1oUvbKAxZY/DYbA/ZUb4Uknog12ETk=
971974
k8s.io/component-base v0.23.0/go.mod h1:DHH5uiFvLC1edCpvcTDV++NKULdYYU6pR9Tt3HIKMKI=
975+
k8s.io/component-base v0.23.4 h1:SziYh48+QKxK+ykJ3Ejqd98XdZIseVBG7sBaNLPqy6M=
972976
k8s.io/component-base v0.23.4/go.mod h1:8o3Gg8i2vnUXGPOwciiYlkSaZT+p+7gA9Scoz8y4W4E=
973977
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
974978
k8s.io/gengo v0.0.0-20210813121822-485abfe95c7c/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=

0 commit comments

Comments
 (0)