Skip to content

Commit 03a2e20

Browse files
add more features to gcpmmp
fix tests fix goimports add comment lint generate fix fix
1 parent 0ea6fdd commit 03a2e20

12 files changed

+735
-163
lines changed

cloud/scope/managedmachinepool.go

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"strings"
2323

2424
"k8s.io/utils/pointer"
25+
infrav1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1"
2526
"sigs.k8s.io/cluster-api-provider-gcp/cloud"
2627
"sigs.k8s.io/cluster-api-provider-gcp/util/location"
2728

@@ -155,34 +156,68 @@ func (s *ManagedMachinePoolScope) NodePoolVersion() *string {
155156
return s.MachinePool.Spec.Template.Spec.Version
156157
}
157158

159+
// NodePoolResourceLabels returns the resource labels of the node pool.
160+
func NodePoolResourceLabels(additionalLabels infrav1.Labels, clusterName string) infrav1.Labels {
161+
resourceLabels := additionalLabels.DeepCopy()
162+
if resourceLabels == nil {
163+
resourceLabels = infrav1.Labels{}
164+
}
165+
resourceLabels[infrav1.ClusterTagKey(clusterName)] = string(infrav1.ResourceLifecycleOwned)
166+
return resourceLabels
167+
}
168+
158169
// ConvertToSdkNodePool converts a node pool to format that is used by GCP SDK.
159-
func ConvertToSdkNodePool(nodePool infrav1exp.GCPManagedMachinePool, machinePool clusterv1exp.MachinePool, regional bool) *containerpb.NodePool {
170+
func ConvertToSdkNodePool(nodePool infrav1exp.GCPManagedMachinePool, machinePool clusterv1exp.MachinePool, regional bool, clusterName string) *containerpb.NodePool {
160171
replicas := *machinePool.Spec.Replicas
161172
if regional {
162173
replicas /= cloud.DefaultNumRegionsPerZone
163174
}
164175
nodePoolName := nodePool.Spec.NodePoolName
165176
if len(nodePoolName) == 0 {
177+
// Use the GCPManagedMachinePool CR name if nodePoolName is not specified
166178
nodePoolName = nodePool.Name
167179
}
180+
// build node pool in GCP SDK format using the GCPManagedMachinePool spec
168181
sdkNodePool := containerpb.NodePool{
169182
Name: nodePoolName,
170183
InitialNodeCount: replicas,
171184
Config: &containerpb.NodeConfig{
172-
Labels: nodePool.Spec.KubernetesLabels,
173-
Taints: infrav1exp.ConvertToSdkTaint(nodePool.Spec.KubernetesTaints),
174-
Metadata: nodePool.Spec.AdditionalLabels,
185+
Labels: nodePool.Spec.KubernetesLabels,
186+
Taints: infrav1exp.ConvertToSdkTaint(nodePool.Spec.KubernetesTaints),
175187
ShieldedInstanceConfig: &containerpb.ShieldedInstanceConfig{
176188
EnableSecureBoot: pointer.BoolDeref(nodePool.Spec.NodeSecurity.EnableSecureBoot, false),
177189
EnableIntegrityMonitoring: pointer.BoolDeref(nodePool.Spec.NodeSecurity.EnableIntegrityMonitoring, false),
178190
},
191+
ResourceLabels: NodePoolResourceLabels(nodePool.Spec.AdditionalLabels, clusterName),
179192
},
180193
}
194+
if nodePool.Spec.MachineType != nil {
195+
sdkNodePool.Config.MachineType = *nodePool.Spec.MachineType
196+
}
197+
if nodePool.Spec.DiskSizeGb != nil {
198+
sdkNodePool.Config.DiskSizeGb = *nodePool.Spec.DiskSizeGb
199+
}
200+
if nodePool.Spec.ImageType != nil {
201+
sdkNodePool.Config.ImageType = *nodePool.Spec.ImageType
202+
}
203+
if nodePool.Spec.LocalSsdCount != nil {
204+
sdkNodePool.Config.LocalSsdCount = *nodePool.Spec.LocalSsdCount
205+
}
206+
if nodePool.Spec.DiskType != nil {
207+
sdkNodePool.Config.DiskType = string(*nodePool.Spec.DiskType)
208+
}
181209
if nodePool.Spec.Scaling != nil {
182-
sdkNodePool.Autoscaling = &containerpb.NodePoolAutoscaling{
183-
Enabled: true,
184-
MinNodeCount: *nodePool.Spec.Scaling.MinCount,
185-
MaxNodeCount: *nodePool.Spec.Scaling.MaxCount,
210+
sdkNodePool.Autoscaling = infrav1exp.ConvertToSdkAutoscaling(nodePool.Spec.Scaling)
211+
}
212+
if nodePool.Spec.Management != nil {
213+
sdkNodePool.Management = &containerpb.NodeManagement{
214+
AutoRepair: nodePool.Spec.Management.AutoRepair,
215+
AutoUpgrade: nodePool.Spec.Management.AutoUpgrade,
216+
}
217+
}
218+
if nodePool.Spec.MaxPodsPerNode != nil {
219+
sdkNodePool.MaxPodsConstraint = &containerpb.MaxPodsConstraint{
220+
MaxPodsPerNode: *nodePool.Spec.MaxPodsPerNode,
186221
}
187222
}
188223
if nodePool.Spec.InstanceType != nil {
@@ -234,10 +269,10 @@ func ConvertToSdkNodePool(nodePool infrav1exp.GCPManagedMachinePool, machinePool
234269
}
235270

236271
// ConvertToSdkNodePools converts node pools to format that is used by GCP SDK.
237-
func ConvertToSdkNodePools(nodePools []infrav1exp.GCPManagedMachinePool, machinePools []clusterv1exp.MachinePool, regional bool) []*containerpb.NodePool {
272+
func ConvertToSdkNodePools(nodePools []infrav1exp.GCPManagedMachinePool, machinePools []clusterv1exp.MachinePool, regional bool, clusterName string) []*containerpb.NodePool {
238273
res := []*containerpb.NodePool{}
239274
for i := range nodePools {
240-
res = append(res, ConvertToSdkNodePool(nodePools[i], machinePools[i], regional))
275+
res = append(res, ConvertToSdkNodePool(nodePools[i], machinePools[i], regional, clusterName))
241276
}
242277
return res
243278
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
})

cloud/scope/scope_suite_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package scope_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestScope(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Scope Suite")
13+
}

cloud/services/container/clusters/reconcile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func (s *Service) createCluster(ctx context.Context, log *logr.Logger) error {
265265
cluster.InitialClusterVersion = *s.scope.GCPManagedControlPlane.Spec.ControlPlaneVersion
266266
}
267267
if !s.scope.IsAutopilotCluster() {
268-
cluster.NodePools = scope.ConvertToSdkNodePools(nodePools, machinePools, isRegional)
268+
cluster.NodePools = scope.ConvertToSdkNodePools(nodePools, machinePools, isRegional, cluster.Name)
269269
}
270270

271271
createClusterRequest := &containerpb.CreateClusterRequest{

0 commit comments

Comments
 (0)