|
| 1 | +# coder-templates: Templates to aid development with Noelware's Charts Platform with Coder (https://coder.com) |
| 2 | +# Copyright (c) 2022 Noelware <team@noelware.org> |
| 3 | +# |
| 4 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +# of this software and associated documentation files (the "Software"), to deal |
| 6 | +# in the Software without restriction, including without limitation the rights |
| 7 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +# copies of the Software, and to permit persons to whom the Software is |
| 9 | +# furnished to do so, subject to the following conditions: |
| 10 | +# |
| 11 | +# The above copyright notice and this permission notice shall be included in all |
| 12 | +# copies or substantial portions of the Software. |
| 13 | +# |
| 14 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +# SOFTWARE. |
| 21 | + |
| 22 | +terraform { |
| 23 | + required_providers { |
| 24 | + coder = { |
| 25 | + source = "coder/coder" |
| 26 | + version = "0.6.5" |
| 27 | + } |
| 28 | + |
| 29 | + kubernetes = { |
| 30 | + source = "hashicorp/kubernetes" |
| 31 | + version = "2.16.1" |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +provider "kubernetes" { |
| 37 | + config_path = var.use_host_kubeconfig == true ? "~/.kube/config" : null |
| 38 | + config_context = try(var.kube_context, "") != "" ? var.kube_context : null |
| 39 | +} |
| 40 | + |
| 41 | +data "coder_workspace" "me" { |
| 42 | +} |
| 43 | + |
| 44 | +resource "coder_agent" "main" { |
| 45 | + arch = "amd64" |
| 46 | + dir = var.dir |
| 47 | + os = "linux" |
| 48 | + |
| 49 | + startup_script = <<-EOF |
| 50 | + #!/bin/bash |
| 51 | +
|
| 52 | + # Install Docker on the container, since we need the client to connect to the |
| 53 | + # privileged pod. |
| 54 | + sudo apt update |
| 55 | + sudo apt install ca-certificates curl gnupg lsb-release |
| 56 | +
|
| 57 | + sudo mkdir -p /etc/apt/keyrings |
| 58 | + curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg |
| 59 | +
|
| 60 | + sudo apt update && sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-buildx-plugin |
| 61 | + sudo systemctl enable --now docker |
| 62 | +
|
| 63 | + sudo usermod -aG docker $USER |
| 64 | +
|
| 65 | + # Now, let's enable dotfiles |
| 66 | + ${var.dotfiles_repo != "" ? "coder dotfiles -y ${var.dotfiles_repo}" : ""} |
| 67 | + git clone https://github.com/charted-dev/charted /home/noel/workspace |
| 68 | + EOF |
| 69 | +} |
| 70 | + |
| 71 | +resource "kubernetes_persistent_volume_claim" "charted-workspace" { |
| 72 | + metadata { |
| 73 | + namespace = var.namespace |
| 74 | + name = "charted-server-workspace" |
| 75 | + } |
| 76 | + |
| 77 | + wait_until_bound = false |
| 78 | + spec { |
| 79 | + access_modes = ["ReadWriteMany"] |
| 80 | + resources { |
| 81 | + requests = { |
| 82 | + storage = "20Gi" |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +resource "kubernetes_pod" "charted-server" { |
| 89 | + metadata { |
| 90 | + namespace = var.namespace |
| 91 | + name = "charted-server" |
| 92 | + |
| 93 | + labels = { |
| 94 | + "k8s.noelware.cloud/component" = "cloud", |
| 95 | + "k8s.noelware.cloud/template" = "charted-dev/coder-templates" |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + spec { |
| 100 | + security_context { |
| 101 | + run_as_user = "1000" |
| 102 | + fs_group = "1000" |
| 103 | + } |
| 104 | + |
| 105 | + container { |
| 106 | + name = "charted-server" |
| 107 | + image = var.docker_image |
| 108 | + command = ["/bin/bash", "-c", coder_agent.main.init_script] |
| 109 | + image_pull_policy = "Always" |
| 110 | + |
| 111 | + env { |
| 112 | + name = "CODER_ACCESS_URL" |
| 113 | + value = "https://coder.floofy.dev" |
| 114 | + } |
| 115 | + |
| 116 | + env { |
| 117 | + name = "CODER_AGENT_TOKEN" |
| 118 | + value = coder_agent.main.token |
| 119 | + } |
| 120 | + |
| 121 | + volume_mount { |
| 122 | + mount_path = "/home/noel" |
| 123 | + read_only = false |
| 124 | + name = "workspace" |
| 125 | + } |
| 126 | + |
| 127 | + security_context { |
| 128 | + run_as_user = "1000" |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + volume { |
| 133 | + name = "workspace" |
| 134 | + persistent_volume_claim { |
| 135 | + claim_name = "charted-workspace" |
| 136 | + read_only = false |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments