Add command line library to integrate clusterctl with airshipctl

Change-Id: I41a73abc7018410b120cff826e87c6ddc559b42d
This commit is contained in:
Kostiantyn Kalynovskyi 2020-04-27 18:48:27 -05:00
parent 081721816e
commit 0e17d6c7f3
9 changed files with 337 additions and 2 deletions

View File

@ -0,0 +1,91 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"sigs.k8s.io/yaml"
airshipv1 "opendev.org/airship/airshipctl/pkg/clusterctl/api/v1alpha1"
"opendev.org/airship/airshipctl/pkg/clusterctl/client"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/environment"
)
// Command adds a layer to clusterctl interface with airshipctl context
type Command struct {
kubeconfigPath string
documentRoot string
client client.Interface
options *airshipv1.Clusterctl
}
// NewCommand returns instance of Command
func NewCommand(rs *environment.AirshipCTLSettings) (*Command, error) {
bundle, err := getBundle(rs.Config)
if err != nil {
return nil, err
}
root, err := rs.Config.CurrentContextTargetPath()
if err != nil {
return nil, err
}
options, err := clusterctlOptions(bundle)
if err != nil {
return nil, err
}
client, err := client.NewClient(root, rs.Debug, options)
if err != nil {
return nil, err
}
kubeConfigPath := rs.Config.KubeConfigPath()
return &Command{
kubeconfigPath: kubeConfigPath,
documentRoot: root,
client: client,
options: options,
}, nil
}
// Init runs clusterctl init
func (c *Command) Init() error {
return c.client.Init(c.kubeconfigPath)
}
func clusterctlOptions(bundle document.Bundle) (*airshipv1.Clusterctl, error) {
doc, err := bundle.SelectOne(document.NewClusterctlSelector())
if err != nil {
return nil, err
}
options := &airshipv1.Clusterctl{}
b, err := doc.AsYAML()
if err != nil {
return nil, err
}
// TODO (kkalynovskyi) instead of this, use kubernetes serializer
err = yaml.Unmarshal(b, options)
if err != nil {
return nil, err
}
return options, nil
}
func getBundle(conf *config.Config) (document.Bundle, error) {
path, err := conf.CurrentContextEntryPoint(config.ClusterctlPhase)
if err != nil {
return nil, err
}
return document.NewBundleByPath(path)
}

View File

@ -0,0 +1,132 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
)
const (
manifestName = "dummy_manifest"
validContext = "dummy_cluster"
)
// TODO (kkalynovskyi) expand test cases
func TestNewCommand(t *testing.T) {
rs := &environment.AirshipCTLSettings{
AirshipConfigPath: "testdata/airshipconfig.yaml",
KubeConfigPath: "testdata/kubeconfig.yaml",
}
rs.InitConfig()
tests := []struct {
name string
expectErr bool
currentConext string
manifests map[string]*config.Manifest
}{
{
name: "default success",
currentConext: validContext,
manifests: map[string]*config.Manifest{
manifestName: {
TargetPath: "testdata",
SubPath: "valid",
PrimaryRepositoryName: "primary",
Repositories: map[string]*config.Repository{
"primary": {},
},
},
},
},
{
name: "Bundle build failure",
currentConext: validContext,
expectErr: true,
manifests: map[string]*config.Manifest{
manifestName: {
TargetPath: "testdata",
SubPath: "invalid-path",
PrimaryRepositoryName: "primary",
Repositories: map[string]*config.Repository{
"primary": {},
},
},
},
},
{
name: "invalid clusterctl kind",
currentConext: validContext,
expectErr: true,
manifests: map[string]*config.Manifest{
manifestName: {
TargetPath: "testdata",
SubPath: "no-clusterctl",
PrimaryRepositoryName: "primary",
Repositories: map[string]*config.Repository{
"primary": {},
},
},
},
},
{
name: "no primary repo",
expectErr: true,
manifests: map[string]*config.Manifest{
manifestName: {
SubPath: "no-clusterctl",
},
},
},
{
name: "cant find context",
currentConext: "invalid-context",
expectErr: true,
manifests: map[string]*config.Manifest{
manifestName: {
TargetPath: "testdata",
SubPath: "can't build bundle",
PrimaryRepositoryName: "primary",
Repositories: map[string]*config.Repository{
"primary": {},
},
},
},
},
}
for _, tt := range tests {
rs.InitConfig()
expectErr := tt.expectErr
manifests := tt.manifests
rs.Config.Manifests = manifests
context := tt.currentConext
t.Run(tt.name, func(t *testing.T) {
rs.Config.Manifests = manifests
rs.Config.CurrentContext = context
command, err := NewCommand(rs)
if expectErr {
assert.Error(t, err)
assert.Nil(t, command)
} else {
require.NoError(t, err)
}
})
}
}

View File

@ -0,0 +1,44 @@
apiVersion: airshipit.org/v1alpha1
bootstrapInfo:
dummy_bootstrap_config:
container:
volume: /tmp/airship:/config
image: quay.io/airshipit/isogen:latest
containerRuntime: docker
builder:
userDataFileName: user-data
networkConfigFileName: network-config
outputMetadataFileName: output-metadata.yaml
remoteDirect:
isoUrl: http://localhost:8099/debian-custom.iso
remoteType: redfish
clusters:
dummycluster:
clusterType:
ephemeral:
bootstrapInfo: dummy_bootstrap_config
clusterKubeconf: dummycluster_ephemeral
contexts:
dummy_cluster:
contextKubeconf: dummy_cluster
manifest: dummy_manifest
currentContext: dummy_cluster
kind: Config
manifests:
dummy_manifest:
primaryRepositoryName: primary
repositories:
primary:
auth:
sshKey: testdata/test-key.pem
type: ssh-key
checkout:
branch: ""
force: false
remoteRef: ""
tag: v1.0.1
url: http://dummy.url.com/primary.git
subPath: site
targetPath: testdata
users:
dummy_user: {}

View File

@ -0,0 +1,19 @@
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdw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NHQvREhUUzc4N1V1SUg5cXBaV09yTFNMOGFBeUxQUHhWSXBteGZmbWRETE9TS2VUemRlTmxoSitUMwowQlBVaHBQTlJBNTNJN0hRQjhVUDR2elNONTkzZ1VFbVlFQ2Jic2RYSzB6ZVR6SDdWWHR2Zmd5WTVWWT0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
server: https://127.0.0.1:6443
name: dummycluster_ephemeral
contexts:
- context:
cluster: dummycluster_ephemeral
user: kubernetes-admin
name: dummy_cluster
current-context: dummy_cluster
kind: Config
preferences: {}
users:
- name: kubernetes-admin
user:
client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM4akNDQWRxZ0F3SUJBZ0lJQXhEdzk2RUY4SXN3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB4T1RBNU1qa3hOekF6TURsYUZ3MHlNREE1TWpneE56QXpNVEphTURReApGekFWQmdOVkJBb1REbk41YzNSbGJUcHRZWE4wWlhKek1Sa3dGd1lEVlFRREV4QnJkV0psY201bGRHVnpMV0ZrCmJXbHVNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQXV6R0pZdlBaNkRvaTQyMUQKSzhXSmFaQ25OQWQycXo1cC8wNDJvRnpRUGJyQWd6RTJxWVZrek9MOHhBVmVSN1NONXdXb1RXRXlGOEVWN3JyLwo0K0hoSEdpcTVQbXF1SUZ5enpuNi9JWmM4alU5eEVmenZpa2NpckxmVTR2UlhKUXdWd2dBU05sMkFXQUloMmRECmRUcmpCQ2ZpS1dNSHlqMFJiSGFsc0J6T3BnVC9IVHYzR1F6blVRekZLdjJkajVWMU5rUy9ESGp5UlJKK0VMNlEKQlltR3NlZzVQNE5iQzllYnVpcG1NVEFxL0p1bU9vb2QrRmpMMm5acUw2Zkk2ZkJ0RjVPR2xwQ0IxWUo4ZnpDdAp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czk4VU5hbk5TQ0xNMW1yaXZHRXJ1VHFnTDM1bU41eFZPdTUxQU5JCm4yNkFtODBJT2JDeEtLa0R0ZXJSaFhHd3g5c1pONVJCbG9VRThZNGJBb0dBQ3ZsdVhMZWRxcng5VkE0bDNoNXUKVDJXRVUxYjgxZ1orcmtRc1I1S0lNWEw4cllBTElUNUpHKzFuendyN3BkaEFXZmFWdVV2SDRhamdYT0h6MUs5aQpFODNSVTNGMG9ldUg0V01PY1RwU0prWm0xZUlXcWRiaEVCb1FGdUlWTXRib1BsV0d4ZUhFRHJoOEtreGp4aThSCmdEcUQyajRwY1IzQ0g5QjJ5a0lqQjVFQ2dZRUExc0xXLys2enE1c1lNSm14K1JXZThhTXJmL3pjQnVTSU1LQWgKY0dNK0wwMG9RSHdDaUU4TVNqcVN1ajV3R214YUFuanhMb3ZwSFlRV1VmUEVaUW95UE1YQ2VhRVBLOU4xbk8xMwp0V2lHRytIZkIxaU5PazFCc0lhNFNDbndOM1FRVTFzeXBaeEgxT3hueS9LYmkvYmEvWEZ5VzNqMGFUK2YvVWxrCmJGV1ZVdWtDZ1lFQTBaMmRTTFlmTjV5eFNtYk5xMWVqZXdWd1BjRzQxR2hQclNUZEJxdHFac1doWGE3aDdLTWEKeHdvamh5SXpnTXNyK2tXODdlajhDQ2h0d21sQ1p5QU92QmdOZytncnJ1cEZLM3FOSkpKeU9YREdHckdpbzZmTQp5aXB3Q2tZVGVxRThpZ1J6UkI5QkdFUGY4eVpjMUtwdmZhUDVhM0lRZmxiV0czbGpUemNNZVZjPQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=

View File

@ -0,0 +1,22 @@
---
apiVersion: airshipit.org/v1alpha1
kind: WrongKind
metadata:
labels:
airshipit.org/deploy-k8s: "false"
name: clusterctl-v1
init-options: {}
providers:
- name: "aws"
type: "InfrastructureProvider"
url: "/manifests/capi/infra/aws/v0.3.0"
clusterctl-repository: true
- name: "custom-infra"
type: "InfrastructureProvider"
url: "/manifests/capi/custom-infra/aws/v0.3.0"
clusterctl-repository: true
- name: "custom-airship-infra"
type: "InfrastructureProvider"
versions:
v0.3.1: functions/capi/infrastructure/v0.3.1
v0.3.2: functions/capi/infrastructure/v0.3.2

View File

@ -0,0 +1,2 @@
resources:
- invalid-clusterctl.yaml

View File

@ -0,0 +1,22 @@
---
apiVersion: airshipit.org/v1alpha1
kind: Clusterctl
metadata:
labels:
airshipit.org/deploy-k8s: "false"
name: clusterctl-v1
init-options: {}
providers:
- name: "aws"
type: "InfrastructureProvider"
url: "/manifests/capi/infra/aws/v0.3.0"
clusterctl-repository: true
- name: "custom-infra"
type: "InfrastructureProvider"
url: "/manifests/capi/custom-infra/aws/v0.3.0"
clusterctl-repository: true
- name: "custom-airship-infra"
type: "InfrastructureProvider"
versions:
v0.3.1: functions/capi/infrastructure/v0.3.1
v0.3.2: functions/capi/infrastructure/v0.3.2

View File

@ -0,0 +1,2 @@
resources:
- clusterctl.yaml

View File

@ -26,8 +26,9 @@ const (
// Constants related to Phases
const (
InitinfraPhase = "initinfra"
BootstrapPhase = "bootstrap"
InitinfraPhase = "initinfra"
ClusterctlPhase = InitinfraPhase
BootstrapPhase = "bootstrap"
)
// AllClusterTypes holds cluster types