diff --git a/api/v1alpha1/external_secrets_config_types.go b/api/v1alpha1/external_secrets_config_types.go index f06a7df79..7ba420df6 100644 --- a/api/v1alpha1/external_secrets_config_types.go +++ b/api/v1alpha1/external_secrets_config_types.go @@ -1,6 +1,7 @@ package v1alpha1 import ( + corev1 "k8s.io/api/core/v1" networkingv1 "k8s.io/api/networking/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -113,6 +114,38 @@ type ControllerConfig struct { // +kubebuilder:validation:Optional Labels map[string]string `json:"labels,omitempty"` + // annotations allows adding custom annotations to all external-secrets component + // Deployments and Pod templates. These annotations are applied globally to all + // operand components (Controller, Webhook, CertController, BitwardenSDKServer). + // These annotations are merged with any default annotations set by the operator. + // User-specified annotations take precedence over defaults in case of conflicts. + // Annotations with keys starting with kubernetes.io/, app.kubernetes.io/, openshift.io/, or k8s.io/ + // are reserved and cannot be overridden. + // + // +kubebuilder:validation:XValidation:rule="self.all(a, !['kubernetes.io/', 'app.kubernetes.io/', 'openshift.io/', 'k8s.io/'].exists(p, a.key.startsWith(p)))",message="annotations with reserved prefixes 'kubernetes.io/', 'app.kubernetes.io/', 'openshift.io/', 'k8s.io/' are not allowed" + // +kubebuilder:validation:MinItems:=0 + // +kubebuilder:validation:MaxItems:=50 + // +kubebuilder:validation:Optional + // +listType=map + // +listMapKey=key + // +optional + Annotations []Annotation `json:"annotations,omitempty"` + + // componentConfigs allows specifying component-specific configuration overrides + // for each external-secrets operand component (Controller, Webhook, CertController, BitwardenSDKServer). + // Each entry targets a specific component by name and can include deployment-level overrides + // (such as revisionHistoryLimit) and custom environment variables. + // The componentName must be unique across all entries. + // + // +kubebuilder:validation:XValidation:rule="self.all(x, self.exists_one(y, x.componentName == y.componentName))",message="componentName must be unique across all componentConfig entries" + // +kubebuilder:validation:MinItems:=0 + // +kubebuilder:validation:MaxItems:=4 + // +kubebuilder:validation:Optional + // +listType=map + // +listMapKey=componentName + // +optional + ComponentConfigs []ComponentConfig `json:"componentConfig,omitempty"` + // networkPolicies specifies the list of network policy configurations // to be applied to external-secrets pods. // @@ -212,17 +245,89 @@ type CertProvidersConfig struct { CertManager *CertManagerConfig `json:"certManager,omitempty"` } -// ComponentName represents the different external-secrets components that can have network policies applied. +// ComponentName represents the different external-secrets operand components +// that can be individually configured with network policies and component-specific overrides. +// +kubebuilder:validation:Enum:=ExternalSecretsCoreController;Webhook;CertController;BitwardenSDKServer type ComponentName string const ( - // CoreController represents the external-secrets component + // CoreController represents the external-secrets core controller component. CoreController ComponentName = "ExternalSecretsCoreController" - // BitwardenSDKServer represents the bitwarden-sdk-server component + // Webhook represents the external-secrets webhook component. + Webhook ComponentName = "Webhook" + + // CertController represents the external-secrets cert-controller component. + CertController ComponentName = "CertController" + + // BitwardenSDKServer represents the bitwarden-sdk-server component. BitwardenSDKServer ComponentName = "BitwardenSDKServer" ) +// ComponentConfig holds configuration overrides for a specific external-secrets operand component. +// It allows specifying deployment-level configuration and custom environment variables +// for each component independently. +type ComponentConfig struct { + // componentName specifies which deployment component this configuration applies to. + // Each component can only appear once in the componentConfig list. + // +kubebuilder:validation:Enum:=ExternalSecretsCoreController;Webhook;CertController;BitwardenSDKServer + // +kubebuilder:validation:Required + ComponentName ComponentName `json:"componentName"` + + // deploymentConfigs allows specifying deployment-level configuration overrides + // for the targeted component, such as revisionHistoryLimit. + // +kubebuilder:validation:Optional + // +optional + DeploymentConfigs DeploymentConfig `json:"deploymentConfigs,omitempty"` + + // overrideEnv allows setting custom environment variables for the component's container. + // These environment variables are merged with the default environment variables set by + // the operator. User-specified variables take precedence in case of conflicts. + // Environment variables starting with HOSTNAME, KUBERNETES_, or EXTERNAL_SECRETS_ are reserved + // and cannot be overridden. + // + // +kubebuilder:validation:XValidation:rule="self.all(e, !['HOSTNAME', 'KUBERNETES_', 'EXTERNAL_SECRETS_'].exists(p, e.name.startsWith(p)))",message="environment variable names with reserved prefixes 'HOSTNAME', 'KUBERNETES_', 'EXTERNAL_SECRETS_' are not allowed" + // +kubebuilder:validation:MinItems:=0 + // +kubebuilder:validation:MaxItems:=50 + // +kubebuilder:validation:Optional + // +listType=atomic + // +optional + OverrideEnv []corev1.EnvVar `json:"overrideEnv,omitempty"` +} + +// DeploymentConfig holds deployment-level configuration overrides for an operand component. +type DeploymentConfig struct { + // revisionHistoryLimit specifies the number of old ReplicaSets to retain for rollback. + // Minimum value of 1 is enforced to ensure rollback capability. + // + // +kubebuilder:validation:Minimum=1 + // +kubebuilder:validation:Optional + // +optional + RevisionHistoryLimit *int32 `json:"revisionHistoryLimit,omitempty"` +} + +// KVPair represents a generic key-value pair for configuration. +type KVPair struct { + // key is the name of the key-value pair entry. + // +kubebuilder:validation:MinLength:=1 + // +kubebuilder:validation:MaxLength:=317 + // +kubebuilder:validation:Required + Key string `json:"key"` + + // value is the value of the key-value pair entry. + // +kubebuilder:validation:MaxLength:=1024 + // +kubebuilder:validation:Optional + // +optional + Value string `json:"value,omitempty"` +} + +// Annotation represents a custom annotation key-value pair. +// Embeds KVPair inline for reusability. +type Annotation struct { + // Embedded KVPair provides key and value fields. + KVPair `json:",inline"` +} + // NetworkPolicy represents a custom network policy configuration for operator-managed components. // It includes a name for identification and the network policy rules to be enforced. type NetworkPolicy struct { @@ -234,7 +339,7 @@ type NetworkPolicy struct { Name string `json:"name"` // componentName specifies which external-secrets component this network policy applies to. - // +kubebuilder:validation:Enum:=ExternalSecretsCoreController;BitwardenSDKServer + // +kubebuilder:validation:Enum:=ExternalSecretsCoreController;Webhook;CertController;BitwardenSDKServer // +kubebuilder:validation:Required ComponentName ComponentName `json:"componentName"` diff --git a/api/v1alpha1/tests/externalsecretsconfig.operator.openshift.io/externalsecretsconfig.testsuite.yaml b/api/v1alpha1/tests/externalsecretsconfig.operator.openshift.io/externalsecretsconfig.testsuite.yaml index 720d7fc23..0a89d8374 100644 --- a/api/v1alpha1/tests/externalsecretsconfig.operator.openshift.io/externalsecretsconfig.testsuite.yaml +++ b/api/v1alpha1/tests/externalsecretsconfig.operator.openshift.io/externalsecretsconfig.testsuite.yaml @@ -493,6 +493,385 @@ tests: webhookConfig: certificateCheckInterval: "15m" operatingNamespace: "test-ns" + - name: Should be able to create ExternalSecretsConfig with annotations + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + annotations: + - key: "example.com/custom-annotation" + value: "my-value" + expected: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + annotations: + - key: "example.com/custom-annotation" + value: "my-value" + - name: Should be able to create ExternalSecretsConfig with multiple annotations + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + annotations: + - key: "example.com/annotation-1" + value: "value-1" + - key: "example.com/annotation-2" + value: "value-2" + expected: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + annotations: + - key: "example.com/annotation-1" + value: "value-1" + - key: "example.com/annotation-2" + value: "value-2" + - name: Should fail with annotation using reserved kubernetes.io prefix + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + annotations: + - key: "kubernetes.io/custom" + value: "not-allowed" + expectedError: "annotations with reserved prefixes" + - name: Should fail with annotation using reserved app.kubernetes.io prefix + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + annotations: + - key: "app.kubernetes.io/name" + value: "not-allowed" + expectedError: "annotations with reserved prefixes" + - name: Should fail with annotation using reserved openshift.io prefix + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + annotations: + - key: "openshift.io/custom" + value: "not-allowed" + expectedError: "annotations with reserved prefixes" + - name: Should fail with annotation using reserved k8s.io prefix + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + annotations: + - key: "k8s.io/custom" + value: "not-allowed" + expectedError: "annotations with reserved prefixes" + - name: Should fail with annotation key empty + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + annotations: + - key: "" + value: "some-value" + expectedError: "spec.controllerConfig.annotations.key in body should be at least 1 chars long" + - name: Should be able to create ExternalSecretsConfig with componentConfig for Controller + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: ExternalSecretsCoreController + deploymentConfigs: + revisionHistoryLimit: 5 + expected: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: ExternalSecretsCoreController + deploymentConfigs: + revisionHistoryLimit: 5 + - name: Should be able to create ExternalSecretsConfig with componentConfig for Webhook + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: Webhook + deploymentConfigs: + revisionHistoryLimit: 3 + expected: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: Webhook + deploymentConfigs: + revisionHistoryLimit: 3 + - name: Should be able to create ExternalSecretsConfig with componentConfig for CertController + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: CertController + deploymentConfigs: + revisionHistoryLimit: 5 + expected: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: CertController + deploymentConfigs: + revisionHistoryLimit: 5 + - name: Should be able to create ExternalSecretsConfig with componentConfig for BitwardenSDKServer + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: BitwardenSDKServer + deploymentConfigs: + revisionHistoryLimit: 3 + expected: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: BitwardenSDKServer + deploymentConfigs: + revisionHistoryLimit: 3 + - name: Should be able to create ExternalSecretsConfig with multiple componentConfigs + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: ExternalSecretsCoreController + deploymentConfigs: + revisionHistoryLimit: 10 + - componentName: Webhook + deploymentConfigs: + revisionHistoryLimit: 5 + - componentName: CertController + deploymentConfigs: + revisionHistoryLimit: 3 + - componentName: BitwardenSDKServer + deploymentConfigs: + revisionHistoryLimit: 3 + expected: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: ExternalSecretsCoreController + deploymentConfigs: + revisionHistoryLimit: 10 + - componentName: Webhook + deploymentConfigs: + revisionHistoryLimit: 5 + - componentName: CertController + deploymentConfigs: + revisionHistoryLimit: 3 + - componentName: BitwardenSDKServer + deploymentConfigs: + revisionHistoryLimit: 3 + - name: Should fail with duplicate componentName in componentConfig + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: ExternalSecretsCoreController + deploymentConfigs: + revisionHistoryLimit: 5 + - componentName: ExternalSecretsCoreController + deploymentConfigs: + revisionHistoryLimit: 10 + expectedError: "componentName must be unique across all componentConfig entries" + - name: Should fail with invalid componentName in componentConfig + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: InvalidComponent + deploymentConfigs: + revisionHistoryLimit: 5 + expectedError: "spec.controllerConfig.componentConfig.componentName: Unsupported value" + - name: Should fail with more than 4 componentConfig entries + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: ExternalSecretsCoreController + deploymentConfigs: + revisionHistoryLimit: 5 + - componentName: Webhook + deploymentConfigs: + revisionHistoryLimit: 5 + - componentName: CertController + deploymentConfigs: + revisionHistoryLimit: 5 + - componentName: BitwardenSDKServer + deploymentConfigs: + revisionHistoryLimit: 5 + - componentName: ExternalSecretsCoreController + deploymentConfigs: + revisionHistoryLimit: 10 + expectedError: "spec.controllerConfig.componentConfig: Too many: 5: must have at most 4 items" + - name: Should fail with revisionHistoryLimit less than 1 + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: ExternalSecretsCoreController + deploymentConfigs: + revisionHistoryLimit: 0 + expectedError: "spec.controllerConfig.componentConfig.deploymentConfigs.revisionHistoryLimit in body should be greater than or equal to 1" + - name: Should be able to create ExternalSecretsConfig with overrideEnv + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: ExternalSecretsCoreController + overrideEnv: + - name: GOMAXPROCS + value: "4" + - name: MY_CUSTOM_VAR + value: "custom-value" + expected: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: ExternalSecretsCoreController + overrideEnv: + - name: GOMAXPROCS + value: "4" + - name: MY_CUSTOM_VAR + value: "custom-value" + - name: Should fail with overrideEnv using reserved HOSTNAME prefix + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: ExternalSecretsCoreController + overrideEnv: + - name: HOSTNAME + value: "custom-host" + expectedError: "environment variable names with reserved prefixes" + - name: Should fail with overrideEnv using reserved KUBERNETES_ prefix + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: ExternalSecretsCoreController + overrideEnv: + - name: KUBERNETES_SERVICE_HOST + value: "10.0.0.1" + expectedError: "environment variable names with reserved prefixes" + - name: Should fail with overrideEnv using reserved EXTERNAL_SECRETS_ prefix + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: ExternalSecretsCoreController + overrideEnv: + - name: EXTERNAL_SECRETS_CONFIG + value: "override" + expectedError: "environment variable names with reserved prefixes" + - name: Should be able to create ExternalSecretsConfig with combined annotations and componentConfig + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + annotations: + - key: "example.com/custom-annotation" + value: "my-value" + componentConfig: + - componentName: ExternalSecretsCoreController + deploymentConfigs: + revisionHistoryLimit: 10 + overrideEnv: + - name: GOMAXPROCS + value: "4" + - componentName: Webhook + deploymentConfigs: + revisionHistoryLimit: 3 + expected: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + annotations: + - key: "example.com/custom-annotation" + value: "my-value" + componentConfig: + - componentName: ExternalSecretsCoreController + deploymentConfigs: + revisionHistoryLimit: 10 + overrideEnv: + - name: GOMAXPROCS + value: "4" + - componentName: Webhook + deploymentConfigs: + revisionHistoryLimit: 3 onUpdate: - name: Should be able to update labels in controller config resourceName: cluster @@ -596,4 +975,98 @@ tests: bitwardenSecretManagerProvider: mode: Enabled secretRef: - name: "bitwarden-certs" \ No newline at end of file + name: "bitwarden-certs" + - name: Should be able to update annotations in controller config + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + annotations: + - key: "example.com/annotation-1" + value: "value-1" + updated: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + annotations: + - key: "example.com/annotation-1" + value: "updated-value" + - key: "example.com/annotation-2" + value: "value-2" + expected: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + annotations: + - key: "example.com/annotation-1" + value: "updated-value" + - key: "example.com/annotation-2" + value: "value-2" + - name: Should be able to update componentConfig after creation + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: ExternalSecretsCoreController + deploymentConfigs: + revisionHistoryLimit: 5 + updated: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: ExternalSecretsCoreController + deploymentConfigs: + revisionHistoryLimit: 10 + expected: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: ExternalSecretsCoreController + deploymentConfigs: + revisionHistoryLimit: 10 + - name: Should be able to add overrideEnv after creation + resourceName: cluster + initial: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: Webhook + deploymentConfigs: + revisionHistoryLimit: 3 + updated: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: Webhook + deploymentConfigs: + revisionHistoryLimit: 3 + overrideEnv: + - name: GOMAXPROCS + value: "4" + expected: | + apiVersion: operator.openshift.io/v1alpha1 + kind: ExternalSecretsConfig + spec: + controllerConfig: + componentConfig: + - componentName: Webhook + deploymentConfigs: + revisionHistoryLimit: 3 + overrideEnv: + - name: GOMAXPROCS + value: "4" \ No newline at end of file diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 99f6ec14b..a088f2b00 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -27,6 +27,22 @@ import ( runtime "k8s.io/apimachinery/pkg/runtime" ) +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Annotation) DeepCopyInto(out *Annotation) { + *out = *in + out.KVPair = in.KVPair +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Annotation. +func (in *Annotation) DeepCopy() *Annotation { + if in == nil { + return nil + } + out := new(Annotation) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ApplicationConfig) DeepCopyInto(out *ApplicationConfig) { *out = *in @@ -162,6 +178,29 @@ func (in *CommonConfigs) DeepCopy() *CommonConfigs { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ComponentConfig) DeepCopyInto(out *ComponentConfig) { + *out = *in + in.DeploymentConfigs.DeepCopyInto(&out.DeploymentConfigs) + if in.OverrideEnv != nil { + in, out := &in.OverrideEnv, &out.OverrideEnv + *out = make([]corev1.EnvVar, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ComponentConfig. +func (in *ComponentConfig) DeepCopy() *ComponentConfig { + if in == nil { + return nil + } + out := new(ComponentConfig) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Condition) DeepCopyInto(out *Condition) { *out = *in @@ -214,6 +253,18 @@ func (in *ControllerConfig) DeepCopyInto(out *ControllerConfig) { (*out)[key] = val } } + if in.Annotations != nil { + in, out := &in.Annotations, &out.Annotations + *out = make([]Annotation, len(*in)) + copy(*out, *in) + } + if in.ComponentConfigs != nil { + in, out := &in.ComponentConfigs, &out.ComponentConfigs + *out = make([]ComponentConfig, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } if in.NetworkPolicies != nil { in, out := &in.NetworkPolicies, &out.NetworkPolicies *out = make([]NetworkPolicy, len(*in)) @@ -253,6 +304,26 @@ func (in *ControllerStatus) DeepCopy() *ControllerStatus { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *DeploymentConfig) DeepCopyInto(out *DeploymentConfig) { + *out = *in + if in.RevisionHistoryLimit != nil { + in, out := &in.RevisionHistoryLimit, &out.RevisionHistoryLimit + *out = new(int32) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DeploymentConfig. +func (in *DeploymentConfig) DeepCopy() *DeploymentConfig { + if in == nil { + return nil + } + out := new(DeploymentConfig) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ExternalSecretsConfig) DeepCopyInto(out *ExternalSecretsConfig) { *out = *in @@ -471,6 +542,21 @@ func (in *GlobalConfig) DeepCopy() *GlobalConfig { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *KVPair) DeepCopyInto(out *KVPair) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KVPair. +func (in *KVPair) DeepCopy() *KVPair { + if in == nil { + return nil + } + out := new(KVPair) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *NetworkPolicy) DeepCopyInto(out *NetworkPolicy) { *out = *in diff --git a/config/crd/bases/operator.openshift.io_externalsecretsconfigs.yaml b/config/crd/bases/operator.openshift.io_externalsecretsconfigs.yaml index ae6890cdc..557db7cca 100644 --- a/config/crd/bases/operator.openshift.io_externalsecretsconfigs.yaml +++ b/config/crd/bases/operator.openshift.io_externalsecretsconfigs.yaml @@ -1173,6 +1173,43 @@ spec: for the controller to use while installing the `external-secrets` operand and the plugins. properties: + annotations: + description: |- + annotations allows adding custom annotations to all external-secrets component + Deployments and Pod templates. These annotations are applied globally to all + operand components (Controller, Webhook, CertController, BitwardenSDKServer). + These annotations are merged with any default annotations set by the operator. + User-specified annotations take precedence over defaults in case of conflicts. + Annotations with keys starting with kubernetes.io/, app.kubernetes.io/, openshift.io/, or k8s.io/ + are reserved and cannot be overridden. + items: + description: |- + Annotation represents a custom annotation key-value pair. + Embeds KVPair inline for reusability. + properties: + key: + description: key is the name of the key-value pair entry. + maxLength: 317 + minLength: 1 + type: string + value: + description: value is the value of the key-value pair entry. + maxLength: 1024 + type: string + required: + - key + type: object + maxItems: 50 + minItems: 0 + type: array + x-kubernetes-list-map-keys: + - key + x-kubernetes-list-type: map + x-kubernetes-validations: + - message: annotations with reserved prefixes 'kubernetes.io/', + 'app.kubernetes.io/', 'openshift.io/', 'k8s.io/' are not allowed + rule: self.all(a, !['kubernetes.io/', 'app.kubernetes.io/', + 'openshift.io/', 'k8s.io/'].exists(p, a.key.startsWith(p))) certProvider: description: certProvider is for defining the configuration for certificate providers used to manage TLS certificates for webhook @@ -1263,6 +1300,195 @@ spec: rule: 'has(self.injectAnnotations) && self.injectAnnotations != ''false'' ? self.mode != ''Disabled'' : true' type: object + componentConfig: + description: |- + componentConfigs allows specifying component-specific configuration overrides + for each external-secrets operand component (Controller, Webhook, CertController, BitwardenSDKServer). + Each entry targets a specific component by name and can include deployment-level overrides + (such as revisionHistoryLimit) and custom environment variables. + The componentName must be unique across all entries. + items: + description: |- + ComponentConfig holds configuration overrides for a specific external-secrets operand component. + It allows specifying deployment-level configuration and custom environment variables + for each component independently. + properties: + componentName: + allOf: + - enum: + - ExternalSecretsCoreController + - Webhook + - CertController + - BitwardenSDKServer + - enum: + - ExternalSecretsCoreController + - Webhook + - CertController + - BitwardenSDKServer + description: |- + componentName specifies which deployment component this configuration applies to. + Each component can only appear once in the componentConfig list. + type: string + deploymentConfigs: + description: |- + deploymentConfigs allows specifying deployment-level configuration overrides + for the targeted component, such as revisionHistoryLimit. + properties: + revisionHistoryLimit: + description: |- + revisionHistoryLimit specifies the number of old ReplicaSets to retain for rollback. + Minimum value of 1 is enforced to ensure rollback capability. + format: int32 + minimum: 1 + type: integer + type: object + overrideEnv: + description: |- + overrideEnv allows setting custom environment variables for the component's container. + These environment variables are merged with the default environment variables set by + the operator. User-specified variables take precedence in case of conflicts. + Environment variables starting with HOSTNAME, KUBERNETES_, or EXTERNAL_SECRETS_ are reserved + and cannot be overridden. + items: + description: EnvVar represents an environment variable + present in a Container. + properties: + name: + description: Name of the environment variable. Must + be a C_IDENTIFIER. + type: string + value: + description: |- + Variable references $(VAR_NAME) are expanded + using the previously defined environment variables in the container and + any service environment variables. If a variable cannot be resolved, + the reference in the input string will be unchanged. Double $$ are reduced + to a single $, which allows for escaping the $(VAR_NAME) syntax: i.e. + "$$(VAR_NAME)" will produce the string literal "$(VAR_NAME)". + Escaped references will never be expanded, regardless of whether the variable + exists or not. + Defaults to "". + type: string + valueFrom: + description: Source for the environment variable's + value. Cannot be used if value is not empty. + properties: + configMapKeyRef: + description: Selects a key of a ConfigMap. + properties: + key: + description: The key to select. + type: string + name: + default: "" + description: |- + Name of the referent. + This field is effectively required, but due to backwards compatibility is + allowed to be empty. Instances of this type with an empty value here are + almost certainly wrong. + More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names + type: string + optional: + description: Specify whether the ConfigMap + or its key must be defined + type: boolean + required: + - key + type: object + x-kubernetes-map-type: atomic + fieldRef: + description: |- + Selects a field of the pod: supports metadata.name, metadata.namespace, `metadata.labels['']`, `metadata.annotations['']`, + spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP, status.podIPs. + properties: + apiVersion: + description: Version of the schema the FieldPath + is written in terms of, defaults to "v1". + type: string + fieldPath: + description: Path of the field to select in + the specified API version. + type: string + required: + - fieldPath + type: object + x-kubernetes-map-type: atomic + resourceFieldRef: + description: |- + Selects a resource of the container: only resources limits and requests + (limits.cpu, limits.memory, limits.ephemeral-storage, requests.cpu, requests.memory and requests.ephemeral-storage) are currently supported. + properties: + containerName: + description: 'Container name: required for + volumes, optional for env vars' + type: string + divisor: + anyOf: + - type: integer + - type: string + description: Specifies the output format of + the exposed resources, defaults to "1" + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + resource: + description: 'Required: resource to select' + type: string + required: + - resource + type: object + x-kubernetes-map-type: atomic + secretKeyRef: + description: Selects a key of a secret in the + pod's namespace + properties: + key: + description: The key of the secret to select + from. Must be a valid secret key. + type: string + name: + default: "" + description: |- + Name of the referent. + This field is effectively required, but due to backwards compatibility is + allowed to be empty. Instances of this type with an empty value here are + almost certainly wrong. + More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names + type: string + optional: + description: Specify whether the Secret or + its key must be defined + type: boolean + required: + - key + type: object + x-kubernetes-map-type: atomic + type: object + required: + - name + type: object + maxItems: 50 + minItems: 0 + type: array + x-kubernetes-list-type: atomic + x-kubernetes-validations: + - message: environment variable names with reserved prefixes + 'HOSTNAME', 'KUBERNETES_', 'EXTERNAL_SECRETS_' are not + allowed + rule: self.all(e, !['HOSTNAME', 'KUBERNETES_', 'EXTERNAL_SECRETS_'].exists(p, + e.name.startsWith(p))) + required: + - componentName + type: object + maxItems: 4 + minItems: 0 + type: array + x-kubernetes-list-map-keys: + - componentName + x-kubernetes-list-type: map + x-kubernetes-validations: + - message: componentName must be unique across all componentConfig + entries + rule: self.all(x, self.exists_one(y, x.componentName == y.componentName)) labels: additionalProperties: type: string @@ -1289,11 +1515,19 @@ spec: It includes a name for identification and the network policy rules to be enforced. properties: componentName: + allOf: + - enum: + - ExternalSecretsCoreController + - Webhook + - CertController + - BitwardenSDKServer + - enum: + - ExternalSecretsCoreController + - Webhook + - CertController + - BitwardenSDKServer description: componentName specifies which external-secrets component this network policy applies to. - enum: - - ExternalSecretsCoreController - - BitwardenSDKServer type: string egress: description: |- diff --git a/pkg/controller/external_secrets/component_config.go b/pkg/controller/external_secrets/component_config.go new file mode 100644 index 000000000..f67c58e76 --- /dev/null +++ b/pkg/controller/external_secrets/component_config.go @@ -0,0 +1,132 @@ +package external_secrets + +import ( + "fmt" + + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + + operatorv1alpha1 "github.com/openshift/external-secrets-operator/api/v1alpha1" +) + +// componentNameToDeploymentAsset maps a ComponentName to its corresponding deployment asset name. +var componentNameToDeploymentAsset = map[operatorv1alpha1.ComponentName]string{ + operatorv1alpha1.CoreController: controllerDeploymentAssetName, + operatorv1alpha1.Webhook: webhookDeploymentAssetName, + operatorv1alpha1.CertController: certControllerDeploymentAssetName, + operatorv1alpha1.BitwardenSDKServer: bitwardenDeploymentAssetName, +} + +// componentNameToContainerName maps a ComponentName to the primary container name within its deployment. +var componentNameToContainerName = map[operatorv1alpha1.ComponentName]string{ + operatorv1alpha1.CoreController: "external-secrets", + operatorv1alpha1.Webhook: "webhook", + operatorv1alpha1.CertController: "cert-controller", + operatorv1alpha1.BitwardenSDKServer: "bitwarden-sdk-server", +} + +// applyAnnotations merges user-specified annotations from ExternalSecretsConfig onto +// both the Deployment ObjectMeta and the Pod template ObjectMeta. Annotations with +// reserved prefixes are skipped (they are validated at the CRD level). +func applyAnnotations(deployment *appsv1.Deployment, annotations []operatorv1alpha1.Annotation) { + if len(annotations) == 0 { + return + } + + // Apply to Deployment ObjectMeta + deployAnnotations := deployment.GetAnnotations() + if deployAnnotations == nil { + deployAnnotations = make(map[string]string, len(annotations)) + } + for _, a := range annotations { + deployAnnotations[a.Key] = a.Value + } + deployment.SetAnnotations(deployAnnotations) + + // Apply to Pod template ObjectMeta + podAnnotations := deployment.Spec.Template.GetAnnotations() + if podAnnotations == nil { + podAnnotations = make(map[string]string, len(annotations)) + } + for _, a := range annotations { + podAnnotations[a.Key] = a.Value + } + deployment.Spec.Template.SetAnnotations(podAnnotations) +} + +// applyComponentConfig applies component-specific configuration overrides to a deployment. +// It looks up the matching ComponentConfig entry for the given component name and applies: +// - revisionHistoryLimit from deploymentConfigs +// - overrideEnv from overrideEnv +func applyComponentConfig(deployment *appsv1.Deployment, componentConfigs []operatorv1alpha1.ComponentConfig, componentName operatorv1alpha1.ComponentName) { + if len(componentConfigs) == 0 { + return + } + + // Find the matching ComponentConfig for this component + var config *operatorv1alpha1.ComponentConfig + for i := range componentConfigs { + if componentConfigs[i].ComponentName == componentName { + config = &componentConfigs[i] + break + } + } + if config == nil { + return + } + + // Apply revisionHistoryLimit if specified + if config.DeploymentConfigs.RevisionHistoryLimit != nil { + deployment.Spec.RevisionHistoryLimit = config.DeploymentConfigs.RevisionHistoryLimit + } + + // Apply override environment variables if specified + if len(config.OverrideEnv) > 0 { + containerName, ok := componentNameToContainerName[componentName] + if !ok { + return + } + applyOverrideEnv(deployment, containerName, config.OverrideEnv) + } +} + +// applyOverrideEnv merges override environment variables into the specified container. +// User-specified variables take precedence over existing ones. Variables with reserved +// prefixes are rejected at the CRD validation level. +func applyOverrideEnv(deployment *appsv1.Deployment, containerName string, overrideEnv []corev1.EnvVar) { + for i := range deployment.Spec.Template.Spec.Containers { + if deployment.Spec.Template.Spec.Containers[i].Name != containerName { + continue + } + + container := &deployment.Spec.Template.Spec.Containers[i] + if container.Env == nil { + container.Env = make([]corev1.EnvVar, 0, len(overrideEnv)) + } + + for _, override := range overrideEnv { + found := false + for j := range container.Env { + if container.Env[j].Name == override.Name { + container.Env[j] = override + found = true + break + } + } + if !found { + container.Env = append(container.Env, override) + } + } + break + } +} + +// getComponentNameForAsset returns the ComponentName associated with a given deployment asset name. +func getComponentNameForAsset(assetName string) (operatorv1alpha1.ComponentName, error) { + for componentName, asset := range componentNameToDeploymentAsset { + if asset == assetName { + return componentName, nil + } + } + return "", fmt.Errorf("no component mapping found for asset: %s", assetName) +} diff --git a/pkg/controller/external_secrets/component_config_test.go b/pkg/controller/external_secrets/component_config_test.go new file mode 100644 index 000000000..0fea8d4d7 --- /dev/null +++ b/pkg/controller/external_secrets/component_config_test.go @@ -0,0 +1,393 @@ +package external_secrets + +import ( + "testing" + + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/ptr" + + operatorv1alpha1 "github.com/openshift/external-secrets-operator/api/v1alpha1" +) + +func TestApplyAnnotations(t *testing.T) { + tests := []struct { + name string + existingAnnotations map[string]string + existingPodAnnotations map[string]string + annotations []operatorv1alpha1.Annotation + expectedAnnotations map[string]string + expectedPodAnnotations map[string]string + }{ + { + name: "no annotations to apply", + existingAnnotations: nil, + existingPodAnnotations: nil, + annotations: nil, + expectedAnnotations: nil, + expectedPodAnnotations: nil, + }, + { + name: "apply single annotation", + existingAnnotations: nil, + existingPodAnnotations: nil, + annotations: []operatorv1alpha1.Annotation{ + {KVPair: operatorv1alpha1.KVPair{Key: "example.com/custom", Value: "test-value"}}, + }, + expectedAnnotations: map[string]string{"example.com/custom": "test-value"}, + expectedPodAnnotations: map[string]string{"example.com/custom": "test-value"}, + }, + { + name: "apply multiple annotations", + existingAnnotations: map[string]string{"existing": "annotation"}, + annotations: []operatorv1alpha1.Annotation{ + {KVPair: operatorv1alpha1.KVPair{Key: "example.com/first", Value: "value-1"}}, + {KVPair: operatorv1alpha1.KVPair{Key: "example.com/second", Value: "value-2"}}, + }, + expectedAnnotations: map[string]string{ + "existing": "annotation", + "example.com/first": "value-1", + "example.com/second": "value-2", + }, + expectedPodAnnotations: map[string]string{ + "example.com/first": "value-1", + "example.com/second": "value-2", + }, + }, + { + name: "override existing annotation", + existingAnnotations: map[string]string{"example.com/key": "old-value"}, + annotations: []operatorv1alpha1.Annotation{ + {KVPair: operatorv1alpha1.KVPair{Key: "example.com/key", Value: "new-value"}}, + }, + expectedAnnotations: map[string]string{"example.com/key": "new-value"}, + expectedPodAnnotations: map[string]string{"example.com/key": "new-value"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + deployment := &appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-deployment", + Annotations: tt.existingAnnotations, + }, + Spec: appsv1.DeploymentSpec{ + Template: corev1.PodTemplateSpec{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: tt.existingPodAnnotations, + }, + }, + }, + } + + applyAnnotations(deployment, tt.annotations) + + // Check deployment annotations + if tt.expectedAnnotations == nil { + if len(deployment.GetAnnotations()) != 0 && tt.existingAnnotations == nil { + t.Errorf("expected no deployment annotations, got %v", deployment.GetAnnotations()) + } + } else { + for k, v := range tt.expectedAnnotations { + actual, ok := deployment.GetAnnotations()[k] + if !ok { + t.Errorf("expected deployment annotation %s not found", k) + } + if actual != v { + t.Errorf("expected deployment annotation %s=%s, got %s", k, v, actual) + } + } + } + + // Check pod template annotations + if tt.expectedPodAnnotations == nil { + if len(deployment.Spec.Template.GetAnnotations()) != 0 && tt.existingPodAnnotations == nil { + t.Errorf("expected no pod annotations, got %v", deployment.Spec.Template.GetAnnotations()) + } + } else { + for k, v := range tt.expectedPodAnnotations { + actual, ok := deployment.Spec.Template.GetAnnotations()[k] + if !ok { + t.Errorf("expected pod annotation %s not found", k) + } + if actual != v { + t.Errorf("expected pod annotation %s=%s, got %s", k, v, actual) + } + } + } + }) + } +} + +func TestApplyComponentConfig(t *testing.T) { + tests := []struct { + name string + componentConfigs []operatorv1alpha1.ComponentConfig + componentName operatorv1alpha1.ComponentName + expectedRevisionHistoryLimit *int32 + expectedEnvVars []corev1.EnvVar + containerName string + }{ + { + name: "no component configs", + componentConfigs: nil, + componentName: operatorv1alpha1.CoreController, + expectedRevisionHistoryLimit: nil, + containerName: "external-secrets", + }, + { + name: "apply revisionHistoryLimit", + componentConfigs: []operatorv1alpha1.ComponentConfig{ + { + ComponentName: operatorv1alpha1.CoreController, + DeploymentConfigs: operatorv1alpha1.DeploymentConfig{ + RevisionHistoryLimit: ptr.To(int32(5)), + }, + }, + }, + componentName: operatorv1alpha1.CoreController, + expectedRevisionHistoryLimit: ptr.To(int32(5)), + containerName: "external-secrets", + }, + { + name: "apply overrideEnv to controller", + componentConfigs: []operatorv1alpha1.ComponentConfig{ + { + ComponentName: operatorv1alpha1.CoreController, + OverrideEnv: []corev1.EnvVar{ + {Name: "GOMAXPROCS", Value: "4"}, + }, + }, + }, + componentName: operatorv1alpha1.CoreController, + expectedEnvVars: []corev1.EnvVar{ + {Name: "GOMAXPROCS", Value: "4"}, + }, + containerName: "external-secrets", + }, + { + name: "apply overrideEnv to webhook", + componentConfigs: []operatorv1alpha1.ComponentConfig{ + { + ComponentName: operatorv1alpha1.Webhook, + OverrideEnv: []corev1.EnvVar{ + {Name: "MY_VAR", Value: "value"}, + }, + }, + }, + componentName: operatorv1alpha1.Webhook, + expectedEnvVars: []corev1.EnvVar{ + {Name: "MY_VAR", Value: "value"}, + }, + containerName: "webhook", + }, + { + name: "no matching component config", + componentConfigs: []operatorv1alpha1.ComponentConfig{ + { + ComponentName: operatorv1alpha1.Webhook, + DeploymentConfigs: operatorv1alpha1.DeploymentConfig{ + RevisionHistoryLimit: ptr.To(int32(3)), + }, + }, + }, + componentName: operatorv1alpha1.CoreController, + expectedRevisionHistoryLimit: nil, + containerName: "external-secrets", + }, + { + name: "apply both revisionHistoryLimit and overrideEnv", + componentConfigs: []operatorv1alpha1.ComponentConfig{ + { + ComponentName: operatorv1alpha1.CertController, + DeploymentConfigs: operatorv1alpha1.DeploymentConfig{ + RevisionHistoryLimit: ptr.To(int32(10)), + }, + OverrideEnv: []corev1.EnvVar{ + {Name: "CUSTOM_VAR", Value: "custom-value"}, + }, + }, + }, + componentName: operatorv1alpha1.CertController, + expectedRevisionHistoryLimit: ptr.To(int32(10)), + expectedEnvVars: []corev1.EnvVar{ + {Name: "CUSTOM_VAR", Value: "custom-value"}, + }, + containerName: "cert-controller", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + deployment := &appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-deployment", + }, + Spec: appsv1.DeploymentSpec{ + Template: corev1.PodTemplateSpec{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + {Name: tt.containerName}, + }, + }, + }, + }, + } + + applyComponentConfig(deployment, tt.componentConfigs, tt.componentName) + + // Check revisionHistoryLimit + if tt.expectedRevisionHistoryLimit != nil { + if deployment.Spec.RevisionHistoryLimit == nil { + t.Error("expected revisionHistoryLimit to be set, got nil") + } else if *deployment.Spec.RevisionHistoryLimit != *tt.expectedRevisionHistoryLimit { + t.Errorf("expected revisionHistoryLimit %d, got %d", *tt.expectedRevisionHistoryLimit, *deployment.Spec.RevisionHistoryLimit) + } + } else { + if deployment.Spec.RevisionHistoryLimit != nil { + t.Errorf("expected revisionHistoryLimit to be nil, got %d", *deployment.Spec.RevisionHistoryLimit) + } + } + + // Check env vars + if tt.expectedEnvVars != nil { + for _, container := range deployment.Spec.Template.Spec.Containers { + if container.Name == tt.containerName { + for _, expected := range tt.expectedEnvVars { + found := false + for _, env := range container.Env { + if env.Name == expected.Name && env.Value == expected.Value { + found = true + break + } + } + if !found { + t.Errorf("expected env var %s=%s not found in container %s", expected.Name, expected.Value, tt.containerName) + } + } + } + } + } + }) + } +} + +func TestApplyOverrideEnv(t *testing.T) { + tests := []struct { + name string + existingEnv []corev1.EnvVar + overrideEnv []corev1.EnvVar + containerName string + expectedEnv []corev1.EnvVar + }{ + { + name: "add env vars to empty container", + existingEnv: nil, + overrideEnv: []corev1.EnvVar{{Name: "VAR1", Value: "val1"}}, + containerName: "external-secrets", + expectedEnv: []corev1.EnvVar{{Name: "VAR1", Value: "val1"}}, + }, + { + name: "override existing env var", + existingEnv: []corev1.EnvVar{{Name: "VAR1", Value: "old-val"}}, + overrideEnv: []corev1.EnvVar{{Name: "VAR1", Value: "new-val"}}, + containerName: "external-secrets", + expectedEnv: []corev1.EnvVar{{Name: "VAR1", Value: "new-val"}}, + }, + { + name: "add new env var alongside existing", + existingEnv: []corev1.EnvVar{{Name: "EXISTING", Value: "val"}}, + overrideEnv: []corev1.EnvVar{{Name: "NEW_VAR", Value: "new-val"}}, + containerName: "external-secrets", + expectedEnv: []corev1.EnvVar{ + {Name: "EXISTING", Value: "val"}, + {Name: "NEW_VAR", Value: "new-val"}, + }, + }, + { + name: "wrong container name - no effect", + existingEnv: nil, + overrideEnv: []corev1.EnvVar{{Name: "VAR1", Value: "val1"}}, + containerName: "wrong-container", + expectedEnv: nil, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + deployment := &appsv1.Deployment{ + Spec: appsv1.DeploymentSpec{ + Template: corev1.PodTemplateSpec{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "external-secrets", + Env: tt.existingEnv, + }, + }, + }, + }, + }, + } + + applyOverrideEnv(deployment, tt.containerName, tt.overrideEnv) + + container := deployment.Spec.Template.Spec.Containers[0] + if tt.expectedEnv == nil && tt.containerName == "wrong-container" { + if len(container.Env) != len(tt.existingEnv) { + t.Errorf("expected env to be unchanged") + } + return + } + + if tt.expectedEnv != nil { + for _, expected := range tt.expectedEnv { + found := false + for _, env := range container.Env { + if env.Name == expected.Name && env.Value == expected.Value { + found = true + break + } + } + if !found { + t.Errorf("expected env var %s=%s not found", expected.Name, expected.Value) + } + } + } + }) + } +} + +func TestGetComponentNameForAsset(t *testing.T) { + tests := []struct { + assetName string + expectedName operatorv1alpha1.ComponentName + expectError bool + }{ + {controllerDeploymentAssetName, operatorv1alpha1.CoreController, false}, + {webhookDeploymentAssetName, operatorv1alpha1.Webhook, false}, + {certControllerDeploymentAssetName, operatorv1alpha1.CertController, false}, + {bitwardenDeploymentAssetName, operatorv1alpha1.BitwardenSDKServer, false}, + {"unknown-asset", "", true}, + } + + for _, tt := range tests { + t.Run(string(tt.assetName), func(t *testing.T) { + name, err := getComponentNameForAsset(tt.assetName) + if tt.expectError { + if err == nil { + t.Error("expected error, got nil") + } + return + } + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if name != tt.expectedName { + t.Errorf("expected %s, got %s", tt.expectedName, name) + } + }) + } +} diff --git a/pkg/controller/external_secrets/deployments.go b/pkg/controller/external_secrets/deployments.go index 47ab49d9c..771b6cdfc 100644 --- a/pkg/controller/external_secrets/deployments.go +++ b/pkg/controller/external_secrets/deployments.go @@ -148,6 +148,17 @@ func (r *Reconciler) getDeploymentObject(assetName string, esc *operatorv1alpha1 return nil, fmt.Errorf("failed to update proxy configuration: %w", err) } + // Apply user-configured annotations to Deployment and Pod template + applyAnnotations(deployment, esc.Spec.ControllerConfig.Annotations) + + // Apply per-component configuration overrides (revisionHistoryLimit, overrideEnv) + componentName, err := getComponentNameForAsset(assetName) + if err != nil { + r.log.V(4).Info("no component mapping found for asset, skipping component config", "asset", assetName) + } else { + applyComponentConfig(deployment, esc.Spec.ControllerConfig.ComponentConfigs, componentName) + } + return deployment, nil } diff --git a/pkg/controller/external_secrets/networkpolicy.go b/pkg/controller/external_secrets/networkpolicy.go index 2e735c2fe..f14577092 100644 --- a/pkg/controller/external_secrets/networkpolicy.go +++ b/pkg/controller/external_secrets/networkpolicy.go @@ -207,6 +207,18 @@ func (r *Reconciler) getPodSelectorForComponent(componentName operatorv1alpha1.C "app.kubernetes.io/name": "external-secrets", }, }, nil + case operatorv1alpha1.Webhook: + return metav1.LabelSelector{ + MatchLabels: map[string]string{ + "app.kubernetes.io/name": "external-secrets-webhook", + }, + }, nil + case operatorv1alpha1.CertController: + return metav1.LabelSelector{ + MatchLabels: map[string]string{ + "app.kubernetes.io/name": "external-secrets-cert-controller", + }, + }, nil case operatorv1alpha1.BitwardenSDKServer: return metav1.LabelSelector{ MatchLabels: map[string]string{