Skip to content

Commit c98c71a

Browse files
committed
feat: Make flags more succinct and user-friendly
1 parent c85aa73 commit c98c71a

File tree

1 file changed

+61
-46
lines changed

1 file changed

+61
-46
lines changed

main.go

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,28 @@ var clusterTemplatePatchesFS embed.FS
3939

4040
func main() {
4141
var (
42-
targetNamespace = "default"
43-
kubernetesVersion = "1.24.0"
44-
controlPlaneMachineCount = 1
45-
workerMachineCount = 1
46-
podNetworkCIDR = "192.168.0.0/16"
47-
serviceCIDR = "10.96.0.0/12"
48-
controlPlaneMachineCPUCores = 2
49-
controlPlaneMachineMemorySize = resource.QuantityValue{Quantity: resource.MustParse("4Gi")}
50-
controlPlaneMachineKernelImage = "smartxworks/capch-kernel-5.15.12"
51-
controlPlaneMachineRootfsImage = "smartxworks/capch-rootfs-1.24.0"
52-
controlPlaneMachineRootfsSize = resource.QuantityValue{Quantity: resource.MustParse("4Gi")}
53-
workerMachineCPUCores = 2
54-
workerMachineMemorySize = resource.QuantityValue{Quantity: resource.MustParse("4Gi")}
55-
workerMachineKernelImage = "smartxworks/capch-kernel-5.15.12"
56-
workerMachineRootfsImage = "smartxworks/capch-rootfs-1.24.0"
57-
workerMachineRootfsSize = resource.QuantityValue{Quantity: resource.MustParse("4Gi")}
58-
persistent = false
59-
persistentControlPlaneMachineRootfsImage = "smartxworks/capch-rootfs-cdi-1.24.0"
60-
persistentWorkerMachineRootfsImage = "smartxworks/capch-rootfs-cdi-1.24.0"
61-
persistentMachineAddresses []string
62-
persistentHostClusterCNI string
63-
clusterTemplate string
42+
targetNamespace = "default"
43+
kubernetesVersion = "1.24.0"
44+
controlPlaneMachineCount = 1
45+
workerMachineCount = 1
46+
newControlPlaneMachineCount = -1
47+
newWorkerMachineCount = -1
48+
podNetworkCIDR = "192.168.0.0/16"
49+
serviceCIDR = "10.96.0.0/12"
50+
controlPlaneMachineCPUCores = 2
51+
controlPlaneMachineMemorySize = resource.QuantityValue{Quantity: resource.MustParse("4Gi")}
52+
controlPlaneMachineKernelImage = ""
53+
controlPlaneMachineRootfsImage = ""
54+
controlPlaneMachineRootfsSize = resource.QuantityValue{Quantity: resource.MustParse("4Gi")}
55+
workerMachineCPUCores = 2
56+
workerMachineMemorySize = resource.QuantityValue{Quantity: resource.MustParse("4Gi")}
57+
workerMachineKernelImage = ""
58+
workerMachineRootfsImage = ""
59+
workerMachineRootfsSize = resource.QuantityValue{Quantity: resource.MustParse("4Gi")}
60+
persistent = false
61+
machineAddresses []string
62+
hostClusterCNI string
63+
from string
6464
)
6565

6666
cmdCreate := &cobra.Command{
@@ -129,8 +129,8 @@ func main() {
129129
fmt.Sprintf("VIRTINK_WORKER_MACHINE_ROOTFS_IMAGE=%s", workerMachineRootfsImage),
130130
fmt.Sprintf("VIRTINK_WORKER_MACHINE_ROOTFS_SIZE=%s", workerMachineRootfsSize.String()))
131131

132-
if clusterTemplate != "" {
133-
generateCmd.Args = append(generateCmd.Args, "--from", clusterTemplate)
132+
if from != "" {
133+
generateCmd.Args = append(generateCmd.Args, "--from", from)
134134
} else {
135135
generateCmd.Args = append(generateCmd.Args, "--infrastructure", fmt.Sprintf("virtink:%s", VirtinkProviderVersion))
136136
if persistent {
@@ -142,21 +142,28 @@ func main() {
142142

143143
clusterTemplatePatches := map[string][]byte{}
144144
if persistent {
145+
if controlPlaneMachineRootfsImage == "" {
146+
controlPlaneMachineRootfsImage = "smartxworks/capch-rootfs-cdi-1.24.0"
147+
}
148+
if workerMachineRootfsImage == "" {
149+
workerMachineRootfsImage = "smartxworks/capch-rootfs-cdi-1.24.0"
150+
}
151+
145152
generateCmd.Env = append(generateCmd.Env,
146-
fmt.Sprintf("VIRTINK_CONTROL_PLANE_MACHINE_ROOTFS_CDI_IMAGE=%s", persistentControlPlaneMachineRootfsImage),
147-
fmt.Sprintf("VIRTINK_WORKER_MACHINE_ROOTFS_CDI_IMAGE=%s", persistentWorkerMachineRootfsImage),
148-
fmt.Sprintf("VIRTINK_NODE_ADDRESSES=[%v]", strings.Join(persistentMachineAddresses, ",")),
153+
fmt.Sprintf("VIRTINK_CONTROL_PLANE_MACHINE_ROOTFS_CDI_IMAGE=%s", controlPlaneMachineRootfsImage),
154+
fmt.Sprintf("VIRTINK_WORKER_MACHINE_ROOTFS_CDI_IMAGE=%s", workerMachineRootfsImage),
155+
fmt.Sprintf("VIRTINK_NODE_ADDRESSES=[%v]", strings.Join(machineAddresses, ",")),
149156
)
150157

151-
if persistentHostClusterCNI != "" {
158+
if hostClusterCNI != "" {
152159
var patchFileName string
153-
switch persistentHostClusterCNI {
160+
switch hostClusterCNI {
154161
case "calico":
155162
patchFileName = filepath.Join("cluster-template-patches", "calico-static-ip-and-mac.yaml")
156163
case "kube-ovn":
157164
patchFileName = filepath.Join("cluster-template-patches", "kube-ovn-static-ip-and-mac.yaml")
158165
default:
159-
return fmt.Errorf("unsupported host cluster CNI: %s", persistentHostClusterCNI)
166+
return fmt.Errorf("unsupported host cluster CNI: %s", hostClusterCNI)
160167
}
161168

162169
patchBytes, err := clusterTemplatePatchesFS.ReadFile(patchFileName)
@@ -166,6 +173,19 @@ func main() {
166173
clusterTemplatePatches[patchFileName] = patchBytes
167174
}
168175
} else {
176+
if controlPlaneMachineKernelImage == "" {
177+
controlPlaneMachineKernelImage = "smartxworks/capch-kernel-5.15.12"
178+
}
179+
if controlPlaneMachineRootfsImage == "" {
180+
controlPlaneMachineRootfsImage = "smartxworks/capch-rootfs-1.24.0"
181+
}
182+
if workerMachineKernelImage == "" {
183+
workerMachineKernelImage = "smartxworks/capch-kernel-5.15.12"
184+
}
185+
if workerMachineRootfsImage == "" {
186+
workerMachineRootfsImage = "smartxworks/capch-rootfs-1.24.0"
187+
}
188+
169189
generateCmd.Env = append(generateCmd.Env,
170190
fmt.Sprintf("VIRTINK_CONTROL_PLANE_MACHINE_ROOTFS_IMAGE=%s", controlPlaneMachineRootfsImage),
171191
fmt.Sprintf("VIRTINK_WORKER_MACHINE_ROOTFS_IMAGE=%s", workerMachineRootfsImage))
@@ -270,11 +290,9 @@ func main() {
270290
cmdCreate.PersistentFlags().StringVar(&workerMachineRootfsImage, "worker-machine-rootfs-image", workerMachineRootfsImage, "The rootfs image of worker machine.")
271291
cmdCreate.PersistentFlags().Var(&workerMachineRootfsSize, "worker-machine-rootfs-size", "The rootfs size of each worker machine.")
272292
cmdCreate.PersistentFlags().BoolVar(&persistent, "persistent", persistent, "The machines of the nested cluster will be persistent, include persistent storage and IP address.")
273-
cmdCreate.PersistentFlags().StringVar(&persistentControlPlaneMachineRootfsImage, "persistent-control-plane-machine-rootfs-image", persistentControlPlaneMachineRootfsImage, "The rootfs image of persisten control plane machine.")
274-
cmdCreate.PersistentFlags().StringVar(&persistentWorkerMachineRootfsImage, "persistent-worker-machine-rootfs-image", persistentWorkerMachineRootfsImage, "The rootfs image of persistent worker machine.")
275-
cmdCreate.PersistentFlags().StringSliceVar(&persistentMachineAddresses, "persistent-machine-addresses", persistentMachineAddresses, "The candidate IP addresses for persistent machines of nested cluster.")
276-
cmdCreate.PersistentFlags().StringVar(&persistentHostClusterCNI, "persistent-host-cluster-cni", persistentHostClusterCNI, "The CNI of the host cluster, support 'calico' and 'kube-ovn'.")
277-
cmdCreate.PersistentFlags().StringVar(&clusterTemplate, "cluster-template", clusterTemplate, fmt.Sprintf("The URL of the cluster template to use for the nested cluster. If unspecified, the cluster template of cluster-api-provider-virtink %s will be used.", VirtinkProviderVersion))
293+
cmdCreate.PersistentFlags().StringSliceVar(&machineAddresses, "machine-addresses", machineAddresses, "The candidate IP addresses for persistent machines of nested cluster.")
294+
cmdCreate.PersistentFlags().StringVar(&hostClusterCNI, "host-cluster-cni", hostClusterCNI, "The CNI of the host cluster, support 'calico' and 'kube-ovn'.")
295+
cmdCreate.PersistentFlags().StringVar(&from, "from", from, fmt.Sprintf("The URL of the cluster template to use for the nested cluster. If unspecified, the cluster template of cluster-api-provider-virtink %s will be used.", VirtinkProviderVersion))
278296

279297
cmdDelete := &cobra.Command{
280298
Use: "delete CLUSTER",
@@ -303,31 +321,28 @@ func main() {
303321
}
304322

305323
cmdScale := &cobra.Command{
306-
Use: "scale CLUSTER CONTROL_PLANE_MACHINE_COUNT:WORKER_MACHINE_COUNT",
307-
Args: cobra.ExactArgs(2),
324+
Use: "scale CLUSTER",
325+
Args: cobra.ExactArgs(1),
308326
Short: "Scale a nested cluster.",
309327
RunE: func(cmd *cobra.Command, args []string) error {
310-
counts := strings.Split(args[1], ":")
311-
if len(counts) != 2 {
312-
return fmt.Errorf("invalid machine counts argument")
313-
}
314-
315-
if counts[0] != "" {
328+
if newControlPlaneMachineCount > 0 {
316329
if err := runCommand(exec.Command("kubectl", "patch", "kubeadmcontrolplane.controlplane.cluster.x-k8s.io", fmt.Sprintf("%s-cp", args[0]),
317-
"--namespace", targetNamespace, "--type", "merge", "--patch", fmt.Sprintf("{\"spec\":{\"replicas\":%s}}", counts[0]))); err != nil {
330+
"--namespace", targetNamespace, "--type", "merge", "--patch", fmt.Sprintf("{\"spec\":{\"replicas\":%d}}", newControlPlaneMachineCount))); err != nil {
318331
return fmt.Errorf("set control-plane replicas: %s", err)
319332
}
320333
}
321334

322-
if counts[1] != "" {
335+
if newWorkerMachineCount >= 0 {
323336
if err := runCommand(exec.Command("kubectl", "patch", "machinedeployment.cluster.x-k8s.io", fmt.Sprintf("%s-md-0", args[0]),
324-
"--namespace", targetNamespace, "--type", "merge", "--patch", fmt.Sprintf("{\"spec\":{\"replicas\":%s}}", counts[1]))); err != nil {
337+
"--namespace", targetNamespace, "--type", "merge", "--patch", fmt.Sprintf("{\"spec\":{\"replicas\":%d}}", newWorkerMachineCount))); err != nil {
325338
return fmt.Errorf("set worker replicas: %s", err)
326339
}
327340
}
328341
return nil
329342
},
330343
}
344+
cmdScale.PersistentFlags().IntVar(&newControlPlaneMachineCount, "control-plane-machine-count", newControlPlaneMachineCount, "The number of control plane machines for the nested cluster.")
345+
cmdScale.PersistentFlags().IntVar(&newWorkerMachineCount, "worker-machine-count", newWorkerMachineCount, "The number of worker machines for the nested cluster.")
331346

332347
var versionOutput string
333348
cmdVersion := &cobra.Command{

0 commit comments

Comments
 (0)