diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 06a5bd872..a7d532910 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -169,7 +169,7 @@ The repository contains a `Makefile`; building and deploying can be configured v | `DEFAULT_DWO_IMG` | Image used for controller when generating default deployment templates. Can be used to override the controller image in the OLM bundle | `quay.io/devfile/devworkspace-controller:next` | | `NAMESPACE` | Namespace to use for deploying controller | `devworkspace-controller` | | `ROUTING_SUFFIX` | Cluster routing suffix (e.g. `$(minikube ip).nip.io`, `apps-crc.testing`). Required for Kubernetes | `192.168.99.100.nip.io` | -| `PULL_POLICY` | Image pull policy for controller | `Always` | +| `PULL_POLICY` | Image pull policy for the controller, the webhook server, and (through the default DevWorkspaceOperatorConfig) workspace containers | `Always` | | `DEVWORKSPACE_API_VERSION` | Branch or tag of the github.com/devfile/api to depend on | `v1alpha1` | Some of the rules supported by the `Makefile`: diff --git a/Makefile b/Makefile index 6e057aab4..71d2ce21f 100644 --- a/Makefile +++ b/Makefile @@ -375,7 +375,7 @@ help: Makefile @echo ' NAMESPACE - Namespace to use for deploying controller' @echo ' KUBECONFIG - Kubeconfig which should be used for accessing to the cluster. Currently is: $(KUBECONFIG)' @echo ' ROUTING_SUFFIX - Cluster routing suffix (e.g. $$(minikube ip).nip.io, apps-crc.testing)' - @echo ' PULL_POLICY - Image pull policy for controller' + @echo ' PULL_POLICY - Image pull policy for controller, webhook server and workspace containers' @echo ' DEVWORKSPACE_API_VERSION - Branch or tag of the github.com/devfile/api to depend on. Defaults to master' # Automatic setup of required binaries: controller-gen, envtest diff --git a/deploy/bundle/manifests/devworkspace-operator.clusterserviceversion.yaml b/deploy/bundle/manifests/devworkspace-operator.clusterserviceversion.yaml index a18cebde0..b8cce7ef0 100644 --- a/deploy/bundle/manifests/devworkspace-operator.clusterserviceversion.yaml +++ b/deploy/bundle/manifests/devworkspace-operator.clusterserviceversion.yaml @@ -413,6 +413,8 @@ spec: value: quay.io/devfile/project-clone:next - name: RELATED_IMAGE_project_backup value: quay.io/devfile/project-backup:next + - name: WEBHOOKS_SERVER_IMAGE_PULL_POLICY + value: Always - name: WATCH_NAMESPACE valueFrom: fieldRef: diff --git a/deploy/deployment/kubernetes/combined.yaml b/deploy/deployment/kubernetes/combined.yaml index 62ee23a21..86e7cd4bc 100644 --- a/deploy/deployment/kubernetes/combined.yaml +++ b/deploy/deployment/kubernetes/combined.yaml @@ -29833,6 +29833,8 @@ spec: value: quay.io/devfile/project-clone:next - name: RELATED_IMAGE_project_backup value: quay.io/devfile/project-backup:next + - name: WEBHOOKS_SERVER_IMAGE_PULL_POLICY + value: Always - name: WATCH_NAMESPACE value: "" - name: POD_NAME diff --git a/deploy/deployment/kubernetes/objects/devworkspace-controller-manager.Deployment.yaml b/deploy/deployment/kubernetes/objects/devworkspace-controller-manager.Deployment.yaml index 6be98e9be..1b6933bce 100644 --- a/deploy/deployment/kubernetes/objects/devworkspace-controller-manager.Deployment.yaml +++ b/deploy/deployment/kubernetes/objects/devworkspace-controller-manager.Deployment.yaml @@ -32,6 +32,8 @@ spec: value: quay.io/devfile/project-clone:next - name: RELATED_IMAGE_project_backup value: quay.io/devfile/project-backup:next + - name: WEBHOOKS_SERVER_IMAGE_PULL_POLICY + value: Always - name: WATCH_NAMESPACE value: "" - name: POD_NAME diff --git a/deploy/deployment/openshift/combined.yaml b/deploy/deployment/openshift/combined.yaml index 6b961bdae..c21d03dda 100644 --- a/deploy/deployment/openshift/combined.yaml +++ b/deploy/deployment/openshift/combined.yaml @@ -29835,6 +29835,8 @@ spec: value: quay.io/devfile/project-clone:next - name: RELATED_IMAGE_project_backup value: quay.io/devfile/project-backup:next + - name: WEBHOOKS_SERVER_IMAGE_PULL_POLICY + value: Always - name: WATCH_NAMESPACE value: "" - name: POD_NAME diff --git a/deploy/deployment/openshift/objects/devworkspace-controller-manager.Deployment.yaml b/deploy/deployment/openshift/objects/devworkspace-controller-manager.Deployment.yaml index 101a59fa3..204a74761 100644 --- a/deploy/deployment/openshift/objects/devworkspace-controller-manager.Deployment.yaml +++ b/deploy/deployment/openshift/objects/devworkspace-controller-manager.Deployment.yaml @@ -32,6 +32,8 @@ spec: value: quay.io/devfile/project-clone:next - name: RELATED_IMAGE_project_backup value: quay.io/devfile/project-backup:next + - name: WEBHOOKS_SERVER_IMAGE_PULL_POLICY + value: Always - name: WATCH_NAMESPACE value: "" - name: POD_NAME diff --git a/deploy/templates/base/manager_image_patch.yaml b/deploy/templates/base/manager_image_patch.yaml index 845df5b9a..c28cc2f19 100644 --- a/deploy/templates/base/manager_image_patch.yaml +++ b/deploy/templates/base/manager_image_patch.yaml @@ -10,7 +10,7 @@ spec: containers: - name: devworkspace-controller image: ${DWO_IMG} - imagePullPolicy: Always + imagePullPolicy: ${PULL_POLICY} env: - name: RELATED_IMAGE_devworkspace_webhook_server value: ${DWO_IMG} @@ -18,3 +18,5 @@ spec: value: ${PROJECT_CLONE_IMG} - name: RELATED_IMAGE_project_backup value: ${PROJECT_BACKUP_IMG} + - name: WEBHOOKS_SERVER_IMAGE_PULL_POLICY + value: ${PULL_POLICY} diff --git a/pkg/config/env.go b/pkg/config/env.go index 95974be47..d24c2d313 100644 --- a/pkg/config/env.go +++ b/pkg/config/env.go @@ -21,6 +21,7 @@ import ( "strconv" "github.com/devfile/devworkspace-operator/pkg/constants" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" ) @@ -35,6 +36,8 @@ const ( WebhooksMemRequestEnvVar = "WEBHOOKS_SERVER_MEMORY_REQUEST" WebhooksCPULimitEnvVar = "WEBHOOKS_SERVER_CPU_LIMIT" WebhooksCPURequestEnvVar = "WEBHOOKS_SERVER_CPU_REQUEST" + + WebhooksImagePullPolicyEnvVar = "WEBHOOKS_SERVER_IMAGE_PULL_POLICY" ) func GetWebhooksSecretName() (string, error) { @@ -73,6 +76,23 @@ func GetResourceQuantityFromEnvVar(env string) (*resource.Quantity, error) { return &quantity, nil } +// GetWebhooksServerImagePullPolicy returns the imagePullPolicy for the webhook server +// container, read from the WEBHOOKS_SERVER_IMAGE_PULL_POLICY environment variable. +// The variable is optional so that deployments created before it existed keep the +// previous behaviour: when it is unset or empty, the policy is Always. +func GetWebhooksServerImagePullPolicy() (corev1.PullPolicy, error) { + policy := corev1.PullPolicy(os.Getenv(WebhooksImagePullPolicyEnvVar)) + switch policy { + case "": + return corev1.PullAlways, nil + case corev1.PullAlways, corev1.PullIfNotPresent, corev1.PullNever: + return policy, nil + default: + return "", fmt.Errorf("environment variable %s has invalid value %q: must be one of %s, %s, %s", + WebhooksImagePullPolicyEnvVar, policy, corev1.PullAlways, corev1.PullIfNotPresent, corev1.PullNever) + } +} + func GetWorkspaceControllerSA() (string, error) { saName := os.Getenv(constants.ControllerServiceAccountNameEnvVar) if saName == "" { diff --git a/pkg/config/env_test.go b/pkg/config/env_test.go new file mode 100644 index 000000000..a94317a40 --- /dev/null +++ b/pkg/config/env_test.go @@ -0,0 +1,55 @@ +// +// Copyright (c) 2019-2026 Red Hat, Inc. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package config + +import ( + "testing" + + "github.com/stretchr/testify/assert" + corev1 "k8s.io/api/core/v1" +) + +func TestGetWebhooksServerImagePullPolicy(t *testing.T) { + tests := []struct { + name string + value string + expected corev1.PullPolicy + wantErr bool + }{ + {name: "defaults to Always when unset", value: "", expected: corev1.PullAlways}, + {name: "accepts Always", value: "Always", expected: corev1.PullAlways}, + {name: "accepts IfNotPresent", value: "IfNotPresent", expected: corev1.PullIfNotPresent}, + {name: "accepts Never", value: "Never", expected: corev1.PullNever}, + {name: "rejects unknown value", value: "Sometimes", wantErr: true}, + {name: "rejects wrong case", value: "never", wantErr: true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Setenv(WebhooksImagePullPolicyEnvVar, tt.value) + + actual, err := GetWebhooksServerImagePullPolicy() + + if tt.wantErr { + assert.Error(t, err) + assert.Contains(t, err.Error(), WebhooksImagePullPolicyEnvVar) + return + } + assert.NoError(t, err) + assert.Equal(t, tt.expected, actual) + }) + } +} diff --git a/pkg/webhook/deployment.go b/pkg/webhook/deployment.go index 6cab96edc..02b884cc7 100755 --- a/pkg/webhook/deployment.go +++ b/pkg/webhook/deployment.go @@ -77,6 +77,11 @@ func getSpecDeployment(webhooksSecretName, namespace string) (*appsv1.Deployment return nil, err } + pullPolicy, err := config.GetWebhooksServerImagePullPolicy() + if err != nil { + return nil, err + } + controllerSA, err := config.GetWorkspaceControllerSA() if err != nil { return nil, err @@ -118,7 +123,7 @@ func getSpecDeployment(webhooksSecretName, namespace string) (*appsv1.Deployment Image: images.GetWebhookServerImage(), Command: []string{"/usr/local/bin/entrypoint"}, Args: []string{"/usr/local/bin/webhook-server", "--metrics-addr=0.0.0.0:9443"}, - ImagePullPolicy: corev1.PullAlways, + ImagePullPolicy: pullPolicy, LivenessProbe: &corev1.Probe{ ProbeHandler: corev1.ProbeHandler{ HTTPGet: &corev1.HTTPGetAction{