add global toggle and enum field for schema

This commit is contained in:
Felix Kunde
2020-05-26 12:37:10 +02:00
parent 2484af436a
commit ae207da172
20 changed files with 132 additions and 59 deletions
+25 -6
View File
@@ -268,9 +268,6 @@ var PostgresCRDResourceValidation = apiextv1beta1.CustomResourceValidation{
"enableMasterLoadBalancer": {
Type: "boolean",
},
"enablePublicLoadBalancer": {
Type: "boolean",
},
"enableReplicaLoadBalancer": {
Type: "boolean",
},
@@ -300,6 +297,17 @@ var PostgresCRDResourceValidation = apiextv1beta1.CustomResourceValidation{
},
},
},
"loadBalancerSchema": {
Type: "string",
Enum: []apiextv1beta1.JSON{
{
Raw: []byte(`"internal"`),
},
{
Raw: []byte(`"public"`),
},
},
},
"logicalBackupSchedule": {
Type: "string",
Pattern: "^(\\d+|\\*)(/\\d+)?(\\s+(\\d+|\\*)(/\\d+)?){4}$",
@@ -1051,6 +1059,9 @@ var OperatorConfigCRDResourceValidation = apiextv1beta1.CustomResourceValidation
"load_balancer": {
Type: "object",
Properties: map[string]apiextv1beta1.JSONSchemaProps{
"allow_public_load_balancers": {
Type: "boolean",
},
"custom_service_annotations": {
Type: "object",
AdditionalProperties: &apiextv1beta1.JSONSchemaPropsOrBool{
@@ -1062,10 +1073,18 @@ var OperatorConfigCRDResourceValidation = apiextv1beta1.CustomResourceValidation
"db_hosted_zone": {
Type: "string",
},
"enable_master_load_balancer": {
Type: "boolean",
"default_load_balancer_schema": {
Type: "string",
Enum: []apiextv1beta1.JSON{
{
Raw: []byte(`"internal"`),
},
{
Raw: []byte(`"public"`),
},
},
},
"enable_public_load_balancer": {
"enable_master_load_balancer": {
Type: "boolean",
},
"enable_replica_load_balancer": {
@@ -100,8 +100,9 @@ type OperatorTimeouts struct {
type LoadBalancerConfiguration struct {
DbHostedZone string `json:"db_hosted_zone,omitempty"`
EnableMasterLoadBalancer bool `json:"enable_master_load_balancer,omitempty"`
EnablePublicLoadBalancer bool `json:"enable_public_load_balancer,omitempty"`
EnableReplicaLoadBalancer bool `json:"enable_replica_load_balancer,omitempty"`
AllowPublicLoadBalancers *bool `json:"allow_public_load_balancers,omitempty"`
DefaultLoadBalancerSchema string `json:"default_load_balancer_schema,omitempty"`
CustomServiceAnnotations map[string]string `json:"custom_service_annotations,omitempty"`
MasterDNSNameFormat config.StringTemplate `json:"master_dns_name_format,omitempty"`
ReplicaDNSNameFormat config.StringTemplate `json:"replica_dns_name_format,omitempty"`
+3 -3
View File
@@ -39,9 +39,9 @@ type PostgresSpec struct {
// vars that enable load balancers are pointers because it is important to know if any of them is omitted from the Postgres manifest
// in that case the var evaluates to nil and the value is taken from the operator config
EnableMasterLoadBalancer *bool `json:"enableMasterLoadBalancer,omitempty"`
EnablePublicLoadBalancer *bool `json:"enablePublicLoadBalancer,omitempty"`
EnableReplicaLoadBalancer *bool `json:"enableReplicaLoadBalancer,omitempty"`
EnableMasterLoadBalancer *bool `json:"enableMasterLoadBalancer,omitempty"`
EnableReplicaLoadBalancer *bool `json:"enableReplicaLoadBalancer,omitempty"`
LoadBalancerSchema string `json:"loadBalancerSchema,omitempty"`
// deprecated load balancer settings maintained for backward compatibility
// see "Load balancers" operator docs
@@ -223,6 +223,11 @@ func (in *KubernetesMetaConfiguration) DeepCopy() *KubernetesMetaConfiguration {
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *LoadBalancerConfiguration) DeepCopyInto(out *LoadBalancerConfiguration) {
*out = *in
if in.AllowPublicLoadBalancers != nil {
in, out := &in.AllowPublicLoadBalancers, &out.AllowPublicLoadBalancers
*out = new(bool)
**out = **in
}
if in.CustomServiceAnnotations != nil {
in, out := &in.CustomServiceAnnotations, &out.CustomServiceAnnotations
*out = make(map[string]string, len(*in))
@@ -525,11 +530,6 @@ func (in *PostgresSpec) DeepCopyInto(out *PostgresSpec) {
*out = new(bool)
**out = **in
}
if in.EnablePublicLoadBalancer != nil {
in, out := &in.EnablePublicLoadBalancer, &out.EnablePublicLoadBalancer
*out = new(bool)
**out = **in
}
if in.EnableReplicaLoadBalancer != nil {
in, out := &in.EnableReplicaLoadBalancer, &out.EnableReplicaLoadBalancer
*out = new(bool)
+33 -12
View File
@@ -403,8 +403,9 @@ func TestServiceAnnotations(t *testing.T) {
enableMasterLoadBalancerOC bool
enableReplicaLoadBalancerSpec *bool
enableReplicaLoadBalancerOC bool
enablePublicLoadBalancerSpec *bool
enablePublicLoadBalancerOC bool
allowPublicLoadBalancersOC *bool
loadBalancerSchemaSpec string
defaultLoadBalancerSchemaOC string
operatorAnnotations map[string]string
clusterAnnotations map[string]string
expect map[string]string
@@ -681,11 +682,12 @@ func TestServiceAnnotations(t *testing.T) {
},
//PUBLIC
{
about: "Public ELB disabled in spec and OperatorConfig",
about: "Internal ELB set in spec and OperatorConfig, but public LB allowed",
role: "master",
enableMasterLoadBalancerSpec: &enabled,
enablePublicLoadBalancerSpec: &disabled,
enablePublicLoadBalancerOC: false,
allowPublicLoadBalancersOC: &enabled,
loadBalancerSchemaSpec: "internal",
defaultLoadBalancerSchemaOC: "internal",
operatorAnnotations: make(map[string]string),
clusterAnnotations: make(map[string]string),
expect: map[string]string{
@@ -695,11 +697,12 @@ func TestServiceAnnotations(t *testing.T) {
},
},
{
about: "Enable public ELB with EnablePublicLoadBalancer in spec but disabled in OperatorConfig",
about: "Internal ELB set in OperatorConfig overwritten by spec. Public LB allowed",
role: "master",
enableMasterLoadBalancerSpec: &enabled,
enablePublicLoadBalancerSpec: &enabled,
enablePublicLoadBalancerOC: false,
allowPublicLoadBalancersOC: &enabled,
loadBalancerSchemaSpec: "public",
defaultLoadBalancerSchemaOC: "internal",
operatorAnnotations: make(map[string]string),
clusterAnnotations: make(map[string]string),
expect: map[string]string{
@@ -708,15 +711,32 @@ func TestServiceAnnotations(t *testing.T) {
},
},
{
about: "Enable public ELB with enable_public_load_balancer in OperatorConfig",
about: "Public ELB set in OperatorConfig overwritten by spec. Public LB allowed",
role: "master",
enableMasterLoadBalancerSpec: &enabled,
enablePublicLoadBalancerOC: true,
allowPublicLoadBalancersOC: &enabled,
loadBalancerSchemaSpec: "internal",
defaultLoadBalancerSchemaOC: "public",
operatorAnnotations: make(map[string]string),
clusterAnnotations: make(map[string]string),
expect: map[string]string{
"external-dns.alpha.kubernetes.io/hostname": "test.acid.db.example.com",
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "3600",
"service.beta.kubernetes.io/aws-load-balancer-internal": "true",
},
},
{
about: "Public ELB set in spec, but not globally not allowed",
role: "master",
enableMasterLoadBalancerSpec: &enabled,
allowPublicLoadBalancersOC: &disabled,
loadBalancerSchemaSpec: "public",
operatorAnnotations: make(map[string]string),
clusterAnnotations: make(map[string]string),
expect: map[string]string{
"external-dns.alpha.kubernetes.io/hostname": "test.acid.db.example.com",
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout": "3600",
"service.beta.kubernetes.io/aws-load-balancer-internal": "true",
},
},
// COMMON
@@ -743,7 +763,8 @@ func TestServiceAnnotations(t *testing.T) {
cl.OpConfig.CustomServiceAnnotations = tt.operatorAnnotations
cl.OpConfig.EnableMasterLoadBalancer = tt.enableMasterLoadBalancerOC
cl.OpConfig.EnableReplicaLoadBalancer = tt.enableReplicaLoadBalancerOC
cl.OpConfig.EnablePublicLoadBalancer = tt.enablePublicLoadBalancerOC
cl.OpConfig.AllowPublicLoadBalancers = tt.allowPublicLoadBalancersOC
cl.OpConfig.DefaultLoadBalancerSchema = tt.defaultLoadBalancerSchemaOC
cl.OpConfig.MasterDNSNameFormat = "{cluster}.{team}.{hostedzone}"
cl.OpConfig.ReplicaDNSNameFormat = "{cluster}-repl.{team}.{hostedzone}"
cl.OpConfig.DbHostedZone = "db.example.com"
@@ -753,7 +774,7 @@ func TestServiceAnnotations(t *testing.T) {
cl.Postgresql.Spec.ServiceAnnotations = tt.clusterAnnotations
cl.Postgresql.Spec.EnableMasterLoadBalancer = tt.enableMasterLoadBalancerSpec
cl.Postgresql.Spec.EnableReplicaLoadBalancer = tt.enableReplicaLoadBalancerSpec
cl.Postgresql.Spec.EnablePublicLoadBalancer = tt.enablePublicLoadBalancerSpec
cl.Postgresql.Spec.LoadBalancerSchema = tt.loadBalancerSchemaSpec
got := cl.generateServiceAnnotations(tt.role, &cl.Postgresql.Spec)
if len(tt.expect) != len(got) {
+8 -3
View File
@@ -1527,13 +1527,18 @@ func (c *Cluster) shouldCreateLoadBalancerForService(role PostgresRole, spec *ac
func (c *Cluster) shouldUseInternalLoadBalancerForService(spec *acidv1.PostgresSpec) bool {
// check if public load balancers are even allowed
if c.OpConfig.AllowPublicLoadBalancers == nil || !(*c.OpConfig.AllowPublicLoadBalancers) {
return true
}
// if the value is explicitly set in a Postgresql manifest, follow this setting
if spec.EnablePublicLoadBalancer != nil {
return !(*spec.EnablePublicLoadBalancer)
if spec.LoadBalancerSchema != "" {
return spec.LoadBalancerSchema == "internal"
}
// otherwise, follow the operator configuration
return !c.OpConfig.EnablePublicLoadBalancer
return c.OpConfig.DefaultLoadBalancerSchema == "internal"
}
func (c *Cluster) generateService(role PostgresRole, spec *acidv1.PostgresSpec) *v1.Service {
+2 -1
View File
@@ -101,8 +101,9 @@ func (c *Controller) importConfigurationFromCRD(fromCRD *acidv1.OperatorConfigur
// load balancer config
result.DbHostedZone = fromCRD.LoadBalancer.DbHostedZone
result.EnableMasterLoadBalancer = fromCRD.LoadBalancer.EnableMasterLoadBalancer
result.EnablePublicLoadBalancer = fromCRD.LoadBalancer.EnablePublicLoadBalancer
result.EnableReplicaLoadBalancer = fromCRD.LoadBalancer.EnableReplicaLoadBalancer
result.AllowPublicLoadBalancers = util.CoalesceBool(fromCRD.LoadBalancer.AllowPublicLoadBalancers, util.True())
result.DefaultLoadBalancerSchema = util.Coalesce(fromCRD.LoadBalancer.DefaultLoadBalancerSchema, "internal")
result.CustomServiceAnnotations = fromCRD.LoadBalancer.CustomServiceAnnotations
result.MasterDNSNameFormat = fromCRD.LoadBalancer.MasterDNSNameFormat
result.ReplicaDNSNameFormat = fromCRD.LoadBalancer.ReplicaDNSNameFormat
+2 -1
View File
@@ -135,8 +135,9 @@ type Config struct {
TeamAdminRole string `name:"team_admin_role" default:"admin"`
EnableAdminRoleForUsers bool `name:"enable_admin_role_for_users" default:"true"`
EnableMasterLoadBalancer bool `name:"enable_master_load_balancer" default:"false"`
EnablePublicLoadBalancer bool `name:"enable_public_load_balancer" default:"false"`
EnableReplicaLoadBalancer bool `name:"enable_replica_load_balancer" default:"false"`
AllowPublicLoadBalancers *bool `name:"allow_public_load_balancers" default:"true"`
DefaultLoadBalancerSchema string `name:"default_load_balancer_schema" default:"true"`
CustomServiceAnnotations map[string]string `name:"custom_service_annotations"`
CustomPodAnnotations map[string]string `name:"custom_pod_annotations"`
EnablePodAntiAffinity bool `name:"enable_pod_antiaffinity" default:"false"`