Merge "Use Helper in clusterctl and kube apply executors"
This commit is contained in:
commit
567e5060db
@ -16,7 +16,6 @@ package client
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
|
||||||
@ -34,7 +33,6 @@ var _ ifc.Executor = &ClusterctlExecutor{}
|
|||||||
// ClusterctlExecutor phase executor
|
// ClusterctlExecutor phase executor
|
||||||
type ClusterctlExecutor struct {
|
type ClusterctlExecutor struct {
|
||||||
clusterName string
|
clusterName string
|
||||||
dumpRoot string
|
|
||||||
|
|
||||||
Interface
|
Interface
|
||||||
bundle document.Bundle
|
bundle document.Bundle
|
||||||
@ -59,11 +57,7 @@ func NewExecutor(cfg ifc.ExecutorConfig) (ifc.Executor, error) {
|
|||||||
if err := cfg.ExecutorDocument.ToAPIObject(options, airshipv1.Scheme); err != nil {
|
if err := cfg.ExecutorDocument.ToAPIObject(options, airshipv1.Scheme); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tgtPath, err := cfg.AirshipConfig.CurrentContextTargetPath()
|
client, err := NewClient(cfg.Helper.TargetPath(), log.DebugEnabled(), options)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
client, err := NewClient(tgtPath, log.DebugEnabled(), options)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -73,7 +67,6 @@ func NewExecutor(cfg ifc.ExecutorConfig) (ifc.Executor, error) {
|
|||||||
bundle: cfg.ExecutorBundle,
|
bundle: cfg.ExecutorBundle,
|
||||||
options: options,
|
options: options,
|
||||||
kubecfg: cfg.KubeConfig,
|
kubecfg: cfg.KubeConfig,
|
||||||
dumpRoot: filepath.Dir(cfg.AirshipConfig.LoadedConfigPath()),
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,7 +93,7 @@ func (c *ClusterctlExecutor) init(opts ifc.RunOptions, evtCh chan events.Event)
|
|||||||
Operation: events.ClusterctlInitStart,
|
Operation: events.ClusterctlInitStart,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
kubeConfigFile, cleanup, err := c.kubecfg.WriteTempFile(c.dumpRoot)
|
kubeConfigFile, cleanup, err := c.kubecfg.GetFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.handleErr(err, evtCh)
|
c.handleErr(err, evtCh)
|
||||||
return
|
return
|
||||||
|
@ -31,6 +31,7 @@ import (
|
|||||||
airerrors "opendev.org/airship/airshipctl/pkg/errors"
|
airerrors "opendev.org/airship/airshipctl/pkg/errors"
|
||||||
"opendev.org/airship/airshipctl/pkg/events"
|
"opendev.org/airship/airshipctl/pkg/events"
|
||||||
"opendev.org/airship/airshipctl/pkg/k8s/kubeconfig"
|
"opendev.org/airship/airshipctl/pkg/k8s/kubeconfig"
|
||||||
|
"opendev.org/airship/airshipctl/pkg/phase"
|
||||||
"opendev.org/airship/airshipctl/pkg/phase/ifc"
|
"opendev.org/airship/airshipctl/pkg/phase/ifc"
|
||||||
"opendev.org/airship/airshipctl/testutil/fs"
|
"opendev.org/airship/airshipctl/testutil/fs"
|
||||||
)
|
)
|
||||||
@ -76,21 +77,12 @@ func TestNewExecutor(t *testing.T) {
|
|||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
settings *config.Config
|
helper ifc.Helper
|
||||||
expectedErr error
|
expectedErr error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "New Clusterctl Executor",
|
name: "New Clusterctl Executor",
|
||||||
settings: sampleSettings(),
|
helper: makeDefaultHelper(t),
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "New Clusterctl Executor",
|
|
||||||
settings: func() *config.Config {
|
|
||||||
s := sampleSettings()
|
|
||||||
s.CurrentContext = "non-existent-ctx"
|
|
||||||
return s
|
|
||||||
}(),
|
|
||||||
expectedErr: config.ErrMissingConfig{What: "Context with name 'non-existent-ctx'"},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
@ -99,7 +91,7 @@ func TestNewExecutor(t *testing.T) {
|
|||||||
_, actualErr := cctlclient.NewExecutor(ifc.ExecutorConfig{
|
_, actualErr := cctlclient.NewExecutor(ifc.ExecutorConfig{
|
||||||
ExecutorDocument: sampleCfgDoc,
|
ExecutorDocument: sampleCfgDoc,
|
||||||
ExecutorBundle: bundle,
|
ExecutorBundle: bundle,
|
||||||
AirshipConfig: tt.settings,
|
Helper: tt.helper,
|
||||||
})
|
})
|
||||||
assert.Equal(t, tt.expectedErr, actualErr)
|
assert.Equal(t, tt.expectedErr, actualErr)
|
||||||
})
|
})
|
||||||
@ -185,7 +177,7 @@ func TestExecutorRun(t *testing.T) {
|
|||||||
ifc.ExecutorConfig{
|
ifc.ExecutorConfig{
|
||||||
ExecutorDocument: tt.cfgDoc,
|
ExecutorDocument: tt.cfgDoc,
|
||||||
ExecutorBundle: bundle,
|
ExecutorBundle: bundle,
|
||||||
AirshipConfig: sampleSettings(),
|
Helper: makeDefaultHelper(t),
|
||||||
KubeConfig: kubeCfg,
|
KubeConfig: kubeCfg,
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@ -208,7 +200,7 @@ func TestExecutorValidate(t *testing.T) {
|
|||||||
ifc.ExecutorConfig{
|
ifc.ExecutorConfig{
|
||||||
ExecutorDocument: sampleCfgDoc,
|
ExecutorDocument: sampleCfgDoc,
|
||||||
ExecutorBundle: bundle,
|
ExecutorBundle: bundle,
|
||||||
AirshipConfig: sampleSettings(),
|
Helper: makeDefaultHelper(t),
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expectedErr := airerrors.ErrNotImplemented{}
|
expectedErr := airerrors.ErrNotImplemented{}
|
||||||
@ -225,7 +217,7 @@ func TestExecutorRender(t *testing.T) {
|
|||||||
ifc.ExecutorConfig{
|
ifc.ExecutorConfig{
|
||||||
ExecutorDocument: sampleCfgDoc,
|
ExecutorDocument: sampleCfgDoc,
|
||||||
ExecutorBundle: bundle,
|
ExecutorBundle: bundle,
|
||||||
AirshipConfig: sampleSettings(),
|
Helper: makeDefaultHelper(t),
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
actualOut := &bytes.Buffer{}
|
actualOut := &bytes.Buffer{}
|
||||||
@ -233,11 +225,16 @@ func TestExecutorRender(t *testing.T) {
|
|||||||
assert.Equal(t, expectedErr, actualErr)
|
assert.Equal(t, expectedErr, actualErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func sampleSettings() *config.Config {
|
func makeDefaultHelper(t *testing.T) ifc.Helper {
|
||||||
|
t.Helper()
|
||||||
cfg := config.NewConfig()
|
cfg := config.NewConfig()
|
||||||
cfg.Manifests[config.AirshipDefaultManifest].TargetPath = "./testdata"
|
cfg.Manifests[config.AirshipDefaultManifest].TargetPath = "./testdata"
|
||||||
|
cfg.Manifests[config.AirshipDefaultManifest].MetadataPath = "metadata.yaml"
|
||||||
cfg.SetLoadedConfigPath(".")
|
cfg.SetLoadedConfigPath(".")
|
||||||
return cfg
|
helper, err := phase.NewHelper(cfg)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, helper)
|
||||||
|
return helper
|
||||||
}
|
}
|
||||||
|
|
||||||
func executorDoc(t *testing.T, action string) document.Document {
|
func executorDoc(t *testing.T, action string) document.Document {
|
||||||
|
2
pkg/clusterctl/client/testdata/metadata.yaml
vendored
Normal file
2
pkg/clusterctl/client/testdata/metadata.yaml
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
phase:
|
||||||
|
path: executor_move
|
@ -22,7 +22,6 @@ import (
|
|||||||
"sigs.k8s.io/cli-utils/pkg/common"
|
"sigs.k8s.io/cli-utils/pkg/common"
|
||||||
|
|
||||||
airshipv1 "opendev.org/airship/airshipctl/pkg/api/v1alpha1"
|
airshipv1 "opendev.org/airship/airshipctl/pkg/api/v1alpha1"
|
||||||
"opendev.org/airship/airshipctl/pkg/config"
|
|
||||||
"opendev.org/airship/airshipctl/pkg/document"
|
"opendev.org/airship/airshipctl/pkg/document"
|
||||||
"opendev.org/airship/airshipctl/pkg/errors"
|
"opendev.org/airship/airshipctl/pkg/errors"
|
||||||
"opendev.org/airship/airshipctl/pkg/events"
|
"opendev.org/airship/airshipctl/pkg/events"
|
||||||
@ -39,7 +38,7 @@ type ExecutorOptions struct {
|
|||||||
ExecutorDocument document.Document
|
ExecutorDocument document.Document
|
||||||
ExecutorBundle document.Bundle
|
ExecutorBundle document.Bundle
|
||||||
Kubeconfig kubeconfig.Interface
|
Kubeconfig kubeconfig.Interface
|
||||||
AirshipConfig *config.Config
|
Helper ifc.Helper
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ ifc.Executor = &Executor{}
|
var _ ifc.Executor = &Executor{}
|
||||||
@ -59,7 +58,7 @@ func RegisterExecutor(registry map[schema.GroupVersionKind]ifc.ExecutorFactory)
|
|||||||
func registerExecutor(cfg ifc.ExecutorConfig) (ifc.Executor, error) {
|
func registerExecutor(cfg ifc.ExecutorConfig) (ifc.Executor, error) {
|
||||||
return NewExecutor(ExecutorOptions{
|
return NewExecutor(ExecutorOptions{
|
||||||
BundleName: cfg.PhaseName,
|
BundleName: cfg.PhaseName,
|
||||||
AirshipConfig: cfg.AirshipConfig,
|
Helper: cfg.Helper,
|
||||||
ExecutorBundle: cfg.ExecutorBundle,
|
ExecutorBundle: cfg.ExecutorBundle,
|
||||||
ExecutorDocument: cfg.ExecutorDocument,
|
ExecutorDocument: cfg.ExecutorDocument,
|
||||||
Kubeconfig: cfg.KubeConfig,
|
Kubeconfig: cfg.KubeConfig,
|
||||||
|
@ -27,6 +27,7 @@ import (
|
|||||||
"opendev.org/airship/airshipctl/pkg/k8s/applier"
|
"opendev.org/airship/airshipctl/pkg/k8s/applier"
|
||||||
"opendev.org/airship/airshipctl/pkg/k8s/kubeconfig"
|
"opendev.org/airship/airshipctl/pkg/k8s/kubeconfig"
|
||||||
"opendev.org/airship/airshipctl/pkg/k8s/utils"
|
"opendev.org/airship/airshipctl/pkg/k8s/utils"
|
||||||
|
"opendev.org/airship/airshipctl/pkg/phase"
|
||||||
"opendev.org/airship/airshipctl/pkg/phase/ifc"
|
"opendev.org/airship/airshipctl/pkg/phase/ifc"
|
||||||
"opendev.org/airship/airshipctl/testutil/fs"
|
"opendev.org/airship/airshipctl/testutil/fs"
|
||||||
)
|
)
|
||||||
@ -84,15 +85,15 @@ func TestNewExecutor(t *testing.T) {
|
|||||||
name string
|
name string
|
||||||
cfgDoc string
|
cfgDoc string
|
||||||
expectedErr string
|
expectedErr string
|
||||||
airConfig *config.Config
|
helper ifc.Helper
|
||||||
kubeconf kubeconfig.Interface
|
kubeconf kubeconfig.Interface
|
||||||
bundleFunc func(t *testing.T) document.Bundle
|
bundleFunc func(t *testing.T) document.Bundle
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "valid executor",
|
name: "valid executor",
|
||||||
cfgDoc: ValidExecutorDoc,
|
cfgDoc: ValidExecutorDoc,
|
||||||
kubeconf: testKubeconfig(testValidKubeconfig),
|
kubeconf: testKubeconfig(testValidKubeconfig),
|
||||||
airConfig: makeDefaultConfig(),
|
helper: makeDefaultHelper(t),
|
||||||
bundleFunc: func(t *testing.T) document.Bundle {
|
bundleFunc: func(t *testing.T) document.Bundle {
|
||||||
return newBundle("testdata/source_bundle", t)
|
return newBundle("testdata/source_bundle", t)
|
||||||
},
|
},
|
||||||
@ -107,7 +108,7 @@ metadata:
|
|||||||
labels:
|
labels:
|
||||||
cli-utils.sigs.k8s.io/inventory-id: "some id"`,
|
cli-utils.sigs.k8s.io/inventory-id: "some id"`,
|
||||||
expectedErr: "wrong config document",
|
expectedErr: "wrong config document",
|
||||||
airConfig: makeDefaultConfig(),
|
helper: makeDefaultHelper(t),
|
||||||
bundleFunc: func(t *testing.T) document.Bundle {
|
bundleFunc: func(t *testing.T) document.Bundle {
|
||||||
return newBundle("testdata/source_bundle", t)
|
return newBundle("testdata/source_bundle", t)
|
||||||
},
|
},
|
||||||
@ -126,7 +127,7 @@ metadata:
|
|||||||
ExecutorDocument: doc,
|
ExecutorDocument: doc,
|
||||||
ExecutorBundle: tt.bundleFunc(t),
|
ExecutorBundle: tt.bundleFunc(t),
|
||||||
Kubeconfig: tt.kubeconf,
|
Kubeconfig: tt.kubeconf,
|
||||||
AirshipConfig: tt.airConfig,
|
Helper: tt.helper,
|
||||||
})
|
})
|
||||||
if tt.expectedErr != "" {
|
if tt.expectedErr != "" {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
@ -151,12 +152,12 @@ func TestExecutorRun(t *testing.T) {
|
|||||||
kubeconf kubeconfig.Interface
|
kubeconf kubeconfig.Interface
|
||||||
execDoc document.Document
|
execDoc document.Document
|
||||||
bundleFunc func(t *testing.T) document.Bundle
|
bundleFunc func(t *testing.T) document.Bundle
|
||||||
airConfig *config.Config
|
helper ifc.Helper
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "cant read kubeconfig error",
|
name: "cant read kubeconfig error",
|
||||||
containsErr: "no such file or directory",
|
containsErr: "no such file or directory",
|
||||||
airConfig: makeDefaultConfig(),
|
helper: makeDefaultHelper(t),
|
||||||
bundleFunc: func(t *testing.T) document.Bundle {
|
bundleFunc: func(t *testing.T) document.Bundle {
|
||||||
return newBundle("testdata/source_bundle", t)
|
return newBundle("testdata/source_bundle", t)
|
||||||
},
|
},
|
||||||
@ -168,7 +169,7 @@ func TestExecutorRun(t *testing.T) {
|
|||||||
execDoc: toKubernetesApply(t, ValidExecutorDoc),
|
execDoc: toKubernetesApply(t, ValidExecutorDoc),
|
||||||
containsErr: "Cannot apply nil bundle",
|
containsErr: "Cannot apply nil bundle",
|
||||||
kubeconf: testKubeconfig(testValidKubeconfig),
|
kubeconf: testKubeconfig(testValidKubeconfig),
|
||||||
airConfig: makeDefaultConfig(),
|
helper: makeDefaultHelper(t),
|
||||||
bundleFunc: func(t *testing.T) document.Bundle {
|
bundleFunc: func(t *testing.T) document.Bundle {
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
@ -180,7 +181,7 @@ func TestExecutorRun(t *testing.T) {
|
|||||||
exec, err := applier.NewExecutor(
|
exec, err := applier.NewExecutor(
|
||||||
applier.ExecutorOptions{
|
applier.ExecutorOptions{
|
||||||
ExecutorDocument: tt.execDoc,
|
ExecutorDocument: tt.execDoc,
|
||||||
AirshipConfig: tt.airConfig,
|
Helper: tt.helper,
|
||||||
ExecutorBundle: tt.bundleFunc(t),
|
ExecutorBundle: tt.bundleFunc(t),
|
||||||
Kubeconfig: tt.kubeconf,
|
Kubeconfig: tt.kubeconf,
|
||||||
})
|
})
|
||||||
@ -219,7 +220,8 @@ func TestRender(t *testing.T) {
|
|||||||
assert.Contains(t, result, "ReplicationController")
|
assert.Contains(t, result, "ReplicationController")
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeDefaultConfig() *config.Config {
|
func makeDefaultHelper(t *testing.T) ifc.Helper {
|
||||||
|
t.Helper()
|
||||||
conf := &config.Config{
|
conf := &config.Config{
|
||||||
CurrentContext: "default",
|
CurrentContext: "default",
|
||||||
Contexts: map[string]*config.Context{
|
Contexts: map[string]*config.Context{
|
||||||
@ -229,12 +231,15 @@ func makeDefaultConfig() *config.Config {
|
|||||||
},
|
},
|
||||||
Manifests: map[string]*config.Manifest{
|
Manifests: map[string]*config.Manifest{
|
||||||
"default-manifest": {
|
"default-manifest": {
|
||||||
MetadataPath: "metadata-path",
|
MetadataPath: "metadata.yaml",
|
||||||
TargetPath: "testdata",
|
TargetPath: "testdata",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return conf
|
helper, err := phase.NewHelper(conf)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, helper)
|
||||||
|
return helper
|
||||||
}
|
}
|
||||||
|
|
||||||
// toKubernetesApply converts string to document object
|
// toKubernetesApply converts string to document object
|
||||||
|
2
pkg/k8s/applier/testdata/metadata.yaml
vendored
Normal file
2
pkg/k8s/applier/testdata/metadata.yaml
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
phase:
|
||||||
|
path: "source_bundle"
|
Loading…
x
Reference in New Issue
Block a user