Skip to content

Commit

Permalink
add client process for CloudStack datacenter config (#5032)
Browse files Browse the repository at this point in the history
  • Loading branch information
cxbrowne1207 authored Feb 22, 2023
1 parent 8d1dc2c commit 9418b06
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/cluster/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cluster
// default processors to build a Config.
func NewDefaultConfigClientBuilder() *ConfigClientBuilder {
return NewConfigClientBuilder().Register(
getCloudStackDatacenter,
getTinkerbellMachineConfigs,
getTinkerbellDatacenter,
getDockerDatacenter,
Expand Down
20 changes: 19 additions & 1 deletion pkg/cluster/cloudstack.go
Original file line number Diff line number Diff line change
@@ -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{
Expand Down Expand Up @@ -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
}
61 changes: 61 additions & 0 deletions pkg/cluster/cloudstack_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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",
},
},
},
}
}

0 comments on commit 9418b06

Please sign in to comment.