Skip to content

Commit

Permalink
feat(terraformrun): can take extra args to tf commands
Browse files Browse the repository at this point in the history
  • Loading branch information
adupays committed Nov 28, 2024
1 parent d1a7ba6 commit 6e53dd9
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 0 deletions.
18 changes: 18 additions & 0 deletions api/v1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const (
DefaultRunRetention int = 10
)

type ExtraArgs []string

type OverrideRunnerSpec struct {
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
Image string `json:"image,omitempty"`
Expand All @@ -22,6 +24,9 @@ type OverrideRunnerSpec struct {
Volumes []corev1.Volume `json:"volumes,omitempty"`
VolumeMounts []corev1.VolumeMount `json:"volumeMounts,omitempty"`
Metadata MetadataOverride `json:"metadata,omitempty"`
ExtraInitArgs ExtraArgs `json:"extraInitArgs,omitempty"`
ExtraPlanArgs ExtraArgs `json:"extraPlanArgs,omitempty"`
ExtraApplyArgs ExtraArgs `json:"extraApplyArgs,omitempty"`
}

type MetadataOverride struct {
Expand Down Expand Up @@ -105,6 +110,9 @@ func GetOverrideRunnerSpec(repository *TerraformRepository, layer *TerraformLaye
ImagePullPolicy: chooseImagePullPolicy(repository.Spec.OverrideRunnerSpec.ImagePullPolicy, layer.Spec.OverrideRunnerSpec.ImagePullPolicy),
ServiceAccountName: chooseString(repository.Spec.OverrideRunnerSpec.ServiceAccountName, layer.Spec.OverrideRunnerSpec.ServiceAccountName),
ImagePullSecrets: mergeImagePullSecrets(repository.Spec.OverrideRunnerSpec.ImagePullSecrets, layer.Spec.OverrideRunnerSpec.ImagePullSecrets),
ExtraInitArgs: overrideExtraArgs(repository.Spec.OverrideRunnerSpec.ExtraInitArgs, layer.Spec.OverrideRunnerSpec.ExtraInitArgs),
ExtraPlanArgs: overrideExtraArgs(repository.Spec.OverrideRunnerSpec.ExtraPlanArgs, layer.Spec.OverrideRunnerSpec.ExtraPlanArgs),
ExtraApplyArgs: overrideExtraArgs(repository.Spec.OverrideRunnerSpec.ExtraApplyArgs, layer.Spec.OverrideRunnerSpec.ExtraApplyArgs),
}
}

Expand Down Expand Up @@ -320,3 +328,13 @@ func mergeMaps(a, b map[string]string) map[string]string {
}
return result
}

func overrideExtraArgs(a, b ExtraArgs) ExtraArgs {
result := b

if len(result) == 0 {
result = a
}

return result
}
198 changes: 198 additions & 0 deletions api/v1alpha1/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,204 @@ func TestOverrideRunnerSpec(t *testing.T) {
},
},
},
{
"ExtraPlanArgsInRepository",
&configv1alpha1.TerraformRepository{
Spec: configv1alpha1.TerraformRepositorySpec{
OverrideRunnerSpec: configv1alpha1.OverrideRunnerSpec{
ExtraPlanArgs: configv1alpha1.ExtraArgs{
"-target",
"'module.this.random_pet.this[\"first\"]'",
},
},
},
},
&configv1alpha1.TerraformLayer{},
configv1alpha1.OverrideRunnerSpec{
ExtraPlanArgs: configv1alpha1.ExtraArgs{
"-target",
"'module.this.random_pet.this[\"first\"]'",
},
},
},
{
"ExtraPlanArgsInLayer",
&configv1alpha1.TerraformRepository{},
&configv1alpha1.TerraformLayer{
Spec: configv1alpha1.TerraformLayerSpec{
OverrideRunnerSpec: configv1alpha1.OverrideRunnerSpec{
ExtraPlanArgs: configv1alpha1.ExtraArgs{
"-target",
"'module.this.random_pet.this[\"second\"]'",
},
},
},
},
configv1alpha1.OverrideRunnerSpec{
ExtraPlanArgs: configv1alpha1.ExtraArgs{
"-target",
"'module.this.random_pet.this[\"second\"]'",
},
},
},
{
"ExtraPlanArgsInBoth",
&configv1alpha1.TerraformRepository{
Spec: configv1alpha1.TerraformRepositorySpec{
OverrideRunnerSpec: configv1alpha1.OverrideRunnerSpec{
ExtraPlanArgs: configv1alpha1.ExtraArgs{
"-target",
"'module.this.random_pet.this[\"first\"]'",
},
},
},
},
&configv1alpha1.TerraformLayer{
Spec: configv1alpha1.TerraformLayerSpec{
OverrideRunnerSpec: configv1alpha1.OverrideRunnerSpec{
ExtraPlanArgs: configv1alpha1.ExtraArgs{
"-target",
"'module.this.random_pet.this[\"second\"]'",
},
},
},
},
configv1alpha1.OverrideRunnerSpec{
ExtraPlanArgs: configv1alpha1.ExtraArgs{
"-target",
"'module.this.random_pet.this[\"second\"]'",
},
},
},
{
"ExtraInitArgsInRepository",
&configv1alpha1.TerraformRepository{
Spec: configv1alpha1.TerraformRepositorySpec{
OverrideRunnerSpec: configv1alpha1.OverrideRunnerSpec{
ExtraInitArgs: configv1alpha1.ExtraArgs{
"-no-color",
},
},
},
},
&configv1alpha1.TerraformLayer{},
configv1alpha1.OverrideRunnerSpec{
ExtraInitArgs: configv1alpha1.ExtraArgs{
"-no-color",
},
},
},
{
"ExtraInitArgsInLayer",
&configv1alpha1.TerraformRepository{},
&configv1alpha1.TerraformLayer{
Spec: configv1alpha1.TerraformLayerSpec{
OverrideRunnerSpec: configv1alpha1.OverrideRunnerSpec{
ExtraInitArgs: configv1alpha1.ExtraArgs{
"-no-color",
},
},
},
},
configv1alpha1.OverrideRunnerSpec{
ExtraInitArgs: configv1alpha1.ExtraArgs{
"-no-color",
},
},
},
{
"ExtraInitArgsInBoth",
&configv1alpha1.TerraformRepository{
Spec: configv1alpha1.TerraformRepositorySpec{
OverrideRunnerSpec: configv1alpha1.OverrideRunnerSpec{
ExtraInitArgs: configv1alpha1.ExtraArgs{
"-upgrade",
},
},
},
},
&configv1alpha1.TerraformLayer{
Spec: configv1alpha1.TerraformLayerSpec{
OverrideRunnerSpec: configv1alpha1.OverrideRunnerSpec{
ExtraInitArgs: configv1alpha1.ExtraArgs{
"-no-color",
},
},
},
},
configv1alpha1.OverrideRunnerSpec{
ExtraInitArgs: configv1alpha1.ExtraArgs{
"-no-color",
},
},
},
{
"ExtraApplyArgsInRepository",
&configv1alpha1.TerraformRepository{
Spec: configv1alpha1.TerraformRepositorySpec{
OverrideRunnerSpec: configv1alpha1.OverrideRunnerSpec{
ExtraApplyArgs: configv1alpha1.ExtraArgs{
"-target",
"'module.this.random_pet.this[\"second\"]'",
},
},
},
},
&configv1alpha1.TerraformLayer{},
configv1alpha1.OverrideRunnerSpec{
ExtraApplyArgs: configv1alpha1.ExtraArgs{
"-target",
"'module.this.random_pet.this[\"second\"]'",
},
},
},
{
"ExtraApplyArgsInLayer",
&configv1alpha1.TerraformRepository{},
&configv1alpha1.TerraformLayer{
Spec: configv1alpha1.TerraformLayerSpec{
OverrideRunnerSpec: configv1alpha1.OverrideRunnerSpec{
ExtraApplyArgs: configv1alpha1.ExtraArgs{
"-target",
"'module.this.random_pet.this[\"second\"]'",
},
},
},
},
configv1alpha1.OverrideRunnerSpec{
ExtraApplyArgs: configv1alpha1.ExtraArgs{
"-target",
"'module.this.random_pet.this[\"second\"]'",
},
},
},
{
"ExtraApplyArgsInBoth",
&configv1alpha1.TerraformRepository{
Spec: configv1alpha1.TerraformRepositorySpec{
OverrideRunnerSpec: configv1alpha1.OverrideRunnerSpec{
ExtraApplyArgs: configv1alpha1.ExtraArgs{
"-target",
"'module.this.random_pet.this[\"second\"]'",
},
},
},
},
&configv1alpha1.TerraformLayer{
Spec: configv1alpha1.TerraformLayerSpec{
OverrideRunnerSpec: configv1alpha1.OverrideRunnerSpec{
ExtraApplyArgs: configv1alpha1.ExtraArgs{
"-target",
},
},
},
},
configv1alpha1.OverrideRunnerSpec{
ExtraApplyArgs: configv1alpha1.ExtraArgs{
"'module.this.random_pet.this[\"second\"]'",
},
},
},
}

for _, tc := range tt {
Expand Down
34 changes: 34 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions internal/controllers/terraformrun/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package terraformrun
import (
"context"
"fmt"
"strings"

configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1"
"github.com/padok-team/burrito/internal/burrito/config"
Expand Down Expand Up @@ -215,6 +216,26 @@ func (r *Reconciler) getPod(run *configv1alpha1.TerraformRun, layer *configv1alp
if len(overrideSpec.Image) > 0 {
defaultSpec.Containers[0].Image = overrideSpec.Image
}

if len(overrideSpec.ExtraInitArgs) > 0 {
defaultSpec.Containers[0].Env = append(defaultSpec.Containers[0].Env, corev1.EnvVar{
Name: "TF_CLI_ARGS_init",
Value: strings.Join(overrideSpec.ExtraInitArgs, " "),
})
}
if len(overrideSpec.ExtraPlanArgs) > 0 {
defaultSpec.Containers[0].Env = append(defaultSpec.Containers[0].Env, corev1.EnvVar{
Name: "TF_CLI_ARGS_plan",
Value: strings.Join(overrideSpec.ExtraPlanArgs, " "),
})
}
if len(overrideSpec.ExtraApplyArgs) > 0 {
defaultSpec.Containers[0].Env = append(defaultSpec.Containers[0].Env, corev1.EnvVar{
Name: "TF_CLI_ARGS_apply",
Value: strings.Join(overrideSpec.ExtraApplyArgs, " "),
})
}

pod := corev1.Pod{
Spec: defaultSpec,
ObjectMeta: metav1.ObjectMeta{
Expand Down
12 changes: 12 additions & 0 deletions manifests/crds/config.terraform.padok.cloud_terraformlayers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,18 @@ spec:
x-kubernetes-map-type: atomic
type: object
type: array
extraApplyArgs:
items:
type: string
type: array
extraInitArgs:
items:
type: string
type: array
extraPlanArgs:
items:
type: string
type: array
image:
type: string
imagePullPolicy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ spec:
x-kubernetes-map-type: atomic
type: object
type: array
extraApplyArgs:
items:
type: string
type: array
extraInitArgs:
items:
type: string
type: array
extraPlanArgs:
items:
type: string
type: array
image:
type: string
imagePullPolicy:
Expand Down
Loading

0 comments on commit 6e53dd9

Please sign in to comment.