Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kubeadm/phases: use common interfaces for init and join phases #74327

Merged
merged 1 commit into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion cmd/kubeadm/app/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ type initOptions struct {
uploadCerts bool
}

// initData defines all the runtime information used when running the kubeadm init workflow;
// compile-time assert that the local data object satisfies the phases data interface.
var _ phases.InitData = &initData{}

// initData defines all the runtime information used when running the kubeadm init worklow;
// this data is shared across all the phases that are included in the workflow.
type initData struct {
cfg *kubeadmapi.InitConfiguration
Expand Down
3 changes: 3 additions & 0 deletions cmd/kubeadm/app/cmd/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ type joinOptions struct {
externalcfg *kubeadmapiv1beta1.JoinConfiguration
}

// compile-time assert that the local data object satisfies the phases data interface.
var _ phases.JoinData = &joinData{}

// joinData defines all the runtime information used when running the kubeadm join worklow;
// this data is shared across all the phases that are included in the workflow.
type joinData struct {
Expand Down
8 changes: 7 additions & 1 deletion cmd/kubeadm/app/cmd/phases/init/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ go_library(
"bootstraptoken.go",
"certs.go",
"controlplane.go",
"data.go",
"etcd.go",
"kubeconfig.go",
"kubelet.go",
Expand Down Expand Up @@ -58,7 +59,10 @@ go_library(

go_test(
name = "go_default_test",
srcs = ["certs_test.go"],
srcs = [
"certs_test.go",
"data_test.go",
],
embed = [":go_default_library"],
deps = [
"//cmd/kubeadm/app/apis/kubeadm:go_default_library",
Expand All @@ -67,6 +71,8 @@ go_test(
"//cmd/kubeadm/app/util/certs:go_default_library",
"//cmd/kubeadm/app/util/pkiutil:go_default_library",
"//cmd/kubeadm/test:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
"//vendor/github.com/spf13/cobra:go_default_library",
],
)
Expand Down
13 changes: 4 additions & 9 deletions cmd/kubeadm/app/cmd/phases/init/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ var (
`)
)

type addonData interface {
Cfg() *kubeadmapi.InitConfiguration
Client() (clientset.Interface, error)
}

// NewAddonPhase returns the addon Cobra command
func NewAddonPhase() workflow.Phase {
return workflow.Phase{
Expand Down Expand Up @@ -76,8 +71,8 @@ func NewAddonPhase() workflow.Phase {
}
}

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

// runCoreDNSAddon installs CoreDNS addon to a Kubernetes cluster
func runCoreDNSAddon(c workflow.RunData) error {
cfg, client, err := getAddonData(c)
cfg, client, err := getInitData(c)
if err != nil {
return err
}
Expand All @@ -100,7 +95,7 @@ func runCoreDNSAddon(c workflow.RunData) error {

// runKubeProxyAddon installs KubeProxy addon to a Kubernetes cluster
func runKubeProxyAddon(c workflow.RunData) error {
cfg, client, err := getAddonData(c)
cfg, client, err := getInitData(c)
if err != nil {
return err
}
Expand Down
12 changes: 1 addition & 11 deletions cmd/kubeadm/app/cmd/phases/init/bootstraptoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (

"github.com/pkg/errors"

clientset "k8s.io/client-go/kubernetes"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
clusterinfophase "k8s.io/kubernetes/cmd/kubeadm/app/phases/bootstraptoken/clusterinfo"
Expand All @@ -46,14 +44,6 @@ var (
`)
)

type bootstrapTokenData interface {
Cfg() *kubeadmapi.InitConfiguration
Client() (clientset.Interface, error)
KubeConfigPath() string
SkipTokenPrint() bool
Tokens() []string
}

// NewBootstrapTokenPhase returns the phase to bootstrapToken
func NewBootstrapTokenPhase() workflow.Phase {
return workflow.Phase{
Expand All @@ -72,7 +62,7 @@ func NewBootstrapTokenPhase() workflow.Phase {
}

func runBootstrapToken(c workflow.RunData) error {
data, ok := c.(bootstrapTokenData)
data, ok := c.(InitData)
if !ok {
return errors.New("bootstrap-token phase invoked with an invalid data struct")
}
Expand Down
18 changes: 4 additions & 14 deletions cmd/kubeadm/app/cmd/phases/init/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,6 @@ var (
csrDir string
)

// certsData defines the behavior that a runtime data struct passed to the certs phase should
// have. Please note that we are using an interface in order to make this phase reusable in different workflows
// (and thus with different runtime data struct, all of them requested to be compliant to this interface)
type certsData interface {
Cfg() *kubeadmapi.InitConfiguration
ExternalCA() bool
CertificateDir() string
CertificateWriteDir() string
}

// NewCertsPhase returns the phase for the certs
func NewCertsPhase() workflow.Phase {
return workflow.Phase{
Expand Down Expand Up @@ -193,7 +183,7 @@ func getSANDescription(certSpec *certsphase.KubeadmCert) string {
}

func runCertsSa(c workflow.RunData) error {
data, ok := c.(certsData)
data, ok := c.(InitData)
if !ok {
return errors.New("certs phase invoked with an invalid data struct")
}
Expand All @@ -209,7 +199,7 @@ func runCertsSa(c workflow.RunData) error {
}

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

func runCAPhase(ca *certsphase.KubeadmCert) func(c workflow.RunData) error {
return func(c workflow.RunData) error {
data, ok := c.(certsData)
data, ok := c.(InitData)
if !ok {
return errors.New("certs phase invoked with an invalid data struct")
}
Expand Down Expand Up @@ -252,7 +242,7 @@ func runCAPhase(ca *certsphase.KubeadmCert) func(c workflow.RunData) error {

func runCertPhase(cert *certsphase.KubeadmCert, caCert *certsphase.KubeadmCert) func(c workflow.RunData) error {
return func(c workflow.RunData) error {
data, ok := c.(certsData)
data, ok := c.(InitData)
if !ok {
return errors.New("certs phase invoked with an invalid data struct")
}
Expand Down
1 change: 1 addition & 0 deletions cmd/kubeadm/app/cmd/phases/init/certs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
)

type testCertsData struct {
testInitData
cfg *kubeadmapi.InitConfiguration
}

Expand Down
11 changes: 2 additions & 9 deletions cmd/kubeadm/app/cmd/phases/init/controlplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/pkg/errors"

kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
Expand Down Expand Up @@ -59,12 +58,6 @@ var (
}
)

type controlPlaneData interface {
Cfg() *kubeadmapi.InitConfiguration
KubeConfigDir() string
ManifestDir() string
}

func getPhaseDescription(component string) string {
return fmt.Sprintf("Generates the %s static Pod manifest", component)
}
Expand Down Expand Up @@ -133,7 +126,7 @@ func getControlPlanePhaseFlags(name string) []string {
}

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

func runControlPlaneSubphase(component string) func(c workflow.RunData) error {
return func(c workflow.RunData) error {
data, ok := c.(controlPlaneData)
data, ok := c.(InitData)
if !ok {
return errors.New("control-plane phase invoked with an invalid data struct")
}
Expand Down
47 changes: 47 additions & 0 deletions cmd/kubeadm/app/cmd/phases/init/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package phases

import (
"io"

"k8s.io/apimachinery/pkg/util/sets"
clientset "k8s.io/client-go/kubernetes"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
)

// InitData is the interface to use for init phases.
// The "initData" type from "cmd/init.go" must satisfy this interface.
type InitData interface {
UploadCerts() bool
CertificateKey() string
SetCertificateKey(key string)
Cfg() *kubeadmapi.InitConfiguration
DryRun() bool
SkipTokenPrint() bool
IgnorePreflightErrors() sets.String
CertificateWriteDir() string
CertificateDir() string
KubeConfigDir() string
KubeConfigPath() string
ManifestDir() string
KubeletDir() string
ExternalCA() bool
OutputWriter() io.Writer
Client() (clientset.Interface, error)
Tokens() []string
}
49 changes: 49 additions & 0 deletions cmd/kubeadm/app/cmd/phases/init/data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package phases

import (
"io"

"k8s.io/apimachinery/pkg/util/sets"
clientset "k8s.io/client-go/kubernetes"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
)

// a package local type for testing purposes.
type testInitData struct{}

// testInitData must satisfy InitData.
var _ InitData = &testInitData{}

func (t *testInitData) UploadCerts() bool { return false }
func (t *testInitData) CertificateKey() string { return "" }
func (t *testInitData) SetCertificateKey(key string) {}
func (t *testInitData) Cfg() *kubeadmapi.InitConfiguration { return nil }
func (t *testInitData) DryRun() bool { return false }
func (t *testInitData) SkipTokenPrint() bool { return false }
func (t *testInitData) IgnorePreflightErrors() sets.String { return nil }
func (t *testInitData) CertificateWriteDir() string { return "" }
func (t *testInitData) CertificateDir() string { return "" }
func (t *testInitData) KubeConfigDir() string { return "" }
func (t *testInitData) KubeConfigPath() string { return "" }
func (t *testInitData) ManifestDir() string { return "" }
func (t *testInitData) KubeletDir() string { return "" }
func (t *testInitData) ExternalCA() bool { return false }
func (t *testInitData) OutputWriter() io.Writer { return nil }
func (t *testInitData) Client() (clientset.Interface, error) { return nil, nil }
func (t *testInitData) Tokens() []string { return nil }
9 changes: 1 addition & 8 deletions cmd/kubeadm/app/cmd/phases/init/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"github.com/pkg/errors"
"k8s.io/klog"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
Expand All @@ -42,12 +41,6 @@ var (
`)
)

type etcdData interface {
Cfg() *kubeadmapi.InitConfiguration
DryRun() bool
ManifestDir() string
}

// NewEtcdPhase creates a kubeadm workflow phase that implements handling of etcd.
func NewEtcdPhase() workflow.Phase {
phase := workflow.Phase{
Expand Down Expand Up @@ -83,7 +76,7 @@ func getEtcdPhaseFlags() []string {

func runEtcdPhaseLocal() func(c workflow.RunData) error {
return func(c workflow.RunData) error {
data, ok := c.(etcdData)
data, ok := c.(InitData)
if !ok {
return errors.New("etcd phase invoked with an invalid data struct")
}
Expand Down
16 changes: 2 additions & 14 deletions cmd/kubeadm/app/cmd/phases/init/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"

"github.com/pkg/errors"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
Expand Down Expand Up @@ -62,17 +61,6 @@ var (
}
)

// kubeConfigData defines the behavior that a runtime data struct passed to the kubeconfig phase
// should have. Please note that we are using an interface in order to make this phase reusable in different workflows
// (and thus with different runtime data struct, all of them requested to be compliant to this interface)
type kubeConfigData interface {
Cfg() *kubeadmapi.InitConfiguration
ExternalCA() bool
CertificateDir() string
CertificateWriteDir() string
KubeConfigDir() string
}

// NewKubeConfigPhase creates a kubeadm workflow phase that creates all kubeconfig files necessary to establish the control plane and the admin kubeconfig file.
func NewKubeConfigPhase() workflow.Phase {
return workflow.Phase{
Expand Down Expand Up @@ -123,7 +111,7 @@ func getKubeConfigPhaseFlags(name string) []string {
}

func runKubeConfig(c workflow.RunData) error {
data, ok := c.(kubeConfigData)
data, ok := c.(InitData)
if !ok {
return errors.New("kubeconfig phase invoked with an invalid data struct")
}
Expand All @@ -135,7 +123,7 @@ func runKubeConfig(c workflow.RunData) error {
// runKubeConfigFile executes kubeconfig creation logic.
func runKubeConfigFile(kubeConfigFileName string) func(workflow.RunData) error {
return func(c workflow.RunData) error {
data, ok := c.(kubeConfigData)
data, ok := c.(InitData)
if !ok {
return errors.New("kubeconfig phase invoked with an invalid data struct")
}
Expand Down