Integrate kubernetes apply executor with phases

Also adds unit test to test GetExecutor() method.

Relates-To #259

Change-Id: I7d89750e671ec1b747a811aa71b86725217b5311
This commit is contained in:
Kostiantyn Kalynovskyi 2020-08-20 17:39:00 -05:00 committed by Kostyantyn Kalynovskyi
parent 85abf1897a
commit a3200e8d62
5 changed files with 69 additions and 1 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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",

View File

@ -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

View File

@ -4,3 +4,4 @@ resources:
- some_exc.yaml
- capi_init.yaml
- clusterctl.yaml
- kubernetes_apply.yaml