Skip to content

Commit

Permalink
Adds utilities to speed up dev process (kubernetes-sigs#177)
Browse files Browse the repository at this point in the history
cluster-api-dev-helper encodes a few common development tasks
such as stand up and teardown within minikube, tailing logs etc...

minikube_build adds a faster development experience by utilising
the Docker endpoint in Minikube so images do not need to be pushed
to an external repository.

Signed-off-by: Naadir Jeewa <[email protected]>
  • Loading branch information
randomvariable authored and k8s-ci-robot committed Oct 6, 2018
1 parent c2566d5 commit 20d2ffc
Show file tree
Hide file tree
Showing 13 changed files with 581 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@ vendor
*.swp

# envfile
.env
envfile
minikube.kubeconfig

# binaries
cmd/cluster-controller/cluster-controller
cmd/machine-controller/machine-controller
64 changes: 45 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,38 @@
# See the License for the specific language governing permissions and
# limitations under the License.

GOFLAGS += -ldflags '-extldflags "-static"'
GOREBUILD :=

.PHONY: gendeepcopy

all: generate build images

depend:
ifndef FASTBUILD
# If FASTBUILD isn't defined, fully rebuild Go binaries, and always
# run dep ensure
GOREBUILD += -a
.PHONY: vendor
endif

vendor:
dep version || go get -u github.com/golang/dep/cmd/dep
dep ensure

depend-update:
dep version || go get -u github.com/golang/dep/cmd/dep
dep ensure -update

generate: gendeepcopy

gendeepcopy: depend
gendeepcopy: vendor
go build -o $$GOPATH/bin/deepcopy-gen sigs.k8s.io/cluster-api-provider-aws/vendor/k8s.io/code-generator/cmd/deepcopy-gen
$$GOPATH/bin/deepcopy-gen \
-i ./cloud/aws/providerconfig,./cloud/aws/providerconfig/v1alpha1 \
-O zz_generated.deepcopy \
-h boilerplate.go.txt

genmocks: depend
genmocks: vendor
hack/generate-mocks.sh "github.com/aws/aws-sdk-go/service/ec2/ec2iface EC2API" "cloud/aws/services/ec2/mock_ec2iface/mock.go"
hack/generate-mocks.sh "github.com/aws/aws-sdk-go/service/elb/elbiface ELBAPI" "cloud/aws/services/elb/mock_elbiface/mock.go"
hack/generate-mocks.sh "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset/typed/cluster/v1alpha1 MachineInterface" "cloud/aws/actuators/machine/mock_machineiface/mock.go"
Expand All @@ -41,45 +52,48 @@ genmocks: depend
hack/generate-mocks.sh "sigs.k8s.io/cluster-api-provider-aws/cloud/aws/services EC2Interface" "cloud/aws/services/mocks/ec2.go"
hack/generate-mocks.sh "sigs.k8s.io/cluster-api-provider-aws/cloud/aws/services ELBInterface" "cloud/aws/services/mocks/elb.go"

build: depend clusterctl-bin clusterawsadm-bin cluster-controller machine-controller
build: clusterctl-bin clusterawsadm-bin cluster-controller machine-controller

clusterctl-bin: vendor
CGO_ENABLED=0 go install $(GOFLAGS) $(GOREBUILD) sigs.k8s.io/cluster-api-provider-aws/clusterctl

clusterctl-bin:
CGO_ENABLED=0 go install -a -ldflags '-extldflags "-static"' sigs.k8s.io/cluster-api-provider-aws/clusterctl
clusterawsadm-bin: vendor
CGO_ENABLED=0 go install $(GOFLAGS) $(GOREBUILD) sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm

clusterawsadm-bin:
CGO_ENABLED=0 go install -a -ldflags '-extldflags "-static"' sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm
cluster-api-dev-helper-bin: vendor
CGO_ENABLED=0 go install $(GOFLAGS) sigs.k8s.io/cluster-api-provider-aws/hack/cluster-api-dev-helper

images: depend
images: vendor
$(MAKE) -C cmd/cluster-controller image
$(MAKE) -C cmd/machine-controller image

dev_push: depend cluster-controller-dev-push machine-controller-dev-push
dev_push: cluster-controller-dev-push machine-controller-dev-push

cluster-controller:
CGO_ENABLED=0 go install -a -ldflags '-extldflags "-static"' sigs.k8s.io/cluster-api-provider-aws/cmd/cluster-controller
cluster-controller: vendor
CGO_ENABLED=0 go install $(GOFLAGS) $(GOREBUILD) sigs.k8s.io/cluster-api-provider-aws/cmd/cluster-controller

cluster-controller-dev-push: cluster-controller
$(MAKE) -C cmd/cluster-controller dev_push

machine-controller:
CGO_ENABLED=0 go install -a -ldflags '-extldflags "-static"' sigs.k8s.io/cluster-api-provider-aws/cmd/machine-controller
machine-controller: vendor
CGO_ENABLED=0 go install $(GOFLAGS) $(GOREBUILD) sigs.k8s.io/cluster-api-provider-aws/cmd/machine-controller

machine-controller-dev-push: machine-controller
$(MAKE) -C cmd/machine-controller dev_push

push: depend
push: vendor
$(MAKE) -C cmd/cluster-controller push
$(MAKE) -C cmd/machine-controller push

check: depend fmt vet
check: fmt vet

test: depend
test: vendor
go test -race -cover ./cmd/... ./cloud/... ./clusterctl/...

fmt:
fmt: vendor
hack/verify-gofmt.sh

vet:
vet: vendor
go vet ./...

lint:
Expand All @@ -100,3 +114,15 @@ envfile: envfile.example

clean:
rm -rf clusterctl/examples/aws/out

# These should become unnecessary after Bazelification

minikube_build: cluster-controller-minikube-build machine-controller-minikube-build

cluster-controller-minikube-build: vendor
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build $(GOFLAGS) -o cmd/cluster-controller/cluster-controller sigs.k8s.io/cluster-api-provider-aws/cmd/cluster-controller
$(MAKE) -C cmd/cluster-controller minikube_build

machine-controller-minikube-build: vendor
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build $(GOFLAGS) -o cmd/machine-controller/machine-controller sigs.k8s.io/cluster-api-provider-aws/cmd/machine-controller
$(MAKE) -C cmd/machine-controller minikube_build
22 changes: 22 additions & 0 deletions cmd/cluster-controller/Dockerfile.minikube
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2018 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.



# Final container
FROM debian:stretch-slim
RUN apt-get update && apt-get install -y ca-certificates openssh-server && rm -rf /var/lib/apt/lists/*

# We don't care about reproducability whilst developing, so copy our local binary
COPY cluster-controller .
3 changes: 3 additions & 0 deletions cmd/cluster-controller/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@ dev_image:

dev_push: dev_image
docker push "$(DEV_PREFIX)/$(NAME):$(DEV_TAG)"

minikube_build:
eval $$(minikube docker-env) && docker build -t "$(DEV_PREFIX)/$(NAME):$(DEV_TAG)" -f ./Dockerfile.minikube .
20 changes: 20 additions & 0 deletions cmd/machine-controller/Dockerfile.minikube
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2018 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Final container
FROM debian:stretch-slim
RUN apt-get update && apt-get install -y ca-certificates openssh-server && rm -rf /var/lib/apt/lists/*

# We don't care about reproducability whilst developing, so copy our local binary
COPY machine-controller .
3 changes: 3 additions & 0 deletions cmd/machine-controller/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@ dev_image:

dev_push: dev_image
docker push "$(DEV_PREFIX)/$(NAME):$(DEV_TAG)"

minikube_build:
eval $$(minikube docker-env) && docker build -t "$(DEV_PREFIX)/$(NAME):$(DEV_TAG)" -f ./Dockerfile.minikube .
23 changes: 23 additions & 0 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
- [Using images on Google Cloud](#using-images-on-google-cloud)
- [Using AWS Elastic Container Registry](#using-aws-elastic-container-registry)
- [Using images on Elastic Container Registry](#using-images-on-elastic-container-registry)
- [Using Minikube only](#using-minikube-only)
- [cluster-api-dev-helper](#cluster-api-dev-helper)

<!-- /TOC -->

Expand Down Expand Up @@ -85,6 +87,27 @@ Then generate the [example configuration](getting-started.md#generating-cluster-
You will also need to configure minikube or your bootstrap cluster with credentials to access ECR, using [image pull secrets][image_pull_secrets]
or another mechanism.

#### Using Minikube only

You can also build and test using only the local storage on the minikube VM,
using:

``` bash
make minikube_build FASTBUILD=true
```

## cluster-api-dev-helper

Some command development tasks have been put into a cluster-api-dev-helper
utility in the /hack directory.

To install it, run:

``` bash
make cluster-api-dev-helper-bin FASTBUILD=true
```


<!-- References -->

[jq]: https://stedolan.github.io/jq/download/
Expand Down
49 changes: 49 additions & 0 deletions hack/cluster-api-dev-helper/cmd/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"log"
"os"
"os/exec"
)

func runShellWithWait(cmd string) {
runCommandWithWait("sh", "-c", cmd)
}

func runCommandWithWait(cmd string, args ...string) bool {
command := runCommand(cmd, args...)
if err := command.Wait(); err != nil {
log.Println(err)
}
return command.ProcessState.Success()
}

func runShell(cmd string) *exec.Cmd {
return runCommand("sh", "-c", cmd)
}

func runCommand(cmd string, args ...string) *exec.Cmd {
command := exec.Command(cmd, args...)
command.Stdout = os.Stdout
command.Stderr = os.Stderr
if err := command.Start(); err != nil {
log.Println(err)
}
return command
}
114 changes: 114 additions & 0 deletions hack/cluster-api-dev-helper/cmd/minikube.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

func defineMinikubeCmd(parent *cobra.Command) {
newCmd := &cobra.Command{
Use: "minikube",
Short: "minikube helper commands",
Long: `minikube helper commands`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(cmd.Help())
},
}

defineMinikubeFixLibVirtNetworkCmd(newCmd)
defineMinikubeClean(newCmd)
defineMinikubeKVMDestroy(newCmd)
defineMinikubeSetup(newCmd)
defineMinikubeReset(newCmd)

parent.AddCommand(newCmd)
}

func defineMinikubeFixLibVirtNetworkCmd(parent *cobra.Command) {
newCmd := &cobra.Command{
Use: "fix-libvirt-network",
Short: "Make the KVM minikube network permanent using LibVirt",
Long: `Make the KVM minikube network permanent using LibVirt. Requires virsh`,
Run: func(cmd *cobra.Command, args []string) {
runCommandWithWait("virsh", "-c", "qemu:///system", "net-autostart", "minikube-net")
},
}
parent.AddCommand(newCmd)
}

func defineMinikubeClean(parent *cobra.Command) {
newCmd := &cobra.Command{
Use: "clean",
Short: "Clean up .minikube directory",
Long: `Clean up .minikube directory, deleting machines`,
Run: func(cmd *cobra.Command, args []string) {
minikubeClean()
},
}
parent.AddCommand(newCmd)
}

func defineMinikubeKVMDestroy(parent *cobra.Command) {
newCmd := &cobra.Command{
Use: "kvm-destroy",
Short: "Destroy the KVM VM backing minikube",
Long: `Destroy the KVM VM backing minikube`,
Run: func(cmd *cobra.Command, args []string) {
runCommandWithWait("virsh", "-c", "qemu:///system", "destroy", "minikube")
runCommandWithWait("virsh", "-c", "qemu:///system", "undefine", "--remove-all-storage", "--delete-snapshots", "minikube")
runCommandWithWait("virsh", "-c", "qemu:///system", "net-undefine", "minikube-net")
minikubeClean()
},
}
parent.AddCommand(newCmd)
}

func defineMinikubeSetup(parent *cobra.Command) {
newCmd := &cobra.Command{
Use: "setup",
Short: "Set up minikube for use with Cluster API",
Long: `Set up minikube for use with Cluster API`,
Run: func(cmd *cobra.Command, args []string) {
runCommandWithWait("minikube", "config", "set", "bootstrapper", "kubeadm")
runCommandWithWait("minikube", "config", "set", "kubernetes-version", "v1.9.4")
},
}
parent.AddCommand(newCmd)
}

func defineMinikubeReset(parent *cobra.Command) {
newCmd := &cobra.Command{
Use: "reset",
Short: "Remove Cluster API specific minikube configuration",
Long: `Remove Cluster API specific minikube configuration`,
Run: func(cmd *cobra.Command, args []string) {
runCommandWithWait("minikube", "config", "unset", "kubernetes-version")
runCommandWithWait("minikube", "config", "unset", "bootstrapper")
},
}
parent.AddCommand(newCmd)
}

func minikubeClean() {
runCommandWithWait("sh", "-c", "rm -rf $HOME/.minikube/machines")
runCommandWithWait("sh", "-c", "rm -rf $HOME/.minikube/profiles")
runCommandWithWait("sh", "-c", "rm -rf $HOME/.minikube/certs")
runCommandWithWait("sh", "-c", "rm -rf $HOME/.minikube/*.pem")
}
Loading

0 comments on commit 20d2ffc

Please sign in to comment.