cleanup-join-phases

pull/564/head
fabriziopandini 2019-02-28 23:37:25 +01:00
parent 83fc13e640
commit 4c27d6a213
6 changed files with 31 additions and 41 deletions

View File

@ -140,7 +140,7 @@ type joinData struct {
skipTokenPrint bool skipTokenPrint bool
initCfg *kubeadmapi.InitConfiguration initCfg *kubeadmapi.InitConfiguration
tlsBootstrapCfg *clientcmdapi.Config tlsBootstrapCfg *clientcmdapi.Config
clientSets map[string]*clientset.Clientset clientSet *clientset.Clientset
ignorePreflightErrors sets.String ignorePreflightErrors sets.String
outputWriter io.Writer outputWriter io.Writer
certificateKey string certificateKey string
@ -178,7 +178,7 @@ func NewCmdJoin(out io.Writer, joinOptions *joinOptions) *cobra.Command {
} }
ctx := map[string]string{ ctx := map[string]string{
"KubeConfigPath": data.KubeConfigPath(), "KubeConfigPath": kubeadmconstants.GetAdminKubeConfigPath(),
"etcdMessage": etcdMessage, "etcdMessage": etcdMessage,
} }
joinControPlaneDoneTemp.Execute(data.outputWriter, ctx) joinControPlaneDoneTemp.Execute(data.outputWriter, ctx)
@ -385,7 +385,6 @@ func newJoinData(cmd *cobra.Command, args []string, options *joinOptions, out io
return &joinData{ return &joinData{
cfg: cfg, cfg: cfg,
clientSets: map[string]*clientset.Clientset{},
ignorePreflightErrors: ignorePreflightErrorsSet, ignorePreflightErrors: ignorePreflightErrorsSet,
outputWriter: out, outputWriter: out,
certificateKey: options.certificateKey, certificateKey: options.certificateKey,
@ -402,11 +401,6 @@ func (j *joinData) Cfg() *kubeadmapi.JoinConfiguration {
return j.cfg return j.cfg
} }
// KubeConfigPath returns the default kubeconfig path.
func (j *joinData) KubeConfigPath() string {
return kubeadmconstants.GetAdminKubeConfigPath()
}
// TLSBootstrapCfg returns the cluster-info (kubeconfig). // TLSBootstrapCfg returns the cluster-info (kubeconfig).
func (j *joinData) TLSBootstrapCfg() (*clientcmdapi.Config, error) { func (j *joinData) TLSBootstrapCfg() (*clientcmdapi.Config, error) {
if j.tlsBootstrapCfg != nil { if j.tlsBootstrapCfg != nil {
@ -432,15 +426,17 @@ func (j *joinData) InitCfg() (*kubeadmapi.InitConfiguration, error) {
return initCfg, err return initCfg, err
} }
func (j *joinData) ClientSetFromFile(path string) (*clientset.Clientset, error) { // ClientSet returns the ClientSet for accessing the cluster with the identity defined in admin.conf.
if client, ok := j.clientSets[path]; ok { func (j *joinData) ClientSet() (*clientset.Clientset, error) {
return client, nil if j.clientSet != nil {
return j.clientSet, nil
} }
path := kubeadmconstants.GetAdminKubeConfigPath()
client, err := kubeconfigutil.ClientSetFromFile(path) client, err := kubeconfigutil.ClientSetFromFile(path)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "[join] couldn't create Kubernetes client") return nil, errors.Wrap(err, "[join] couldn't create Kubernetes client")
} }
j.clientSets[path] = client j.clientSet = client
return client, nil return client, nil
} }

View File

@ -21,7 +21,6 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow" "k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
etcdphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/etcd" etcdphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/etcd"
) )
@ -61,7 +60,7 @@ func runCheckEtcdPhase(c workflow.RunData) error {
// Checks that the etcd cluster is healthy // Checks that the etcd cluster is healthy
// NB. this check cannot be implemented before because it requires the admin.conf and all the certificates // NB. this check cannot be implemented before because it requires the admin.conf and all the certificates
// for connecting to etcd already in place // for connecting to etcd already in place
client, err := data.ClientSetFromFile(kubeadmconstants.GetAdminKubeConfigPath()) client, err := data.ClientSet()
if err != nil { if err != nil {
return err return err
} }

View File

@ -109,9 +109,8 @@ func runEtcdPhase(c workflow.RunData) error {
return nil return nil
} }
kubeConfigFile := data.KubeConfigPath() // gets access to the cluster using the identity defined in admin.conf
client, err := data.ClientSet()
client, err := data.ClientSetFromFile(kubeConfigFile)
if err != nil { if err != nil {
return errors.Wrap(err, "couldn't create Kubernetes client") return errors.Wrap(err, "couldn't create Kubernetes client")
} }
@ -151,9 +150,8 @@ func runUploadConfigPhase(c workflow.RunData) error {
return nil return nil
} }
kubeConfigFile := data.KubeConfigPath() // gets access to the cluster using the identity defined in admin.conf
client, err := data.ClientSet()
client, err := data.ClientSetFromFile(kubeConfigFile)
if err != nil { if err != nil {
return errors.Wrap(err, "couldn't create Kubernetes client") return errors.Wrap(err, "couldn't create Kubernetes client")
} }
@ -179,9 +177,8 @@ func runMarkControlPlanePhase(c workflow.RunData) error {
return nil return nil
} }
kubeConfigFile := data.KubeConfigPath() // gets access to the cluster using the identity defined in admin.conf
client, err := data.ClientSet()
client, err := data.ClientSetFromFile(kubeConfigFile)
if err != nil { if err != nil {
return errors.Wrap(err, "couldn't create Kubernetes client") return errors.Wrap(err, "couldn't create Kubernetes client")
} }

View File

@ -30,10 +30,9 @@ import (
type JoinData interface { type JoinData interface {
CertificateKey() string CertificateKey() string
Cfg() *kubeadmapi.JoinConfiguration Cfg() *kubeadmapi.JoinConfiguration
KubeConfigPath() string
TLSBootstrapCfg() (*clientcmdapi.Config, error) TLSBootstrapCfg() (*clientcmdapi.Config, error)
InitCfg() (*kubeadmapi.InitConfiguration, error) InitCfg() (*kubeadmapi.InitConfiguration, error)
ClientSetFromFile(path string) (*clientset.Clientset, error) ClientSet() (*clientset.Clientset, error)
IgnorePreflightErrors() sets.String IgnorePreflightErrors() sets.String
OutputWriter() io.Writer OutputWriter() io.Writer
} }

View File

@ -31,11 +31,10 @@ type testJoinData struct{}
// testJoinData must satisfy JoinData. // testJoinData must satisfy JoinData.
var _ JoinData = &testJoinData{} var _ JoinData = &testJoinData{}
func (j *testJoinData) CertificateKey() string { return "" } func (j *testJoinData) CertificateKey() string { return "" }
func (j *testJoinData) Cfg() *kubeadmapi.JoinConfiguration { return nil } func (j *testJoinData) Cfg() *kubeadmapi.JoinConfiguration { return nil }
func (j *testJoinData) KubeConfigPath() string { return "" } func (j *testJoinData) TLSBootstrapCfg() (*clientcmdapi.Config, error) { return nil, nil }
func (j *testJoinData) TLSBootstrapCfg() (*clientcmdapi.Config, error) { return nil, nil } func (j *testJoinData) InitCfg() (*kubeadmapi.InitConfiguration, error) { return nil, nil }
func (j *testJoinData) InitCfg() (*kubeadmapi.InitConfiguration, error) { return nil, nil } func (j *testJoinData) ClientSet() (*clientset.Clientset, error) { return nil, nil }
func (j *testJoinData) ClientSetFromFile(path string) (*clientset.Clientset, error) { return nil, nil } func (j *testJoinData) IgnorePreflightErrors() sets.String { return nil }
func (j *testJoinData) IgnorePreflightErrors() sets.String { return nil } func (j *testJoinData) OutputWriter() io.Writer { return nil }
func (j *testJoinData) OutputWriter() io.Writer { return nil }

View File

@ -73,28 +73,28 @@ func NewKubeletStartPhase() workflow.Phase {
} }
} }
func getKubeletStartJoinData(c workflow.RunData) (JoinData, *kubeadmapi.JoinConfiguration, *kubeadmapi.InitConfiguration, *clientcmdapi.Config, error) { func getKubeletStartJoinData(c workflow.RunData) (*kubeadmapi.JoinConfiguration, *kubeadmapi.InitConfiguration, *clientcmdapi.Config, error) {
data, ok := c.(JoinData) data, ok := c.(JoinData)
if !ok { if !ok {
return nil, nil, nil, nil, errors.New("kubelet-start phase invoked with an invalid data struct") return nil, nil, nil, errors.New("kubelet-start phase invoked with an invalid data struct")
} }
cfg := data.Cfg() cfg := data.Cfg()
initCfg, err := data.InitCfg() initCfg, err := data.InitCfg()
if err != nil { if err != nil {
return nil, nil, nil, nil, err return nil, nil, nil, err
} }
tlsBootstrapCfg, err := data.TLSBootstrapCfg() tlsBootstrapCfg, err := data.TLSBootstrapCfg()
if err != nil { if err != nil {
return nil, nil, nil, nil, err return nil, nil, nil, err
} }
return data, cfg, initCfg, tlsBootstrapCfg, nil return cfg, initCfg, tlsBootstrapCfg, nil
} }
// runKubeletStartJoinPhase executes the kubelet TLS bootstrap process. // runKubeletStartJoinPhase executes the kubelet TLS bootstrap process.
// This process is executed by the kubelet and completes with the node joining the cluster // This process is executed by the kubelet and completes with the node joining the cluster
// with a dedicates set of credentials as required by the node authorizer // with a dedicates set of credentials as required by the node authorizer
func runKubeletStartJoinPhase(c workflow.RunData) error { func runKubeletStartJoinPhase(c workflow.RunData) error {
data, cfg, initCfg, tlsBootstrapCfg, err := getKubeletStartJoinData(c) cfg, initCfg, tlsBootstrapCfg, err := getKubeletStartJoinData(c)
if err != nil { if err != nil {
return err return err
} }
@ -119,7 +119,7 @@ func runKubeletStartJoinPhase(c workflow.RunData) error {
return err return err
} }
bootstrapClient, err := data.ClientSetFromFile(bootstrapKubeConfigFile) bootstrapClient, err := kubeconfigutil.ClientSetFromFile(bootstrapKubeConfigFile)
if err != nil { if err != nil {
return errors.Errorf("couldn't create client from kubeconfig file %q", bootstrapKubeConfigFile) return errors.Errorf("couldn't create client from kubeconfig file %q", bootstrapKubeConfigFile)
} }
@ -156,7 +156,7 @@ func runKubeletStartJoinPhase(c workflow.RunData) error {
} }
// When we know the /etc/kubernetes/kubelet.conf file is available, get the client // When we know the /etc/kubernetes/kubelet.conf file is available, get the client
client, err := data.ClientSetFromFile(kubeadmconstants.GetKubeletKubeConfigPath()) client, err := kubeconfigutil.ClientSetFromFile(kubeadmconstants.GetKubeletKubeConfigPath())
if err != nil { if err != nil {
return err return err
} }