diff --git a/CHANGES/1380.feature b/CHANGES/1380.feature new file mode 100644 index 000000000..8479205e1 --- /dev/null +++ b/CHANGES/1380.feature @@ -0,0 +1 @@ +Make the IP family of every Service the operator creates for pulp-api, pulp-content, the database, and otel (SingleStack/PreferDualStack/RequireDualStack, IPv4/IPv6/dual-stack) configurable via two new Pulp CR fields, `ip_family_policy` and `ip_families`, mirroring `corev1.ServiceSpec`'s own fields exactly. Defaults (unset) are unchanged from today's hardcoded SingleStack/IPv4 behavior. diff --git a/apis/repo-manager.pulpproject.org/v1/pulp_types.go b/apis/repo-manager.pulpproject.org/v1/pulp_types.go index f8837cc22..57dd5b821 100644 --- a/apis/repo-manager.pulpproject.org/v1/pulp_types.go +++ b/apis/repo-manager.pulpproject.org/v1/pulp_types.go @@ -96,6 +96,35 @@ type PulpSpec struct { // +operator-sdk:csv:customresourcedefinitions:type=spec,xDescriptors={"urn:alm:descriptor:io.kubernetes:Secret","urn:alm:descriptor:com.tectonic.ui:advanced"} SigningScripts string `json:"signing_scripts,omitempty"` + // The IP family policy to use for the Services the operator creates for + // pulp-api, pulp-content, the database, and otel. Does not apply to + // pulp-web or redis, which inherit the cluster's default IP family. + // Mirrors corev1.ServiceSpec.IPFamilyPolicy exactly: SingleStack, + // PreferDualStack, or RequireDualStack. + // Default: SingleStack + // Kubernetes only allows changing this on an existing Service from + // SingleStack to a dual-stack policy; other transitions are rejected + // by the API server as immutable, so set it before first creating a Pulp CR. + // +kubebuilder:validation:Optional + // +kubebuilder:validation:Enum:=SingleStack;PreferDualStack;RequireDualStack + // +operator-sdk:csv:customresourcedefinitions:type=spec,xDescriptors={"urn:alm:descriptor:com.tectonic.ui:advanced"} + IPFamilyPolicy *corev1.IPFamilyPolicy `json:"ip_family_policy,omitempty"` + + // The IP families to assign to the Services the operator creates for + // pulp-api, pulp-content, the database, and otel. Does not apply to + // pulp-web or redis, which inherit the cluster's default IP family. + // Mirrors corev1.ServiceSpec.IPFamilies exactly: a list of "IPv4" and/or + // "IPv6", order determines primary family under PreferDualStack/RequireDualStack. + // Must match what the cluster's CNI/cluster-cidr actually supports; the + // operator does not validate this and passes it straight through. + // Default: ["IPv4"] + // See ip_family_policy for the same create-time-only caveat. + // +kubebuilder:validation:Optional + // +kubebuilder:validation:MaxItems:=2 + // +listType:=set + // +operator-sdk:csv:customresourcedefinitions:type=spec,xDescriptors={"urn:alm:descriptor:com.tectonic.ui:advanced"} + IPFamilies []corev1.IPFamily `json:"ip_families,omitempty"` + // The ingress type to use to reach the deployed instance. // Default: none (will not expose the service) // +kubebuilder:validation:Optional diff --git a/apis/repo-manager.pulpproject.org/v1/zz_generated.deepcopy.go b/apis/repo-manager.pulpproject.org/v1/zz_generated.deepcopy.go index bd87395a5..fb9674df3 100644 --- a/apis/repo-manager.pulpproject.org/v1/zz_generated.deepcopy.go +++ b/apis/repo-manager.pulpproject.org/v1/zz_generated.deepcopy.go @@ -662,6 +662,16 @@ func (in *PulpRestoreStatus) DeepCopy() *PulpRestoreStatus { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *PulpSpec) DeepCopyInto(out *PulpSpec) { *out = *in + if in.IPFamilyPolicy != nil { + in, out := &in.IPFamilyPolicy, &out.IPFamilyPolicy + *out = new(corev1.IPFamilyPolicy) + **out = **in + } + if in.IPFamilies != nil { + in, out := &in.IPFamilies, &out.IPFamilies + *out = make([]corev1.IPFamily, len(*in)) + copy(*out, *in) + } if in.IngressAnnotations != nil { in, out := &in.IngressAnnotations, &out.IngressAnnotations *out = make(map[string]string, len(*in)) diff --git a/bundle/manifests/pulp-operator.clusterserviceversion.yaml b/bundle/manifests/pulp-operator.clusterserviceversion.yaml index e5848ba3d..6ef954d79 100644 --- a/bundle/manifests/pulp-operator.clusterserviceversion.yaml +++ b/bundle/manifests/pulp-operator.clusterserviceversion.yaml @@ -65,7 +65,7 @@ metadata: capabilities: Full Lifecycle categories: Integration & Delivery containerImage: quay.io/pulp/pulp-operator:v1.0.0 - createdAt: "2026-03-17T12:18:57Z" + createdAt: "2026-11-05T15:58:44Z" description: Pulp is a platform for managing repositories of software packages and making them available to a large number of consumers. operators.operatorframework.io/builder: operator-sdk-v1.31.0 @@ -754,6 +754,29 @@ spec: path: inhibit_version_constraint x-descriptors: - urn:alm:descriptor:com.tectonic.ui:advanced + - description: 'The IP families to assign to the Services the operator creates + for pulp-api, pulp-content, the database, and otel. Does not apply to pulp-web + or redis, which inherit the cluster''s default IP family. Mirrors corev1.ServiceSpec.IPFamilies + exactly: a list of "IPv4" and/or "IPv6", order determines primary family + under PreferDualStack/RequireDualStack. Must match what the cluster''s CNI/cluster-cidr + actually supports; the operator does not validate this and passes it straight + through. Default: ["IPv4"] See ip_family_policy for the same create-time-only + caveat.' + displayName: IPFamilies + path: ip_families + x-descriptors: + - urn:alm:descriptor:com.tectonic.ui:advanced + - description: 'The IP family policy to use for the Services the operator creates + for pulp-api, pulp-content, the database, and otel. Does not apply to pulp-web + or redis, which inherit the cluster''s default IP family. Mirrors corev1.ServiceSpec.IPFamilyPolicy + exactly: SingleStack, PreferDualStack, or RequireDualStack. Default: SingleStack + Kubernetes only allows changing this on an existing Service from SingleStack + to a dual-stack policy; other transitions are rejected by the API server + as immutable, so set it before first creating a Pulp CR.' + displayName: IPFamily Policy + path: ip_family_policy + x-descriptors: + - urn:alm:descriptor:com.tectonic.ui:advanced - description: Disable ipv6 for pulpcore and pulp-web pods displayName: IPv6 Disabled path: ipv6_disabled diff --git a/bundle/manifests/repo-manager.pulpproject.org_pulps.yaml b/bundle/manifests/repo-manager.pulpproject.org_pulps.yaml index 842ef8249..7c48f1d41 100644 --- a/bundle/manifests/repo-manager.pulpproject.org_pulps.yaml +++ b/bundle/manifests/repo-manager.pulpproject.org_pulps.yaml @@ -7475,6 +7475,41 @@ spec: Relax the check of image_version and image_web_version not matching. Default: "false" type: boolean + ip_families: + description: |- + The IP families to assign to the Services the operator creates for + pulp-api, pulp-content, the database, and otel. Does not apply to + pulp-web or redis, which inherit the cluster's default IP family. + Mirrors corev1.ServiceSpec.IPFamilies exactly: a list of "IPv4" and/or + "IPv6", order determines primary family under PreferDualStack/RequireDualStack. + Must match what the cluster's CNI/cluster-cidr actually supports; the + operator does not validate this and passes it straight through. + Default: ["IPv4"] + See ip_family_policy for the same create-time-only caveat. + items: + description: |- + IPFamily represents the IP Family (IPv4 or IPv6). This type is used + to express the family of an IP expressed by a type (e.g. service.spec.ipFamilies). + type: string + maxItems: 2 + type: array + x-kubernetes-list-type: set + ip_family_policy: + description: |- + The IP family policy to use for the Services the operator creates for + pulp-api, pulp-content, the database, and otel. Does not apply to + pulp-web or redis, which inherit the cluster's default IP family. + Mirrors corev1.ServiceSpec.IPFamilyPolicy exactly: SingleStack, + PreferDualStack, or RequireDualStack. + Default: SingleStack + Kubernetes only allows changing this on an existing Service from + SingleStack to a dual-stack policy; other transitions are rejected + by the API server as immutable, so set it before first creating a Pulp CR. + enum: + - SingleStack + - PreferDualStack + - RequireDualStack + type: string ipv6_disabled: description: Disable ipv6 for pulpcore and pulp-web pods type: boolean diff --git a/config/crd/bases/repo-manager.pulpproject.org_pulps.yaml b/config/crd/bases/repo-manager.pulpproject.org_pulps.yaml index 01b3adb48..7c6615746 100644 --- a/config/crd/bases/repo-manager.pulpproject.org_pulps.yaml +++ b/config/crd/bases/repo-manager.pulpproject.org_pulps.yaml @@ -7475,6 +7475,41 @@ spec: Relax the check of image_version and image_web_version not matching. Default: "false" type: boolean + ip_families: + description: |- + The IP families to assign to the Services the operator creates for + pulp-api, pulp-content, the database, and otel. Does not apply to + pulp-web or redis, which inherit the cluster's default IP family. + Mirrors corev1.ServiceSpec.IPFamilies exactly: a list of "IPv4" and/or + "IPv6", order determines primary family under PreferDualStack/RequireDualStack. + Must match what the cluster's CNI/cluster-cidr actually supports; the + operator does not validate this and passes it straight through. + Default: ["IPv4"] + See ip_family_policy for the same create-time-only caveat. + items: + description: |- + IPFamily represents the IP Family (IPv4 or IPv6). This type is used + to express the family of an IP expressed by a type (e.g. service.spec.ipFamilies). + type: string + maxItems: 2 + type: array + x-kubernetes-list-type: set + ip_family_policy: + description: |- + The IP family policy to use for the Services the operator creates for + pulp-api, pulp-content, the database, and otel. Does not apply to + pulp-web or redis, which inherit the cluster's default IP family. + Mirrors corev1.ServiceSpec.IPFamilyPolicy exactly: SingleStack, + PreferDualStack, or RequireDualStack. + Default: SingleStack + Kubernetes only allows changing this on an existing Service from + SingleStack to a dual-stack policy; other transitions are rejected + by the API server as immutable, so set it before first creating a Pulp CR. + enum: + - SingleStack + - PreferDualStack + - RequireDualStack + type: string ipv6_disabled: description: Disable ipv6 for pulpcore and pulp-web pods type: boolean diff --git a/config/manifests/bases/pulp-operator.clusterserviceversion.yaml b/config/manifests/bases/pulp-operator.clusterserviceversion.yaml index 4cae58950..8f4d4c204 100644 --- a/config/manifests/bases/pulp-operator.clusterserviceversion.yaml +++ b/config/manifests/bases/pulp-operator.clusterserviceversion.yaml @@ -763,6 +763,29 @@ spec: path: inhibit_version_constraint x-descriptors: - urn:alm:descriptor:com.tectonic.ui:advanced + - description: 'The IP families to assign to the Services the operator creates + for pulp-api, pulp-content, the database, and otel. Does not apply to pulp-web + or redis, which inherit the cluster''s default IP family. Mirrors corev1.ServiceSpec.IPFamilies + exactly: a list of "IPv4" and/or "IPv6", order determines primary family + under PreferDualStack/RequireDualStack. Must match what the cluster''s CNI/cluster-cidr + actually supports; the operator does not validate this and passes it straight + through. Default: ["IPv4"] See ip_family_policy for the same create-time-only + caveat.' + displayName: IPFamilies + path: ip_families + x-descriptors: + - urn:alm:descriptor:com.tectonic.ui:advanced + - description: 'The IP family policy to use for the Services the operator creates + for pulp-api, pulp-content, the database, and otel. Does not apply to pulp-web + or redis, which inherit the cluster''s default IP family. Mirrors corev1.ServiceSpec.IPFamilyPolicy + exactly: SingleStack, PreferDualStack, or RequireDualStack. Default: SingleStack + Kubernetes only allows changing this on an existing Service from SingleStack + to a dual-stack policy; other transitions are rejected by the API server + as immutable, so set it before first creating a Pulp CR.' + displayName: IPFamily Policy + path: ip_family_policy + x-descriptors: + - urn:alm:descriptor:com.tectonic.ui:advanced - description: Disable ipv6 for pulpcore and pulp-web pods displayName: IPv6 Disabled path: ipv6_disabled diff --git a/controllers/repo_manager/README.md b/controllers/repo_manager/README.md index 04ad93ace..61609d3a2 100644 --- a/controllers/repo_manager/README.md +++ b/controllers/repo_manager/README.md @@ -214,6 +214,8 @@ PulpSpec defines the desired state of Pulp | db_fields_encryption_secret | Secret where the Fernet symmetric encryption key is stored. Default: -\"-db-fields-encryption\" | string | false | | signing_secret | Name of the Secret where the gpg key is stored. | string | false | | signing_scripts | Name of the Secret where the signing scripts are stored. | string | false | +| ip_family_policy | The IP family policy to use for the Services the operator creates for pulp-api, pulp-content, the database, and otel. Does not apply to pulp-web or redis, which inherit the cluster's default IP family. Mirrors corev1.ServiceSpec.IPFamilyPolicy exactly: SingleStack, PreferDualStack, or RequireDualStack. Default: SingleStack Kubernetes only allows changing this on an existing Service from SingleStack to a dual-stack policy; other transitions are rejected by the API server as immutable, so set it before first creating a Pulp CR. | *corev1.IPFamilyPolicy | false | +| ip_families | The IP families to assign to the Services the operator creates for pulp-api, pulp-content, the database, and otel. Does not apply to pulp-web or redis, which inherit the cluster's default IP family. Mirrors corev1.ServiceSpec.IPFamilies exactly: a list of \"IPv4\" and/or \"IPv6\", order determines primary family under PreferDualStack/RequireDualStack. Must match what the cluster's CNI/cluster-cidr actually supports; the operator does not validate this and passes it straight through. Default: [\"IPv4\"] See ip_family_policy for the same create-time-only caveat. | []corev1.IPFamily | false | | ingress_type | The ingress type to use to reach the deployed instance. Default: none (will not expose the service) | string | false | | ingress_annotations | Annotations for the Ingress | map[string]string | false | | ingress_class_name | IngressClassName is used to inform the operator which ingressclass should be used to provision the ingress. Default: \"\" (will use the default ingress class) | string | false | diff --git a/controllers/repo_manager/api.go b/controllers/repo_manager/api.go index cc3ff7ede..c807d8fde 100644 --- a/controllers/repo_manager/api.go +++ b/controllers/repo_manager/api.go @@ -144,7 +144,6 @@ func serviceAPIObject(pulp pulpv1.Pulp) *corev1.Service { func serviceAPISpec(pulp pulpv1.Pulp) corev1.ServiceSpec { serviceInternalTrafficPolicyCluster := corev1.ServiceInternalTrafficPolicyType("Cluster") - ipFamilyPolicyType := corev1.IPFamilyPolicyType("SingleStack") serviceAffinity := corev1.ServiceAffinity("None") servicePortProto := corev1.Protocol("TCP") targetPort := intstr.IntOrString{IntVal: 24817} @@ -152,8 +151,8 @@ func serviceAPISpec(pulp pulpv1.Pulp) corev1.ServiceSpec { return corev1.ServiceSpec{ InternalTrafficPolicy: &serviceInternalTrafficPolicyCluster, - IPFamilies: []corev1.IPFamily{"IPv4"}, - IPFamilyPolicy: &ipFamilyPolicyType, + IPFamilies: settings.IPFamilies(pulp), + IPFamilyPolicy: settings.IPFamilyPolicy(pulp), Ports: []corev1.ServicePort{{ Name: "api-24817", Port: 24817, diff --git a/controllers/repo_manager/content.go b/controllers/repo_manager/content.go index 342becd43..f9861cfb6 100644 --- a/controllers/repo_manager/content.go +++ b/controllers/repo_manager/content.go @@ -115,7 +115,6 @@ func serviceContentObject(pulp pulpv1.Pulp) *corev1.Service { func serviceContentSpec(pulp pulpv1.Pulp) corev1.ServiceSpec { serviceInternalTrafficPolicyCluster := corev1.ServiceInternalTrafficPolicyType("Cluster") - ipFamilyPolicyType := corev1.IPFamilyPolicyType("SingleStack") serviceAffinity := corev1.ServiceAffinity("None") servicePortProto := corev1.Protocol("TCP") targetPort := intstr.IntOrString{IntVal: 24816} @@ -123,8 +122,8 @@ func serviceContentSpec(pulp pulpv1.Pulp) corev1.ServiceSpec { return corev1.ServiceSpec{ InternalTrafficPolicy: &serviceInternalTrafficPolicyCluster, - IPFamilies: []corev1.IPFamily{"IPv4"}, - IPFamilyPolicy: &ipFamilyPolicyType, + IPFamilies: settings.IPFamilies(pulp), + IPFamilyPolicy: settings.IPFamilyPolicy(pulp), Ports: []corev1.ServicePort{{ Name: "content-24816", Port: 24816, diff --git a/controllers/repo_manager/database.go b/controllers/repo_manager/database.go index 60e967cdd..7fcbe9bfb 100644 --- a/controllers/repo_manager/database.go +++ b/controllers/repo_manager/database.go @@ -475,7 +475,6 @@ func labelsForDatabasePods(m *pulpv1.Pulp) map[string]string { // serviceForDatabase returns a service object for postgres pods func serviceForDatabase(m *pulpv1.Pulp) *corev1.Service { serviceInternalTrafficPolicyCluster := corev1.ServiceInternalTrafficPolicyType("Cluster") - ipFamilyPolicyType := corev1.IPFamilyPolicyType("SingleStack") serviceAffinity := corev1.ServiceAffinity("None") servicePortProto := corev1.Protocol("TCP") targetPort := intstr.IntOrString{IntVal: 5432} @@ -492,8 +491,8 @@ func serviceForDatabase(m *pulpv1.Pulp) *corev1.Service { ClusterIP: "None", ClusterIPs: []string{"None"}, InternalTrafficPolicy: &serviceInternalTrafficPolicyCluster, - IPFamilies: []corev1.IPFamily{"IPv4"}, - IPFamilyPolicy: &ipFamilyPolicyType, + IPFamilies: settings.IPFamilies(*m), + IPFamilyPolicy: settings.IPFamilyPolicy(*m), Ports: []corev1.ServicePort{{ Port: 5432, Protocol: servicePortProto, diff --git a/controllers/repo_manager/precheck.go b/controllers/repo_manager/precheck.go index ca84de204..6e9ca0c01 100644 --- a/controllers/repo_manager/precheck.go +++ b/controllers/repo_manager/precheck.go @@ -24,6 +24,7 @@ import ( "github.com/go-logr/logr" pulpv1 "github.com/pulp/pulp-operator/apis/repo-manager.pulpproject.org/v1" "github.com/pulp/pulp-operator/controllers" + "github.com/pulp/pulp-operator/controllers/settings" corev1 "k8s.io/api/core/v1" v1 "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -89,6 +90,14 @@ func prechecks(ctx context.Context, r *RepoManagerReconciler, pulp *pulpv1.Pulp) return reconcile, nil } + // verify ip_families only contains valid values + if reconcile := checkIPFamiliesValid(pulp); reconcile != nil { + return reconcile, nil + } + + // warn (non-blocking) if ipv6_disabled and ip_families are inconsistent + checkIPv6DisabledConsistency(r, pulp) + return nil, nil } @@ -323,3 +332,68 @@ func checkCAConfigmap(r *RepoManagerReconciler, pulp pulpv1.Pulp) *ctrl.Result { } return nil } + +// checkIPv6DisabledConsistency warns (but does not block reconciliation) if +// spec.ipv6_disabled and spec.ip_families disagree about whether IPv6 should +// be used. ipv6_disabled controls app-level binding (gunicorn/nginx listen +// addresses); ip_families controls which ClusterIP(s) each Service gets. +// Setting ipv6_disabled=true while ip_families includes IPv6 means a Service +// would get a v6 ClusterIP routing to a pod that isn't actually listening on +// v6, a real but non-fatal misconfiguration. This is intentionally a +// warning, not a blocking precheck: the operator does not validate ip_families +// against cluster/CNI capabilities either, by design (see controllers/settings +// IPFamilies/IPFamilyPolicy). +// +// Surfaced via a Kubernetes Event, not a log line. The API server coalesces +// repeated identical (reason, involvedObject) Events into one Event with an +// incrementing count/lastTimestamp, so this avoids spamming the operator's +// own logs every reconcile while still being visible via +// `kubectl describe pulp`/`kubectl get events`. +func checkIPv6DisabledConsistency(r *RepoManagerReconciler, pulp *pulpv1.Pulp) { + if !ipv6DisabledConflictsWithIPFamilies(*pulp) { + return + } + r.recorder.Event(pulp, corev1.EventTypeWarning, "IPv6FamilyMismatch", + "spec.ipv6_disabled is true but spec.ip_families includes IPv6. "+ + "Services will get an IPv6 ClusterIP, but pulp-api/pulp-web will "+ + "only be listening on IPv4 (ipv6_disabled forces 0.0.0.0-only "+ + "binding), so traffic routed over IPv6 will not reach them. "+ + "Set ip_families to [\"IPv4\"] to match ipv6_disabled, or unset "+ + "ipv6_disabled to match ip_families.") +} + +// ipv6DisabledConflictsWithIPFamilies reports whether spec.ipv6_disabled and +// spec.ip_families are mutually inconsistent (ipv6_disabled forces IPv4-only +// app-level binding, but ip_families requests an IPv6 ClusterIP). Pulled out +// of checkIPv6DisabledConsistency as a pure predicate so it's directly +// testable without needing to capture the zap logger's output. +func ipv6DisabledConflictsWithIPFamilies(pulp pulpv1.Pulp) bool { + if !controllers.Ipv6Disabled(pulp) { + return false + } + for _, family := range settings.IPFamilies(pulp) { + if family == corev1.IPv6Protocol { + return true + } + } + return false +} + +// checkIPFamiliesValid verifies that every entry in spec.ip_families is a +// recognized IP family. x-kubernetes-list-type=set and MaxItems=2 (set via +// kubebuilder markers on the field) already stop duplicates and +// over-long lists at the API server; this catches invalid literal values +// (e.g. "foo") that the field's underlying string type doesn't restrict on +// its own, failing fast with a clear message instead of letting Service +// creation be rejected downstream with a generic API server error. +func checkIPFamiliesValid(pulp *pulpv1.Pulp) *ctrl.Result { + for _, family := range pulp.Spec.IPFamilies { + if family != corev1.IPv4Protocol && family != corev1.IPv6Protocol { + controllers.CustomZapLogger().Error( + "spec.ip_families contains an invalid value: \"" + string(family) + "\". " + + "Valid values are \"IPv4\" and \"IPv6\".") + return &ctrl.Result{} + } + } + return nil +} diff --git a/controllers/repo_manager/precheck_ipv6_test.go b/controllers/repo_manager/precheck_ipv6_test.go new file mode 100644 index 000000000..21c3f0d87 --- /dev/null +++ b/controllers/repo_manager/precheck_ipv6_test.go @@ -0,0 +1,135 @@ +package repo_manager + +import ( + "testing" + + pulpv1 "github.com/pulp/pulp-operator/apis/repo-manager.pulpproject.org/v1" + corev1 "k8s.io/api/core/v1" +) + +func TestIpv6DisabledConflictsWithIPFamilies_NoConflictByDefault(t *testing.T) { + pulp := pulpv1.Pulp{} + + if ipv6DisabledConflictsWithIPFamilies(pulp) { + t.Errorf("expected no conflict when neither field is set") + } +} + +func TestIpv6DisabledConflictsWithIPFamilies_NoConflictWhenIPv6NotDisabled(t *testing.T) { + pulp := pulpv1.Pulp{ + Spec: pulpv1.PulpSpec{ + IPFamilies: []corev1.IPFamily{corev1.IPv6Protocol}, + }, + } + + if ipv6DisabledConflictsWithIPFamilies(pulp) { + t.Errorf("expected no conflict when ipv6_disabled is unset, regardless of ip_families") + } +} + +func TestIpv6DisabledConflictsWithIPFamilies_NoConflictWhenFamiliesAreIPv4Only(t *testing.T) { + disabled := true + pulp := pulpv1.Pulp{ + Spec: pulpv1.PulpSpec{ + IPv6Disabled: &disabled, + IPFamilies: []corev1.IPFamily{corev1.IPv4Protocol}, + }, + } + + if ipv6DisabledConflictsWithIPFamilies(pulp) { + t.Errorf("expected no conflict when ipv6_disabled=true and ip_families=[IPv4] agree") + } +} + +func TestIpv6DisabledConflictsWithIPFamilies_ConflictWhenIPv6Only(t *testing.T) { + disabled := true + pulp := pulpv1.Pulp{ + Spec: pulpv1.PulpSpec{ + IPv6Disabled: &disabled, + IPFamilies: []corev1.IPFamily{corev1.IPv6Protocol}, + }, + } + + if !ipv6DisabledConflictsWithIPFamilies(pulp) { + t.Errorf("expected conflict when ipv6_disabled=true but ip_families=[IPv6]") + } +} + +func TestIpv6DisabledConflictsWithIPFamilies_ConflictWhenDualStackIncludesIPv6(t *testing.T) { + disabled := true + pulp := pulpv1.Pulp{ + Spec: pulpv1.PulpSpec{ + IPv6Disabled: &disabled, + IPFamilies: []corev1.IPFamily{corev1.IPv4Protocol, corev1.IPv6Protocol}, + }, + } + + if !ipv6DisabledConflictsWithIPFamilies(pulp) { + t.Errorf("expected conflict when ipv6_disabled=true but ip_families includes IPv6 (dual-stack)") + } +} + +func TestIpv6DisabledConflictsWithIPFamilies_NoConflictWhenExplicitlyFalse(t *testing.T) { + // Explicitly setting ipv6_disabled=false must behave identically to + // leaving it unset (nil), not a separate code path. + disabled := false + pulp := pulpv1.Pulp{ + Spec: pulpv1.PulpSpec{ + IPv6Disabled: &disabled, + IPFamilies: []corev1.IPFamily{corev1.IPv6Protocol}, + }, + } + + if ipv6DisabledConflictsWithIPFamilies(pulp) { + t.Errorf("expected no conflict when ipv6_disabled is explicitly false, regardless of ip_families") + } +} + +func TestIpv6DisabledConflictsWithIPFamilies_NoConflictWhenFamiliesExplicitlyEmpty(t *testing.T) { + // An explicitly empty (non-nil) ip_families slice must default to IPv4, + // same as unset, not be treated as "no families" with no conflict for + // the wrong reason. + disabled := true + pulp := pulpv1.Pulp{ + Spec: pulpv1.PulpSpec{ + IPv6Disabled: &disabled, + IPFamilies: []corev1.IPFamily{}, + }, + } + + if ipv6DisabledConflictsWithIPFamilies(pulp) { + t.Errorf("expected no conflict when ip_families is explicitly empty (defaults to IPv4)") + } +} + +func TestCheckIPFamiliesValid_ValidWhenUnset(t *testing.T) { + pulp := pulpv1.Pulp{} + + if got := checkIPFamiliesValid(&pulp); got != nil { + t.Errorf("checkIPFamiliesValid() = %v, want nil (unset is valid)", got) + } +} + +func TestCheckIPFamiliesValid_ValidForIPv4AndIPv6(t *testing.T) { + pulp := pulpv1.Pulp{ + Spec: pulpv1.PulpSpec{ + IPFamilies: []corev1.IPFamily{corev1.IPv4Protocol, corev1.IPv6Protocol}, + }, + } + + if got := checkIPFamiliesValid(&pulp); got != nil { + t.Errorf("checkIPFamiliesValid() = %v, want nil", got) + } +} + +func TestCheckIPFamiliesValid_RejectsUnrecognizedValue(t *testing.T) { + pulp := pulpv1.Pulp{ + Spec: pulpv1.PulpSpec{ + IPFamilies: []corev1.IPFamily{"foo"}, + }, + } + + if got := checkIPFamiliesValid(&pulp); got == nil { + t.Errorf("checkIPFamiliesValid() = nil, want a non-nil result to block reconciliation on an invalid value") + } +} diff --git a/controllers/settings/services.go b/controllers/settings/services.go index fcae22851..53c7e6df5 100644 --- a/controllers/settings/services.go +++ b/controllers/settings/services.go @@ -8,6 +8,12 @@ package settings +import ( + corev1 "k8s.io/api/core/v1" + + pulpv1 "github.com/pulp/pulp-operator/apis/repo-manager.pulpproject.org/v1" +) + func ApiService(pulpName string) string { return pulpName + "-api-svc" } @@ -26,3 +32,32 @@ func DBService(pulpName string) string { func CacheService(pulpName string) string { return pulpName + "-redis-svc" } + +// IPFamilyPolicy resolves the IP family policy to use for a Service from the +// Pulp CR spec, defaulting to SingleStack (current, pre-existing behavior) +// when the field is unset. Returns a fresh pointer rather than aliasing +// pulp.Spec.IPFamilyPolicy, so callers can't accidentally mutate the CR spec +// through it. +func IPFamilyPolicy(pulp pulpv1.Pulp) *corev1.IPFamilyPolicy { + if pulp.Spec.IPFamilyPolicy != nil { + policy := *pulp.Spec.IPFamilyPolicy + return &policy + } + policy := corev1.IPFamilyPolicySingleStack + return &policy +} + +// IPFamilies resolves the IP families to assign to a Service from the Pulp CR +// spec, defaulting to ["IPv4"] (current, pre-existing behavior) when the +// field is unset. The operator does not validate this against what the +// cluster's CNI actually supports; it is passed straight through. Returns a +// copy rather than aliasing pulp.Spec.IPFamilies, so callers can't +// accidentally mutate the CR spec through the returned slice. +func IPFamilies(pulp pulpv1.Pulp) []corev1.IPFamily { + if len(pulp.Spec.IPFamilies) > 0 { + families := make([]corev1.IPFamily, len(pulp.Spec.IPFamilies)) + copy(families, pulp.Spec.IPFamilies) + return families + } + return []corev1.IPFamily{corev1.IPv4Protocol} +} diff --git a/controllers/settings/services_test.go b/controllers/settings/services_test.go new file mode 100644 index 000000000..126779bea --- /dev/null +++ b/controllers/settings/services_test.go @@ -0,0 +1,140 @@ +package settings + +import ( + "slices" + "testing" + + corev1 "k8s.io/api/core/v1" + + pulpv1 "github.com/pulp/pulp-operator/apis/repo-manager.pulpproject.org/v1" +) + +func TestIPFamilyPolicyDefaultsToSingleStackWhenUnset(t *testing.T) { + pulp := pulpv1.Pulp{} + + got := IPFamilyPolicy(pulp) + + if got == nil { + t.Fatalf("IPFamilyPolicy() = nil, want a pointer to SingleStack") + } + if *got != corev1.IPFamilyPolicySingleStack { + t.Errorf("IPFamilyPolicy() = %q, want %q", *got, corev1.IPFamilyPolicySingleStack) + } +} + +func TestIPFamilyPolicyPassesThroughCROverride(t *testing.T) { + want := corev1.IPFamilyPolicyPreferDualStack + pulp := pulpv1.Pulp{ + Spec: pulpv1.PulpSpec{ + IPFamilyPolicy: &want, + }, + } + + got := IPFamilyPolicy(pulp) + + if got == nil { + t.Fatalf("IPFamilyPolicy() = nil, want %q", want) + } + if *got != want { + t.Errorf("IPFamilyPolicy() = %q, want %q", *got, want) + } +} + +func TestIPFamilyPolicyDoesNotAliasCRField(t *testing.T) { + // The returned pointer must be a copy: mutating it must not mutate the + // Pulp CR's own spec field. + original := corev1.IPFamilyPolicyPreferDualStack + pulp := pulpv1.Pulp{ + Spec: pulpv1.PulpSpec{ + IPFamilyPolicy: &original, + }, + } + + got := IPFamilyPolicy(pulp) + *got = corev1.IPFamilyPolicyRequireDualStack + + if *pulp.Spec.IPFamilyPolicy != corev1.IPFamilyPolicyPreferDualStack { + t.Errorf("mutating the returned pointer changed pulp.Spec.IPFamilyPolicy to %q, want it unchanged at %q", + *pulp.Spec.IPFamilyPolicy, corev1.IPFamilyPolicyPreferDualStack) + } +} + +func TestIPFamiliesDefaultsToIPv4OnlyWhenUnset(t *testing.T) { + pulp := pulpv1.Pulp{} + + got := IPFamilies(pulp) + + want := []corev1.IPFamily{corev1.IPv4Protocol} + if !slices.Equal(got, want) { + t.Errorf("IPFamilies() = %v, want %v", got, want) + } +} + +func TestIPFamiliesDefaultsToIPv4OnlyWhenExplicitlyEmptySlice(t *testing.T) { + // A non-nil but empty slice should be treated the same as unset. + pulp := pulpv1.Pulp{ + Spec: pulpv1.PulpSpec{ + IPFamilies: []corev1.IPFamily{}, + }, + } + + got := IPFamilies(pulp) + + want := []corev1.IPFamily{corev1.IPv4Protocol} + if !slices.Equal(got, want) { + t.Errorf("IPFamilies() = %v, want %v", got, want) + } +} + +func TestIPFamiliesPassesThroughCROverride(t *testing.T) { + want := []corev1.IPFamily{corev1.IPv6Protocol} + pulp := pulpv1.Pulp{ + Spec: pulpv1.PulpSpec{ + IPFamilies: want, + }, + } + + got := IPFamilies(pulp) + + if !slices.Equal(got, want) { + t.Errorf("IPFamilies() = %v, want %v", got, want) + } +} + +func TestIPFamiliesPassesThroughDualStackOrder(t *testing.T) { + // Order matters: it determines the primary family under + // PreferDualStack/RequireDualStack. Confirm it's passed through + // unmodified, not reordered or deduplicated. + want := []corev1.IPFamily{corev1.IPv6Protocol, corev1.IPv4Protocol} + pulp := pulpv1.Pulp{ + Spec: pulpv1.PulpSpec{ + IPFamilies: want, + }, + } + + got := IPFamilies(pulp) + + if !slices.Equal(got, want) { + t.Errorf("IPFamilies() = %v, want %v", got, want) + } +} + +func TestIPFamiliesDoesNotAliasCRField(t *testing.T) { + // The returned slice must be a copy: mutating it must not mutate the + // Pulp CR's own spec field. + original := []corev1.IPFamily{corev1.IPv4Protocol, corev1.IPv6Protocol} + pulp := pulpv1.Pulp{ + Spec: pulpv1.PulpSpec{ + IPFamilies: original, + }, + } + + got := IPFamilies(pulp) + got[0] = corev1.IPv6Protocol + + want := []corev1.IPFamily{corev1.IPv4Protocol, corev1.IPv6Protocol} + if !slices.Equal(pulp.Spec.IPFamilies, want) { + t.Errorf("mutating the returned slice changed pulp.Spec.IPFamilies to %v, want it unchanged at %v", + pulp.Spec.IPFamilies, want) + } +} diff --git a/controllers/telemetry.go b/controllers/telemetry.go index d9f4b166d..6685da1c5 100644 --- a/controllers/telemetry.go +++ b/controllers/telemetry.go @@ -180,7 +180,6 @@ service: // serviceOtel defines a service to expose otel metrics func ServiceOtel(resources FunctionResources) client.Object { serviceInternalTrafficPolicyCluster := corev1.ServiceInternalTrafficPolicyType("Cluster") - ipFamilyPolicyType := corev1.IPFamilyPolicyType("SingleStack") serviceAffinity := corev1.ServiceAffinity("None") servicePortProto := corev1.Protocol("TCP") targetPort := intstr.IntOrString{IntVal: settings.OtelContainerPort} @@ -197,8 +196,8 @@ func ServiceOtel(resources FunctionResources) client.Object { }, Spec: corev1.ServiceSpec{ InternalTrafficPolicy: &serviceInternalTrafficPolicyCluster, - IPFamilies: []corev1.IPFamily{"IPv4"}, - IPFamilyPolicy: &ipFamilyPolicyType, + IPFamilies: settings.IPFamilies(*resources.Pulp), + IPFamilyPolicy: settings.IPFamilyPolicy(*resources.Pulp), Ports: []corev1.ServicePort{{ Name: "otel-" + strconv.Itoa(settings.OtelContainerPort), Port: settings.OtelContainerPort,