Skip to content
This repository has been archived by the owner on Sep 24, 2021. It is now read-only.

Commit

Permalink
confige machine deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
Amy Chen committed Jun 27, 2019
1 parent e9226bc commit 9d1e0ac
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions cmd/capdctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ func (mo *machineOptions) initFlags(fs *flag.FlagSet) {
mo.version = fs.String("version", "v1.14.2", "The Kubernetes version to run")
}

type machineDeyploymentOptions struct {
name, namespace, clusterName, kubeletVersion *string
replicas *int
}

func (mo *machineDeyploymentOptions) initFlags(fs *flag.FlagSet) {
mo.name = fs.String("name", "my-machine-deployment", "The name of the machine deployment")
mo.namespace = fs.String("namespace", "my-namespace", "The namespece of the machine deployment")
mo.clusterName = fs.String("cluster-name", "my-cluster", "The name of the cluster the machine belongs to")
mo.kubeletVersion = fs.String("kubelet-version", "v1.14.2", "The Kubernetes kubelet version to run")
mo.replicas = fs.Int("replicas", 1, "The number of replicas")
}

func main() {
setup := flag.NewFlagSet("setup", flag.ExitOnError)
managementClusterName := setup.String("cluster-name", "kind", "The name of the management cluster")
Expand All @@ -71,6 +84,10 @@ func main() {
clusterName := cluster.String("cluster-name", "my-cluster", "The name of the cluster")
clusterNamespace := cluster.String("namespace", "my-namespace", "The namespace the cluster belongs to")

machineDeployment := flag.NewFlagSet("machine-deployment", flag.ExitOnError)
machineDeploymentOpts := new(machineDeyploymentOptions)
machineDeploymentOpts.initFlags(machineDeployment)

if len(os.Args) < 2 {
fmt.Println("At least one subcommand is requied.")
fmt.Println(usage())
Expand All @@ -94,6 +111,9 @@ func main() {
case "cluster":
cluster.Parse(os.Args[2:])
fmt.Fprintf(os.Stdout, clusterYAML(*clusterName, *clusterNamespace))
case "machine-deployment":
machineDeployment.Parse(os.Args[2:])
fmt.Fprint(os.Stdout, machineDeploymentYAML(machineDeploymentOpts))
case "help":
fmt.Println(usage())
default:
Expand Down Expand Up @@ -124,6 +144,9 @@ subcommands are:
cluster - Write a capd cluster object to stdout
example: capdctl cluster -cluster-name my-cluster -namespace my-namespace | kubectl apply -f -
machine-deployment - Write a machine deployment objecto to stdout
example: capdctl machine-deployment -name my-machine-deployment -cluster-name my-cluster -namespace my-namespace -kubelet-version v1.14.2 -replicas 1| kubectl apply -f -
`
}

Expand All @@ -143,6 +166,49 @@ spec:
providerSpec: {}`, name, namespace)
}

func machineDeploymentYAML(opts *machineDeyploymentOptions) string {
replicas := int32(*opts.replicas)
labels := map[string]string{
"cluster.k8s.io/cluster-name": *opts.clusterName,
"set": "node",
}
deployment := v1alpha1.MachineDeployment{
TypeMeta: metav1.TypeMeta{
Kind: "MachineDeployment",
APIVersion: "cluster.k8s.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: *opts.name,
Namespace: *opts.namespace,
Labels: labels,
},
Spec: v1alpha1.MachineDeploymentSpec{
Replicas: &replicas,
Selector: metav1.LabelSelector{
MatchLabels: labels,
},
Template: v1alpha1.MachineTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
Spec: v1alpha1.MachineSpec{
ProviderSpec: v1alpha1.ProviderSpec{},
Versions: v1alpha1.MachineVersionInfo{
Kubelet: *opts.kubeletVersion,
},
},
},
},
}

b, err := json.Marshal(deployment)
// TODO don't panic on the error
if err != nil {
panic(err)
}
return string(b)
}

func machineYAML(opts *machineOptions) string {
machine := v1alpha1.Machine{
TypeMeta: metav1.TypeMeta{
Expand Down

0 comments on commit 9d1e0ac

Please sign in to comment.