Skip to content

Commit 46735e6

Browse files
authored
added token helper for faster kubectl (#298)
* added token helper for faster kubectl * updated umask for TOKEN so it is writable by opc
1 parent df1ea54 commit 46735e6

19 files changed

+122
-52
lines changed

CHANGELOG.adoc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,29 @@ The format is based on {uri-changelog}[Keep a Changelog].
1010
= Unreleased
1111

1212
=== Additions
13-
13+
* Added faster kubectl script (Thanks @joelezell-conga, @rgmccaw, Richard Exley)
1414
* Added support for VCN native endpoint for Kubernetes
1515
** Added a subnet for control plane (#270)
1616
** Added 2 parameters (cluster_access and cluster_source) to control access to Kubernetes API endpoint (#270)
17+
* Added support for initial node labels (#265)
18+
** Node labels can now be specified in node pools
19+
* Added support for enforcing use of signed images from registry (#274)
20+
* Added ability to specify node_pool_os_version (#281)
21+
* Added cluster_id and nodepool_ids for improved reusability (Thanks @yasn77)
1722

1823
=== Changes
1924

20-
* Changed base module version to 2.2.0. This allows choosing between Oracle Linux 7.X or 8 for the operator host as well as supporting custom route rules on the NAT gateway route table (#279). Custom route rules will make hybrid deployment easier to manage.
25+
* Updated permissions required in documentation (#292)
26+
* Made node pool image updatable (#286)
27+
* Changed deprecated map function (#283)
28+
* Changed base module version to 2.2.1. This allows controlling the state of the bastion (RUNNING or STOPPED), choosing between Oracle Linux 7.X or 8 for the operator host as well as supporting custom route rules on the NAT gateway route table (#279). Custom route rules will make hybrid deployment easier to manage.
2129
* Reworked the subnet boundaries for bastion and operator hosts (#270)
2230
* Updated and simplified OKE security lists to support VCN native endpoints (#270)
2331
* All port numbers and stateless are now in integer and boolean formats respectively (#270)
2432
* Updated default Kubernetes version to v 1.19.7
2533
* Updated documentation and topology diagrams
2634
* Fixed incorrect namespace issue when creating secret for OCIR (#267)
27-
* Narrow permissions for kubeconfig file
35+
* Narrow permissions for kubeconfig file
2836

2937
=== Deletions
3038
Removed hardcoded WAF CIDRs and used data source instead.

modules/oke/activeworker.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ data "template_file" "check_active_worker" {
1111
count = local.post_provisioning_ops == true && var.check_node_active != "none" ? 1 : 0
1212
}
1313

14-
resource null_resource "check_worker_active" {
14+
resource null_resource "check_worker_active" {
1515
triggers = {
1616
node_pools = length(data.oci_containerengine_node_pools.all_node_pools.node_pools)
1717
}
@@ -44,4 +44,4 @@ resource null_resource "check_worker_active" {
4444
}
4545

4646
count = local.post_provisioning_ops == true && var.check_node_active != "none" ? 1 : 0
47-
}
47+
}

modules/oke/cluster.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ resource "oci_containerengine_cluster" "k8s_cluster" {
1717

1818
content {
1919
is_policy_enabled = true
20-
20+
2121
dynamic "key_details" {
2222
iterator = signing_keys_iterator
2323
for_each = var.oke_cluster.image_signing_keys

modules/oke/datasources.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ data "oci_containerengine_node_pool_option" "node_pool_options" {
1313

1414
# retrieve for creating ocir secret
1515
data "oci_objectstorage_namespace" "object_storage_namespace" {
16-
}
16+
}

modules/oke/drain.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ data "template_file" "drain" {
1010
data "template_file" "drainlist" {
1111
template = file("${path.module}/scripts/drainlist.py")
1212

13-
vars = {
14-
cluster_id = oci_containerengine_cluster.k8s_cluster.id
15-
compartment_id = var.compartment_id
16-
region = var.region
17-
pools_to_drain = var.label_prefix == "none" ? trim(join(",", formatlist("'%s'", var.node_pools_to_drain)), "'") : trim(join(",", formatlist("'%s-%s'", var.label_prefix, var.node_pools_to_drain)), "'")
18-
}
13+
vars = {
14+
cluster_id = oci_containerengine_cluster.k8s_cluster.id
15+
compartment_id = var.compartment_id
16+
region = var.region
17+
pools_to_drain = var.label_prefix == "none" ? trim(join(",", formatlist("'%s'", var.node_pools_to_drain)), "'") : trim(join(",", formatlist("'%s-%s'", var.label_prefix, var.node_pools_to_drain)), "'")
18+
}
1919

2020
count = var.nodepool_drain == true ? 1 : 0
2121
}

modules/oke/k8stools.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ data "template_file" "install_kubectl" {
77

88
vars = {
99
ol = var.oke_operator.operator_version
10-
}
10+
}
1111
}
1212

1313
resource "null_resource" "install_kubectl_operator" {
@@ -36,7 +36,7 @@ resource "null_resource" "install_kubectl_operator" {
3636
]
3737
}
3838

39-
count = var.oke_operator.bastion_enabled == true && var.oke_operator.bastion_state == "RUNNING" && var.oke_operator.operator_enabled == true ? 1 : 0
39+
count = var.oke_operator.bastion_enabled == true && var.oke_operator.bastion_state == "RUNNING" && var.oke_operator.operator_enabled == true ? 1 : 0
4040
}
4141

4242
# helm

modules/oke/kubeconfig.tf

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl
33

44
data "oci_containerengine_cluster_kube_config" "kube_config" {
5-
cluster_id = oci_containerengine_cluster.k8s_cluster.id
5+
cluster_id = oci_containerengine_cluster.k8s_cluster.id
66
}
77

88
resource "null_resource" "create_local_kubeconfig" {
@@ -20,9 +20,9 @@ resource "null_resource" "create_local_kubeconfig" {
2020
}
2121

2222
resource "local_file" "kube_config_file" {
23-
content = data.oci_containerengine_cluster_kube_config.kube_config.content
24-
depends_on = [null_resource.create_local_kubeconfig, oci_containerengine_cluster.k8s_cluster]
25-
filename = "${path.root}/generated/kubeconfig"
23+
content = data.oci_containerengine_cluster_kube_config.kube_config.content
24+
depends_on = [null_resource.create_local_kubeconfig, oci_containerengine_cluster.k8s_cluster]
25+
filename = "${path.root}/generated/kubeconfig"
2626
file_permission = "0600"
2727
}
2828

@@ -37,6 +37,29 @@ data "template_file" "generate_kubeconfig" {
3737
count = local.post_provisioning_ops == true ? 1 : 0
3838
}
3939

40+
data "template_file" "token_helper" {
41+
template = file("${path.module}/scripts/token_helper.template.sh")
42+
43+
vars = {
44+
cluster-id = oci_containerengine_cluster.k8s_cluster.id
45+
region = var.region
46+
}
47+
48+
count = local.post_provisioning_ops == true ? 1 : 0
49+
}
50+
51+
data "template_file" "set_credentials" {
52+
template = file("${path.module}/scripts/kubeconfig_set_credentials.template.sh")
53+
54+
vars = {
55+
cluster-id = oci_containerengine_cluster.k8s_cluster.id
56+
cluster-id-11 = substr(oci_containerengine_cluster.k8s_cluster.id, (length(oci_containerengine_cluster.k8s_cluster.id) - 11), length(oci_containerengine_cluster.k8s_cluster.id))
57+
region = var.region
58+
}
59+
60+
count = local.post_provisioning_ops == true ? 1 : 0
61+
}
62+
4063
resource "null_resource" "write_kubeconfig_on_operator" {
4164
connection {
4265
host = var.oke_operator.operator_private_ip
@@ -57,11 +80,26 @@ resource "null_resource" "write_kubeconfig_on_operator" {
5780
destination = "~/generate_kubeconfig.sh"
5881
}
5982

83+
provisioner "file" {
84+
content = data.template_file.token_helper[0].rendered
85+
destination = "~/token_helper.sh"
86+
}
87+
88+
provisioner "file" {
89+
content = data.template_file.set_credentials[0].rendered
90+
destination = "~/kubeconfig_set_credentials.sh"
91+
}
92+
6093
provisioner "remote-exec" {
6194
inline = [
6295
"chmod +x $HOME/generate_kubeconfig.sh",
6396
"$HOME/generate_kubeconfig.sh",
64-
"rm -f $HOME/generate_kubeconfig.sh"
97+
"chmod +x $HOME/token_helper.sh",
98+
"sudo mv $HOME/token_helper.sh /usr/local/bin",
99+
"chmod +x $HOME/kubeconfig_set_credentials.sh",
100+
"$HOME/kubeconfig_set_credentials.sh",
101+
"rm -f $HOME/generate_kubeconfig.sh",
102+
"rm -f $HOME/kubeconfig_set_credentials.sh"
65103
]
66104
}
67105

modules/oke/outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl
33

44
output "cluster_id" {
5-
value = oci_containerengine_cluster.k8s_cluster.id
5+
value = oci_containerengine_cluster.k8s_cluster.id
66
}
77

88
output "nodepool_ids" {
9-
value = zipmap( values(oci_containerengine_node_pool.nodepools)[*].name, values(oci_containerengine_node_pool.nodepools)[*].id)
9+
value = zipmap(values(oci_containerengine_node_pool.nodepools)[*].name, values(oci_containerengine_node_pool.nodepools)[*].id)
1010
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
# Copyright 2021 Oracle Corporation and/or affiliates. All rights reserved.
3+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl
4+
5+
kubectl config set-credentials "user-${cluster-id-11}" --exec-command="./token_helper.sh" \
6+
--exec-arg="ce" \
7+
--exec-arg="cluster" \
8+
--exec-arg="generate-token" \
9+
--exec-arg="--cluster-id" \
10+
--exec-arg="${cluster-id}" \
11+
--exec-arg="--region" \
12+
--exec-arg="${region}"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
# Copyright 2021 Oracle Corporation and/or affiliates. All rights reserved.
3+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl
4+
5+
TOKEN_FILE=~/.kube/TOKEN
6+
7+
if ! test -f "$TOKEN_FILE" || test $(( `date +%s` - `stat -L -c %Y $TOKEN_FILE` )) -gt 240; then
8+
umask 022
9+
oci ce cluster generate-token --cluster-id ${cluster-id} --region ${region} >$TOKEN_FILE
10+
fi
11+
12+
cat $TOKEN_FILE

0 commit comments

Comments
 (0)