Skip to content

Commit 84d6bc2

Browse files
authored
[Feature] make OCIR secret namespace configurable (#323)
* making secret namespace configurable * changes with respect to review comments
1 parent 8086427 commit 84d6bc2

File tree

7 files changed

+27
-6
lines changed

7 files changed

+27
-6
lines changed

docs/configuration.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,10 @@ Refer to {uri-topology}[topology] for more thorough examples.
205205

206206
The {uri-oci-authtoken}#[Auth Token] must first be manually created and stored in {uri-oci-secret}#[OCI Secret in Vault]. It will subsequently be used to create a Kubernetes secret, which can then be used as an imagePullSecrets in a deployment. If you do not need to use private OCIR repositories, then leave the *secret_id* parameter empty. Refer to the {uri-instructions}#creating-a-secret-for-ocir[instructions] for how to create the Auth Token and the Secret in Vault.
207207

208-
The secret is created in the kube-system namespace. To copy it to your namespace, use the following command:
208+
The secret is created in the "default" namespace. To copy it to your namespace, use the following command:
209209

210210
----
211-
kubectl --namespace=kube-system get secret ocirsecret --export -o yaml | kubectl apply --namespace=<newnamespace> -f -
211+
kubectl --namespace=default get secret ocirsecret --export -o yaml | kubectl apply --namespace=<newnamespace> -f -
212212
----
213213

214214
{uri-terraform-options}#ocir[Reference]

locals.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ locals {
141141
secret_id = var.secret_id
142142
secret_name = var.secret_name
143143
username = var.username
144+
secret_ns = var.secret_ns
144145
}
145146

146147
calico = {

modules/oke/scripts/secret.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
secret_name = '${secret_name}'
1616
tenancy_namespace = '${tenancy_namespace}'
1717
username = '${username}'
18+
namespace = '${secret_ns}'
1819

1920
signer = oci.auth.signers.InstancePrincipalsSecurityTokenSigner()
2021

@@ -35,11 +36,21 @@ def read_secret_value(secret_client, secret_id):
3536
try:
3637
secret_content = read_secret_value(secret_client, secret_id=secret_id)
3738
secret_content = re.escape(secret_content)
38-
delsecret = "kubectl -n default delete secret ${secret_name}"
39+
delsecret = "kubectl -n ${secret_ns} delete secret ${secret_name}"
3940
os.system(delsecret)
4041

41-
crtsecret = ("kubectl create secret docker-registry ${secret_name} -n default --docker-server=${region_registry} --docker-username=${tenancy_namespace}/${username} --docker-email=${email_address} --docker-password=%s" % secret_content)
42-
42+
# TODO: keep an eye on the k8s API changes, make sure
43+
# that a new k8s version has this version, if not - update this template.
44+
create_namespace = f"""
45+
cat <<EOF | kubectl apply -f -
46+
apiVersion: v1
47+
kind: Namespace
48+
metadata:
49+
name: {namespace}
50+
"""
51+
subprocess.call(["/bin/bash" , "-c" , create_namespace])
52+
53+
crtsecret = ("kubectl create secret docker-registry ${secret_name} --namespace ${secret_ns} --docker-server=${region_registry} --docker-username=${tenancy_namespace}/${username} --docker-email=${email_address} --docker-password=%s" % secret_content)
4354
subprocess.call(["/bin/bash" , "-c" , crtsecret])
4455

4556
except Exception as e:

modules/oke/secrets.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ data "template_file" "secret" {
1414
secret_name = var.oke_ocir.secret_name
1515
tenancy_namespace = data.oci_objectstorage_namespace.object_storage_namespace.namespace
1616
username = var.oke_ocir.username
17-
17+
secret_ns = var.oke_ocir.secret_ns
1818
}
1919
count = local.post_provisioning_ops == true && var.oke_ocir.secret_id != "none" ? 1 : 0
2020
}

modules/oke/variables.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ variable "oke_ocir" {
8585
secret_id = string
8686
secret_name = string
8787
username = string
88+
secret_ns = string
8889
})
8990
}
9091

terraform.tfvars.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ secret_name = "ocirsecret"
258258

259259
username = ""
260260

261+
secret_ns = "default"
262+
261263
# calico
262264
calico_enabled = false
263265
calico_version = "3.19"

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,12 @@ variable "username" {
531531
type = string
532532
}
533533

534+
variable "secret_ns" {
535+
default = "default"
536+
description = "the k8s namespace for a secret."
537+
type = string
538+
}
539+
534540
# calico
535541
variable "calico_enabled" {
536542
description = "whether to install calico for network pod security policy"

0 commit comments

Comments
 (0)