Skip to content

Commit 8d973ff

Browse files
committed
Add kaniko example
1 parent b9cb7b2 commit 8d973ff

36 files changed

Lines changed: 9992 additions & 72 deletions

File tree

cmd/up.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type UpCmdFlags struct {
4242
build bool
4343
sync bool
4444
deploy bool
45+
switchContext bool
4546
portforwarding bool
4647
verboseSync bool
4748
container string
@@ -54,6 +55,7 @@ var UpFlagsDefault = &UpCmdFlags{
5455
initRegistries: true,
5556
build: false,
5657
sync: true,
58+
switchContext: false,
5759
deploy: false,
5860
portforwarding: true,
5961
verboseSync: false,
@@ -93,6 +95,7 @@ Starts and connects your DevSpace:
9395
cobraCmd.Flags().BoolVar(&cmd.flags.verboseSync, "verbose-sync", cmd.flags.verboseSync, "When enabled the sync will log every file change")
9496
cobraCmd.Flags().BoolVar(&cmd.flags.portforwarding, "portforwarding", cmd.flags.portforwarding, "Enable port forwarding")
9597
cobraCmd.Flags().BoolVarP(&cmd.flags.deploy, "deploy", "d", cmd.flags.deploy, "Force chart deployment")
98+
cobraCmd.Flags().BoolVar(&cmd.flags.switchContext, "switch-context", cmd.flags.switchContext, "Switch kubectl context to the devspace context")
9699
}
97100

98101
// Run executes the command logic
@@ -114,7 +117,7 @@ func (cmd *UpCmd) Run(cobraCmd *cobra.Command, args []string) {
114117
}
115118

116119
// Create kubectl client
117-
cmd.kubectl, err = kubectl.NewClient()
120+
cmd.kubectl, err = kubectl.NewClientWithContextSwitch(cmd.flags.switchContext)
118121
if err != nil {
119122
log.Fatalf("Unable to create new kubectl client: %v", err)
120123
}
@@ -189,7 +192,7 @@ func (cmd *UpCmd) ensureClusterRoleBinding() error {
189192

190193
_, err := cmd.kubectl.RbacV1beta1().ClusterRoleBindings().Get(clusterRoleBindingName, metav1.GetOptions{})
191194
if err != nil {
192-
clusterConfig, _ := kubectl.GetClientConfig()
195+
clusterConfig, _ := kubectl.GetClientConfig(false)
193196
if clusterConfig.AuthProvider != nil && clusterConfig.AuthProvider.Name == "gcp" {
194197
createRoleBinding := stdinutil.GetFromStdin(&stdinutil.GetFromStdinParams{
195198
Question: "Do you want the ClusterRoleBinding '" + clusterRoleBindingName + "' to be created automatically? (yes|no)",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
logs/
2+
overwrite.yaml
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
version: v1alpha1
2+
cluster:
3+
cloudProvider: devspace-cloud
4+
devSpace:
5+
deployments:
6+
- helm:
7+
chartPath: ./chart
8+
name: devspace-default
9+
ports:
10+
- labelSelector:
11+
release: devspace-default
12+
portMappings:
13+
- localPort: 8080
14+
remotePort: 8080
15+
sync:
16+
- containerPath: /app
17+
labelSelector:
18+
release: devspace-default
19+
localSubPath: ./
20+
uploadExcludePaths:
21+
- Dockerfile
22+
- .devspace/
23+
- chart/
24+
images:
25+
default:
26+
build:
27+
kaniko:
28+
cache: true
29+
namespace: ""
30+
name: yourdockername/kaniko

examples/kaniko/.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Dockerfile
2+
.devspace/
3+
chart/

examples/kaniko/Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM golang:1.11
2+
3+
RUN mkdir -p "$GOPATH/src/app" && ln -s $GOPATH/src/app /app
4+
ADD . $GOPATH/src/app
5+
6+
RUN cd $GOPATH/src/app && go get ./... && go build . && cd /app
7+
8+
WORKDIR /app
9+
CMD ["$GOPATH/src/app/app"]

examples/kaniko/README.md

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,111 @@
11
# Kaniko example
22

3-
In this example is shown, how kaniko can be used instead of docker to build and push an docker image.
3+
This example shows how kaniko can be used instead of docker to build and push an docker image directly inside the cluster.
4+
5+
# Step 0: Prerequisites
6+
7+
In order for this example to work you need access to a docker registry, where you can push images to (e.g. hub.docker.com, gcr.io etc.). There are two options how you can push images to registries with devspace.
8+
9+
## Option 1: Use docker credentials store
10+
If you have docker installed, devspace can take the required auth information directly out of the docker credentials store and will create the needed secret for you in the target cluster automatically. Make sure you are logged in the registry with `docker login`.
11+
12+
## Option 2: Provide auth information yourself
13+
As a second option you can provide your credentials directly in the config.yaml. See example below:
14+
15+
```yaml
16+
images:
17+
default:
18+
build:
19+
kaniko:
20+
cache: true
21+
namespace: ""
22+
# Don't prefix image name with registry url
23+
name: name/devspace
24+
registry: myRegistry
25+
registries:
26+
myRegistry:
27+
# Registry url here
28+
url: gcr.io
29+
auth:
30+
username: my-user
31+
password: my-password
32+
```
33+
34+
devspace will then automatically create a secret for you which kaniko can use to push to that registry.
35+
36+
## Optional: Use self hosted cluster (minikube, GKS etc.) instead of devspace-cloud
37+
38+
If you want to use your own cluster instead of the devspace-cloud as deployment target, make sure `kubectl` is configured correctly to access the target cluster. Then just erase the `cluster` section in the `.devspace/config.yaml` and devspace will use your current kubectl context as deployment target.
39+
40+
# Step 1: Start the devspace
41+
42+
To deploy the application to the target cluster simply run `devspace up`. The output of the command should look similar to this:
43+
44+
```
45+
[INFO] Building image 'fabian1991/devspace' with engine 'kaniko'
46+
[DONE] √ Authentication successful (hub.docker.com)
47+
[DONE] √ Kaniko build pod started
48+
[DONE] √ Uploaded files to container
49+
[INFO] build >>> WARN[0001] Error while retrieving image from cache: getting image from path: open /cache/sha256:df8db4a3a7dee9782e0f1bdcc9d676bc2de0dc1d2dc2952d9b9b3718445b1455: no such file or directory
50+
[INFO] build >>> INFO[0001] Downloading base image golang:1.11
51+
[INFO] build >>> ERROR: logging before flag.Parse: E1018 09:27:56.335503 34 metadata.go:142] while reading 'google-dockercfg' metadata: http status code: 404 while fetching url http://metadata.google.internal./computeMetadata/v1/instance/attributes/google-dockercfg
52+
[INFO] build >>> ERROR: logging before flag.Parse: E1018 09:27:56.337941 34 metadata.go:159] while reading 'google-dockercfg-url' metadata: http status code: 404 while fetching url http://metadata.google.internal./computeMetadata/v1/instance/attributes/google-dockercfg-url
53+
[INFO] build >>> INFO[0002] Executing 0 build triggers
54+
[INFO] build >>> INFO[0002] Extracting layer 0
55+
[INFO] build >>> INFO[0017] Extracting layer 1
56+
[INFO] build >>> INFO[0020] Extracting layer 2
57+
[INFO] build >>> INFO[0021] Extracting layer 3
58+
[INFO] build >>> INFO[0040] Extracting layer 4
59+
[INFO] build >>> INFO[0060] Extracting layer 5
60+
[INFO] build >>> INFO[0103] Extracting layer 6
61+
[INFO] build >>> INFO[0104] Taking snapshot of full filesystem...
62+
[INFO] build >>> INFO[0503] RUN mkdir -p "$GOPATH/src/app" && ln -s $GOPATH/src/app /app
63+
[INFO] build >>> INFO[0503] Checking for cached layer index.docker.io/fabian1991/devspace/cache:f141885fc82d849f3eba2d72bf74ce9842b3f9874ef5b003dd9b846726ee46b4...
64+
[INFO] build >>> ERROR: logging before flag.Parse: E1018 09:36:18.645323 34 metadata.go:142] while reading 'google-dockercfg' metadata: http status code: 404 while fetching url http://metadata.google.internal./computeMetadata/v1/instance/attributes/google-dockercfg
65+
[INFO] build >>> ERROR: logging before flag.Parse: E1018 09:36:18.648540 34 metadata.go:159] while reading 'google-dockercfg-url' metadata: http status code: 404 while fetching url http://metadata.google.internal./computeMetadata/v1/instance/attributes/google-dockercfg-url
66+
[INFO] build >>> INFO[0505] No cached layer found, executing command...
67+
[INFO] build >>> INFO[0505] cmd: /bin/sh
68+
[INFO] build >>> INFO[0505] args: [-c mkdir -p "$GOPATH/src/app" && ln -s $GOPATH/src/app /app]
69+
[INFO] build >>> INFO[0506] Using files from context: [/src]
70+
[INFO] build >>> INFO[0506] ADD . $GOPATH/src/app
71+
[INFO] build >>> INFO[0506] RUN cd $GOPATH/src/app && go get ./... && go build . && cd /app
72+
[INFO] build >>> INFO[0506] Checking for cached layer index.docker.io/fabian1991/devspace/cache:c58d7863dc5744a3c34de75247c0fa5f6d0a0bcaeb46981ff1c190470bc277a4...
73+
[INFO] build >>> INFO[0507] No cached layer found, executing command...
74+
[INFO] build >>> INFO[0507] cmd: /bin/sh
75+
[INFO] build >>> INFO[0507] args: [-c cd $GOPATH/src/app && go get ./... && go build . && cd /app]
76+
[INFO] build >>> INFO[0533] WORKDIR /app
77+
[INFO] build >>> INFO[0533] cmd: workdir
78+
[INFO] build >>> INFO[0533] Changed working directory to /app
79+
[INFO] build >>> INFO[0533] CMD ["$GOPATH/src/app/app"]
80+
[INFO] build >>> INFO[0533] Taking snapshot of full filesystem...
81+
[INFO] build >>> ERROR: logging before flag.Parse: E1018 09:42:58.250833 34 metadata.go:142] while reading 'google-dockercfg' metadata: http status code: 404 while fetching url http://metadata.google.internal./computeMetadata/v1/instance/attributes/google-dockercfg
82+
[INFO] build >>> ERROR: logging before flag.Parse: E1018 09:42:58.255121 34 metadata.go:159] while reading 'google-dockercfg-url' metadata: http status code: 404 while fetching url http://metadata.google.internal./computeMetadata/v1/instance/attributes/google-dockercfg-url
83+
[INFO] build >>> 2018/10/18 09:42:59 mounted blob: sha256:bc9ab73e5b14b9fbd3687a4d8c1f1360533d6ee9ffc3f5ecc6630794b40257b7
84+
[INFO] build >>> 2018/10/18 09:42:59 mounted blob: sha256:997731689cfbc58c8e74f2a20079338ce66965a40b21f27169b3d5a45ab61cbd
85+
[INFO] build >>> 2018/10/18 09:42:59 mounted blob: sha256:1bc310ac474b880a5e4aeec02e6423d1304d137f1a8990074cb3ac6386a0b654
86+
[INFO] build >>> 2018/10/18 09:42:59 mounted blob: sha256:e5c3f8c317dc30af45021092a3d76f16ba7aa1ee5f18fec742c84d4960818580
87+
[INFO] build >>> 2018/10/18 09:42:59 mounted blob: sha256:193a6306c92af328dbd41bbbd3200a2c90802624cccfe5725223324428110d7f
88+
[INFO] build >>> 2018/10/18 09:42:59 mounted blob: sha256:202760eb4a0043cd84cd9971c47052617855ff653abec8ae479e89d369afd500
89+
[INFO] build >>> 2018/10/18 09:42:59 mounted blob: sha256:a587a86c9dcb9df6584180042becf21e36ecd8b460a761711227b4b06889a005
90+
[INFO] build >>> 2018/10/18 09:43:00 pushed blob sha256:3299c5b4c55d202d9faab27d32f34cda6622d1f3d2d40c9ff30d16949aed41dc
91+
[INFO] build >>> 2018/10/18 09:43:03 pushed blob sha256:c425aa94dbf6aa35a519a459df4536d905cc4be08ecb803ad2721a917c27c4d2
92+
[INFO] build >>> 2018/10/18 09:43:03 index.docker.io/fabian1991/devspace:82i7ApF: digest: sha256:16639a35fd55e1b982e50b6ae1a4039d224c820e5fb4387ba9819d816e922cc1 size: 1578
93+
[DONE] √ Done building image
94+
[INFO] Image pushed to registry (hub.docker.com)
95+
[DONE] √ Done building and pushing image 'fabian1991/devspace'
96+
[INFO] Deploying devspace-default with helm
97+
[DONE] √ Deployed helm chart (Release revision: 2)
98+
[DONE] √ Successfully deployed devspace-default
99+
[DONE] √ Sync started on /go-workspace/src/github.com/covexo/devspace/examples/kaniko <-> /app (Pod: e388779b2b49465855bb0322057a9fff/devspace-default-864d677f99-t5488)
100+
root@devspace-default-864d677f99-t5488:/go/src/app#
101+
```
102+
103+
The command created a new kubernetes namespace for you in the devspace-cloud and built your Dockerfile with a kaniko build pod and pushed it to the target docker registry. Afterwards, it deployed the chart in the `chart` folder to that namespace. It also created a new kubectl context for you. If you want to access kubernetes resources via kubectl in the devspace-cloud you can simply change your kubectl context via `devspace up --switch-context`. Now you can check the running pods via `kubectl get po`.
104+
105+
Furthermore a bi-directional sync was started between the local folder `/go-workspace/src/github.com/covexo/devspace/examples/kaniko` and `/app` within the docker container. Whenever you change a file in either of those two folders the change will be synchronized. In addition the container port 8080 was forwarded to your local port 8080.
106+
107+
# Step 2: Start developing
108+
109+
You can start the server now with `go run main.go` in the open terminal. Now navigate in your browser to `localhost:8080` and you should see the output 'Hello World!'.
110+
111+
Change something in `main.go` and re-run `go run main.go`. Now just refresh your browser and you should see the changes immediately.

examples/kaniko/chart/Chart.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name: my-app
2+
version: v0.0.1
3+
description: A Kubernetes-Native Application
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apiVersion: extensions/v1beta1
2+
kind: Deployment
3+
metadata:
4+
name: {{ .Release.Name }}
5+
labels:
6+
release: "{{ .Release.Name }}"
7+
spec:
8+
replicas: 1
9+
template:
10+
metadata:
11+
annotations:
12+
revision: "{{ .Release.Revision }}"
13+
labels:
14+
release: "{{ .Release.Name }}"
15+
spec:
16+
containers:
17+
- name: default
18+
image: "{{ .Values.containers.default.image }}"
19+
command:
20+
{{- range $index, $command := .Values.containers.default.command }}
21+
- "{{ $command }}"
22+
{{- end }}
23+
args:
24+
{{- range $index, $arg := .Values.containers.default.args }}
25+
- "{{ $arg }}"
26+
{{- end }}
27+
imagePullSecrets:
28+
{{- range $index, $secretName := .Values.pullSecrets }}
29+
- name: "{{ $secretName }}"
30+
{{- end }}

examples/kaniko/chart/values.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
containers:
2+
default:
3+
image: "will be overriden by devspace up"
4+
command:
5+
- sleep
6+
args:
7+
- 99999999
8+
9+
pullSecrets: []

examples/kaniko/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import (
66
)
77

88
func handler(w http.ResponseWriter, r *http.Request) {
9-
fmt.Fprintf(w, "Hello World, I'm Golang!")
9+
fmt.Fprintf(w, "Hello World!")
1010
}
1111

1212
func main() {
1313
http.HandleFunc("/", handler)
1414
http.ListenAndServe(":8080", nil)
15+
16+
fmt.Println("Started server on :8080")
1517
}

0 commit comments

Comments
 (0)