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

configure machine deployment #30

Merged
merged 1 commit into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A sample is built and hosted at `gcr.io/kubernetes1-226021/capd-manager:latest`

### External Dependencies

- `go, 1.12+`
- `go, 1.12+`
- `kind, >= 0.3.0`
- `kubectl`
- `docker`
Expand Down Expand Up @@ -44,7 +44,7 @@ Tested on: Linux, works ok on OS X sometimes

Make sure you have `kind` > 0.3.0 and `kubectl`.

1. Install capdctl:
1. Install capdctl:

`go install ./cmd/capdctl`

Expand Down
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 namespace of the machine deployment")
mo.clusterName = fs.String("cluster-name", "my-cluster", "The name of the cluster the machine deployment creates machines for")
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 object 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{
amy marked this conversation as resolved.
Show resolved Hide resolved
Replicas: &replicas,
Selector: metav1.LabelSelector{
MatchLabels: labels,
},
Template: v1alpha1.MachineTemplateSpec{
amy marked this conversation as resolved.
Show resolved Hide resolved
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
Spec: v1alpha1.MachineSpec{
amy marked this conversation as resolved.
Show resolved Hide resolved
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