diff --git a/pkg/k8s/applier/executor.go b/pkg/k8s/applier/executor.go index 9a9fc57b1..5f3e66c41 100644 --- a/pkg/k8s/applier/executor.go +++ b/pkg/k8s/applier/executor.go @@ -18,6 +18,7 @@ import ( "io" "time" + "k8s.io/apimachinery/pkg/runtime/schema" "sigs.k8s.io/cli-utils/pkg/common" airshipv1 "opendev.org/airship/airshipctl/pkg/api/v1alpha1" @@ -41,6 +42,30 @@ type ExecutorOptions struct { AirshipConfig *config.Config } +var _ ifc.Executor = &Executor{} + +// RegisterExecutor adds executor to phase executor registry +func RegisterExecutor(registry map[schema.GroupVersionKind]ifc.ExecutorFactory) error { + obj := &airshipv1.KubernetesApply{} + gvks, _, err := airshipv1.Scheme.ObjectKinds(obj) + if err != nil { + return err + } + registry[gvks[0]] = registerExecutor + return nil +} + +// registerExecutor is here so that executor in theory can be used outside phases +func registerExecutor(cfg ifc.ExecutorConfig) (ifc.Executor, error) { + return NewExecutor(ExecutorOptions{ + BundleName: cfg.PhaseName, + AirshipConfig: cfg.AirshipSettings.Config, + ExecutorBundle: cfg.ExecutorBundle, + ExecutorDocument: cfg.ExecutorDocument, + Kubeconfig: cfg.KubeConfig, + }) +} + // Executor applies resources to kubernetes type Executor struct { Options ExecutorOptions diff --git a/pkg/phase/phase.go b/pkg/phase/phase.go index 3971c3361..571743702 100644 --- a/pkg/phase/phase.go +++ b/pkg/phase/phase.go @@ -26,6 +26,7 @@ import ( "opendev.org/airship/airshipctl/pkg/document" "opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/events" + "opendev.org/airship/airshipctl/pkg/k8s/applier" "opendev.org/airship/airshipctl/pkg/k8s/kubeconfig" k8sutils "opendev.org/airship/airshipctl/pkg/k8s/utils" "opendev.org/airship/airshipctl/pkg/log" @@ -43,6 +44,9 @@ func DefaultExecutorRegistry() map[schema.GroupVersionKind]ifc.ExecutorFactory { if err := clusterctl.RegisterExecutor(execMap); err != nil { log.Fatal(ErrExecutorRegistration{ExecutorName: "clusterctl", Err: err}) } + if err := applier.RegisterExecutor(execMap); err != nil { + log.Fatal(ErrExecutorRegistration{ExecutorName: "kubernetes-apply", Err: err}) + } return execMap } diff --git a/pkg/phase/phase_test.go b/pkg/phase/phase_test.go index 4312f43ad..9c99c5ac4 100644 --- a/pkg/phase/phase_test.go +++ b/pkg/phase/phase_test.go @@ -31,6 +31,7 @@ import ( "opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/document" "opendev.org/airship/airshipctl/pkg/environment" + "opendev.org/airship/airshipctl/pkg/k8s/applier" "opendev.org/airship/airshipctl/pkg/phase" "opendev.org/airship/airshipctl/pkg/phase/ifc" ) @@ -229,6 +230,21 @@ func TestGetExecutor(t *testing.T) { }, }, }, + { + name: "Get registered executor", + settings: makeDefaultSettings, + phase: &airshipv1.Phase{ + Config: airshipv1.PhaseConfig{ + ExecutorRef: &corev1.ObjectReference{ + APIVersion: "airshipit.org/v1alpha1", + Kind: "KubernetesApply", + Name: "kubernetes-apply", + }, + DocumentEntryPoint: "valid_site/phases", + }, + }, + expectedExc: &applier.Executor{}, + }, } for _, test := range testCases { @@ -237,11 +253,21 @@ func TestGetExecutor(t *testing.T) { cmd := phase.Cmd{AirshipCTLSettings: tt.settings()} actualExc, actualErr := cmd.GetExecutor(tt.phase) assert.Equal(t, tt.expectedErr, actualErr) - assert.Equal(t, tt.expectedExc, actualExc) + assertEqualExecutor(t, tt.expectedExc, actualExc) }) } } +// assertEqualExecutor allows to compare executor interfaces +// check if we expect nil, and if so actual interface must be nil also otherwise compare types +func assertEqualExecutor(t *testing.T, expected, actual ifc.Executor) { + if expected == nil { + assert.Nil(t, actual) + return + } + assert.IsType(t, expected, actual) +} + func makeDefaultSettings() *environment.AirshipCTLSettings { testSettings := &environment.AirshipCTLSettings{ AirshipConfigPath: "testdata/airshipconfig.yaml", diff --git a/pkg/phase/testdata/valid_site/phases/kubernetes_apply.yaml b/pkg/phase/testdata/valid_site/phases/kubernetes_apply.yaml new file mode 100644 index 000000000..2bfc137bf --- /dev/null +++ b/pkg/phase/testdata/valid_site/phases/kubernetes_apply.yaml @@ -0,0 +1,12 @@ +--- +apiVersion: airshipit.org/v1alpha1 +kind: KubernetesApply +metadata: + labels: + airshipit.org/deploy-k8s: "false" + name: kubernetes-apply +config: + waitOptions: + timeout: 600 + pruneOptions: + prune: false diff --git a/pkg/phase/testdata/valid_site/phases/kustomization.yaml b/pkg/phase/testdata/valid_site/phases/kustomization.yaml index 5385fa3b4..799051b7a 100644 --- a/pkg/phase/testdata/valid_site/phases/kustomization.yaml +++ b/pkg/phase/testdata/valid_site/phases/kustomization.yaml @@ -4,3 +4,4 @@ resources: - some_exc.yaml - capi_init.yaml - clusterctl.yaml + - kubernetes_apply.yaml