Skip to content

Commit

Permalink
Promote backend config to GA
Browse files Browse the repository at this point in the history
  • Loading branch information
skmatti committed Jan 28, 2020
1 parent 74c0565 commit ec02214
Show file tree
Hide file tree
Showing 25 changed files with 233 additions and 194 deletions.
21 changes: 18 additions & 3 deletions pkg/annotations/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,17 @@ const (
// on the Service, and is applied by the NEG Controller.
NEGStatusKey = "cloud.google.com/neg-status"

// BackendConfigKey is a stringified JSON with two fields:
// BetaBackendConfigKey is a stringified JSON with two fields:
// - "ports": a map of port names or port numbers to backendConfig names
// - "default": denotes the default backendConfig name for all ports except
// those are explicitly referenced.
// Examples:
// - '{"ports":{"my-https-port":"config-https","my-http-port":"config-http"}}'
// - '{"default":"config-default","ports":{"my-https-port":"config-https"}}'
BackendConfigKey = "beta.cloud.google.com/backend-config"
BetaBackendConfigKey = "beta.cloud.google.com/backend-config"

// BackendConfigKey is GA version of backend config key.
BackendConfigKey = "cloud.google.com/backend-config"

// ProtocolHTTP protocol for a service
ProtocolHTTP AppProtocol = "HTTP"
Expand Down Expand Up @@ -256,7 +259,7 @@ type BackendConfigs struct {

// GetBackendConfigs returns BackendConfigs for the service.
func (svc *Service) GetBackendConfigs() (*BackendConfigs, error) {
val, ok := svc.v[BackendConfigKey]
val, ok := svc.getBackendConfigAnnotation()
if !ok {
return nil, ErrBackendConfigAnnotationMissing
}
Expand All @@ -270,3 +273,15 @@ func (svc *Service) GetBackendConfigs() (*BackendConfigs, error) {
}
return &configs, nil
}

// getBackendConfigAnnotation returns specified backendconfig annotation value.
// Returns false if both beta and ga annotations are not specified.
func (svc *Service) getBackendConfigAnnotation() (string, bool) {
for _, bcKey := range []string{BackendConfigKey, BetaBackendConfigKey} {
val, ok := svc.v[bcKey]
if ok {
return val, ok
}
}
return "", false
}
10 changes: 5 additions & 5 deletions pkg/backendconfig/backendconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

"k8s.io/ingress-gce/pkg/annotations"
apisbackendconfig "k8s.io/ingress-gce/pkg/apis/backendconfig"
backendconfigv1beta1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1beta1"
backendconfigv1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1"
"k8s.io/ingress-gce/pkg/crd"
)

Expand All @@ -44,13 +44,13 @@ func CRDMeta() *crd.CRDMeta {
"backendconfig",
"backendconfigs",
)
meta.AddValidationInfo("k8s.io/ingress-gce/pkg/apis/backendconfig/v1beta1.BackendConfig", backendconfigv1beta1.GetOpenAPIDefinitions)
meta.AddValidationInfo("k8s.io/ingress-gce/pkg/apis/backendconfig/v1.BackendConfig", backendconfigv1.GetOpenAPIDefinitions)
return meta
}

// GetBackendConfigForServicePort returns the corresponding BackendConfig for
// the given ServicePort if specified.
func GetBackendConfigForServicePort(backendConfigLister cache.Store, svc *apiv1.Service, svcPort *apiv1.ServicePort) (*backendconfigv1beta1.BackendConfig, error) {
func GetBackendConfigForServicePort(backendConfigLister cache.Store, svc *apiv1.Service, svcPort *apiv1.ServicePort) (*backendconfigv1.BackendConfig, error) {
backendConfigs, err := annotations.FromService(svc).GetBackendConfigs()
if err != nil {
// If the user did not provide the annotation at all, then we
Expand All @@ -67,7 +67,7 @@ func GetBackendConfigForServicePort(backendConfigLister cache.Store, svc *apiv1.
}

obj, exists, err := backendConfigLister.Get(
&backendconfigv1beta1.BackendConfig{
&backendconfigv1.BackendConfig{
ObjectMeta: metav1.ObjectMeta{
Name: configName,
Namespace: svc.Namespace,
Expand All @@ -80,5 +80,5 @@ func GetBackendConfigForServicePort(backendConfigLister cache.Store, svc *apiv1.
return nil, ErrBackendConfigDoesNotExist
}

return obj.(*backendconfigv1beta1.BackendConfig), nil
return obj.(*backendconfigv1.BackendConfig), nil
}
13 changes: 11 additions & 2 deletions pkg/backendconfig/backendconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
apiv1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/cache"

backendconfigv1beta1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1beta1"
backendconfigv1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1"
)

func TestGetBackendConfigForServicePort(t *testing.T) {
Expand All @@ -33,9 +33,18 @@ func TestGetBackendConfigForServicePort(t *testing.T) {
svc *apiv1.Service
svcPort *apiv1.ServicePort
getFunc func(obj interface{}) (item interface{}, exists bool, err error)
expectedConfig *backendconfigv1beta1.BackendConfig
expectedConfig *backendconfigv1.BackendConfig
expectedErr error
}{
{
desc: "service port name with backend config beta annotation key",
svc: SvcWithTestConfigBeta,
svcPort: &apiv1.ServicePort{Name: "port1"},
getFunc: func(obj interface{}) (interface{}, bool, error) {
return TestBackendConfig, true, nil
},
expectedConfig: TestBackendConfig,
},
{
desc: "service port name with backend config",
svc: SvcWithTestConfig,
Expand Down
19 changes: 17 additions & 2 deletions pkg/backendconfig/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ import (
"strconv"

"k8s.io/ingress-gce/pkg/annotations"
backendconfigv1beta1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1beta1"
backendconfigv1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1"

apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// The below vars are used for sharing unit testing types with multiple packages.
var (
TestBackendConfig = &backendconfigv1beta1.BackendConfig{
TestBackendConfig = &backendconfigv1.BackendConfig{
ObjectMeta: metav1.ObjectMeta{
Name: "config-test",
Namespace: "test",
Expand Down Expand Up @@ -59,6 +59,21 @@ var (
},
}

SvcWithTestConfigBeta = &apiv1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-test-config-beta",
Namespace: "test",
Annotations: map[string]string{
annotations.BetaBackendConfigKey: `{"ports": {"port1": "config-test"}}`,
},
},
Spec: apiv1.ServiceSpec{
Ports: []apiv1.ServicePort{
{Name: "port1"},
},
},
}

SvcWithTestConfigPortNumber = &apiv1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-test-config-port-number",
Expand Down
8 changes: 4 additions & 4 deletions pkg/backendconfig/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
backendconfigv1beta1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1beta1"
backendconfigv1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1"
)

const (
Expand All @@ -35,7 +35,7 @@ var supportedAffinities = map[string]bool{
"GENERATED_COOKIE": true,
}

func Validate(kubeClient kubernetes.Interface, beConfig *backendconfigv1beta1.BackendConfig) error {
func Validate(kubeClient kubernetes.Interface, beConfig *backendconfigv1.BackendConfig) error {
if beConfig == nil {
return nil
}
Expand All @@ -53,7 +53,7 @@ func Validate(kubeClient kubernetes.Interface, beConfig *backendconfigv1beta1.Ba

// TODO(rramkumar): Return errors as constants so that the unit tests can distinguish
// between which error is returned.
func validateIAP(kubeClient kubernetes.Interface, beConfig *backendconfigv1beta1.BackendConfig) error {
func validateIAP(kubeClient kubernetes.Interface, beConfig *backendconfigv1.BackendConfig) error {
// If IAP settings are not found or IAP is not enabled then don't bother continuing.
if beConfig.Spec.Iap == nil || beConfig.Spec.Iap.Enabled == false {
return nil
Expand Down Expand Up @@ -83,7 +83,7 @@ func validateIAP(kubeClient kubernetes.Interface, beConfig *backendconfigv1beta1
return nil
}

func validateSessionAffinity(kubeClient kubernetes.Interface, beConfig *backendconfigv1beta1.BackendConfig) error {
func validateSessionAffinity(kubeClient kubernetes.Interface, beConfig *backendconfigv1.BackendConfig) error {
if beConfig.Spec.SessionAffinity == nil {
return nil
}
Expand Down
46 changes: 23 additions & 23 deletions pkg/backendconfig/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ import (
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
backendconfigv1beta1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1beta1"
backendconfigv1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1"
)

var (
goodTTL int64 = 86400
badTTL int64 = 86400 + 1

defaultBeConfig = &backendconfigv1beta1.BackendConfig{
defaultBeConfig = &backendconfigv1.BackendConfig{
ObjectMeta: meta_v1.ObjectMeta{
Namespace: "default",
},
Spec: backendconfigv1beta1.BackendConfigSpec{
Iap: &backendconfigv1beta1.IAPConfig{
Spec: backendconfigv1.BackendConfigSpec{
Iap: &backendconfigv1.IAPConfig{
Enabled: true,
OAuthClientCredentials: &backendconfigv1beta1.OAuthClientCredentials{
OAuthClientCredentials: &backendconfigv1.OAuthClientCredentials{
SecretName: "foo",
},
},
Expand All @@ -49,7 +49,7 @@ func TestValidateIAP(t *testing.T) {
testCases := []struct {
desc string
init func(kubeClient kubernetes.Interface)
beConfig *backendconfigv1beta1.BackendConfig
beConfig *backendconfigv1.BackendConfig
expectError bool
}{
{
Expand Down Expand Up @@ -120,15 +120,15 @@ func TestValidateIAP(t *testing.T) {
},
{
desc: "iap and cdn enabled at the same time",
beConfig: &backendconfigv1beta1.BackendConfig{
beConfig: &backendconfigv1.BackendConfig{
ObjectMeta: meta_v1.ObjectMeta{
Namespace: "default",
},
Spec: backendconfigv1beta1.BackendConfigSpec{
Iap: &backendconfigv1beta1.IAPConfig{
Spec: backendconfigv1.BackendConfigSpec{
Iap: &backendconfigv1.IAPConfig{
Enabled: true,
},
Cdn: &backendconfigv1beta1.CDNConfig{
Cdn: &backendconfigv1.CDNConfig{
Enabled: true,
},
},
Expand Down Expand Up @@ -166,18 +166,18 @@ func TestValidateIAP(t *testing.T) {
func TestValidateSessionAffinity(t *testing.T) {
testCases := []struct {
desc string
beConfig *backendconfigv1beta1.BackendConfig
beConfig *backendconfigv1.BackendConfig
expectError bool
}{

{
desc: "unsupported affinity type",
beConfig: &backendconfigv1beta1.BackendConfig{
beConfig: &backendconfigv1.BackendConfig{
ObjectMeta: meta_v1.ObjectMeta{
Namespace: "default",
},
Spec: backendconfigv1beta1.BackendConfigSpec{
SessionAffinity: &backendconfigv1beta1.SessionAffinityConfig{
Spec: backendconfigv1.BackendConfigSpec{
SessionAffinity: &backendconfigv1.SessionAffinityConfig{
AffinityType: "WRONG_TYPE",
},
},
Expand All @@ -186,12 +186,12 @@ func TestValidateSessionAffinity(t *testing.T) {
},
{
desc: "supported affinity type",
beConfig: &backendconfigv1beta1.BackendConfig{
beConfig: &backendconfigv1.BackendConfig{
ObjectMeta: meta_v1.ObjectMeta{
Namespace: "default",
},
Spec: backendconfigv1beta1.BackendConfigSpec{
SessionAffinity: &backendconfigv1beta1.SessionAffinityConfig{
Spec: backendconfigv1.BackendConfigSpec{
SessionAffinity: &backendconfigv1.SessionAffinityConfig{
AffinityType: "CLIENT_IP",
},
},
Expand All @@ -200,12 +200,12 @@ func TestValidateSessionAffinity(t *testing.T) {
},
{
desc: "unsupported ttl value",
beConfig: &backendconfigv1beta1.BackendConfig{
beConfig: &backendconfigv1.BackendConfig{
ObjectMeta: meta_v1.ObjectMeta{
Namespace: "default",
},
Spec: backendconfigv1beta1.BackendConfigSpec{
SessionAffinity: &backendconfigv1beta1.SessionAffinityConfig{
Spec: backendconfigv1.BackendConfigSpec{
SessionAffinity: &backendconfigv1.SessionAffinityConfig{
AffinityCookieTtlSec: &badTTL,
},
},
Expand All @@ -214,12 +214,12 @@ func TestValidateSessionAffinity(t *testing.T) {
},
{
desc: "supported ttl value",
beConfig: &backendconfigv1beta1.BackendConfig{
beConfig: &backendconfigv1.BackendConfig{
ObjectMeta: meta_v1.ObjectMeta{
Namespace: "default",
},
Spec: backendconfigv1beta1.BackendConfigSpec{
SessionAffinity: &backendconfigv1beta1.SessionAffinityConfig{
Spec: backendconfigv1.BackendConfigSpec{
SessionAffinity: &backendconfigv1.SessionAffinityConfig{
AffinityCookieTtlSec: &goodTTL,
},
},
Expand Down
30 changes: 15 additions & 15 deletions pkg/backends/features/affinity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package features
import (
"testing"

backendconfigv1beta1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1beta1"
backendconfigv1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1"
"k8s.io/ingress-gce/pkg/composite"
"k8s.io/ingress-gce/pkg/utils"
)
Expand All @@ -36,8 +36,8 @@ func TestEnsureAffinity(t *testing.T) {
{
desc: "affinity settings missing from spec, no update needed",
sp: utils.ServicePort{
BackendConfig: &backendconfigv1beta1.BackendConfig{
Spec: backendconfigv1beta1.BackendConfigSpec{},
BackendConfig: &backendconfigv1.BackendConfig{
Spec: backendconfigv1.BackendConfigSpec{},
},
},
be: &composite.BackendService{
Expand All @@ -49,9 +49,9 @@ func TestEnsureAffinity(t *testing.T) {
{
desc: "sessionaffinity setting differing, update needed",
sp: utils.ServicePort{
BackendConfig: &backendconfigv1beta1.BackendConfig{
Spec: backendconfigv1beta1.BackendConfigSpec{
SessionAffinity: &backendconfigv1beta1.SessionAffinityConfig{
BackendConfig: &backendconfigv1.BackendConfig{
Spec: backendconfigv1.BackendConfigSpec{
SessionAffinity: &backendconfigv1.SessionAffinityConfig{
AffinityType: "CLIENT_IP",
},
},
Expand All @@ -65,9 +65,9 @@ func TestEnsureAffinity(t *testing.T) {
{
desc: "affinity ttl setting differing, update needed",
sp: utils.ServicePort{
BackendConfig: &backendconfigv1beta1.BackendConfig{
Spec: backendconfigv1beta1.BackendConfigSpec{
SessionAffinity: &backendconfigv1beta1.SessionAffinityConfig{
BackendConfig: &backendconfigv1.BackendConfig{
Spec: backendconfigv1.BackendConfigSpec{
SessionAffinity: &backendconfigv1.SessionAffinityConfig{
AffinityCookieTtlSec: &testTTL,
},
},
Expand All @@ -81,9 +81,9 @@ func TestEnsureAffinity(t *testing.T) {
{
desc: "sessionaffinity and ttl settings differing, update needed",
sp: utils.ServicePort{
BackendConfig: &backendconfigv1beta1.BackendConfig{
Spec: backendconfigv1beta1.BackendConfigSpec{
SessionAffinity: &backendconfigv1beta1.SessionAffinityConfig{
BackendConfig: &backendconfigv1.BackendConfig{
Spec: backendconfigv1.BackendConfigSpec{
SessionAffinity: &backendconfigv1.SessionAffinityConfig{
AffinityType: "CLIENT_IP",
AffinityCookieTtlSec: &testTTL,
},
Expand All @@ -99,9 +99,9 @@ func TestEnsureAffinity(t *testing.T) {
{
desc: "affinity settings identical, no updated needed",
sp: utils.ServicePort{
BackendConfig: &backendconfigv1beta1.BackendConfig{
Spec: backendconfigv1beta1.BackendConfigSpec{
SessionAffinity: &backendconfigv1beta1.SessionAffinityConfig{
BackendConfig: &backendconfigv1.BackendConfig{
Spec: backendconfigv1.BackendConfigSpec{
SessionAffinity: &backendconfigv1.SessionAffinityConfig{
AffinityType: "CLIENT_IP",
AffinityCookieTtlSec: &testTTL,
},
Expand Down
Loading

0 comments on commit ec02214

Please sign in to comment.