From ef8f31e1fbdf9cf6d89786732aac98c05da053e0 Mon Sep 17 00:00:00 2001 From: Spike Curtis Date: Mon, 6 Jun 2022 13:55:25 -0700 Subject: [PATCH 1/2] feat: K8s template uses authenticated environment Signed-off-by: Spike Curtis --- .../kubernetes-multi-service/README.md | 106 +++++++++--------- .../kubernetes-multi-service/main.tf | 59 ++-------- 2 files changed, 62 insertions(+), 103 deletions(-) diff --git a/examples/templates/kubernetes-multi-service/README.md b/examples/templates/kubernetes-multi-service/README.md index dd9944f4b227e..575c17eab8def 100644 --- a/examples/templates/kubernetes-multi-service/README.md +++ b/examples/templates/kubernetes-multi-service/README.md @@ -4,74 +4,70 @@ description: Get started with Kubernetes development. tags: [cloud, kubernetes] --- -# Authentication +# Getting started -This template has several ways to authenticate to a Kubernetes cluster. +## RBAC -## kubeconfig (Coder host) +The Coder provisioner requires permission to administer pods to use this template. The template +creates workspaces in a single Kubernetes namespace, using the `workspaces_namespace` parameter set +while creating the template. -If the Coder host has a local `~/.kube/config`, this can be used to authenticate with Coder. Make sure this is on the same user running the `coder` service. +Create a role as follows and bind it to the user or service account that runs the coder host. -## ServiceAccount +```yaml +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: coder +rules: +- apiGroups: [""] + resources: ["pods"] + verbs: ["*"] +``` -Create a ServiceAccount and role on your cluster to authenticate your template with Coder. +## Authentication -1. Run the following command on a device with Kubernetes context: +This template can authenticate using in-cluster authentication, or using a kubeconfig local to the +Coder host. For additional authentication options, consult the [Kubernetes provider +documentation](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs). - ```sh - CODER_NAMESPACE=default - kubectl apply -n $CODER_NAMESPACE -f - < Date: Mon, 6 Jun 2022 13:56:03 -0700 Subject: [PATCH 2/2] fmt Signed-off-by: Spike Curtis --- examples/templates/kubernetes-multi-service/main.tf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/templates/kubernetes-multi-service/main.tf b/examples/templates/kubernetes-multi-service/main.tf index 75b78a4c9edf6..cd5623d790db5 100644 --- a/examples/templates/kubernetes-multi-service/main.tf +++ b/examples/templates/kubernetes-multi-service/main.tf @@ -31,12 +31,12 @@ variable "workspaces_namespace" { type = string sensitive = true description = "The namespace to create workspaces in (must exist prior to creating workspaces)" - default = "coder-workspaces" + default = "coder-workspaces" } provider "kubernetes" { # Authenticate via ~/.kube/config or a Coder-specific ServiceAccount, depending on admin preferences - config_path = var.use_kubeconfig == true ? "~/.kube/config" : null + config_path = var.use_kubeconfig == true ? "~/.kube/config" : null } data "coder_workspace" "me" {} @@ -59,7 +59,7 @@ resource "coder_agent" "ubuntu" { resource "kubernetes_pod" "main" { count = data.coder_workspace.me.start_count metadata { - name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}" + name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}" namespace = var.workspaces_namespace } spec {