From 736c059f50cd8bc1425660c3357c2914a28a4b8e Mon Sep 17 00:00:00 2001 From: Robert Fratto Date: Fri, 2 Apr 2021 09:24:03 -0400 Subject: [PATCH] update k3d example to work with k3d v4.0.0 --- example/k3d/README.md | 13 +++++------- example/k3d/environment/main.jsonnet | 22 ++++++++++++-------- example/k3d/environment/spec.json | 2 +- example/k3d/scripts/create.bash | 10 ++++++---- example/k3d/scripts/merge_k3d.bash | 30 ---------------------------- 5 files changed, 26 insertions(+), 51 deletions(-) delete mode 100755 example/k3d/scripts/merge_k3d.bash diff --git a/example/k3d/README.md b/example/k3d/README.md index 5f19fe738594..4f4b6d84574c 100644 --- a/example/k3d/README.md +++ b/example/k3d/README.md @@ -9,7 +9,7 @@ that - Kubectl - Docker - [Tanka >= v0.9.2](https://github.com/grafana/tanka) -- [k3d >= v1.5.1 < v3.0.0](https://github.com/rancher/k3d) +- [k3d >= v4.0.0](https://github.com/rancher/k3d) ## Getting Started @@ -21,17 +21,14 @@ Run the following to create your cluster: # Create a new k3d cluster ./scripts/create.bash -# Merge the k3d cluster config with your local kubectl config -./scripts/merge_k3d.bash - # Import images into k3d if they are not available on docker hub -k3d import-images -n agent-k3d grafana/agent -k3d import-images -n agent-k3d grafana/agentctl +k3d image import -c agent-k3d grafana/agent:latest +k3d image import -c agent-k3d grafana/agentctl:latest tk apply ./environment -# Navigate to localhost:30080 in your browser to view dashboards +# Navigate to grafana.k3d.localhost:30080 in your browser to view dashboards # Delete the k3d cluster when you're done with it -k3d delete -n agent-k3d +k3d cluster delete agent-k3d ``` diff --git a/example/k3d/environment/main.jsonnet b/example/k3d/environment/main.jsonnet index 2809a58f0c62..569ac9aeee6d 100644 --- a/example/k3d/environment/main.jsonnet +++ b/example/k3d/environment/main.jsonnet @@ -6,7 +6,11 @@ local k = import 'ksonnet-util/kausal.libsonnet'; local loki_config = import 'default/loki_config.libsonnet'; local grafana_agent = import 'grafana-agent/v1/main.libsonnet'; +local ingress = k.networking.v1beta1.ingress; +local path = k.networking.v1beta1.httpIngressPath; +local rule = k.networking.v1beta1.ingressRule; local service = k.core.v1.service; + local images = { agent: 'grafana/agent:latest', agentctl: 'grafana/agentctl:latest', @@ -15,14 +19,16 @@ local images = { { default: default.new(namespace='default') { grafana+: { - // Expose Grafana on 30080 on the k3d agent, which is exposed to the host - // machine. - service+: - local bindNodePort(port) = port { nodePort: port.port + 30000 }; - service.mixin.spec.withPorts([ - { name: 'grafana', nodePort: 30080, port: 30080, targetPort: 80 }, - ]) + - service.mixin.spec.withType('NodePort'), + ingress+: + ingress.new('grafana-ingress') + + ingress.mixin.spec.withRules([ + rule.withHost('grafana.k3d.localhost') + + rule.http.withPaths([ + path.withPath('/') + + path.backend.withServiceName('grafana') + + path.backend.withServicePort(80), + ]), + ]), }, }, diff --git a/example/k3d/environment/spec.json b/example/k3d/environment/spec.json index 49c4aa1747e6..8c90817a7a99 100644 --- a/example/k3d/environment/spec.json +++ b/example/k3d/environment/spec.json @@ -5,7 +5,7 @@ "name": "default" }, "spec": { - "apiServer": "https://localhost:50443", + "apiServer": "https://0.0.0.0:50443", "namespace": "default" } } diff --git a/example/k3d/scripts/create.bash b/example/k3d/scripts/create.bash index 225957837196..d7ddcf245561 100755 --- a/example/k3d/scripts/create.bash +++ b/example/k3d/scripts/create.bash @@ -1,8 +1,10 @@ #!/usr/bin/env bash -k3d create \ - --name agent-k3d \ - --publish 30080:30080 \ +k3d cluster create agent-k3d \ + --port 30080:80@loadbalancer \ --api-port 50443 \ -v /var/lib/k3d/agent-k3d/storage/:/var/lib/rancher/k3s/storage/ \ - -v /etc/machine-id:/etc/machine-id + -v /etc/machine-id:/etc/machine-id \ + --kubeconfig-update-default=true \ + --kubeconfig-switch-context=true \ + --wait diff --git a/example/k3d/scripts/merge_k3d.bash b/example/k3d/scripts/merge_k3d.bash deleted file mode 100755 index d5940ca4901c..000000000000 --- a/example/k3d/scripts/merge_k3d.bash +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env bash -# -# merge_k3d_config.bash -# -# Depends on https://github.com/kislyuk/yq: -# pip install yq -set -e - -command -v yq >/dev/null 2>&1 || { - echo "yq is required but not found. install yq with 'pip install yq'." - exit 1 -} - -NEW_KUBECONFIG=$(k3d get-kubeconfig --name='agent-k3d') - -yq -s -y \ "(\ - .[0].clusters[0].name = \"agent-k3d\" | \ - .[0].users[0].name = \"agent-k3d\" | \ - .[0].contexts[0].context.cluster = \"agent-k3d\" | \ - .[0].contexts[0].context.user = \"agent-k3d\" | \ - .[0].contexts[0].name = \"agent-k3d\" | \ - .[0][\"current-context\"] = \"agent-k3d\" \ - ) | .[0]" $NEW_KUBECONFIG > ${NEW_KUBECONFIG}.bk - -mv ${NEW_KUBECONFIG}.bk $NEW_KUBECONFIG - -KUBECONFIG=${KUBECONFIG:=$HOME/.kube/config} -cp $KUBECONFIG ${KUBECONFIG}.bk -KUBECONFIG=$NEW_KUBECONFIG:${KUBECONFIG}.bk kubectl config view --raw > $KUBECONFIG -rm ${KUBECONFIG}.bk