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

View File

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

View File

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

View File

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

View File

@ -31,11 +31,10 @@ type testJoinData struct{}
// testJoinData must satisfy JoinData.
var _ JoinData = &testJoinData{}
func (j *testJoinData) CertificateKey() string { return "" }
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) InitCfg() (*kubeadmapi.InitConfiguration, 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) OutputWriter() io.Writer { return nil }
func (j *testJoinData) CertificateKey() string { return "" }
func (j *testJoinData) Cfg() *kubeadmapi.JoinConfiguration { return nil }
func (j *testJoinData) TLSBootstrapCfg() (*clientcmdapi.Config, 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) IgnorePreflightErrors() sets.String { 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)
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()
initCfg, err := data.InitCfg()
if err != nil {
return nil, nil, nil, nil, err
return nil, nil, nil, err
}
tlsBootstrapCfg, err := data.TLSBootstrapCfg()
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.
// 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
func runKubeletStartJoinPhase(c workflow.RunData) error {
data, cfg, initCfg, tlsBootstrapCfg, err := getKubeletStartJoinData(c)
cfg, initCfg, tlsBootstrapCfg, err := getKubeletStartJoinData(c)
if err != nil {
return err
}
@ -119,7 +119,7 @@ func runKubeletStartJoinPhase(c workflow.RunData) error {
return err
}
bootstrapClient, err := data.ClientSetFromFile(bootstrapKubeConfigFile)
bootstrapClient, err := kubeconfigutil.ClientSetFromFile(bootstrapKubeConfigFile)
if err != nil {
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
client, err := data.ClientSetFromFile(kubeadmconstants.GetKubeletKubeConfigPath())
client, err := kubeconfigutil.ClientSetFromFile(kubeadmconstants.GetKubeletKubeConfigPath())
if err != nil {
return err
}