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

Commit 7deac5c

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 8f3451b commit 7deac5c

File tree

3 files changed

+77
-16
lines changed

3 files changed

+77
-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 (ignored for TKGs)")
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
@@ -55,18 +55,19 @@ type DeleteMachineDeploymentOptions struct {
5555

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

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

138146
baseMD.Annotations = map[string]string{}
@@ -147,6 +155,10 @@ func DoSetMachineDeployment(clusterClient clusterclient.Client, options *SetMach
147155
}
148156

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

pkg/v1/tkg/client/machine_deployment_test.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,15 @@ var _ = Describe("Machine Deployment", func() {
12291229
},
12301230
}
12311231
})
1232+
When("the user selected machine deployment doesn't exist", func() {
1233+
BeforeEach(func() {
1234+
clusterClient.GetMDObjectForClusterReturns([]capi.MachineDeployment{md1}, nil)
1235+
options.MachineDeploymentBase = "test-cluster-md-3"
1236+
})
1237+
It("should return an error", func() {
1238+
Expect(err).Should(MatchError("unable to find base machine deployment with name test-cluster-md-3"))
1239+
})
1240+
})
12321241
Context("vSphere machine deployment", func() {
12331242
var vSphereMachineTemplate vsphere.VSphereMachineTemplate
12341243
BeforeEach(func() {
@@ -1402,7 +1411,12 @@ var _ = Describe("Machine Deployment", func() {
14021411
},
14031412
},
14041413
}
1405-
clusterClient.GetMDObjectForClusterReturns([]capi.MachineDeployment{md1}, nil)
1414+
md3.Spec.Template.Labels = map[string]string{
1415+
"existing": "md3",
1416+
}
1417+
md3.Spec.Template.Spec.InfrastructureRef.Kind = constants.AWSMachineTemplate
1418+
md3.Spec.Selector.MatchLabels = map[string]string{}
1419+
clusterClient.GetMDObjectForClusterReturns([]capi.MachineDeployment{md1, md3}, nil)
14061420
awsMachineTemplate = aws.AWSMachineTemplate{
14071421
ObjectMeta: metav1.ObjectMeta{
14081422
Name: "test-cluster-np-1-mt",
@@ -1438,7 +1452,38 @@ var _ = Describe("Machine Deployment", func() {
14381452
clusterClient.CreateResourceReturnsOnCall(0, nil)
14391453
clusterClient.CreateResourceReturnsOnCall(1, nil)
14401454
clusterClient.CreateNamespaceReturnsOnCall(2, nil)
1441-
When("Machine Deployment creates successfully", func() {
1455+
When("Machine Deployment creates successfully from default md", func() {
1456+
It("should properly set all values for created resources", func() {
1457+
Expect(err).ToNot(HaveOccurred())
1458+
Expect(clusterClient.CreateResourceCallCount()).To(Equal(3))
1459+
1460+
obj, _, _, _ := clusterClient.CreateResourceArgsForCall(0)
1461+
kct := obj.(*v1beta1.KubeadmConfigTemplate)
1462+
Expect(kct.Annotations).To(Equal(map[string]string{}))
1463+
Expect(kct.ResourceVersion).To(Equal(""))
1464+
Expect(kct.Name).To(Equal("test-cluster-np-2-kct"))
1465+
Expect(kct.Spec.Template.Spec.JoinConfiguration.NodeRegistration.KubeletExtraArgs["node-labels"]).To(Equal("key1=value1,key2=value2"))
1466+
1467+
obj, _, _, _ = clusterClient.CreateResourceArgsForCall(1)
1468+
mt := obj.(*aws.AWSMachineTemplate)
1469+
Expect(mt.Name).To(Equal("test-cluster-np-2-mt"))
1470+
Expect(mt.Annotations).To(Equal(map[string]string{}))
1471+
Expect(mt.ResourceVersion).To(Equal(""))
1472+
Expect(mt.Spec.Template.Spec.InstanceType).To(Equal(options.NodeMachineType))
1473+
1474+
obj, _, _, _ = clusterClient.CreateResourceArgsForCall(2)
1475+
md := obj.(*capi.MachineDeployment)
1476+
Expect(md.Name).To(Equal("test-cluster-np-2"))
1477+
Expect(md.Annotations).To(Equal(map[string]string{}))
1478+
Expect(md.ResourceVersion).To(Equal(""))
1479+
Expect(*md.Spec.Template.Spec.FailureDomain).To(Equal(options.AZ))
1480+
Expect(md.Spec.Replicas).To(Equal(options.Replicas))
1481+
})
1482+
})
1483+
When("Machine Deployment creates successfully from user selected md", func() {
1484+
BeforeEach(func() {
1485+
options.MachineDeploymentBase = "test-cluster-np-3"
1486+
})
14421487
It("should properly set all values for created resources", func() {
14431488
Expect(err).ToNot(HaveOccurred())
14441489
Expect(clusterClient.CreateResourceCallCount()).To(Equal(3))
@@ -1464,6 +1509,7 @@ var _ = Describe("Machine Deployment", func() {
14641509
Expect(md.ResourceVersion).To(Equal(""))
14651510
Expect(*md.Spec.Template.Spec.FailureDomain).To(Equal(options.AZ))
14661511
Expect(md.Spec.Replicas).To(Equal(options.Replicas))
1512+
Expect(md.Spec.Template.Labels).To(HaveKeyWithValue("existing", "md3"))
14671513
})
14681514
})
14691515
})

0 commit comments

Comments
 (0)