|
| 1 | +package scope |
| 2 | + |
| 3 | +import ( |
| 4 | + "cloud.google.com/go/container/apiv1/containerpb" |
| 5 | + . "github.com/onsi/ginkgo/v2" |
| 6 | + . "github.com/onsi/gomega" |
| 7 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 8 | + infrav1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1" |
| 9 | + "sigs.k8s.io/cluster-api-provider-gcp/cloud" |
| 10 | + "sigs.k8s.io/cluster-api-provider-gcp/exp/api/v1beta1" |
| 11 | + clusterv1exp "sigs.k8s.io/cluster-api/exp/api/v1beta1" |
| 12 | +) |
| 13 | + |
| 14 | +var TestGCPMMP *v1beta1.GCPManagedMachinePool |
| 15 | +var TestMP *clusterv1exp.MachinePool |
| 16 | +var TestClusterName string |
| 17 | + |
| 18 | +var _ = Describe("GCPManagedMachinePool Scope", func() { |
| 19 | + BeforeEach(func() { |
| 20 | + TestClusterName = "test-cluster" |
| 21 | + gcpmmpName := "test-gcpmmp" |
| 22 | + nodePoolName := "test-pool" |
| 23 | + namespace := "capg-system" |
| 24 | + replicas := int32(1) |
| 25 | + |
| 26 | + TestGCPMMP = &v1beta1.GCPManagedMachinePool{ |
| 27 | + ObjectMeta: metav1.ObjectMeta{ |
| 28 | + Name: gcpmmpName, |
| 29 | + Namespace: namespace, |
| 30 | + }, |
| 31 | + Spec: v1beta1.GCPManagedMachinePoolSpec{ |
| 32 | + NodePoolName: nodePoolName, |
| 33 | + }, |
| 34 | + } |
| 35 | + TestMP = &clusterv1exp.MachinePool{ |
| 36 | + Spec: clusterv1exp.MachinePoolSpec{ |
| 37 | + Replicas: &replicas, |
| 38 | + }, |
| 39 | + } |
| 40 | + }) |
| 41 | + |
| 42 | + Context("Test NodePoolResourceLabels", func() { |
| 43 | + It("should append cluster owned label", func() { |
| 44 | + labels := infrav1.Labels{"test-key": "test-value"} |
| 45 | + |
| 46 | + Expect(NodePoolResourceLabels(labels, TestClusterName)).To(Equal(infrav1.Labels{ |
| 47 | + "test-key": "test-value", |
| 48 | + infrav1.ClusterTagKey(TestClusterName): string(infrav1.ResourceLifecycleOwned), |
| 49 | + })) |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + Context("Test ConvertToSdkNodePool", func() { |
| 54 | + It("should convert to SDK node pool with default values", func() { |
| 55 | + sdkNodePool := ConvertToSdkNodePool(*TestGCPMMP, *TestMP, false, TestClusterName) |
| 56 | + |
| 57 | + Expect(sdkNodePool).To(Equal(&containerpb.NodePool{ |
| 58 | + Name: TestGCPMMP.Spec.NodePoolName, |
| 59 | + InitialNodeCount: *TestMP.Spec.Replicas, |
| 60 | + Config: &containerpb.NodeConfig{ |
| 61 | + ResourceLabels: NodePoolResourceLabels(nil, TestClusterName), |
| 62 | + ShieldedInstanceConfig: &containerpb.ShieldedInstanceConfig{}, |
| 63 | + }, |
| 64 | + })) |
| 65 | + }) |
| 66 | + |
| 67 | + It("should convert to SDK node pool node count in a regional cluster", func() { |
| 68 | + replicas := int32(6) |
| 69 | + TestMP.Spec.Replicas = &replicas |
| 70 | + |
| 71 | + sdkNodePool := ConvertToSdkNodePool(*TestGCPMMP, *TestMP, true, TestClusterName) |
| 72 | + |
| 73 | + Expect(sdkNodePool).To(Equal(&containerpb.NodePool{ |
| 74 | + Name: TestGCPMMP.Spec.NodePoolName, |
| 75 | + InitialNodeCount: replicas / cloud.DefaultNumRegionsPerZone, |
| 76 | + Config: &containerpb.NodeConfig{ |
| 77 | + ResourceLabels: NodePoolResourceLabels(nil, TestClusterName), |
| 78 | + ShieldedInstanceConfig: &containerpb.ShieldedInstanceConfig{}, |
| 79 | + }, |
| 80 | + })) |
| 81 | + }) |
| 82 | + |
| 83 | + It("should convert to SDK node pool using GCPManagedMachinePool", func() { |
| 84 | + machineType := "n1-standard-1" |
| 85 | + diskSizeGb := int32(128) |
| 86 | + imageType := "ubuntu_containerd" |
| 87 | + localSsdCount := int32(2) |
| 88 | + diskType := v1beta1.SSD |
| 89 | + maxPodsPerNode := int64(20) |
| 90 | + enableAutoscaling := false |
| 91 | + scaling := v1beta1.NodePoolAutoScaling{ |
| 92 | + EnableAutoscaling: &enableAutoscaling, |
| 93 | + } |
| 94 | + labels := infrav1.Labels{"test-key": "test-value"} |
| 95 | + taints := v1beta1.Taints{ |
| 96 | + { |
| 97 | + Key: "test-key", |
| 98 | + Value: "test-value", |
| 99 | + Effect: "NoSchedule", |
| 100 | + }, |
| 101 | + } |
| 102 | + resourceLabels := infrav1.Labels{"test-key": "test-value"} |
| 103 | + |
| 104 | + TestGCPMMP.Spec.MachineType = &machineType |
| 105 | + TestGCPMMP.Spec.DiskSizeGb = &diskSizeGb |
| 106 | + TestGCPMMP.Spec.ImageType = &imageType |
| 107 | + TestGCPMMP.Spec.LocalSsdCount = &localSsdCount |
| 108 | + TestGCPMMP.Spec.DiskType = &diskType |
| 109 | + TestGCPMMP.Spec.Scaling = &scaling |
| 110 | + TestGCPMMP.Spec.MaxPodsPerNode = &maxPodsPerNode |
| 111 | + TestGCPMMP.Spec.KubernetesLabels = labels |
| 112 | + TestGCPMMP.Spec.KubernetesTaints = taints |
| 113 | + TestGCPMMP.Spec.AdditionalLabels = resourceLabels |
| 114 | + |
| 115 | + sdkNodePool := ConvertToSdkNodePool(*TestGCPMMP, *TestMP, false, TestClusterName) |
| 116 | + |
| 117 | + Expect(sdkNodePool).To(Equal(&containerpb.NodePool{ |
| 118 | + Name: TestGCPMMP.Spec.NodePoolName, |
| 119 | + InitialNodeCount: *TestMP.Spec.Replicas, |
| 120 | + Config: &containerpb.NodeConfig{ |
| 121 | + Labels: labels, |
| 122 | + Taints: v1beta1.ConvertToSdkTaint(taints), |
| 123 | + ResourceLabels: NodePoolResourceLabels(resourceLabels, TestClusterName), |
| 124 | + MachineType: machineType, |
| 125 | + DiskSizeGb: diskSizeGb, |
| 126 | + ImageType: imageType, |
| 127 | + LocalSsdCount: localSsdCount, |
| 128 | + DiskType: string(diskType), |
| 129 | + ShieldedInstanceConfig: &containerpb.ShieldedInstanceConfig{}, |
| 130 | + }, |
| 131 | + Autoscaling: v1beta1.ConvertToSdkAutoscaling(&scaling), |
| 132 | + MaxPodsConstraint: &containerpb.MaxPodsConstraint{ |
| 133 | + MaxPodsPerNode: maxPodsPerNode, |
| 134 | + }, |
| 135 | + })) |
| 136 | + }) |
| 137 | + }) |
| 138 | +}) |
0 commit comments