Skip to content

Commit e5e2d0e

Browse files
committed
Add k8s-admin user
Create a non-root k8s-admin account on instances and update Terraform/Ansible to use it. Signed-off-by: Sudharshan Muralidharan <sudharshan.muralidharan1@ibm.com>
1 parent 92b4aee commit e5e2d0e

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

kubetest2-tf/data/vpc/main.tf

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ resource "ibm_is_instance_template" "node_template" {
3737
subnet = local.subnet_id
3838
security_groups = [local.security_group_id]
3939
}
40+
41+
user_data = <<-EOT
42+
#!/bin/bash
43+
# Create k8s-admin user for Kubernetes cluster management
44+
useradd -m -s /bin/bash k8s-admin
45+
# Add to sudo group (Ubuntu/Debian) or wheel group (RHEL/CentOS)
46+
usermod -aG sudo k8s-admin 2>/dev/null || usermod -aG wheel k8s-admin
47+
# Allow passwordless sudo
48+
echo "k8s-admin ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/k8s-admin
49+
chmod 0440 /etc/sudoers.d/k8s-admin
50+
# Setup SSH directory and copy authorized keys from root
51+
mkdir -p /home/k8s-admin/.ssh
52+
cp /root/.ssh/authorized_keys /home/k8s-admin/.ssh/authorized_keys
53+
chown -R k8s-admin:k8s-admin /home/k8s-admin/.ssh
54+
chmod 700 /home/k8s-admin/.ssh
55+
chmod 600 /home/k8s-admin/.ssh/authorized_keys
56+
EOT
4057
}
4158

4259
module "master" {
@@ -61,14 +78,14 @@ module "workers" {
6178
resource "null_resource" "wait-for-master-completes" {
6279
connection {
6380
type = "ssh"
64-
user = "root"
81+
user = "k8s-admin"
6582
host = module.master.public_ip
6683
private_key = file(var.ssh_private_key)
6784
timeout = "20m"
6885
}
6986
provisioner "remote-exec" {
7087
inline = [
71-
"cloud-init status -w"
88+
"sudo cloud-init status -w"
7289
]
7390
}
7491
}
@@ -77,14 +94,14 @@ resource "null_resource" "wait-for-workers-completes" {
7794
count = var.workers_count
7895
connection {
7996
type = "ssh"
80-
user = "root"
97+
user = "k8s-admin"
8198
host = module.workers[count.index].public_ip
8299
private_key = file(var.ssh_private_key)
83100
timeout = "15m"
84101
}
85102
provisioner "remote-exec" {
86103
inline = [
87-
"cloud-init status -w"
104+
"sudo cloud-init status -w"
88105
]
89106
}
90107
}

kubetest2-tf/deployer/deployer.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ const (
5050
[workers]
5151
{{range .Workers}}{{.}}
5252
{{end}}
53+
{{if .IsVPC}}
54+
[masters:vars]
55+
ansible_user=k8s-admin
56+
ansible_become=true
57+
ansible_become_method=sudo
58+
59+
[workers:vars]
60+
ansible_user=k8s-admin
61+
ansible_become=true
62+
ansible_become_method=sudo
63+
{{end}}
5364
`
5465
)
5566

@@ -58,6 +69,7 @@ var GitTag string
5869
type AnsibleInventory struct {
5970
Masters []string
6071
Workers []string
72+
IsVPC bool
6173
}
6274

6375
// Add additional Linux package dependencies here, used by checkDependencies()
@@ -269,7 +281,9 @@ func (d *deployer) Up() error {
269281
break
270282
}
271283
}
272-
inventory := AnsibleInventory{}
284+
inventory := AnsibleInventory{
285+
IsVPC: d.TargetProvider == "vpc",
286+
}
273287
tfMetaOutput, err := terraform.Output(d.tmpDir, d.TargetProvider)
274288
if err != nil {
275289
return err

0 commit comments

Comments
 (0)