Skip to content
This repository was archived by the owner on Oct 10, 2023. It is now read-only.

Commit 4cf5800

Browse files
committed
Add MachineDeployment specifier to node pool
Adds a command line flag to specify the MachineDeployment to use as a base when creating a new node pool
1 parent 95c7834 commit 4cf5800

File tree

3 files changed

+68
-16
lines changed

3 files changed

+68
-16
lines changed

cmd/cli/plugin/cluster/set_node_pool.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import (
1717
)
1818

1919
type clusterSetNodePoolCmdOptions struct {
20-
FilePath string
21-
Namespace string
20+
FilePath string
21+
Namespace string
22+
MachineDeploymentBase string
2223
}
2324

2425
var setNodePoolOptions clusterSetNodePoolCmdOptions
@@ -32,6 +33,7 @@ var clusterSetNodePoolCmd = &cobra.Command{
3233
func init() {
3334
clusterSetNodePoolCmd.Flags().StringVarP(&setNodePoolOptions.FilePath, "file", "f", "", "The file describing the node pool (required)")
3435
clusterSetNodePoolCmd.Flags().StringVar(&setNodePoolOptions.Namespace, "namespace", "default", "The namespace the cluster is found in.")
36+
clusterSetNodePoolCmd.Flags().StringVar(&setNodePoolOptions.MachineDeploymentBase, "machine-deployment-base", "", "The machine deployment to use as a base for creating a new node pool")
3537
_ = clusterSetNodePoolCmd.MarkFlagRequired("file")
3638
clusterNodePoolCmd.AddCommand(clusterSetNodePoolCmd)
3739
}
@@ -64,6 +66,7 @@ func SetNodePool(server *v1alpha1.Server, clusterName string) error {
6466
if err = yaml.Unmarshal(fileContent, &nodePool); err != nil {
6567
return errors.Wrap(err, "Could not parse file contents")
6668
}
69+
nodePool.MachineDeploymentBase = setNodePoolOptions.MachineDeploymentBase
6770

6871
options := client.SetMachineDeploymentOptions{
6972
ClusterName: clusterName,

pkg/v1/tkg/client/machine_deployment.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,19 @@ type DeleteMachineDeploymentOptions struct {
5454

5555
// NodePool a struct describing a node pool
5656
type NodePool struct {
57-
Name string `yaml:"name"`
58-
Replicas *int32 `yaml:"replicas,omitempty"`
59-
AZ string `yaml:"az,omitempty"`
60-
NodeMachineType string `yaml:"nodeMachineType,omitempty"`
61-
Labels *map[string]string `yaml:"labels,omitempty"`
62-
VSphere VSphereNodePool `yaml:"vsphere,omitempty"`
63-
Taints *[]corev1.Taint `yaml:"taints,omitempty"`
64-
VMClass string `yaml:"vmClass,omitempty"`
65-
StorageClass string `yaml:"storageClass,omitempty"`
66-
Volumes *[]tkgsv1alpha2.Volume `yaml:"volumes,omitempty"`
67-
TKR tkgsv1alpha2.TKRReference `yaml:"tkr,omitempty"`
68-
NodeDrainTimeout *metav1.Duration `yaml:"nodeDrainTimeout,omitempty"`
57+
Name string `yaml:"name"`
58+
Replicas *int32 `yaml:"replicas,omitempty"`
59+
AZ string `yaml:"az,omitempty"`
60+
NodeMachineType string `yaml:"nodeMachineType,omitempty"`
61+
Labels *map[string]string `yaml:"labels,omitempty"`
62+
VSphere VSphereNodePool `yaml:"vsphere,omitempty"`
63+
Taints *[]corev1.Taint `yaml:"taints,omitempty"`
64+
VMClass string `yaml:"vmClass,omitempty"`
65+
StorageClass string `yaml:"storageClass,omitempty"`
66+
Volumes *[]tkgsv1alpha2.Volume `yaml:"volumes,omitempty"`
67+
TKR tkgsv1alpha2.TKRReference `yaml:"tkr,omitempty"`
68+
NodeDrainTimeout *metav1.Duration `yaml:"nodeDrainTimeout,omitempty"`
69+
MachineDeploymentBase string `yaml:"machineDeploymentBase,omitempty"`
6970
}
7071

7172
// VSphereNodePool a struct describing properties necessary for a node pool on vSphere
@@ -132,6 +133,13 @@ func DoSetMachineDeployment(clusterClient clusterclient.Client, options *SetMach
132133
update = true
133134
break
134135
}
136+
if workerMDs[i].Name == options.MachineDeploymentBase {
137+
baseMD = workerMDs[i]
138+
}
139+
}
140+
141+
if !update && (options.MachineDeploymentBase != "" && baseMD.Name != options.MachineDeploymentBase) {
142+
return errors.Errorf("unable to find base machine deployment with name %s", options.MachineDeploymentBase)
135143
}
136144

137145
baseMD.Annotations = map[string]string{}
@@ -146,6 +154,10 @@ func DoSetMachineDeployment(clusterClient clusterclient.Client, options *SetMach
146154
}
147155

148156
if !update {
157+
if options.MachineDeploymentBase == "" {
158+
log.Warningf("Using machine deployment %s as baseline for new node pool", baseMD.Name)
159+
}
160+
149161
kcTemplateName := baseMD.Spec.Template.Spec.Bootstrap.ConfigRef.Name
150162
kcTemplate, err := retrieveKubeadmConfigTemplate(clusterClient, kcTemplateName, options.Namespace)
151163
if err != nil {

pkg/v1/tkg/client/machine_deployment_test.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,7 +1403,12 @@ var _ = Describe("Machine Deployment", func() {
14031403
},
14041404
},
14051405
}
1406-
clusterClient.GetMDObjectForClusterReturns([]capi.MachineDeployment{md1}, nil)
1406+
md3.Spec.Template.Labels = map[string]string{
1407+
"existing": "md3",
1408+
}
1409+
md3.Spec.Template.Spec.InfrastructureRef.Kind = constants.AWSMachineTemplate
1410+
md3.Spec.Selector.MatchLabels = map[string]string{}
1411+
clusterClient.GetMDObjectForClusterReturns([]capi.MachineDeployment{md1, md3}, nil)
14071412
awsMachineTemplate = aws.AWSMachineTemplate{
14081413
ObjectMeta: metav1.ObjectMeta{
14091414
Name: "test-cluster-np-1-mt",
@@ -1439,7 +1444,38 @@ var _ = Describe("Machine Deployment", func() {
14391444
clusterClient.CreateResourceReturnsOnCall(0, nil)
14401445
clusterClient.CreateResourceReturnsOnCall(1, nil)
14411446
clusterClient.CreateNamespaceReturnsOnCall(2, nil)
1442-
When("Machine Deployment creates successfully", func() {
1447+
When("Machine Deployment creates successfully from default md", func() {
1448+
It("should properly set all values for created resources", func() {
1449+
Expect(err).ToNot(HaveOccurred())
1450+
Expect(clusterClient.CreateResourceCallCount()).To(Equal(3))
1451+
1452+
obj, _, _, _ := clusterClient.CreateResourceArgsForCall(0)
1453+
kct := obj.(*v1alpha3.KubeadmConfigTemplate)
1454+
Expect(kct.Annotations).To(Equal(map[string]string{}))
1455+
Expect(kct.ResourceVersion).To(Equal(""))
1456+
Expect(kct.Name).To(Equal("test-cluster-np-2-kct"))
1457+
Expect(kct.Spec.Template.Spec.JoinConfiguration.NodeRegistration.KubeletExtraArgs["node-labels"]).To(Equal("key1=value1,key2=value2"))
1458+
1459+
obj, _, _, _ = clusterClient.CreateResourceArgsForCall(1)
1460+
mt := obj.(*aws.AWSMachineTemplate)
1461+
Expect(mt.Name).To(Equal("test-cluster-np-2-mt"))
1462+
Expect(mt.Annotations).To(Equal(map[string]string{}))
1463+
Expect(mt.ResourceVersion).To(Equal(""))
1464+
Expect(mt.Spec.Template.Spec.InstanceType).To(Equal(options.NodeMachineType))
1465+
1466+
obj, _, _, _ = clusterClient.CreateResourceArgsForCall(2)
1467+
md := obj.(*capi.MachineDeployment)
1468+
Expect(md.Name).To(Equal("test-cluster-np-2"))
1469+
Expect(md.Annotations).To(Equal(map[string]string{}))
1470+
Expect(md.ResourceVersion).To(Equal(""))
1471+
Expect(*md.Spec.Template.Spec.FailureDomain).To(Equal(options.AZ))
1472+
Expect(md.Spec.Replicas).To(Equal(options.Replicas))
1473+
})
1474+
})
1475+
When("Machine Deployment creates successfully from user selected md", func() {
1476+
BeforeEach(func() {
1477+
options.MachineDeploymentBase = "test-cluster-np-3"
1478+
})
14431479
It("should properly set all values for created resources", func() {
14441480
Expect(err).ToNot(HaveOccurred())
14451481
Expect(clusterClient.CreateResourceCallCount()).To(Equal(3))
@@ -1465,6 +1501,7 @@ var _ = Describe("Machine Deployment", func() {
14651501
Expect(md.ResourceVersion).To(Equal(""))
14661502
Expect(*md.Spec.Template.Spec.FailureDomain).To(Equal(options.AZ))
14671503
Expect(md.Spec.Replicas).To(Equal(options.Replicas))
1504+
Expect(md.Spec.Template.Labels).To(HaveKeyWithValue("existing", "md3"))
14681505
})
14691506
})
14701507
})

0 commit comments

Comments
 (0)