From b851589bda21f0b05532d0cbc67957f9018e452e Mon Sep 17 00:00:00 2001 From: AfsalMcQueen <48438931+Afsalmc@users.noreply.github.com> Date: Fri, 5 Jun 2026 20:48:23 +0530 Subject: [PATCH 1/6] Add Git credential volume mount to apply container If the cloned Terraform module has remote references that need Git credentials, Git credential volume mount to apply container is necessary. --- controllers/process/container/init.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/controllers/process/container/init.go b/controllers/process/container/init.go index 37a5cc4c..4dab027a 100644 --- a/controllers/process/container/init.go +++ b/controllers/process/container/init.go @@ -37,6 +37,14 @@ func (a *Assembler) InitContainer() v1.Container { }) } + if a.GitCredential { + mounts = append(mounts, + v1.VolumeMount{ + Name: types.GitAuthConfigVolumeName, + MountPath: types.GitAuthConfigVolumeMountPath, + }) + } + c := v1.Container{ Name: types.TerraformInitContainerName, Image: a.TerraformImage, From 8c42bd59dd944fb40cacf667e4b875b16ffd96a1 Mon Sep 17 00:00:00 2001 From: AfsalMcQueen <48438931+Afsalmc@users.noreply.github.com> Date: Sun, 7 Jun 2026 13:52:21 +0530 Subject: [PATCH 2/6] Add openssh package to Dockerfile If the remote module is a private ssh git module, ssh is needed. --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 994c0dd7..d78b5aec 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,6 +27,6 @@ COPY --from=builder /workspace/manager . # COPY terraform binary COPY bin/terraform /usr/bin/terraform #RUN chmod +x /usr/bin/terraform -RUN apk add git +RUN apk add git openssh ENTRYPOINT ["/manager"] From 8c53f1b4aad49a4aa8ecf73edcc48f920e2d20b4 Mon Sep 17 00:00:00 2001 From: AfsalMcQueen <48438931+Afsalmc@users.noreply.github.com> Date: Sun, 7 Jun 2026 14:13:57 +0530 Subject: [PATCH 3/6] Revert openssh installation from Dockerfile --- Dockerfile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index d78b5aec..37e48c55 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,6 +27,5 @@ COPY --from=builder /workspace/manager . # COPY terraform binary COPY bin/terraform /usr/bin/terraform #RUN chmod +x /usr/bin/terraform -RUN apk add git openssh - +RUN apk add git ENTRYPOINT ["/manager"] From 9c5eb8f3313e3f33c48e24c832506d11b7e36714 Mon Sep 17 00:00:00 2001 From: AfsalMcQueen <48438931+Afsalmc@users.noreply.github.com> Date: Sun, 7 Jun 2026 14:14:40 +0530 Subject: [PATCH 4/6] Revert --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index 37e48c55..994c0dd7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,4 +28,5 @@ COPY --from=builder /workspace/manager . COPY bin/terraform /usr/bin/terraform #RUN chmod +x /usr/bin/terraform RUN apk add git + ENTRYPOINT ["/manager"] From 08ad90266408cb6f831dedf86aeca9447349335d Mon Sep 17 00:00:00 2001 From: AfsalMcQueen <48438931+Afsalmc@users.noreply.github.com> Date: Sun, 7 Jun 2026 17:03:22 +0530 Subject: [PATCH 5/6] Refactor Terraform init command handling Refactor command for Terraform init to use a dynamic method that includes SSH agent setup if Git credentials are present. --- controllers/process/container/init.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/controllers/process/container/init.go b/controllers/process/container/init.go index 4dab027a..e5c5f156 100644 --- a/controllers/process/container/init.go +++ b/controllers/process/container/init.go @@ -49,13 +49,27 @@ func (a *Assembler) InitContainer() v1.Container { Name: types.TerraformInitContainerName, Image: a.TerraformImage, ImagePullPolicy: v1.PullIfNotPresent, - Command: []string{ - "sh", - "-c", - "terraform init", - }, + Command: a.getInitCommand(), VolumeMounts: mounts, Env: a.Envs, } return c } + +// getInitCommand dynamically builds the terraform init command, injecting the SSH agent if needed. +func (a *Assembler) getInitCommand() []string { + cmd := "terraform init" + + // If Git credentials exist, start the ssh-agent and add the key BEFORE running terraform init + if a.GitCredential { + sshCommand := fmt.Sprintf("eval `ssh-agent` && ssh-add %s/%s", types.GitAuthConfigVolumeMountPath, v1.SSHAuthPrivateKey) + cmd = fmt.Sprintf("%s && %s", sshCommand, cmd) + } + + command := []string{ + "sh", + "-c", + cmd, + } + return command +} From 3d348df8fcd2f93b05aecb1802e82e79da1e5942 Mon Sep 17 00:00:00 2001 From: AfsalMcQueen <48438931+Afsalmc@users.noreply.github.com> Date: Sun, 7 Jun 2026 17:10:44 +0530 Subject: [PATCH 6/6] Import 'fmt' package in init.go Added import for 'fmt' package in init.go --- controllers/process/container/init.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/controllers/process/container/init.go b/controllers/process/container/init.go index e5c5f156..88f7f0c0 100644 --- a/controllers/process/container/init.go +++ b/controllers/process/container/init.go @@ -1,6 +1,8 @@ package container import ( + "fmt" + "github.com/oam-dev/terraform-controller/api/types" v1 "k8s.io/api/core/v1" )