Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runner-config): ✨ override image pull policy default value #1422

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/v1alpha2/reference_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ type RunnerPodSpec struct {
// +optional
Image string `json:"image,omitempty"`

// Runner pod ImagePullPolicy to use. Default is IfNotPresent.
// +optional
// +kubebuilder:validation:Enum=Always;IfNotPresent;Never
ImagePullPolicy string `json:"imagePullPolicy,omitempty"`
// List of sources to populate environment variables in the container.
// The keys defined within a source must be a C_IDENTIFIER. All invalid keys
// will be reported as an event when the container is starting. When a key exists in multiple
Expand Down
8 changes: 8 additions & 0 deletions charts/tofu-controller/crds/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6743,6 +6743,14 @@ spec:
image:
description: Runner pod image to use other than default
type: string
imagePullPolicy:
description: Runner pod ImagePullPolicy to use. Default is
IfNotPresent.
enum:
- Always
- IfNotPresent
- Never
type: string
initContainers:
description: Set up Init Containers for the Runner
items:
Expand Down
8 changes: 8 additions & 0 deletions config/crd/bases/infra.contrib.fluxcd.io_terraforms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6743,6 +6743,14 @@ spec:
image:
description: Runner pod image to use other than default
type: string
imagePullPolicy:
description: Runner pod ImagePullPolicy to use. Default is
IfNotPresent.
enum:
- Always
- IfNotPresent
- Never
type: string
initContainers:
description: Set up Init Containers for the Runner
items:
Expand Down
71 changes: 71 additions & 0 deletions controllers/tc000260_runner_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func Test_000260_runner_pod_test(t *testing.T) {
spec := reconciler.runnerPodSpec(helloWorldTF, "runner.tls-123")
g.Expect(spec.ServiceAccountName).To(Equal(serviceAccountName))
g.Expect(spec.Containers[0].Image).To(Equal(runnerPodImage))
g.Expect(spec.Containers[0].ImagePullPolicy).To(Equal(corev1.PullIfNotPresent))
g.Expect(spec.HostAliases[0].Hostnames).To(Equal([]string{"foo", "bar"}))

podTemplate, err := runnerPodTemplate(helloWorldTF, "runner.tls-123", revision)
Expand Down Expand Up @@ -178,6 +179,76 @@ func Test_000260_runner_pod_test_env_vars(t *testing.T) {
}()).To(BeTrue())
}

func Test_000260_runner_pod_a_test_image_pull_policy(t *testing.T) {
Spec("This spec describes a runner pod creation process")

const (
terraformName = "runner-pod-test"
sourceName = "runner-pod-test"
serviceAccountName = "helloworld-tf-runner"
runnerPodImage = "ghcr.io/weaveworks/tf-runner:test"
revision = "v2.6@sha256:c7fd0cc69b924aa5f9a6928477311737e439ca1b9e444855b0377e8a8ec65bb5"
)

var stringMap = map[string]string{
"company.com/abc": "xyz",
"company.com/xyz": "abc",
"tf.weave.works/tls-secret-name": "runner.tls-123",
}

g := NewWithT(t)

It("generate a runner pod template")
By("passing a terraform object, the runner pod template should be accurate")
helloWorldTF := infrav1.Terraform{
ObjectMeta: metav1.ObjectMeta{
Name: terraformName,
Namespace: "flux-system",
},
Spec: infrav1.TerraformSpec{
ApprovePlan: "auto",
Path: "./terraform-hello-world-example",
SourceRef: infrav1.CrossNamespaceSourceReference{
Kind: "GitRepository",
Name: sourceName,
Namespace: "flux-system",
},
ServiceAccountName: serviceAccountName,
RunnerPodTemplate: infrav1.RunnerPodTemplate{
Metadata: infrav1.RunnerPodMetadata{
Labels: stringMap,
Annotations: stringMap,
},
Spec: infrav1.RunnerPodSpec{
Image: runnerPodImage,
ImagePullPolicy: "Always",
},
},
},
}

spec := reconciler.runnerPodSpec(helloWorldTF, "runner.tls-123")
g.Expect(spec.ServiceAccountName).To(Equal(serviceAccountName))
g.Expect(spec.Containers[0].Image).To(Equal(runnerPodImage))
g.Expect(spec.Containers[0].ImagePullPolicy).To(Equal(corev1.PullAlways))

podTemplate, err := runnerPodTemplate(helloWorldTF, "runner.tls-123", revision)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(func() bool {
for k, v := range stringMap {
if v != podTemplate.ObjectMeta.Labels[k] {
return false
}
}
for k, v := range stringMap {
if v != podTemplate.ObjectMeta.Annotations[k] {
return false
}
}
return true
}()).To(BeTrue())
}

func Test_000260_runner_pod_test_env_vars_proxy(t *testing.T) {
Spec("This spec describes a runner pod creation process")

Expand Down
10 changes: 9 additions & 1 deletion controllers/tf_controller_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func getRunnerPodImage(image string) string {
return runnerPodImage
}

func getRunnerImagePullPolicy(imagePullPolicy string) v1.PullPolicy {
runnerImagePullPolicy := v1.PullPolicy(imagePullPolicy)
if runnerImagePullPolicy == "" {
runnerImagePullPolicy = v1.PullIfNotPresent
}
return runnerImagePullPolicy
}

func runnerPodTemplate(terraform infrav1.Terraform, secretName string, revision string) (v1.Pod, error) {
podNamespace := terraform.Namespace
podName := fmt.Sprintf("%s-tf-runner", terraform.Name)
Expand Down Expand Up @@ -272,7 +280,7 @@ func (r *TerraformReconciler) runnerPodSpec(terraform infrav1.Terraform, tlsSecr
"--grpc-max-message-size", fmt.Sprintf("%d", r.RunnerGRPCMaxMessageSize),
},
Image: getRunnerPodImage(terraform.Spec.RunnerPodTemplate.Spec.Image),
ImagePullPolicy: v1.PullIfNotPresent,
ImagePullPolicy: getRunnerImagePullPolicy(terraform.Spec.RunnerPodTemplate.Spec.ImagePullPolicy),
Ports: []v1.ContainerPort{
{
Name: "grpc",
Expand Down
24 changes: 24 additions & 0 deletions docs/References/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,18 @@ string
</tr>
<tr>
<td>
<code>imagePullPolicy</code><br>
<em>
string
</em>
</td>
<td>
<em>(Optional)</em>
<p>Runner pod ImagePullPolicy to use other than default</p>
</td>
</tr>
<tr>
<td>
<code>envFrom</code><br>
<em>
<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.23/#envfromsource-v1-core">
Expand Down Expand Up @@ -1097,6 +1109,18 @@ string
</tr>
<tr>
<td>
<code>imagePullPolicy</code><br>
<em>
string
</em>
</td>
<td>
<em>(Optional)</em>
<p>Runner pod ImagePullPolicy to use other than default</p>
</td>
</tr>
<tr>
<td>
<code>envFrom</code><br>
<em>
<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.23/#envfromsource-v1-core">
Expand Down
Loading