Allow to overwrite default ExternalTrafficPolicy for the service (#1136)
* Allow to overwrite default ExternalTrafficPolicy for the service
This commit is contained in:
		
							parent
							
								
									03437b6374
								
							
						
					
					
						commit
						d8884a4003
					
				|  | @ -460,6 +460,9 @@ In the CRD-based configuration they are grouped under the `load_balancer` key. | ||||||
|   replaced with the hosted zone (the value of the `db_hosted_zone` parameter). |   replaced with the hosted zone (the value of the `db_hosted_zone` parameter). | ||||||
|   No other placeholders are allowed. |   No other placeholders are allowed. | ||||||
| 
 | 
 | ||||||
|  | * **external_traffic_policy** define external traffic policy for the load | ||||||
|  | balancer, it will default to `Cluster` if undefined. | ||||||
|  | 
 | ||||||
| ## AWS or GCP interaction | ## AWS or GCP interaction | ||||||
| 
 | 
 | ||||||
| The options in this group configure operator interactions with non-Kubernetes | The options in this group configure operator interactions with non-Kubernetes | ||||||
|  |  | ||||||
|  | @ -1129,6 +1129,17 @@ var OperatorConfigCRDResourceValidation = apiextv1beta1.CustomResourceValidation | ||||||
| 							"replica_dns_name_format": { | 							"replica_dns_name_format": { | ||||||
| 								Type: "string", | 								Type: "string", | ||||||
| 							}, | 							}, | ||||||
|  | 							"external_traffic_policy": { | ||||||
|  | 								Type: "string", | ||||||
|  | 								Enum: []apiextv1beta1.JSON{ | ||||||
|  | 									{ | ||||||
|  | 										Raw: []byte(`"Cluster"`), | ||||||
|  | 									}, | ||||||
|  | 									{ | ||||||
|  | 										Raw: []byte(`"Local"`), | ||||||
|  | 									}, | ||||||
|  | 								}, | ||||||
|  | 							}, | ||||||
| 						}, | 						}, | ||||||
| 					}, | 					}, | ||||||
| 					"aws_or_gcp": { | 					"aws_or_gcp": { | ||||||
|  |  | ||||||
|  | @ -109,6 +109,7 @@ type LoadBalancerConfiguration struct { | ||||||
| 	CustomServiceAnnotations  map[string]string     `json:"custom_service_annotations,omitempty"` | 	CustomServiceAnnotations  map[string]string     `json:"custom_service_annotations,omitempty"` | ||||||
| 	MasterDNSNameFormat       config.StringTemplate `json:"master_dns_name_format,omitempty"` | 	MasterDNSNameFormat       config.StringTemplate `json:"master_dns_name_format,omitempty"` | ||||||
| 	ReplicaDNSNameFormat      config.StringTemplate `json:"replica_dns_name_format,omitempty"` | 	ReplicaDNSNameFormat      config.StringTemplate `json:"replica_dns_name_format,omitempty"` | ||||||
|  | 	ExternalTrafficPolicy     string                `json:"external_traffic_policy" default:"Cluster"` | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // AWSGCPConfiguration defines the configuration for AWS
 | // AWSGCPConfiguration defines the configuration for AWS
 | ||||||
|  |  | ||||||
|  | @ -1619,6 +1619,7 @@ func (c *Cluster) generateService(role PostgresRole, spec *acidv1.PostgresSpec) | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		c.logger.Debugf("final load balancer source ranges as seen in a service spec (not necessarily applied): %q", serviceSpec.LoadBalancerSourceRanges) | 		c.logger.Debugf("final load balancer source ranges as seen in a service spec (not necessarily applied): %q", serviceSpec.LoadBalancerSourceRanges) | ||||||
|  | 		serviceSpec.ExternalTrafficPolicy = v1.ServiceExternalTrafficPolicyType(c.OpConfig.ExternalTrafficPolicy) | ||||||
| 		serviceSpec.Type = v1.ServiceTypeLoadBalancer | 		serviceSpec.Type = v1.ServiceTypeLoadBalancer | ||||||
| 	} else if role == Replica { | 	} else if role == Replica { | ||||||
| 		// before PR #258, the replica service was only created if allocated a LB
 | 		// before PR #258, the replica service was only created if allocated a LB
 | ||||||
|  |  | ||||||
|  | @ -1742,3 +1742,83 @@ func TestSidecars(t *testing.T) { | ||||||
| 	}) | 	}) | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func TestGenerateService(t *testing.T) { | ||||||
|  | 	var spec acidv1.PostgresSpec | ||||||
|  | 	var cluster *Cluster | ||||||
|  | 	var enableLB bool = true | ||||||
|  | 	spec = acidv1.PostgresSpec{ | ||||||
|  | 		TeamID: "myapp", NumberOfInstances: 1, | ||||||
|  | 		Resources: acidv1.Resources{ | ||||||
|  | 			ResourceRequests: acidv1.ResourceDescription{CPU: "1", Memory: "10"}, | ||||||
|  | 			ResourceLimits:   acidv1.ResourceDescription{CPU: "1", Memory: "10"}, | ||||||
|  | 		}, | ||||||
|  | 		Volume: acidv1.Volume{ | ||||||
|  | 			Size: "1G", | ||||||
|  | 		}, | ||||||
|  | 		Sidecars: []acidv1.Sidecar{ | ||||||
|  | 			acidv1.Sidecar{ | ||||||
|  | 				Name: "cluster-specific-sidecar", | ||||||
|  | 			}, | ||||||
|  | 			acidv1.Sidecar{ | ||||||
|  | 				Name: "cluster-specific-sidecar-with-resources", | ||||||
|  | 				Resources: acidv1.Resources{ | ||||||
|  | 					ResourceRequests: acidv1.ResourceDescription{CPU: "210m", Memory: "0.8Gi"}, | ||||||
|  | 					ResourceLimits:   acidv1.ResourceDescription{CPU: "510m", Memory: "1.4Gi"}, | ||||||
|  | 				}, | ||||||
|  | 			}, | ||||||
|  | 			acidv1.Sidecar{ | ||||||
|  | 				Name:        "replace-sidecar", | ||||||
|  | 				DockerImage: "overwrite-image", | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 		EnableMasterLoadBalancer: &enableLB, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	cluster = New( | ||||||
|  | 		Config{ | ||||||
|  | 			OpConfig: config.Config{ | ||||||
|  | 				PodManagementPolicy: "ordered_ready", | ||||||
|  | 				ProtectedRoles:      []string{"admin"}, | ||||||
|  | 				Auth: config.Auth{ | ||||||
|  | 					SuperUsername:       superUserName, | ||||||
|  | 					ReplicationUsername: replicationUserName, | ||||||
|  | 				}, | ||||||
|  | 				Resources: config.Resources{ | ||||||
|  | 					DefaultCPURequest:    "200m", | ||||||
|  | 					DefaultCPULimit:      "500m", | ||||||
|  | 					DefaultMemoryRequest: "0.7Gi", | ||||||
|  | 					DefaultMemoryLimit:   "1.3Gi", | ||||||
|  | 				}, | ||||||
|  | 				SidecarImages: map[string]string{ | ||||||
|  | 					"deprecated-global-sidecar": "image:123", | ||||||
|  | 				}, | ||||||
|  | 				SidecarContainers: []v1.Container{ | ||||||
|  | 					v1.Container{ | ||||||
|  | 						Name: "global-sidecar", | ||||||
|  | 					}, | ||||||
|  | 					// will be replaced by a cluster specific sidecar with the same name
 | ||||||
|  | 					v1.Container{ | ||||||
|  | 						Name:  "replace-sidecar", | ||||||
|  | 						Image: "replaced-image", | ||||||
|  | 					}, | ||||||
|  | 				}, | ||||||
|  | 				Scalyr: config.Scalyr{ | ||||||
|  | 					ScalyrAPIKey:        "abc", | ||||||
|  | 					ScalyrImage:         "scalyr-image", | ||||||
|  | 					ScalyrCPURequest:    "220m", | ||||||
|  | 					ScalyrCPULimit:      "520m", | ||||||
|  | 					ScalyrMemoryRequest: "0.9Gi", | ||||||
|  | 					// ise default memory limit
 | ||||||
|  | 				}, | ||||||
|  | 				ExternalTrafficPolicy: "Cluster", | ||||||
|  | 			}, | ||||||
|  | 		}, k8sutil.KubernetesClient{}, acidv1.Postgresql{}, logger, eventRecorder) | ||||||
|  | 
 | ||||||
|  | 	service := cluster.generateService(Master, &spec) | ||||||
|  | 	assert.Equal(t, v1.ServiceExternalTrafficPolicyTypeCluster, service.Spec.ExternalTrafficPolicy) | ||||||
|  | 	cluster.OpConfig.ExternalTrafficPolicy = "Local" | ||||||
|  | 	service = cluster.generateService(Master, &spec) | ||||||
|  | 	assert.Equal(t, v1.ServiceExternalTrafficPolicyTypeLocal, service.Spec.ExternalTrafficPolicy) | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -124,6 +124,7 @@ func (c *Controller) importConfigurationFromCRD(fromCRD *acidv1.OperatorConfigur | ||||||
| 	result.CustomServiceAnnotations = fromCRD.LoadBalancer.CustomServiceAnnotations | 	result.CustomServiceAnnotations = fromCRD.LoadBalancer.CustomServiceAnnotations | ||||||
| 	result.MasterDNSNameFormat = fromCRD.LoadBalancer.MasterDNSNameFormat | 	result.MasterDNSNameFormat = fromCRD.LoadBalancer.MasterDNSNameFormat | ||||||
| 	result.ReplicaDNSNameFormat = fromCRD.LoadBalancer.ReplicaDNSNameFormat | 	result.ReplicaDNSNameFormat = fromCRD.LoadBalancer.ReplicaDNSNameFormat | ||||||
|  | 	result.ExternalTrafficPolicy = util.Coalesce(fromCRD.LoadBalancer.ExternalTrafficPolicy, "Cluster") | ||||||
| 
 | 
 | ||||||
| 	// AWS or GCP config
 | 	// AWS or GCP config
 | ||||||
| 	result.WALES3Bucket = fromCRD.AWSGCP.WALES3Bucket | 	result.WALES3Bucket = fromCRD.AWSGCP.WALES3Bucket | ||||||
|  |  | ||||||
|  | @ -175,6 +175,8 @@ type Config struct { | ||||||
| 	EnablePodAntiAffinity                  bool              `name:"enable_pod_antiaffinity" default:"false"` | 	EnablePodAntiAffinity                  bool              `name:"enable_pod_antiaffinity" default:"false"` | ||||||
| 	PodAntiAffinityTopologyKey             string            `name:"pod_antiaffinity_topology_key" default:"kubernetes.io/hostname"` | 	PodAntiAffinityTopologyKey             string            `name:"pod_antiaffinity_topology_key" default:"kubernetes.io/hostname"` | ||||||
| 	StorageResizeMode                      string            `name:"storage_resize_mode" default:"ebs"` | 	StorageResizeMode                      string            `name:"storage_resize_mode" default:"ebs"` | ||||||
|  | 	// ExternalTrafficPolicy for load balancer
 | ||||||
|  | 	ExternalTrafficPolicy string `name:"external_traffic_policy" default:"Cluster"` | ||||||
| 	// deprecated and kept for backward compatibility
 | 	// deprecated and kept for backward compatibility
 | ||||||
| 	EnableLoadBalancer        *bool             `name:"enable_load_balancer"` | 	EnableLoadBalancer        *bool             `name:"enable_load_balancer"` | ||||||
| 	MasterDNSNameFormat       StringTemplate    `name:"master_dns_name_format" default:"{cluster}.{team}.{hostedzone}"` | 	MasterDNSNameFormat       StringTemplate    `name:"master_dns_name_format" default:"{cluster}.{team}.{hostedzone}"` | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue