Skip to content

Commit 0ef1cef

Browse files
authored
Merge pull request #116 from hyder/issue-113
added scripts for serviceaccount
2 parents 9259c98 + 0545f35 commit 0ef1cef

File tree

9 files changed

+152
-5
lines changed

9 files changed

+152
-5
lines changed

docs/terraformoptions.adoc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Configuration Terraform Options:
3737
. link:#calico[Calico]
3838
. link:#kubernetes-metrics-server[Kubernetes Metrics Server]
3939
. link:#kms-integration[KMS integration]
40+
. link:#service-account[Service Account]
4041

4142
== Identity and access
4243

@@ -583,3 +584,35 @@ Refer to {uri-topology}[topology] for more thorough examples.
583584
|id of existing KMS key
584585
|
585586
|
587+
|===
588+
589+
== Service Account
590+
591+
[stripes=odd,cols="1d,4d,3a,3a", options=header,width="100%"]
592+
|===
593+
|Parameter
594+
|Description
595+
|Values
596+
|Default
597+
598+
|create_service_account
599+
|Whether to create a service account. A service account is required for CI/CD. See https://docs.cloud.oracle.com/iaas/Content/ContEng/Tasks/contengaddingserviceaccttoken.htm
600+
|true/false
601+
|false
602+
603+
|service_account_name
604+
|The name of service account to create
605+
|
606+
|kubeconfigsa
607+
608+
|service_account_namespace
609+
|The Kubernetes namespace where to create the service account
610+
|
611+
|kube-system
612+
613+
|service_account_cluster_role_binding
614+
|The name of the cluster role binding for the service account
615+
|
616+
|
617+
618+
|===

locals.tf

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ locals {
148148
}
149149

150150
helm = {
151-
helm_version = var.helm_version
152-
install_helm = var.install_helm
151+
helm_version = var.helm_version
152+
install_helm = var.install_helm
153153
}
154154

155155
calico = {
@@ -161,4 +161,11 @@ locals {
161161
use_encryption = var.use_encryption
162162
key_id = var.existing_key_id
163163
}
164+
165+
service_account = {
166+
create_service_account = var.create_service_account
167+
service_account_name = var.service_account_name
168+
service_account_namespace = var.service_account_namespace
169+
service_account_cluster_role_binding = var.service_account_cluster_role_binding
170+
}
164171
}

main.tf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module "base" {
1919

2020
# bastion parameters
2121
oci_base_bastion = local.oci_base_bastion
22-
22+
2323
# admin server parameters
2424
oci_base_admin = local.oci_base_admin
2525

@@ -107,4 +107,7 @@ module "oke" {
107107

108108
# metric server
109109
install_metricserver = var.install_metricserver
110+
111+
# service account
112+
service_account = local.service_account
110113
}

modules/oke/locals.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ locals {
1919
total_nodes = length(flatten([
2020
for nodes in local.node_pools_size_list : range(nodes)
2121
]))
22+
23+
service_account_cluster_role_binding_name = var.service_account.service_account_cluster_role_binding == "" ? "${var.service_account.service_account_name}-crb" : var.service_account.service_account_cluster_role_binding
2224
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
# Copyright 2017, 2019, 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+
if [ ${service_account_namespace} != kube-system ]; then
6+
kubectl create ns ${service_account_namespace}
7+
fi
8+
9+
kubectl -n ${service_account_namespace} create serviceaccount ${service_account_name}
10+
11+
kubectl create clusterrolebinding ${service_account_cluster_role_binding} --clusterrole=cluster-admin --serviceaccount=${service_account_namespace}:${service_account_name}

modules/oke/serviceaccount.tf

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2017, 2019 Oracle Corporation and/or affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl
3+
4+
data "template_file" "create_service_account" {
5+
template = file("${path.module}/scripts/create_service_account.template.sh")
6+
7+
vars = {
8+
service_account_name = var.service_account.service_account_name
9+
service_account_namespace = var.service_account.service_account_namespace
10+
service_account_cluster_role_binding = local.service_account_cluster_role_binding_name
11+
}
12+
13+
count = var.service_account.create_service_account == true ? 1 : 0
14+
}
15+
16+
resource null_resource "create_service_account" {
17+
connection {
18+
host = var.oke_admin.admin_private_ip
19+
private_key = file(var.oke_ssh_keys.ssh_private_key_path)
20+
timeout = "40m"
21+
type = "ssh"
22+
user = "opc"
23+
24+
bastion_host = var.oke_admin.bastion_public_ip
25+
bastion_user = "opc"
26+
bastion_private_key = file(var.oke_ssh_keys.ssh_private_key_path)
27+
}
28+
29+
depends_on = [null_resource.install_kubectl_admin, null_resource.write_kubeconfig_on_admin]
30+
31+
provisioner "file" {
32+
content = data.template_file.create_service_account[0].rendered
33+
destination = "~/create_service_account.sh"
34+
}
35+
36+
provisioner "remote-exec" {
37+
inline = [
38+
"chmod +x $HOME/create_service_account.sh",
39+
"$HOME/create_service_account.sh",
40+
# "rm -f $HOME/create_service_account.sh"
41+
]
42+
}
43+
44+
count = var.oke_admin.bastion_enabled == true && var.oke_admin.admin_enabled == true && var.service_account.create_service_account == true ? 1 : 0
45+
}

modules/oke/variables.tf

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ variable "oke_ocir" {
9999
# helm
100100
variable "helm" {
101101
type = object({
102-
helm_version = string
103-
install_helm = bool
102+
helm_version = string
103+
install_helm = bool
104104
})
105105
}
106106

@@ -117,3 +117,14 @@ variable "calico" {
117117
variable "install_metricserver" {
118118
default = false
119119
}
120+
121+
# service account
122+
123+
variable "service_account" {
124+
type = object({
125+
create_service_account = bool
126+
service_account_name = string
127+
service_account_namespace = string
128+
service_account_cluster_role_binding = string
129+
})
130+
}

terraform.tfvars.example

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,12 @@ install_metricserver = false
163163
use_encryption = false
164164

165165
existing_key_id = ""
166+
167+
# service account
168+
create_service_account = true
169+
170+
service_account_name = "gitlab"
171+
172+
service_account_namespace = "kube-system"
173+
174+
service_account_cluster_role_binding = "gitlab-crb"

variables.tf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,3 +460,29 @@ variable "existing_key_id" {
460460
default = ""
461461
type = string
462462
}
463+
464+
# serviceaccount
465+
466+
variable "create_service_account" {
467+
description = "whether to create a service account. A service account is required for CI/CD. See https://docs.cloud.oracle.com/iaas/Content/ContEng/Tasks/contengaddingserviceaccttoken.htm"
468+
default = false
469+
type = bool
470+
}
471+
472+
variable "service_account_name" {
473+
description = "name of service account to create"
474+
default = "kubeconfigsa"
475+
type = string
476+
}
477+
478+
variable "service_account_namespace" {
479+
description = "kubernetes namespace where to create the service account"
480+
default = "kube-system"
481+
type = string
482+
}
483+
484+
variable "service_account_cluster_role_binding" {
485+
description = "cluster role binding name"
486+
default = ""
487+
type = string
488+
}

0 commit comments

Comments
 (0)