Skip to content

Commit

Permalink
support customization of numOfProbe and probeInterval when externaltr…
Browse files Browse the repository at this point in the history
…afficpolicy is local
  • Loading branch information
MartinForReal committed Jul 3, 2023
1 parent b83f26b commit e1b4fa5
Show file tree
Hide file tree
Showing 8 changed files with 826 additions and 649 deletions.
41 changes: 0 additions & 41 deletions pkg/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,47 +442,6 @@ const RateLimited = "rate limited"
// CreatedByTag tag key for CSI drivers
const CreatedByTag = "k8s-azure-created-by"

// port specific
const (
PortAnnotationPrefixPattern = "service.beta.kubernetes.io/port_%d_%s"
PortAnnotationNoLBRule PortParams = "no_lb_rule"
// NoHealthProbeRule determines whether the port is only used for health probe. no lb probe rule will be created.
PortAnnotationNoHealthProbeRule PortParams = "no_probe_rule"
)

type PortParams string

// health probe
const (
HealthProbeAnnotationPrefixPattern = "health-probe_%s"

// HealthProbeParamsProtocol determines the protocol for the health probe params.
// It always takes priority over spec.appProtocol or any other specified protocol
HealthProbeParamsProtocol HealthProbeParams = "protocol"

// HealthProbeParamsPort determines the probe port for the health probe params.
// It always takes priority over the NodePort of the spec.ports in a Service
HealthProbeParamsPort HealthProbeParams = "port"

// HealthProbeParamsProbeInterval determines the probe interval of the load balancer health probe.
// The minimum probe interval is 5 seconds and the default value is 5. The total duration of all intervals cannot exceed 120 seconds.
HealthProbeParamsProbeInterval HealthProbeParams = "interval"
HealthProbeDefaultProbeInterval int32 = 5

// HealthProbeParamsNumOfProbe determines the minimum number of unhealthy responses which load balancer cannot tolerate.
// The minimum number of probe is 2. The total duration of all intervals cannot exceed 120 seconds.
HealthProbeParamsNumOfProbe HealthProbeParams = "num-of-probe"
HealthProbeDefaultNumOfProbe int32 = 2

// HealthProbeParamsRequestPath determines the request path of the load balancer health probe.
// This is only useful for the HTTP and HTTPS, and would be ignored when using TCP. If not set,
// `/healthz` would be configured by default.
HealthProbeParamsRequestPath HealthProbeParams = "request-path"
HealthProbeDefaultRequestPath string = "/"
)

type HealthProbeParams string

// private link service
const (
// ServiceAnnotationPLSCreation determines whether a PLS needs to be created.
Expand Down
42 changes: 6 additions & 36 deletions pkg/consts/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,27 @@ import (

// IsK8sServiceHasHAModeEnabled return if HA Mode is enabled in kubernetes service annotations
func IsK8sServiceHasHAModeEnabled(service *v1.Service) bool {
return expectAttributeInSvcAnnotationBeEqualTo(service.Annotations, ServiceAnnotationLoadBalancerEnableHighAvailabilityPorts, TrueAnnotationValue)
return ExpectAttributeInSvcAnnotationBeEqualTo(service.Annotations, ServiceAnnotationLoadBalancerEnableHighAvailabilityPorts, TrueAnnotationValue)
}

// IsK8sServiceUsingInternalLoadBalancer return if service is using an internal load balancer.
func IsK8sServiceUsingInternalLoadBalancer(service *v1.Service) bool {
return expectAttributeInSvcAnnotationBeEqualTo(service.Annotations, ServiceAnnotationLoadBalancerInternal, TrueAnnotationValue)
return ExpectAttributeInSvcAnnotationBeEqualTo(service.Annotations, ServiceAnnotationLoadBalancerInternal, TrueAnnotationValue)
}

// IsK8sServiceDisableLoadBalancerFloatingIP return if floating IP in load balancer is disabled in kubernetes service annotations
func IsK8sServiceDisableLoadBalancerFloatingIP(service *v1.Service) bool {
return expectAttributeInSvcAnnotationBeEqualTo(service.Annotations, ServiceAnnotationDisableLoadBalancerFloatingIP, TrueAnnotationValue)
}

// GetHealthProbeConfigOfPortFromK8sSvcAnnotation get health probe configuration for port
func GetHealthProbeConfigOfPortFromK8sSvcAnnotation(annotations map[string]string, port int32, key HealthProbeParams, validators ...BusinessValidator) (*string, error) {
return GetAttributeValueInSvcAnnotation(annotations, BuildHealthProbeAnnotationKeyForPort(port, key), validators...)
}

// IsHealthProbeRuleOnK8sServicePortDisabled return if port is for health probe only
func IsHealthProbeRuleOnK8sServicePortDisabled(annotations map[string]string, port int32) (bool, error) {
return expectAttributeInSvcAnnotationBeEqualTo(annotations, BuildAnnotationKeyForPort(port, PortAnnotationNoHealthProbeRule), TrueAnnotationValue), nil
}

// IsHealthProbeRuleOnK8sServicePortDisabled return if port is for health probe only
func IsLBRuleOnK8sServicePortDisabled(annotations map[string]string, port int32) (bool, error) {
return expectAttributeInSvcAnnotationBeEqualTo(annotations, BuildAnnotationKeyForPort(port, PortAnnotationNoLBRule), TrueAnnotationValue), nil
return ExpectAttributeInSvcAnnotationBeEqualTo(service.Annotations, ServiceAnnotationDisableLoadBalancerFloatingIP, TrueAnnotationValue)
}

// IsPLSProxyProtocolEnabled return true if ServiceAnnotationPLSProxyProtocol is true
func IsPLSProxyProtocolEnabled(annotations map[string]string) bool {
return expectAttributeInSvcAnnotationBeEqualTo(annotations, ServiceAnnotationPLSProxyProtocol, TrueAnnotationValue)
return ExpectAttributeInSvcAnnotationBeEqualTo(annotations, ServiceAnnotationPLSProxyProtocol, TrueAnnotationValue)
}

// IsPLSEnabled return true if ServiceAnnotationPLSCreation is true
func IsPLSEnabled(annotations map[string]string) bool {
return expectAttributeInSvcAnnotationBeEqualTo(annotations, ServiceAnnotationPLSCreation, TrueAnnotationValue)
return ExpectAttributeInSvcAnnotationBeEqualTo(annotations, ServiceAnnotationPLSCreation, TrueAnnotationValue)
}

// Getint32ValueFromK8sSvcAnnotation get health probe configuration for port
Expand All @@ -74,21 +59,6 @@ func Getint32ValueFromK8sSvcAnnotation(annotations map[string]string, key string
return nil, err
}

// BuildHealthProbeAnnotationKeyForPort get health probe configuration key for port
func BuildAnnotationKeyForPort(port int32, key PortParams) string {
return fmt.Sprintf(PortAnnotationPrefixPattern, port, string(key))
}

// BuildHealthProbeAnnotationKeyForPort get health probe configuration key for port
func BuildHealthProbeAnnotationKeyForPort(port int32, key HealthProbeParams) string {
return BuildAnnotationKeyForPort(port, PortParams(fmt.Sprintf(HealthProbeAnnotationPrefixPattern, key)))
}

// GetInt32HealthProbeConfigOfPortFromK8sSvcAnnotation get health probe configuration for port
func GetInt32HealthProbeConfigOfPortFromK8sSvcAnnotation(annotations map[string]string, port int32, key HealthProbeParams, validators ...Int32BusinessValidator) (*int32, error) {
return Getint32ValueFromK8sSvcAnnotation(annotations, BuildHealthProbeAnnotationKeyForPort(port, key), validators...)
}

// Int32BusinessValidator is validator function which is invoked after values are parsed in order to make sure input value meets the businees need.
type Int32BusinessValidator func(*int32) error

Expand Down Expand Up @@ -131,7 +101,7 @@ func GetAttributeValueInSvcAnnotation(annotations map[string]string, key string,
}

// expectAttributeInSvcAnnotation get key in svc annotation and compare with target value
func expectAttributeInSvcAnnotationBeEqualTo(annotations map[string]string, key string, value string) bool {
func ExpectAttributeInSvcAnnotationBeEqualTo(annotations map[string]string, key string, value string) bool {
if l, err := GetAttributeValueInSvcAnnotation(annotations, key); err == nil && l != nil {
return strings.EqualFold(*l, value)
}
Expand Down
113 changes: 1 addition & 112 deletions pkg/consts/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,33 +145,6 @@ func TestIsK8sServiceUsingInternalLoadBalancer(t *testing.T) {
}
}

func TestGetHealthProbeConfigOfPortFromK8sSvcAnnotation(t *testing.T) {
type args struct {
annotations map[string]string
port int32
key HealthProbeParams
validators []BusinessValidator
}
tests := []struct {
name string
args args
want *string
wantErr bool
}{}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetHealthProbeConfigOfPortFromK8sSvcAnnotation(tt.args.annotations, tt.args.port, tt.args.key, tt.args.validators...)
if (err != nil) != tt.wantErr {
t.Errorf("GetHealthProbeConfigOfPortFromK8sSvcAnnotation() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("GetHealthProbeConfigOfPortFromK8sSvcAnnotation() = %v, want %v", got, tt.want)
}
})
}
}

func Test_extractInt32FromString(t *testing.T) {
type args struct {
val string
Expand Down Expand Up @@ -322,93 +295,9 @@ func Test_expectAttributeInSvcAnnotationBeEqualTo(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := expectAttributeInSvcAnnotationBeEqualTo(tt.args.annotations, tt.args.key, tt.args.value); got != tt.want {
if got := ExpectAttributeInSvcAnnotationBeEqualTo(tt.args.annotations, tt.args.key, tt.args.value); got != tt.want {
t.Errorf("expectAttributeInSvcAnnotationBeEqualTo() = %v, want %v", got, tt.want)
}
})
}
}

func TestGetInt32HealthProbeConfigOfPortFromK8sSvcAnnotation(t *testing.T) {
type args struct {
annotations map[string]string
port int32
key HealthProbeParams
validators []Int32BusinessValidator
}
tests := []struct {
name string
args args
want *int32
wantErr bool
}{
{
name: "get numeric value from health probe related annotation",
args: args{
annotations: map[string]string{BuildHealthProbeAnnotationKeyForPort(80, HealthProbeParamsNumOfProbe): "2"},
port: 80,
key: HealthProbeParamsNumOfProbe,
},
want: pointer.Int32(2),
wantErr: false,
},
{
name: "health probe related annotation is not found",
args: args{
annotations: map[string]string{},
port: 80,
key: HealthProbeParamsNumOfProbe,
},
want: nil,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetInt32HealthProbeConfigOfPortFromK8sSvcAnnotation(tt.args.annotations, tt.args.port, tt.args.key, tt.args.validators...)
if (err != nil) != tt.wantErr {
t.Errorf("GetInt32HealthProbeConfigOfPortFromK8sSvcAnnotation() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetInt32HealthProbeConfigOfPortFromK8sSvcAnnotation() = %v, want %v", got, tt.want)
}
})
}
}

func TestBuildAnnotationKeyForPort(t *testing.T) {
type args struct {
port int32
key PortParams
}
tests := []struct {
name string
args args
want string
}{
{
name: "no lb rule",
args: args{
port: 80,
key: PortAnnotationNoLBRule,
},
want: "service.beta.kubernetes.io/port_80_no_lb_rule",
},
{
name: "no lb rule",
args: args{
port: 80,
key: PortAnnotationNoHealthProbeRule,
},
want: "service.beta.kubernetes.io/port_80_no_probe_rule",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := BuildAnnotationKeyForPort(tt.args.port, tt.args.key); got != tt.want {
t.Errorf("BuildAnnotationKeyForPort() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit e1b4fa5

Please sign in to comment.