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

Refactor addBackendPolicyIfConfigured to allow future features #192

Merged
merged 2 commits into from
Sep 13, 2024
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
48 changes: 48 additions & 0 deletions pkg/i2gw/providers/gce/extensions/input_extensions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package extensions

import (
"fmt"

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/intermediate"
backendconfigv1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1"
)

func ValidateBeConfig(beConfig *backendconfigv1.BackendConfig) error {
if beConfig.Spec.SessionAffinity != nil {
if err := validateSessionAffinity(beConfig); err != nil {
return err
}
}

return nil
}

func validateSessionAffinity(beConfig *backendconfigv1.BackendConfig) error {
if beConfig.Spec.SessionAffinity.AffinityCookieTtlSec != nil && beConfig.Spec.SessionAffinity.AffinityType != "GENERATED_COOKIE" {
return fmt.Errorf("BackendConfig has affinityCookieTtlSec set, but affinityType is not GENERATED_COOKIE")
}
return nil
}

func BuildIRSessionAffinityConfig(beConfig *backendconfigv1.BackendConfig) *intermediate.SessionAffinityConfig {
return &intermediate.SessionAffinityConfig{
AffinityType: beConfig.Spec.SessionAffinity.AffinityType,
CookieTTLSec: beConfig.Spec.SessionAffinity.AffinityCookieTtlSec,
}
}
33 changes: 33 additions & 0 deletions pkg/i2gw/providers/gce/extensions/output_extensions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package extensions

import (
gkegatewayv1 "github.com/GoogleCloudPlatform/gke-gateway-api/apis/networking/v1"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/intermediate"
)

func BuildBackendPolicySessionAffinityConfig(serviceIR intermediate.ProviderSpecificServiceIR) *gkegatewayv1.SessionAffinityConfig {
affinityType := serviceIR.Gce.SessionAffinity.AffinityType
saConfig := gkegatewayv1.SessionAffinityConfig{
Type: &affinityType,
}
if affinityType == "GENERATED_COOKIE" {
saConfig.CookieTTLSec = serviceIR.Gce.SessionAffinity.CookieTTLSec

Choose a reason for hiding this comment

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

Nit: how do we usually handle invalid config? Could we add a log here if the backendConfig had affinityCookieTtlSec set without affinityType of GENERATED_COOKIE?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good! Created a function to check backend config before processing translation.

}
return &saConfig
}
27 changes: 12 additions & 15 deletions pkg/i2gw/providers/gce/gce_extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/intermediate"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/notifications"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/gce/extensions"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -44,6 +45,10 @@ func buildGceServiceIR(ctx context.Context, storage *storage, ir *intermediate.I
if beConfig == nil {
continue
}
if err := extensions.ValidateBeConfig(beConfig); err != nil {
notify(notifications.ErrorNotification, err.Error(), beConfig)
continue
}
gceServiceIR := beConfigToGceServiceIR(beConfig)
services := beConfigToSvcs[beConfigKey]
for _, svcKey := range services {
Expand Down Expand Up @@ -141,11 +146,7 @@ func parseBackendConfigName(ctx context.Context, val string) (string, bool) {
func beConfigToGceServiceIR(beConfig *backendconfigv1.BackendConfig) intermediate.GceServiceIR {
var gceServiceIR intermediate.GceServiceIR
if beConfig.Spec.SessionAffinity != nil {
saConfig := intermediate.SessionAffinityConfig{
AffinityType: beConfig.Spec.SessionAffinity.AffinityType,
CookieTTLSec: beConfig.Spec.SessionAffinity.AffinityCookieTtlSec,
}
gceServiceIR.SessionAffinity = &saConfig
gceServiceIR.SessionAffinity = extensions.BuildIRSessionAffinityConfig(beConfig)
}

return gceServiceIR
Expand All @@ -167,32 +168,28 @@ func buildGceServiceExtensions(ir intermediate.IR, gatewayResources *i2gw.Gatewa
}

func addBackendPolicyIfConfigured(serviceNamespacedName types.NamespacedName, serviceIR intermediate.ProviderSpecificServiceIR) *gkegatewayv1.GCPBackendPolicy {
if serviceIR.Gce == nil || serviceIR.Gce.SessionAffinity == nil {
if serviceIR.Gce == nil {
return nil
}
affinityType := serviceIR.Gce.SessionAffinity.AffinityType
backendPolicy := gkegatewayv1.GCPBackendPolicy{
ObjectMeta: metav1.ObjectMeta{
Namespace: serviceNamespacedName.Namespace,
Name: serviceNamespacedName.Name,
},
Spec: gkegatewayv1.GCPBackendPolicySpec{
Default: &gkegatewayv1.GCPBackendPolicyConfig{
SessionAffinity: &gkegatewayv1.SessionAffinityConfig{
Type: &affinityType,
},
},
Default: &gkegatewayv1.GCPBackendPolicyConfig{},
TargetRef: gatewayv1alpha2.NamespacedPolicyTargetReference{
Group: "",
Kind: "Service",
Name: gatewayv1.ObjectName(serviceNamespacedName.Name),
},
},
}
if affinityType == "GENERATED_COOKIE" {
backendPolicy.Spec.Default.SessionAffinity.CookieTTLSec = serviceIR.Gce.SessionAffinity.CookieTTLSec
backendPolicy.SetGroupVersionKind(GCPBackendPolicyGVK)

if serviceIR.Gce.SessionAffinity != nil {
backendPolicy.Spec.Default.SessionAffinity = extensions.BuildBackendPolicySessionAffinityConfig(serviceIR)
}

backendPolicy.SetGroupVersionKind(GCPBackendPolicyGVK)
return &backendPolicy
}
Loading