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

Patch NEG version into features.go and add more docs for features package #384

Merged
merged 2 commits into from
Jun 29, 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
20 changes: 13 additions & 7 deletions pkg/backends/backends_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestBackendPoolAdd(t *testing.T) {
t.Fatalf("Port %v not added to instance group", sp)
}

hc, err := pool.healthChecker.Get(beName, sp.Version())
hc, err := pool.healthChecker.Get(beName, features.VersionFromServicePort(&sp))
if err != nil {
t.Fatalf("Unexpected err when querying fake healthchecker: %v", err)
}
Expand Down Expand Up @@ -201,7 +201,7 @@ func TestHealthCheckMigration(t *testing.T) {
pool.Ensure([]utils.ServicePort{p}, nil)

// Assert the proper health check was created
hc, _ := pool.healthChecker.Get(beName, p.Version())
hc, _ := pool.healthChecker.Get(beName, features.VersionFromServicePort(&p))
if hc == nil || hc.Protocol() != p.Protocol {
t.Fatalf("Expected %s health check, received %v: ", p.Protocol, hc)
}
Expand Down Expand Up @@ -235,7 +235,7 @@ func TestBackendPoolUpdateHTTPS(t *testing.T) {
}

// Assert the proper health check was created
hc, _ := pool.healthChecker.Get(beName, p.Version())
hc, _ := pool.healthChecker.Get(beName, features.VersionFromServicePort(&p))
if hc == nil || hc.Protocol() != p.Protocol {
t.Fatalf("Expected %s health check, received %v: ", p.Protocol, hc)
}
Expand All @@ -255,7 +255,7 @@ func TestBackendPoolUpdateHTTPS(t *testing.T) {
}

// Assert the proper health check was created
hc, _ = pool.healthChecker.Get(beName, p.Version())
hc, _ = pool.healthChecker.Get(beName, features.VersionFromServicePort(&p))
if hc == nil || hc.Protocol() != p.Protocol {
t.Fatalf("Expected %s health check, received %v: ", p.Protocol, hc)
}
Expand All @@ -280,7 +280,7 @@ func TestBackendPoolUpdateHTTP2(t *testing.T) {
}

// Assert the proper health check was created
hc, _ := pool.healthChecker.Get(beName, p.Version())
hc, _ := pool.healthChecker.Get(beName, features.VersionFromServicePort(&p))
if hc == nil || hc.Protocol() != p.Protocol {
t.Fatalf("Expected %s health check, received %v: ", p.Protocol, hc)
}
Expand Down Expand Up @@ -599,13 +599,19 @@ func TestBackendPoolSyncQuota(t *testing.T) {
quota := len(tc.newPorts)

// Add hooks to simulate quota changes & errors.
(fakeGCE.Compute().(*cloud.MockGCE)).MockBackendServices.InsertHook = func(ctx context.Context, key *meta.Key, be *compute.BackendService, m *cloud.MockBackendServices) (bool, error) {
insertFunc := func(ctx context.Context, key *meta.Key, beName string) (bool, error) {
if bsCreated+1 > quota {
return true, &googleapi.Error{Code: http.StatusForbidden, Body: be.Name}
return true, &googleapi.Error{Code: http.StatusForbidden, Body: beName}
}
bsCreated += 1
return false, nil
}
(fakeGCE.Compute().(*cloud.MockGCE)).MockBackendServices.InsertHook = func(ctx context.Context, key *meta.Key, be *compute.BackendService, m *cloud.MockBackendServices) (bool, error) {
return insertFunc(ctx, key, be.Name)
}
(fakeGCE.Compute().(*cloud.MockGCE)).MockBetaBackendServices.InsertHook = func(ctx context.Context, key *meta.Key, be *computebeta.BackendService, m *cloud.MockBetaBackendServices) (bool, error) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a Beta hook here as well.

return insertFunc(ctx, key, be.Name)
}
(fakeGCE.Compute().(*cloud.MockGCE)).MockBackendServices.DeleteHook = func(ctx context.Context, key *meta.Key, m *cloud.MockBackendServices) (bool, error) {
bsCreated -= 1
return false, nil
Expand Down
6 changes: 5 additions & 1 deletion pkg/backends/features/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ limitations under the License.

// This package contains the implementations of backend service
// features.
// TODO(mrhohn): Document what needs to happen when a feature is added.
// For features that requrie non-GA compute API, please make sure to
// update `versionToFeatures` and `featuresFromServicePort()` in
// features.go (upon both feature addition and promotion). It will make
// sure the controller interacts with compute service using the proper
// API version.
package features
7 changes: 6 additions & 1 deletion pkg/backends/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ const (
FeatureHTTP2 = "HTTP2"
// FeatureSecurityPolicy defines the feature name of SecurityPolicy.
FeatureSecurityPolicy = "SecurityPolicy"
// FeatureNEG defines the feature name of NEG.
FeatureNEG = "NEG"
)

var (
// versionToFeatures stores the mapping from the required API
// version to feature names.
versionToFeatures = map[meta.Version][]string{
meta.VersionAlpha: []string{FeatureHTTP2},
meta.VersionBeta: []string{FeatureSecurityPolicy},
meta.VersionBeta: []string{FeatureSecurityPolicy, FeatureNEG},
}
)

Expand All @@ -57,6 +59,9 @@ func featuresFromServicePort(sp *utils.ServicePort) []string {
if sp.BackendConfig != nil && sp.BackendConfig.Spec.SecurityPolicy != nil {
features = append(features, FeatureSecurityPolicy)
}
if sp.NEGEnabled {
features = append(features, FeatureNEG)
}
// Keep feature names sorted to be consistent.
sort.Strings(features)
return features
Expand Down
15 changes: 15 additions & 0 deletions pkg/backends/features/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ var (
},
},
}

svcPortWithNEG = utils.ServicePort{
ID: fakeSvcPortID,
NEGEnabled: true,
}
)

func TestFeaturesFromServicePort(t *testing.T) {
Expand All @@ -92,6 +97,11 @@ func TestFeaturesFromServicePort(t *testing.T) {
svcPort: svcPortWithSecurityPolicy,
expectedFeatures: []string{"SecurityPolicy"},
},
{
desc: "NEG",
svcPort: svcPortWithNEG,
expectedFeatures: []string{"NEG"},
},
{
desc: "HTTP2 + SecurityPolicy",
svcPort: svcPortWithHTTP2SecurityPolicy,
Expand Down Expand Up @@ -128,6 +138,11 @@ func TestVersionFromFeatures(t *testing.T) {
features: []string{FeatureSecurityPolicy},
expectedVersion: meta.VersionBeta,
},
{
desc: "NEG",
features: []string{FeatureNEG},
expectedVersion: meta.VersionBeta,
},
{
desc: "HTTP2 + SecurityPolicy",
features: []string{FeatureHTTP2, FeatureSecurityPolicy},
Expand Down
15 changes: 0 additions & 15 deletions pkg/utils/serviceport.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
extensions "k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/kubernetes/pkg/cloudprovider/providers/gce/cloud/meta"

"k8s.io/ingress-gce/pkg/annotations"
backendconfigv1beta1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1beta1"
Expand Down Expand Up @@ -54,20 +53,6 @@ func (sp ServicePort) GetDescription() Description {
}
}

// Version returns the support version of the ServicePort configuration
// Alpha includes HTTP2
// Beta includes NEG
// The rest are v1
func (sp ServicePort) Version() meta.Version {
if sp.Protocol == annotations.ProtocolHTTP2 {
return meta.VersionAlpha
}
if sp.NEGEnabled {
return meta.VersionBeta
}
return meta.VersionGA
}

// BackendName returns the name of the backend which would be used for this ServicePort.
func (sp ServicePort) BackendName(namer *Namer) string {
if !sp.NEGEnabled {
Expand Down