diff --git a/pkg/clusterctl/client/executor.go b/pkg/clusterctl/client/executor.go index e73225a6c..e0f969ec2 100644 --- a/pkg/clusterctl/client/executor.go +++ b/pkg/clusterctl/client/executor.go @@ -145,6 +145,7 @@ func (c *ClusterctlExecutor) init(opts ifc.RunOptions, evtCh chan events.Event) } return } + // Use cluster name as context in kubeconfig file err = c.Init(kubeConfigFile, c.clusterName) if err != nil { c.handleErr(err, evtCh) diff --git a/pkg/k8s/applier/executor.go b/pkg/k8s/applier/executor.go index c248826d6..825622f26 100644 --- a/pkg/k8s/applier/executor.go +++ b/pkg/k8s/applier/executor.go @@ -33,7 +33,8 @@ import ( // ExecutorOptions provide a way to configure executor type ExecutorOptions struct { - BundleName string + BundleName string + ClusterName string ExecutorDocument document.Document ExecutorBundle document.Bundle @@ -57,6 +58,7 @@ func RegisterExecutor(registry map[schema.GroupVersionKind]ifc.ExecutorFactory) // registerExecutor is here so that executor in theory can be used outside phases func registerExecutor(cfg ifc.ExecutorConfig) (ifc.Executor, error) { return NewExecutor(ExecutorOptions{ + ClusterName: cfg.ClusterName, BundleName: cfg.PhaseName, Helper: cfg.Helper, ExecutorBundle: cfg.ExecutorBundle, @@ -125,8 +127,8 @@ func (e *Executor) prepareApplier(ch chan events.Event) (*Applier, document.Bund } // set up cleanup only if all calls up to here were successful e.cleanup = cleanup - - factory := utils.FactoryFromKubeConfigPath(path) + // Use cluster name as context in kubeconfig file + factory := utils.FactoryFromKubeConfig(path, e.Options.ClusterName) streams := utils.Streams() return NewApplier(ch, factory, streams), bundle, nil } diff --git a/pkg/k8s/client/client.go b/pkg/k8s/client/client.go index 23862ecd2..2f0a140dd 100644 --- a/pkg/k8s/client/client.go +++ b/pkg/k8s/client/client.go @@ -65,7 +65,8 @@ func NewClient(cfg *config.Config) (Interface, error) { client := new(Client) var err error - f := k8sutils.FactoryFromKubeConfigPath(cfg.KubeConfigPath()) + // TODO add support for kubeconfig context, for now use current context + f := k8sutils.FactoryFromKubeConfig(cfg.KubeConfigPath(), "") pathToBufferDir := filepath.Dir(cfg.LoadedConfigPath()) client.kubectl = kubectl.NewKubectl(f).WithBufferDir(pathToBufferDir) diff --git a/pkg/k8s/kubectl/kubectl_test.go b/pkg/k8s/kubectl/kubectl_test.go index 63d3aae44..cce363474 100644 --- a/pkg/k8s/kubectl/kubectl_test.go +++ b/pkg/k8s/kubectl/kubectl_test.go @@ -39,7 +39,7 @@ var ( ) func TestNewKubectlFromKubeConfigPath(t *testing.T) { - f := k8sutils.FactoryFromKubeConfigPath(kubeconfigPath) + f := k8sutils.FactoryFromKubeConfig(kubeconfigPath, "") kctl := kubectl.NewKubectl(f).WithBufferDir("/tmp/.airship") assert.NotNil(t, kctl.Factory) diff --git a/pkg/k8s/poller/poller_test.go b/pkg/k8s/poller/poller_test.go index 54b327c09..d344c43c0 100755 --- a/pkg/k8s/poller/poller_test.go +++ b/pkg/k8s/poller/poller_test.go @@ -30,7 +30,7 @@ import ( func TestNewStatusPoller(t *testing.T) { airClient := fake.NewClient() - f := k8sutils.FactoryFromKubeConfigPath("testdata/kubeconfig.yaml") + f := k8sutils.FactoryFromKubeConfig("testdata/kubeconfig.yaml", "") restConfig, err := f.ToRESTConfig() require.NoError(t, err) restMapper, err := f.ToRESTMapper() diff --git a/pkg/k8s/utils/utils.go b/pkg/k8s/utils/utils.go index 37ab539fd..dbd933f66 100644 --- a/pkg/k8s/utils/utils.go +++ b/pkg/k8s/utils/utils.go @@ -28,11 +28,12 @@ import ( "opendev.org/airship/airshipctl/pkg/document" ) -// FactoryFromKubeConfigPath returns a factory with the -// default Kubernetes resources for the given kube config path -func FactoryFromKubeConfigPath(kp string) cmdutil.Factory { +// FactoryFromKubeConfig returns a factory with the +// default Kubernetes resources for the given kube config path and context +func FactoryFromKubeConfig(path, context string) cmdutil.Factory { kf := genericclioptions.NewConfigFlags(false) - kf.KubeConfig = &kp + kf.KubeConfig = &path + kf.Context = &context return cmdutil.NewFactory(kf) } diff --git a/pkg/k8s/utils/utils_test.go b/pkg/k8s/utils/utils_test.go index b0fbf164a..51ef14b54 100644 --- a/pkg/k8s/utils/utils_test.go +++ b/pkg/k8s/utils/utils_test.go @@ -29,7 +29,7 @@ import ( func TestDefaultManifestFactory(t *testing.T) { bundle, err := document.NewBundleByPath("testdata/source_bundle") require.NoError(t, err) - reader := DefaultManifestReaderFactory(false, bundle, FactoryFromKubeConfigPath("testdata/kubeconfig.yaml")) + reader := DefaultManifestReaderFactory(false, bundle, FactoryFromKubeConfig("testdata/kubeconfig.yaml", "")) require.NotNil(t, reader) } @@ -64,7 +64,7 @@ func TestManifestBundleReader(t *testing.T) { for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { - reader := NewManifestBundleReader(false, bundle, FactoryFromKubeConfigPath("testdata/kubeconfig.yaml")) + reader := NewManifestBundleReader(false, bundle, FactoryFromKubeConfig("testdata/kubeconfig.yaml", "")) if tt.reader != nil { reader.StreamReader.Reader = tt.reader } diff --git a/pkg/phase/apply/apply.go b/pkg/phase/apply/apply.go index 9a494ea68..8ef8f834a 100644 --- a/pkg/phase/apply/apply.go +++ b/pkg/phase/apply/apply.go @@ -42,7 +42,7 @@ type Options struct { // Initialize Options with required field, such as Applier func (o *Options) Initialize(kubeConfigPath string) { - f := utils.FactoryFromKubeConfigPath(kubeConfigPath) + f := utils.FactoryFromKubeConfig(kubeConfigPath, "") streams := utils.Streams() o.EventChannel = make(chan events.Event) o.Applier = applier.NewApplier(o.EventChannel, f, streams)