From 9540c8715b0f545de84874a7fc74ce0c19ebbfd4 Mon Sep 17 00:00:00 2001 From: Cavaughn Browne <113555337+cxbrowne1207@users.noreply.github.com> Date: Wed, 22 Feb 2023 17:11:11 -0600 Subject: [PATCH] add client process for CloudStack datacenter config (#5032) --- pkg/cluster/build.go | 1 + pkg/cluster/cloudstack.go | 20 ++++++++++- pkg/cluster/cloudstack_test.go | 61 ++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/pkg/cluster/build.go b/pkg/cluster/build.go index e40695b64aff..2e0d654d1cbc 100644 --- a/pkg/cluster/build.go +++ b/pkg/cluster/build.go @@ -4,6 +4,7 @@ package cluster // default processors to build a Config. func NewDefaultConfigClientBuilder() *ConfigClientBuilder { return NewConfigClientBuilder().Register( + getCloudStackDatacenter, getTinkerbellMachineConfigs, getTinkerbellDatacenter, getDockerDatacenter, diff --git a/pkg/cluster/cloudstack.go b/pkg/cluster/cloudstack.go index e06170c62be7..e5be7b48c4c2 100644 --- a/pkg/cluster/cloudstack.go +++ b/pkg/cluster/cloudstack.go @@ -1,6 +1,10 @@ package cluster -import anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1" +import ( + "context" + + anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1" +) func cloudstackEntry() *ConfigManagerEntry { return &ConfigManagerEntry{ @@ -88,3 +92,17 @@ func processCloudStackMachineConfig(c *Config, objects ObjectLookup, machineRef c.CloudStackMachineConfigs[m.GetName()] = m.(*anywherev1.CloudStackMachineConfig) } + +func getCloudStackDatacenter(ctx context.Context, client Client, c *Config) error { + if c.Cluster.Spec.DatacenterRef.Kind != anywherev1.CloudStackDatacenterKind { + return nil + } + + datacenter := &anywherev1.CloudStackDatacenterConfig{} + if err := client.Get(ctx, c.Cluster.Spec.DatacenterRef.Name, c.Cluster.Namespace, datacenter); err != nil { + return err + } + + c.CloudStackDatacenter = datacenter + return nil +} diff --git a/pkg/cluster/cloudstack_test.go b/pkg/cluster/cloudstack_test.go index 2320e95528ec..6cb8ae2a9bcd 100644 --- a/pkg/cluster/cloudstack_test.go +++ b/pkg/cluster/cloudstack_test.go @@ -1,10 +1,14 @@ package cluster_test import ( + "context" "testing" . "github.com/onsi/gomega" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "github.com/aws/eks-anywhere/internal/test" + anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1" "github.com/aws/eks-anywhere/pkg/cluster" ) @@ -15,3 +19,60 @@ func TestParseConfigMissingCloudstackDatacenter(t *testing.T) { g.Expect(err).To(Not(HaveOccurred())) g.Expect(got.CloudStackDatacenter).To(BeNil()) } + +func TestDefaultConfigClientBuilderBuildCloudStackClusterSuccess(t *testing.T) { + g := NewWithT(t) + ctx := context.Background() + b := cluster.NewDefaultConfigClientBuilder() + cluster := cloudStackCluster() + datacenter := &anywherev1.CloudStackDatacenterConfig{ + TypeMeta: metav1.TypeMeta{ + Kind: anywherev1.CloudStackDatacenterKind, + APIVersion: anywherev1.GroupVersion.String(), + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "datacenter", + Namespace: "default", + }, + } + + client := test.NewFakeKubeClient(datacenter) + config, err := b.Build(ctx, client, cluster) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(config).NotTo(BeNil()) + g.Expect(config.Cluster).To(Equal(cluster)) + g.Expect(config.CloudStackDatacenter).To(Equal(datacenter)) +} + +func TestDefaultConfigClientBuilderBuildCloudStackClusterFailure(t *testing.T) { + g := NewWithT(t) + ctx := context.Background() + b := cluster.NewDefaultConfigClientBuilder() + cluster := cloudStackCluster() + client := test.NewFakeKubeClient() + _, err := b.Build(ctx, client, cluster) + g.Expect(err).To(MatchError(ContainSubstring("cloudstackdatacenterconfigs.anywhere.eks.amazonaws.com \"datacenter\" not found"))) +} + +func cloudStackCluster() *anywherev1.Cluster { + return &anywherev1.Cluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-cluster", + Namespace: "default", + }, + Spec: anywherev1.ClusterSpec{ + DatacenterRef: anywherev1.Ref{ + Kind: anywherev1.CloudStackDatacenterKind, + Name: "datacenter", + }, + ControlPlaneConfiguration: anywherev1.ControlPlaneConfiguration{ + Count: 1, + }, + WorkerNodeGroupConfigurations: []anywherev1.WorkerNodeGroupConfiguration{ + { + Name: "md-0", + }, + }, + }, + } +}