Skip to content

Commit fc30b38

Browse files
authored
Merge pull request kubernetes#74756 from fabriziopandini/cleanup-join-phases
Cleanup join data struct
2 parents 646145f + 4c27d6a commit fc30b38

File tree

6 files changed

+31
-41
lines changed

6 files changed

+31
-41
lines changed

cmd/kubeadm/app/cmd/join.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ type joinData struct {
140140
skipTokenPrint bool
141141
initCfg *kubeadmapi.InitConfiguration
142142
tlsBootstrapCfg *clientcmdapi.Config
143-
clientSets map[string]*clientset.Clientset
143+
clientSet *clientset.Clientset
144144
ignorePreflightErrors sets.String
145145
outputWriter io.Writer
146146
certificateKey string
@@ -178,7 +178,7 @@ func NewCmdJoin(out io.Writer, joinOptions *joinOptions) *cobra.Command {
178178
}
179179

180180
ctx := map[string]string{
181-
"KubeConfigPath": data.KubeConfigPath(),
181+
"KubeConfigPath": kubeadmconstants.GetAdminKubeConfigPath(),
182182
"etcdMessage": etcdMessage,
183183
}
184184
joinControPlaneDoneTemp.Execute(data.outputWriter, ctx)
@@ -385,7 +385,6 @@ func newJoinData(cmd *cobra.Command, args []string, options *joinOptions, out io
385385

386386
return &joinData{
387387
cfg: cfg,
388-
clientSets: map[string]*clientset.Clientset{},
389388
ignorePreflightErrors: ignorePreflightErrorsSet,
390389
outputWriter: out,
391390
certificateKey: options.certificateKey,
@@ -402,11 +401,6 @@ func (j *joinData) Cfg() *kubeadmapi.JoinConfiguration {
402401
return j.cfg
403402
}
404403

405-
// KubeConfigPath returns the default kubeconfig path.
406-
func (j *joinData) KubeConfigPath() string {
407-
return kubeadmconstants.GetAdminKubeConfigPath()
408-
}
409-
410404
// TLSBootstrapCfg returns the cluster-info (kubeconfig).
411405
func (j *joinData) TLSBootstrapCfg() (*clientcmdapi.Config, error) {
412406
if j.tlsBootstrapCfg != nil {
@@ -432,15 +426,17 @@ func (j *joinData) InitCfg() (*kubeadmapi.InitConfiguration, error) {
432426
return initCfg, err
433427
}
434428

435-
func (j *joinData) ClientSetFromFile(path string) (*clientset.Clientset, error) {
436-
if client, ok := j.clientSets[path]; ok {
437-
return client, nil
429+
// ClientSet returns the ClientSet for accessing the cluster with the identity defined in admin.conf.
430+
func (j *joinData) ClientSet() (*clientset.Clientset, error) {
431+
if j.clientSet != nil {
432+
return j.clientSet, nil
438433
}
434+
path := kubeadmconstants.GetAdminKubeConfigPath()
439435
client, err := kubeconfigutil.ClientSetFromFile(path)
440436
if err != nil {
441437
return nil, errors.Wrap(err, "[join] couldn't create Kubernetes client")
442438
}
443-
j.clientSets[path] = client
439+
j.clientSet = client
444440
return client, nil
445441
}
446442

cmd/kubeadm/app/cmd/phases/join/checketcd.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"github.com/pkg/errors"
2323
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
24-
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
2524
etcdphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/etcd"
2625
)
2726

@@ -61,7 +60,7 @@ func runCheckEtcdPhase(c workflow.RunData) error {
6160
// Checks that the etcd cluster is healthy
6261
// NB. this check cannot be implemented before because it requires the admin.conf and all the certificates
6362
// for connecting to etcd already in place
64-
client, err := data.ClientSetFromFile(kubeadmconstants.GetAdminKubeConfigPath())
63+
client, err := data.ClientSet()
6564
if err != nil {
6665
return err
6766
}

cmd/kubeadm/app/cmd/phases/join/controlplanejoin.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,8 @@ func runEtcdPhase(c workflow.RunData) error {
111111
return nil
112112
}
113113

114-
kubeConfigFile := data.KubeConfigPath()
115-
116-
client, err := data.ClientSetFromFile(kubeConfigFile)
114+
// gets access to the cluster using the identity defined in admin.conf
115+
client, err := data.ClientSet()
117116
if err != nil {
118117
return errors.Wrap(err, "couldn't create Kubernetes client")
119118
}
@@ -153,9 +152,8 @@ func runUploadConfigPhase(c workflow.RunData) error {
153152
return nil
154153
}
155154

156-
kubeConfigFile := data.KubeConfigPath()
157-
158-
client, err := data.ClientSetFromFile(kubeConfigFile)
155+
// gets access to the cluster using the identity defined in admin.conf
156+
client, err := data.ClientSet()
159157
if err != nil {
160158
return errors.Wrap(err, "couldn't create Kubernetes client")
161159
}
@@ -181,9 +179,8 @@ func runMarkControlPlanePhase(c workflow.RunData) error {
181179
return nil
182180
}
183181

184-
kubeConfigFile := data.KubeConfigPath()
185-
186-
client, err := data.ClientSetFromFile(kubeConfigFile)
182+
// gets access to the cluster using the identity defined in admin.conf
183+
client, err := data.ClientSet()
187184
if err != nil {
188185
return errors.Wrap(err, "couldn't create Kubernetes client")
189186
}

cmd/kubeadm/app/cmd/phases/join/data.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ import (
3030
type JoinData interface {
3131
CertificateKey() string
3232
Cfg() *kubeadmapi.JoinConfiguration
33-
KubeConfigPath() string
3433
TLSBootstrapCfg() (*clientcmdapi.Config, error)
3534
InitCfg() (*kubeadmapi.InitConfiguration, error)
36-
ClientSetFromFile(path string) (*clientset.Clientset, error)
35+
ClientSet() (*clientset.Clientset, error)
3736
IgnorePreflightErrors() sets.String
3837
OutputWriter() io.Writer
3938
}

cmd/kubeadm/app/cmd/phases/join/data_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ type testJoinData struct{}
3131
// testJoinData must satisfy JoinData.
3232
var _ JoinData = &testJoinData{}
3333

34-
func (j *testJoinData) CertificateKey() string { return "" }
35-
func (j *testJoinData) Cfg() *kubeadmapi.JoinConfiguration { return nil }
36-
func (j *testJoinData) KubeConfigPath() string { return "" }
37-
func (j *testJoinData) TLSBootstrapCfg() (*clientcmdapi.Config, error) { return nil, nil }
38-
func (j *testJoinData) InitCfg() (*kubeadmapi.InitConfiguration, error) { return nil, nil }
39-
func (j *testJoinData) ClientSetFromFile(path string) (*clientset.Clientset, error) { return nil, nil }
40-
func (j *testJoinData) IgnorePreflightErrors() sets.String { return nil }
41-
func (j *testJoinData) OutputWriter() io.Writer { return nil }
34+
func (j *testJoinData) CertificateKey() string { return "" }
35+
func (j *testJoinData) Cfg() *kubeadmapi.JoinConfiguration { return nil }
36+
func (j *testJoinData) TLSBootstrapCfg() (*clientcmdapi.Config, error) { return nil, nil }
37+
func (j *testJoinData) InitCfg() (*kubeadmapi.InitConfiguration, error) { return nil, nil }
38+
func (j *testJoinData) ClientSet() (*clientset.Clientset, error) { return nil, nil }
39+
func (j *testJoinData) IgnorePreflightErrors() sets.String { return nil }
40+
func (j *testJoinData) OutputWriter() io.Writer { return nil }

cmd/kubeadm/app/cmd/phases/join/kubelet.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,28 @@ func NewKubeletStartPhase() workflow.Phase {
7474
}
7575
}
7676

77-
func getKubeletStartJoinData(c workflow.RunData) (JoinData, *kubeadmapi.JoinConfiguration, *kubeadmapi.InitConfiguration, *clientcmdapi.Config, error) {
77+
func getKubeletStartJoinData(c workflow.RunData) (*kubeadmapi.JoinConfiguration, *kubeadmapi.InitConfiguration, *clientcmdapi.Config, error) {
7878
data, ok := c.(JoinData)
7979
if !ok {
80-
return nil, nil, nil, nil, errors.New("kubelet-start phase invoked with an invalid data struct")
80+
return nil, nil, nil, errors.New("kubelet-start phase invoked with an invalid data struct")
8181
}
8282
cfg := data.Cfg()
8383
initCfg, err := data.InitCfg()
8484
if err != nil {
85-
return nil, nil, nil, nil, err
85+
return nil, nil, nil, err
8686
}
8787
tlsBootstrapCfg, err := data.TLSBootstrapCfg()
8888
if err != nil {
89-
return nil, nil, nil, nil, err
89+
return nil, nil, nil, err
9090
}
91-
return data, cfg, initCfg, tlsBootstrapCfg, nil
91+
return cfg, initCfg, tlsBootstrapCfg, nil
9292
}
9393

9494
// runKubeletStartJoinPhase executes the kubelet TLS bootstrap process.
9595
// This process is executed by the kubelet and completes with the node joining the cluster
9696
// with a dedicates set of credentials as required by the node authorizer
9797
func runKubeletStartJoinPhase(c workflow.RunData) error {
98-
data, cfg, initCfg, tlsBootstrapCfg, err := getKubeletStartJoinData(c)
98+
cfg, initCfg, tlsBootstrapCfg, err := getKubeletStartJoinData(c)
9999
if err != nil {
100100
return err
101101
}
@@ -120,7 +120,7 @@ func runKubeletStartJoinPhase(c workflow.RunData) error {
120120
return err
121121
}
122122

123-
bootstrapClient, err := data.ClientSetFromFile(bootstrapKubeConfigFile)
123+
bootstrapClient, err := kubeconfigutil.ClientSetFromFile(bootstrapKubeConfigFile)
124124
if err != nil {
125125
return errors.Errorf("couldn't create client from kubeconfig file %q", bootstrapKubeConfigFile)
126126
}
@@ -157,7 +157,7 @@ func runKubeletStartJoinPhase(c workflow.RunData) error {
157157
}
158158

159159
// When we know the /etc/kubernetes/kubelet.conf file is available, get the client
160-
client, err := data.ClientSetFromFile(kubeadmconstants.GetKubeletKubeConfigPath())
160+
client, err := kubeconfigutil.ClientSetFromFile(kubeadmconstants.GetKubeletKubeConfigPath())
161161
if err != nil {
162162
return err
163163
}

0 commit comments

Comments
 (0)