Skip to content

Commit

Permalink
feat(kubernetes): connect to kube cluster with generated creds
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimonemms committed Jun 24, 2024
1 parent 0cbfaf1 commit 24c2b1b
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 0 deletions.
22 changes: 22 additions & 0 deletions modules/kubernetes/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions modules/kubernetes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Kubernetes

<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.0 |
| <a name="requirement_kubernetes"></a> [kubernetes](#requirement\_kubernetes) | >= 2.31.0, < 3.0.0 |

## Providers

No providers.

## Modules

No modules.

## Resources

No resources.

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_kube_context"></a> [kube\_context](#input\_kube\_context) | Kubernetes context to use | `string` | `"default"` | no |
| <a name="input_kubeconfig"></a> [kubeconfig](#input\_kubeconfig) | Kubeconfig for the cluster | `string` | n/a | yes |

## Outputs

No outputs.
<!-- END_TF_DOCS -->
20 changes: 20 additions & 0 deletions modules/kubernetes/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2024 Simon Emms <[email protected]>
#
# 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.

locals {
kubeconfig = yamldecode(var.kubeconfig)
kubeconfig_clusters = { for context in local.kubeconfig.clusters : context.name => context.cluster }
kubeconfig_users = { for context in local.kubeconfig.users : context.name => context.user }
kubeconfig_by_context = { for context, cluster in local.kubeconfig_clusters : context => merge(cluster, local.kubeconfig_users[context]) }
}
30 changes: 30 additions & 0 deletions modules/kubernetes/terraform.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2024 Simon Emms <[email protected]>
#
# 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.

terraform {
required_version = ">= 1.0.0"
required_providers {
kubernetes = {
source = "hashicorp/kubernetes"
version = ">= 2.31.0, < 3.0.0"
}
}
}

provider "kubernetes" {
host = local.kubeconfig_by_context[var.kube_context].server
client_certificate = base64decode(local.kubeconfig_by_context[var.kube_context].client-certificate-data)
client_key = base64decode(local.kubeconfig_by_context[var.kube_context].client-key-data)
cluster_ca_certificate = base64decode(local.kubeconfig_by_context[var.kube_context].certificate-authority-data)
}
13 changes: 13 additions & 0 deletions modules/kubernetes/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2024 Simon Emms <[email protected]>
#
# 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.
25 changes: 25 additions & 0 deletions modules/kubernetes/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2024 Simon Emms <[email protected]>
#
# 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.

variable "kubeconfig" {
type = string
description = "Kubeconfig for the cluster"
sensitive = true
}

variable "kube_context" {
type = string
description = "Kubernetes context to use"
default = "default"
}
22 changes: 22 additions & 0 deletions stacks/dev/kubernetes/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions stacks/dev/kubernetes/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2024 Simon Emms <[email protected]>
#
# 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.

terraform {
source = "../../../modules/${basename(get_terragrunt_dir())}"
}

include {
path = "../../common.hcl"
}

dependency "hetzner" {
config_path = "../hetzner"
}

inputs = {
kubeconfig = dependency.hetzner.outputs.kubeconfig
}

0 comments on commit 24c2b1b

Please sign in to comment.