Skip to content

Commit d834885

Browse files
authored
Merge pull request kubernetes#74327 from neolit123/fix-join-phase
kubeadm/phases: use common interfaces for init and join phases
2 parents 272d78f + de5e17e commit d834885

26 files changed

+234
-176
lines changed

cmd/kubeadm/app/cmd/init.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ type initOptions struct {
8787
uploadCerts bool
8888
}
8989

90-
// initData defines all the runtime information used when running the kubeadm init workflow;
90+
// compile-time assert that the local data object satisfies the phases data interface.
91+
var _ phases.InitData = &initData{}
92+
93+
// initData defines all the runtime information used when running the kubeadm init worklow;
9194
// this data is shared across all the phases that are included in the workflow.
9295
type initData struct {
9396
cfg *kubeadmapi.InitConfiguration

cmd/kubeadm/app/cmd/join.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ type joinOptions struct {
129129
externalcfg *kubeadmapiv1beta1.JoinConfiguration
130130
}
131131

132+
// compile-time assert that the local data object satisfies the phases data interface.
133+
var _ phases.JoinData = &joinData{}
134+
132135
// joinData defines all the runtime information used when running the kubeadm join worklow;
133136
// this data is shared across all the phases that are included in the workflow.
134137
type joinData struct {

cmd/kubeadm/app/cmd/phases/init/BUILD

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ go_library(
77
"bootstraptoken.go",
88
"certs.go",
99
"controlplane.go",
10+
"data.go",
1011
"etcd.go",
1112
"kubeconfig.go",
1213
"kubelet.go",
@@ -58,7 +59,10 @@ go_library(
5859

5960
go_test(
6061
name = "go_default_test",
61-
srcs = ["certs_test.go"],
62+
srcs = [
63+
"certs_test.go",
64+
"data_test.go",
65+
],
6266
embed = [":go_default_library"],
6367
deps = [
6468
"//cmd/kubeadm/app/apis/kubeadm:go_default_library",
@@ -67,6 +71,8 @@ go_test(
6771
"//cmd/kubeadm/app/util/certs:go_default_library",
6872
"//cmd/kubeadm/app/util/pkiutil:go_default_library",
6973
"//cmd/kubeadm/test:go_default_library",
74+
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
75+
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
7076
"//vendor/github.com/spf13/cobra:go_default_library",
7177
],
7278
)

cmd/kubeadm/app/cmd/phases/init/addons.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ var (
4040
`)
4141
)
4242

43-
type addonData interface {
44-
Cfg() *kubeadmapi.InitConfiguration
45-
Client() (clientset.Interface, error)
46-
}
47-
4843
// NewAddonPhase returns the addon Cobra command
4944
func NewAddonPhase() workflow.Phase {
5045
return workflow.Phase{
@@ -76,8 +71,8 @@ func NewAddonPhase() workflow.Phase {
7671
}
7772
}
7873

79-
func getAddonData(c workflow.RunData) (*kubeadmapi.InitConfiguration, clientset.Interface, error) {
80-
data, ok := c.(addonData)
74+
func getInitData(c workflow.RunData) (*kubeadmapi.InitConfiguration, clientset.Interface, error) {
75+
data, ok := c.(InitData)
8176
if !ok {
8277
return nil, nil, errors.New("addon phase invoked with an invalid data struct")
8378
}
@@ -91,7 +86,7 @@ func getAddonData(c workflow.RunData) (*kubeadmapi.InitConfiguration, clientset.
9186

9287
// runCoreDNSAddon installs CoreDNS addon to a Kubernetes cluster
9388
func runCoreDNSAddon(c workflow.RunData) error {
94-
cfg, client, err := getAddonData(c)
89+
cfg, client, err := getInitData(c)
9590
if err != nil {
9691
return err
9792
}
@@ -100,7 +95,7 @@ func runCoreDNSAddon(c workflow.RunData) error {
10095

10196
// runKubeProxyAddon installs KubeProxy addon to a Kubernetes cluster
10297
func runKubeProxyAddon(c workflow.RunData) error {
103-
cfg, client, err := getAddonData(c)
98+
cfg, client, err := getInitData(c)
10499
if err != nil {
105100
return err
106101
}

cmd/kubeadm/app/cmd/phases/init/bootstraptoken.go

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

2222
"github.com/pkg/errors"
2323

24-
clientset "k8s.io/client-go/kubernetes"
25-
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
2624
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
2725
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
2826
clusterinfophase "k8s.io/kubernetes/cmd/kubeadm/app/phases/bootstraptoken/clusterinfo"
@@ -46,14 +44,6 @@ var (
4644
`)
4745
)
4846

49-
type bootstrapTokenData interface {
50-
Cfg() *kubeadmapi.InitConfiguration
51-
Client() (clientset.Interface, error)
52-
KubeConfigPath() string
53-
SkipTokenPrint() bool
54-
Tokens() []string
55-
}
56-
5747
// NewBootstrapTokenPhase returns the phase to bootstrapToken
5848
func NewBootstrapTokenPhase() workflow.Phase {
5949
return workflow.Phase{
@@ -72,7 +62,7 @@ func NewBootstrapTokenPhase() workflow.Phase {
7262
}
7363

7464
func runBootstrapToken(c workflow.RunData) error {
75-
data, ok := c.(bootstrapTokenData)
65+
data, ok := c.(InitData)
7666
if !ok {
7767
return errors.New("bootstrap-token phase invoked with an invalid data struct")
7868
}

cmd/kubeadm/app/cmd/phases/init/certs.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,6 @@ var (
5454
csrDir string
5555
)
5656

57-
// certsData defines the behavior that a runtime data struct passed to the certs phase should
58-
// have. Please note that we are using an interface in order to make this phase reusable in different workflows
59-
// (and thus with different runtime data struct, all of them requested to be compliant to this interface)
60-
type certsData interface {
61-
Cfg() *kubeadmapi.InitConfiguration
62-
ExternalCA() bool
63-
CertificateDir() string
64-
CertificateWriteDir() string
65-
}
66-
6757
// NewCertsPhase returns the phase for the certs
6858
func NewCertsPhase() workflow.Phase {
6959
return workflow.Phase{
@@ -193,7 +183,7 @@ func getSANDescription(certSpec *certsphase.KubeadmCert) string {
193183
}
194184

195185
func runCertsSa(c workflow.RunData) error {
196-
data, ok := c.(certsData)
186+
data, ok := c.(InitData)
197187
if !ok {
198188
return errors.New("certs phase invoked with an invalid data struct")
199189
}
@@ -209,7 +199,7 @@ func runCertsSa(c workflow.RunData) error {
209199
}
210200

211201
func runCerts(c workflow.RunData) error {
212-
data, ok := c.(certsData)
202+
data, ok := c.(InitData)
213203
if !ok {
214204
return errors.New("certs phase invoked with an invalid data struct")
215205
}
@@ -220,7 +210,7 @@ func runCerts(c workflow.RunData) error {
220210

221211
func runCAPhase(ca *certsphase.KubeadmCert) func(c workflow.RunData) error {
222212
return func(c workflow.RunData) error {
223-
data, ok := c.(certsData)
213+
data, ok := c.(InitData)
224214
if !ok {
225215
return errors.New("certs phase invoked with an invalid data struct")
226216
}
@@ -252,7 +242,7 @@ func runCAPhase(ca *certsphase.KubeadmCert) func(c workflow.RunData) error {
252242

253243
func runCertPhase(cert *certsphase.KubeadmCert, caCert *certsphase.KubeadmCert) func(c workflow.RunData) error {
254244
return func(c workflow.RunData) error {
255-
data, ok := c.(certsData)
245+
data, ok := c.(InitData)
256246
if !ok {
257247
return errors.New("certs phase invoked with an invalid data struct")
258248
}

cmd/kubeadm/app/cmd/phases/init/certs_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
)
3131

3232
type testCertsData struct {
33+
testInitData
3334
cfg *kubeadmapi.InitConfiguration
3435
}
3536

cmd/kubeadm/app/cmd/phases/init/controlplane.go

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

2222
"github.com/pkg/errors"
2323

24-
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
2524
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
2625
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
2726
cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
@@ -59,12 +58,6 @@ var (
5958
}
6059
)
6160

62-
type controlPlaneData interface {
63-
Cfg() *kubeadmapi.InitConfiguration
64-
KubeConfigDir() string
65-
ManifestDir() string
66-
}
67-
6861
func getPhaseDescription(component string) string {
6962
return fmt.Sprintf("Generates the %s static Pod manifest", component)
7063
}
@@ -133,7 +126,7 @@ func getControlPlanePhaseFlags(name string) []string {
133126
}
134127

135128
func runControlPlanePhase(c workflow.RunData) error {
136-
data, ok := c.(controlPlaneData)
129+
data, ok := c.(InitData)
137130
if !ok {
138131
return errors.New("control-plane phase invoked with an invalid data struct")
139132
}
@@ -144,7 +137,7 @@ func runControlPlanePhase(c workflow.RunData) error {
144137

145138
func runControlPlaneSubphase(component string) func(c workflow.RunData) error {
146139
return func(c workflow.RunData) error {
147-
data, ok := c.(controlPlaneData)
140+
data, ok := c.(InitData)
148141
if !ok {
149142
return errors.New("control-plane phase invoked with an invalid data struct")
150143
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package phases
18+
19+
import (
20+
"io"
21+
22+
"k8s.io/apimachinery/pkg/util/sets"
23+
clientset "k8s.io/client-go/kubernetes"
24+
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
25+
)
26+
27+
// InitData is the interface to use for init phases.
28+
// The "initData" type from "cmd/init.go" must satisfy this interface.
29+
type InitData interface {
30+
UploadCerts() bool
31+
CertificateKey() string
32+
SetCertificateKey(key string)
33+
Cfg() *kubeadmapi.InitConfiguration
34+
DryRun() bool
35+
SkipTokenPrint() bool
36+
IgnorePreflightErrors() sets.String
37+
CertificateWriteDir() string
38+
CertificateDir() string
39+
KubeConfigDir() string
40+
KubeConfigPath() string
41+
ManifestDir() string
42+
KubeletDir() string
43+
ExternalCA() bool
44+
OutputWriter() io.Writer
45+
Client() (clientset.Interface, error)
46+
Tokens() []string
47+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package phases
18+
19+
import (
20+
"io"
21+
22+
"k8s.io/apimachinery/pkg/util/sets"
23+
clientset "k8s.io/client-go/kubernetes"
24+
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
25+
)
26+
27+
// a package local type for testing purposes.
28+
type testInitData struct{}
29+
30+
// testInitData must satisfy InitData.
31+
var _ InitData = &testInitData{}
32+
33+
func (t *testInitData) UploadCerts() bool { return false }
34+
func (t *testInitData) CertificateKey() string { return "" }
35+
func (t *testInitData) SetCertificateKey(key string) {}
36+
func (t *testInitData) Cfg() *kubeadmapi.InitConfiguration { return nil }
37+
func (t *testInitData) DryRun() bool { return false }
38+
func (t *testInitData) SkipTokenPrint() bool { return false }
39+
func (t *testInitData) IgnorePreflightErrors() sets.String { return nil }
40+
func (t *testInitData) CertificateWriteDir() string { return "" }
41+
func (t *testInitData) CertificateDir() string { return "" }
42+
func (t *testInitData) KubeConfigDir() string { return "" }
43+
func (t *testInitData) KubeConfigPath() string { return "" }
44+
func (t *testInitData) ManifestDir() string { return "" }
45+
func (t *testInitData) KubeletDir() string { return "" }
46+
func (t *testInitData) ExternalCA() bool { return false }
47+
func (t *testInitData) OutputWriter() io.Writer { return nil }
48+
func (t *testInitData) Client() (clientset.Interface, error) { return nil, nil }
49+
func (t *testInitData) Tokens() []string { return nil }

0 commit comments

Comments
 (0)