Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add util functions for backendConfig annotation #247

Merged
merged 1 commit into from
May 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions pkg/annotations/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ const (
// 3. Adding this annotation on ingress.
NetworkEndpointGroupAlphaAnnotation = "alpha.cloud.google.com/load-balancer-neg"

// BackendConfigKey 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 = "alpha.cloud.google.com/backend-config"

// ProtocolHTTP protocol for a service
ProtocolHTTP AppProtocol = "HTTP"
// ProtocolHTTPS protocol for a service
Expand Down Expand Up @@ -93,3 +102,25 @@ func (svc Service) NEGEnabled() bool {
v, ok := svc.v[NetworkEndpointGroupAlphaAnnotation]
return ok && v == "true"
}

type BackendConfigs struct {
Default string `json:"default,omitempty"`
Ports map[string]string `json:"ports,omitempty"`
}

// GetBackendConfigs returns BackendConfigs for the service.
func (svc Service) GetBackendConfigs() (*BackendConfigs, error) {
val, ok := svc.v[BackendConfigKey]
if !ok {
return nil, nil
}

configs := BackendConfigs{}
if err := json.Unmarshal([]byte(val), &configs); err != nil {
return nil, err
}
if configs.Default == "" && len(configs.Ports) == 0 {
return nil, fmt.Errorf("no backendConfigs found in annotation: %v", val)
}
return &configs, nil
}
97 changes: 97 additions & 0 deletions pkg/annotations/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,100 @@ func TestService(t *testing.T) {
}
}
}

func TestBackendConfigs(t *testing.T) {
testcases := []struct {
desc string
svc *v1.Service
expectedConfigs *BackendConfigs
expectErr bool
}{
{
desc: "no backendConfig annotation",
svc: &v1.Service{},
},
{
desc: "single backendConfig",
svc: &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
BackendConfigKey: `{"ports":{"http": "config-http"}}`,
},
},
},
expectedConfigs: &BackendConfigs{
Ports: map[string]string{
"http": "config-http",
},
},
},
{
desc: "multiple backendConfigs",
svc: &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
BackendConfigKey: `{"ports":{"http": "config-http", "https": "config-https"}}`,
},
},
},
expectedConfigs: &BackendConfigs{
Ports: map[string]string{
"http": "config-http",
"https": "config-https",
},
},
}, {
desc: "multiple backendConfigs with default",
svc: &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
BackendConfigKey: `{"default": "config-default", "ports":{"http": "config-http", "https": "config-https"}}`,
},
},
},
expectedConfigs: &BackendConfigs{
Default: "config-default",
Ports: map[string]string{
"http": "config-http",
"https": "config-https",
},
},
},
{
desc: "invalid backendConfig annotation",
svc: &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
BackendConfigKey: `invalid`,
},
},
},
expectErr: true,
},
{
desc: "wrong field name in backendConfig annotation",
svc: &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
BackendConfigKey: `{"portstypo":{"https": "config-https"}}`,
},
},
},
expectErr: true,
},
}

for _, tc := range testcases {
svc := FromService(tc.svc)
configs, err := svc.GetBackendConfigs()
if tc.expectErr {
if err == nil {
t.Errorf("%s: for annotions %+v; svc.GetBackendConfigs() = %v, _; want _, error", tc.desc, svc.v, configs)
}
continue
}
if err != nil || !reflect.DeepEqual(configs, tc.expectedConfigs) {
t.Errorf("%s: for annotions %+v; svc.GetBackendConfigs() = %v, %v; want %v, nil", tc.desc, svc.v, configs, err, tc.expectedConfigs)
}
}
}