Skip to content

Commit

Permalink
Output with kubeconfig s3 url (#3)
Browse files Browse the repository at this point in the history
* s3 url output

* get kubeconfig as data object

* get kubeconfig as data object

Co-authored-by: romanprog <[email protected]>
  • Loading branch information
romanprog and arti-shalb authored Feb 3, 2021
1 parent b0b9d88 commit 8b30fed
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module
| Name | Version |
|------|---------|
| aws | ~> 3.0 |
| local | n/a |
| null | ~> 2.1 |
| random | ~> 2.2 |
| template | ~> 2.1 |
Expand All @@ -60,6 +61,7 @@ module
| extra\_api\_args | A list of additional arguments for kubeapi | `map` | `{}` | no |
| k3s\_version | Version of k3s engine: https://github.com/rancher/k3s/releases | `string` | n/a | yes |
| key\_name | The key name to use for the instances | `string` | n/a | yes |
| kubeconfig\_filename | Name of file to save kubeconfig local. | `string` | `"./kubeconfig"` | no |
| master\_additional\_tags | A list of additional tags for master nodes instances | `map(string)` | `{}` | no |
| master\_iam\_instance\_profile | IAM instance profile to be attached to master instances | `string` | `""` | no |
| master\_instance\_type | Instance type for master nodes. | `string` | `"t3.medium"` | no |
Expand All @@ -80,6 +82,7 @@ module
|------|-------------|
| k8s\_nlb\_dns\_name | n/a |
| kubeconfig | n/a |
| kubeconfig\_s3\_url | n/a |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

Expand Down
2 changes: 1 addition & 1 deletion data.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ data "template_file" "init-master" {
instance_index = count.index
k3s_server_token = random_password.k3s_server_token.result
k3s_version = var.k3s_version
cluster_dns_zone = local.cluster_dns_zone
cluster_name = var.cluster_name
cluster_domain = local.cluster_domain
s3_bucket = var.s3_bucket
node_labels = local.master_node_labels
Expand Down
4 changes: 2 additions & 2 deletions files/k3s.tpl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ software_install() {
%{ if instance_index == "0" }
cp /etc/rancher/k3s/k3s.yaml /tmp/
sed -i 's/127.0.0.1/${cluster_domain}/g' /tmp/k3s.yaml
aws s3 cp --content-type text/plain /tmp/k3s.yaml s3://${s3_bucket}/${cluster_dns_zone}/${kubeconfig_name}
aws s3 cp --content-type text/plain /tmp/k3s.yaml s3://${s3_bucket}/${cluster_name}/${kubeconfig_name}
mkdir -p /var/lib/rancher/k3s/server/db/snapshots/
echo "15 */12 * * * root aws s3 sync --delete /var/lib/rancher/k3s/server/db/snapshots/ s3://${s3_bucket}/${cluster_dns_zone}/backups/" >> /etc/crontab
echo "15 */12 * * * root aws s3 sync --delete /var/lib/rancher/k3s/server/db/snapshots/ s3://${s3_bucket}/${cluster_name}/backups/" >> /etc/crontab
echo "" >> /etc/crontab
%{ endif }

Expand Down
9 changes: 7 additions & 2 deletions addons.tf → kubeconfig.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
resource "null_resource" "wait_cluster_ready" {
provisioner "local-exec" {
# command = "until (curl --connect-timeout 2 https://${local.cluster_domain}:6443/ping --insecure) >/dev/null 2>&1; do sleep 1; echo waiting for k3s; done"
command = "until (aws s3 cp s3://${var.s3_bucket}/${local.cluster_dns_zone}/${local.kubeconfig_filename} ./kubeconfig && for i in $(seq 10); do kubectl version --kubeconfig ./kubeconfig --request-timeout=5s || exit 1; sleep 1; done) >/dev/null 2>&1; do sleep 1; echo waiting for kubeconfig; done"
command = "until (aws s3 cp s3://${var.s3_bucket}/${var.cluster_name}/${local.kubeconfig_filename} ./kubeconfig_tmp && for i in $(seq 10); do kubectl version --kubeconfig ./kubeconfig_tmp --request-timeout=5s || exit 1; sleep 1; done) >/dev/null 2>&1; do sleep 1; echo waiting for kubeconfig; done"
}
depends_on = [
aws_autoscaling_group.master,
Expand All @@ -11,9 +11,14 @@ resource "null_resource" "wait_cluster_ready" {

# Not really secure as it will keep entire file as a plain text in tfstate
data "aws_s3_bucket_object" "get_kubeconfig" {
key = "${local.cluster_dns_zone}/${local.kubeconfig_filename}"
key = "${var.cluster_name}/${local.kubeconfig_filename}"
bucket = var.s3_bucket
depends_on = [
null_resource.wait_cluster_ready
]
}

resource "local_file" "kubeconfig" {
content = "${data.aws_s3_bucket_object.get_kubeconfig.body}"
filename = var.kubeconfig_filename
}
4 changes: 4 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ output "k8s_nlb_dns_name" {
output "kubeconfig" {
value = data.aws_s3_bucket_object.get_kubeconfig.body
}

output "kubeconfig_s3_url" {
value = "s3://${var.s3_bucket}/${var.cluster_name}/${local.kubeconfig_filename}"
}
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,9 @@ variable worker_iam_instance_profile {
type = string
default = ""
}

variable "kubeconfig_filename" {
description = "Name of file to save kubeconfig local."
type = string
default = "./kubeconfig"
}

0 comments on commit 8b30fed

Please sign in to comment.