func applyServerSideValues()

in pkg/controller/common/service_control.go [90:165]


func applyServerSideValues(expected, reconciled *corev1.Service) {
	// skip if the service type changes from something different to the default ClusterIP value
	if reconciled.Spec.Type != corev1.ServiceTypeClusterIP && expected.Spec.Type != reconciled.Spec.Type {
		return
	}

	// Type may be defaulted by the api server
	if expected.Spec.Type == "" {
		expected.Spec.Type = reconciled.Spec.Type
	}
	// ClusterIPs might not exist in the expected service,
	// but might have been set after creation by k8s on the actual resource.
	// In such case, we want to use these values for comparison.
	// But only if we are not changing the type of service and the api server has assigned an IP
	if expected.Spec.Type == reconciled.Spec.Type {
		if expected.Spec.ClusterIP == "" {
			expected.Spec.ClusterIP = reconciled.Spec.ClusterIP
		}
		if len(expected.Spec.ClusterIPs) == 0 {
			expected.Spec.ClusterIPs = reconciled.Spec.ClusterIPs
		}
	}

	// SessionAffinity may be defaulted by the api server
	if expected.Spec.SessionAffinity == "" {
		expected.Spec.SessionAffinity = reconciled.Spec.SessionAffinity
	}

	// same for the target port and node port
	if len(expected.Spec.Ports) == len(reconciled.Spec.Ports) {
		for i := range expected.Spec.Ports {
			if expected.Spec.Ports[i].TargetPort.IntValue() == 0 {
				expected.Spec.Ports[i].TargetPort = reconciled.Spec.Ports[i].TargetPort
			}
			// check if NodePort makes sense for this service type
			if hasNodePort(expected.Spec.Type) && expected.Spec.Ports[i].NodePort == 0 {
				expected.Spec.Ports[i].NodePort = reconciled.Spec.Ports[i].NodePort
			}
		}
	}

	if expected.Spec.HealthCheckNodePort == 0 {
		expected.Spec.HealthCheckNodePort = reconciled.Spec.HealthCheckNodePort
	}

	expected.Annotations = maps.MergePreservingExistingKeys(expected.Annotations, reconciled.Annotations)
	expected.Labels = maps.MergePreservingExistingKeys(expected.Labels, reconciled.Labels)

	// IPFamily is immutable and cannot be modified so we should retain the existing value from the server if there's no explicit override.
	if expected.Spec.IPFamilies == nil {
		expected.Spec.IPFamilies = reconciled.Spec.IPFamilies
	}

	// IPFamilyPolicy is immutable and cannot be modified so we should retain the existing value from the server if there's no explicit override.
	if expected.Spec.IPFamilyPolicy == nil {
		expected.Spec.IPFamilyPolicy = reconciled.Spec.IPFamilyPolicy
	}

	// InternalTrafficPolicy may be defaulted by the api server starting K8S v1.22
	if expected.Spec.InternalTrafficPolicy == nil {
		expected.Spec.InternalTrafficPolicy = reconciled.Spec.InternalTrafficPolicy
	}

	if expected.Spec.ExternalTrafficPolicy == "" {
		expected.Spec.ExternalTrafficPolicy = reconciled.Spec.ExternalTrafficPolicy
	}

	// LoadBalancerClass may be defaulted by the API server starting K8s v.1.24
	if expected.Spec.Type == corev1.ServiceTypeLoadBalancer && expected.Spec.LoadBalancerClass == nil {
		expected.Spec.LoadBalancerClass = reconciled.Spec.LoadBalancerClass
	}

	if expected.Spec.AllocateLoadBalancerNodePorts == nil {
		expected.Spec.AllocateLoadBalancerNodePorts = reconciled.Spec.AllocateLoadBalancerNodePorts
	}
}