Skip to content

Commit

Permalink
add external traffic policy for gw2 gw params (#10420)
Browse files Browse the repository at this point in the history
Signed-off-by: Daneyon Hansen <[email protected]>
Co-authored-by: changelog-bot <changelog-bot>
Co-authored-by: Daneyon Hansen <[email protected]>
Co-authored-by: Yuval Kohavi <[email protected]>
Co-authored-by: Sam Heilbron <[email protected]>
Co-authored-by: Nathan Fudenberg <[email protected]>
Co-authored-by: Ryan Old <[email protected]>
Co-authored-by: soloio-bulldozer[bot] <48420018+soloio-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: Bernie Birnbaum <[email protected]>
Co-authored-by: Rachael Graham <[email protected]>
Co-authored-by: Ravinder Punia <[email protected]>
Co-authored-by: sheidkamp <[email protected]>
Co-authored-by: Jenny Shu <[email protected]>
Co-authored-by: Nadine Spies <[email protected]>
Co-authored-by: David Jumani <[email protected]>
Co-authored-by: Eitan Yarmush <[email protected]>
Co-authored-by: Nina Polshakova <[email protected]>
Co-authored-by: Steven Landow <[email protected]>
Co-authored-by: Ariana W. <[email protected]>
  • Loading branch information
18 people authored Dec 31, 2024
1 parent 599a44e commit ecaab37
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 14 deletions.
6 changes: 6 additions & 0 deletions changelog/v1.19.0-beta3/external-traffic-policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
changelog:
- type: NEW_FEATURE
issueLink: https://github.com/k8sgateway/k8sgateway/issues/9879
resolvesIssue: true
description: >-
Add ability to configure proxy service External Traffic Policy via Gateway Params
Original file line number Diff line number Diff line change
Expand Up @@ -7735,6 +7735,13 @@ Resource Types:
<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>externalTrafficPolicy</b></td>
<td>string</td>
<td>
<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>extraAnnotations</b></td>
<td>map[string]string</td>
Expand Down
5 changes: 3 additions & 2 deletions docs/content/reference/values.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
|kubeGateway.gatewayParameters.glooGateway.service.type|string|LoadBalancer|K8s service type. If set to null, a default of LoadBalancer will be imposed.|
|kubeGateway.gatewayParameters.glooGateway.service.extraLabels.NAME|string||Extra labels to add to the service.|
|kubeGateway.gatewayParameters.glooGateway.service.extraAnnotations.NAME|string||Extra annotations to add to the service.|
|kubeGateway.gatewayParameters.glooGateway.service.externalTrafficPolicy|string||Set the external traffic policy on the provisioned service.|
|kubeGateway.gatewayParameters.glooGateway.serviceAccount.extraLabels.NAME|string||Extra labels to add to the service account.|
|kubeGateway.gatewayParameters.glooGateway.serviceAccount.extraAnnotations.NAME|string||Extra annotations to add to the service account.|
|kubeGateway.gatewayParameters.glooGateway.sdsContainer.image.tag|string|<release_version, ex: 1.2.3>|The image tag for the container.|
Expand Down Expand Up @@ -1002,7 +1003,7 @@
|gatewayProxies.NAME.service.httpsNodePort|int||HTTPS nodeport for the gateway service if using type NodePort|
|gatewayProxies.NAME.service.clusterIP|string||static clusterIP (or `None`) when `gatewayProxies[].gatewayProxy.service.type` is `ClusterIP`|
|gatewayProxies.NAME.service.extraAnnotations.NAME|string|||
|gatewayProxies.NAME.service.externalTrafficPolicy|string|||
|gatewayProxies.NAME.service.externalTrafficPolicy|string||Set the external traffic policy on the provisioned service.|
|gatewayProxies.NAME.service.name|string||Custom name override for the service resource of the proxy|
|gatewayProxies.NAME.service.httpsFirst|bool||List HTTPS port before HTTP|
|gatewayProxies.NAME.service.loadBalancerIP|string||IP address of the load balancer|
Expand Down Expand Up @@ -1255,7 +1256,7 @@
|gatewayProxies.gatewayProxy.service.httpsNodePort|int||HTTPS nodeport for the gateway service if using type NodePort|
|gatewayProxies.gatewayProxy.service.clusterIP|string||static clusterIP (or `None`) when `gatewayProxies[].gatewayProxy.service.type` is `ClusterIP`|
|gatewayProxies.gatewayProxy.service.extraAnnotations.NAME|string|||
|gatewayProxies.gatewayProxy.service.externalTrafficPolicy|string|||
|gatewayProxies.gatewayProxy.service.externalTrafficPolicy|string||Set the external traffic policy on the provisioned service.|
|gatewayProxies.gatewayProxy.service.name|string||Custom name override for the service resource of the proxy|
|gatewayProxies.gatewayProxy.service.httpsFirst|bool||List HTTPS port before HTTP|
|gatewayProxies.gatewayProxy.service.loadBalancerIP|string||IP address of the load balancer|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2093,6 +2093,8 @@ spec:
properties:
clusterIP:
type: string
externalTrafficPolicy:
type: string
extraAnnotations:
additionalProperties:
type: string
Expand Down
9 changes: 5 additions & 4 deletions install/helm/gloo/generate/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,10 @@ type ProvisionedDeployment struct {
}

type ProvisionedService struct {
Type *string `json:"type,omitempty" desc:"K8s service type. If set to null, a default of LoadBalancer will be imposed."`
ExtraLabels map[string]string `json:"extraLabels,omitempty" desc:"Extra labels to add to the service."`
ExtraAnnotations map[string]string `json:"extraAnnotations,omitempty" desc:"Extra annotations to add to the service."`
Type *string `json:"type,omitempty" desc:"K8s service type. If set to null, a default of LoadBalancer will be imposed."`
ExtraLabels map[string]string `json:"extraLabels,omitempty" desc:"Extra labels to add to the service."`
ExtraAnnotations map[string]string `json:"extraAnnotations,omitempty" desc:"Extra annotations to add to the service."`
ExternalTrafficPolicy *string `json:"externalTrafficPolicy,omitempty" desc:"Set the external traffic policy on the provisioned service."`
}

type ProvisionedServiceAccount struct {
Expand Down Expand Up @@ -717,7 +718,7 @@ type GatewayProxyService struct {
HttpsNodePort *int `json:"httpsNodePort,omitempty" desc:"HTTPS nodeport for the gateway service if using type NodePort"`
ClusterIP *string "json:\"clusterIP,omitempty\" desc:\"static clusterIP (or `None`) when `gatewayProxies[].gatewayProxy.service.type` is `ClusterIP`\""
ExtraAnnotations map[string]string `json:"extraAnnotations,omitempty"`
ExternalTrafficPolicy *string `json:"externalTrafficPolicy,omitempty"`
ExternalTrafficPolicy *string `json:"externalTrafficPolicy,omitempty" desc:"Set the external traffic policy on the provisioned service."`
Name *string `json:"name,omitempty" desc:"Custom name override for the service resource of the proxy"`
HttpsFirst *bool `json:"httpsFirst,omitempty" desc:"List HTTPS port before HTTP"`
LoadBalancerIP *string `json:"loadBalancerIP,omitempty" desc:"IP address of the load balancer"`
Expand Down
3 changes: 3 additions & 0 deletions install/helm/gloo/templates/43-gatewayparameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ spec:
{{- end }}{{/* if $gg.service */}}
service:
type: {{ $serviceType }}
{{- with ($gg.service).externalTrafficPolicy }}
externalTrafficPolicy: {{ . }}
{{- end }}
{{- with ($gg.service).extraLabels }}
extraLabels:
{{- toYaml . | nindent 8 }}
Expand Down
4 changes: 4 additions & 0 deletions install/test/k8sgateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ var _ = Describe("Kubernetes Gateway API integration", func() {
fmt.Sprintf("kubeGateway.gatewayParameters.glooGateway.envoyContainer.resources.limits.cpu=%s", envoyLimits["cpu"].ToUnstructured()),
"kubeGateway.gatewayParameters.glooGateway.proxyDeployment.replicas=5",
"kubeGateway.gatewayParameters.glooGateway.service.type=ClusterIP",
"kubeGateway.gatewayParameters.glooGateway.service.externalTrafficPolicy=Local",
"kubeGateway.gatewayParameters.glooGateway.service.extraLabels.svclabel1=x",
"kubeGateway.gatewayParameters.glooGateway.service.extraAnnotations.svcanno1=y",
"kubeGateway.gatewayParameters.glooGateway.serviceAccount.extraLabels.label1=a",
Expand Down Expand Up @@ -246,6 +247,7 @@ var _ = Describe("Kubernetes Gateway API integration", func() {
Expect(gwpKube.GetSdsContainer().GetResources().Limits).To(matchers.ContainMapElements(sdsLimits))

Expect(*gwpKube.GetService().GetType()).To(Equal(corev1.ServiceTypeClusterIP))
Expect(gwpKube.GetService().GetExternalTrafficPolicy()).To(HaveValue(Equal(corev1.ServiceExternalTrafficPolicyLocal)))
Expect(gwpKube.GetService().GetExtraLabels()).To(matchers.ContainMapElements(map[string]string{"svclabel1": "x"}))
Expect(gwpKube.GetService().GetExtraAnnotations()).To(matchers.ContainMapElements(map[string]string{"svcanno1": "y"}))

Expand Down Expand Up @@ -282,6 +284,7 @@ var _ = Describe("Kubernetes Gateway API integration", func() {
"kubeGateway.gatewayParameters.glooGateway.envoyContainer.image.pullPolicy=Always",
"kubeGateway.gatewayParameters.glooGateway.proxyDeployment.replicas=5",
"kubeGateway.gatewayParameters.glooGateway.service.type=ClusterIP",
"kubeGateway.gatewayParameters.glooGateway.service.externalTrafficPolicy=Local",
"kubeGateway.gatewayParameters.glooGateway.service.extraLabels.svclabel1=a",
"kubeGateway.gatewayParameters.glooGateway.service.extraAnnotations.svcanno1=b",
"kubeGateway.gatewayParameters.glooGateway.serviceAccount.extraLabels.label1=a",
Expand Down Expand Up @@ -324,6 +327,7 @@ var _ = Describe("Kubernetes Gateway API integration", func() {
Expect(*gwpKube.GetSdsContainer().GetBootstrap().GetLogLevel()).To(Equal("debug"))

Expect(*gwpKube.GetService().GetType()).To(Equal(corev1.ServiceTypeClusterIP))
Expect(gwpKube.GetService().GetExternalTrafficPolicy()).To(HaveValue(Equal(corev1.ServiceExternalTrafficPolicyLocal)))
Expect(gwpKube.GetService().GetExtraLabels()).To(matchers.ContainMapElements(map[string]string{"svclabel1": "a"}))
Expect(gwpKube.GetService().GetExtraAnnotations()).To(matchers.ContainMapElements(map[string]string{"svcanno1": "b"}))

Expand Down
12 changes: 12 additions & 0 deletions projects/gateway2/api/v1alpha1/kube_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ type Service struct {
//
// +kubebuilder:validation:Optional
ExtraAnnotations map[string]string `json:"extraAnnotations,omitempty"`

// External Traffic Policy on the Service object.
//
// +kubebuilder:validation:Optional
ExternalTrafficPolicy *corev1.ServiceExternalTrafficPolicy `json:"externalTrafficPolicy,omitempty"`
}

func (in *Service) GetType() *corev1.ServiceType {
Expand Down Expand Up @@ -126,6 +131,13 @@ func (in *Service) GetExtraAnnotations() map[string]string {
return in.ExtraAnnotations
}

func (in *Service) GetExternalTrafficPolicy() *corev1.ServiceExternalTrafficPolicy {
if in == nil {
return nil
}
return in.ExternalTrafficPolicy
}

type ServiceAccount struct {
// Additional labels to add to the ServiceAccount object metadata.
//
Expand Down
5 changes: 5 additions & 0 deletions projects/gateway2/api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions projects/gateway2/deployer/deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ var _ = Describe("Deployer", func() {
ExtraAnnotations: map[string]string{
"foo": "bar",
},
ExternalTrafficPolicy: ptr.To(corev1.ServiceExternalTrafficPolicyCluster),
},
ServiceAccount: &gw2_v1alpha1.ServiceAccount{
ExtraLabels: map[string]string{
Expand Down Expand Up @@ -598,6 +599,7 @@ var _ = Describe("Deployer", func() {
ExtraAnnotations: map[string]string{
"override-foo": "override-bar",
},
ExternalTrafficPolicy: ptr.To(corev1.ServiceExternalTrafficPolicyLocal),
},
ServiceAccount: &gw2_v1alpha1.ServiceAccount{
ExtraLabels: map[string]string{
Expand Down Expand Up @@ -665,6 +667,7 @@ var _ = Describe("Deployer", func() {
"foo": "bar",
"override-foo": "override-bar",
},
ExternalTrafficPolicy: ptr.To(corev1.ServiceExternalTrafficPolicyLocal),
},
ServiceAccount: &gw2_v1alpha1.ServiceAccount{
ExtraLabels: map[string]string{
Expand Down Expand Up @@ -832,6 +835,7 @@ var _ = Describe("Deployer", func() {
Expect(svc.GetLabels()).To(matchers.ContainMapElements(expectedGwp.Service.ExtraLabels))
Expect(svc.Spec.Type).To(Equal(*expectedGwp.Service.Type))
Expect(svc.Spec.ClusterIP).To(Equal(*expectedGwp.Service.ClusterIP))
Expect(svc.Spec.ExternalTrafficPolicy).To(Equal(*expectedGwp.Service.ExternalTrafficPolicy))

sa := objs.findServiceAccount(defaultNamespace, defaultServiceAccountName)
Expect(sa).ToNot(BeNil())
Expand Down Expand Up @@ -948,6 +952,7 @@ var _ = Describe("Deployer", func() {
Expect(svc.GetLabels()).To(matchers.ContainMapElements(expectedGwp.Service.ExtraLabels))
Expect(svc.Spec.Type).To(Equal(*expectedGwp.Service.Type))
Expect(svc.Spec.ClusterIP).To(Equal(*expectedGwp.Service.ClusterIP))
Expect(svc.Spec.ExternalTrafficPolicy).To(Equal(*expectedGwp.Service.ExternalTrafficPolicy))

sa := objs.findServiceAccount(defaultNamespace, defaultServiceAccountName)
Expect(sa).ToNot(BeNil())
Expand Down Expand Up @@ -1552,6 +1557,7 @@ func fullyDefinedGatewayParameters(name, namespace string) *gw2_v1alpha1.Gateway
ExtraLabels: map[string]string{
"service-label": "foo",
},
ExternalTrafficPolicy: ptr.To(corev1.ServiceExternalTrafficPolicyLocal),
},
ServiceAccount: &gw2_v1alpha1.ServiceAccount{
ExtraLabels: map[string]string{
Expand Down
4 changes: 4 additions & 0 deletions projects/gateway2/deployer/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,10 @@ func deepMergeService(dst, src *v1alpha1.Service) *v1alpha1.Service {
dst.ExtraLabels = deepMergeMaps(dst.GetExtraLabels(), src.GetExtraLabels())
dst.ExtraAnnotations = deepMergeMaps(dst.GetExtraAnnotations(), src.GetExtraAnnotations())

if src.GetExternalTrafficPolicy() != nil {
dst.ExternalTrafficPolicy = src.GetExternalTrafficPolicy()
}

return dst
}

Expand Down
26 changes: 26 additions & 0 deletions projects/gateway2/deployer/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
gw2_v1alpha1 "github.com/solo-io/gloo/projects/gateway2/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/ptr"
)

Expand Down Expand Up @@ -134,4 +135,29 @@ var _ = Describe("deepMergeGatewayParameters", func() {
Expect(out.Spec.Kube.ServiceAccount.ExtraLabels).To(Equal(expectedMap))
Expect(out.Spec.Kube.ServiceAccount.ExtraAnnotations).To(Equal(expectedMap))
})

It("merges service strings", func() {

dst := &gw2_v1alpha1.GatewayParameters{
Spec: gw2_v1alpha1.GatewayParametersSpec{
Kube: &gw2_v1alpha1.KubernetesProxyConfig{
Service: &gw2_v1alpha1.Service{
ExternalTrafficPolicy: ptr.To(corev1.ServiceExternalTrafficPolicyLocal),
},
},
},
}
src := &gw2_v1alpha1.GatewayParameters{
Spec: gw2_v1alpha1.GatewayParametersSpec{
Kube: &gw2_v1alpha1.KubernetesProxyConfig{
Service: &gw2_v1alpha1.Service{
ExternalTrafficPolicy: ptr.To(corev1.ServiceExternalTrafficPolicyCluster),
},
},
},
}

out := deepMergeGatewayParameters(dst, src)
Expect(out.Spec.Kube.Service.ExternalTrafficPolicy).To(Equal(ptr.To(corev1.ServiceExternalTrafficPolicyCluster)))
})
})
9 changes: 5 additions & 4 deletions projects/gateway2/deployer/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ type helmImage struct {
}

type helmService struct {
Type *string `json:"type,omitempty"`
ClusterIP *string `json:"clusterIP,omitempty"`
ExtraAnnotations map[string]string `json:"extraAnnotations,omitempty"`
ExtraLabels map[string]string `json:"extraLabels,omitempty"`
Type *string `json:"type,omitempty"`
ClusterIP *string `json:"clusterIP,omitempty"`
ExtraAnnotations map[string]string `json:"extraAnnotations,omitempty"`
ExtraLabels map[string]string `json:"extraLabels,omitempty"`
ExternalTrafficPolicy *string `json:"externalTrafficPolicy,omitempty"`
}

type helmServiceAccount struct {
Expand Down
13 changes: 9 additions & 4 deletions projects/gateway2/deployer/values_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,16 @@ func getServiceValues(svcConfig *v1alpha1.Service) *helmService {
if svcConfig.GetType() != nil {
svcType = ptr.To(string(*svcConfig.GetType()))
}
var svcExternalTrafficPolicy *string
if svcConfig.GetExternalTrafficPolicy() != nil {
svcExternalTrafficPolicy = ptr.To(string(*svcConfig.GetExternalTrafficPolicy()))
}
return &helmService{
Type: svcType,
ClusterIP: svcConfig.GetClusterIP(),
ExtraAnnotations: svcConfig.GetExtraAnnotations(),
ExtraLabels: svcConfig.GetExtraLabels(),
Type: svcType,
ClusterIP: svcConfig.GetClusterIP(),
ExtraAnnotations: svcConfig.GetExtraAnnotations(),
ExtraLabels: svcConfig.GetExtraLabels(),
ExternalTrafficPolicy: svcExternalTrafficPolicy,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,9 @@ metadata:
{{- end }}
spec:
type: {{ $gateway.service.type }}
{{- if $gateway.service.externalTrafficPolicy }}
externalTrafficPolicy: {{ $gateway.service.externalTrafficPolicy }}
{{- end }}
{{- with $gateway.service.clusterIP }}
clusterIP: {{ . }}
{{- end }}
Expand Down
9 changes: 9 additions & 0 deletions test/kubernetes/e2e/features/deployer/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ func (s *testingSuite) TestConfigureProxiesFromGatewayParameters() {
s.testInstallation.Assertions.Gomega.Expect(svc.GetAnnotations()).To(
gomega.HaveKeyWithValue("svc-anno-key", "svc-anno-val"))

// check that external traffic policy got passwed through from GatewayParameters to the Service
s.testInstallation.Assertions.Gomega.Expect(svc.Spec.ExternalTrafficPolicy).To(
gomega.Equal(corev1.ServiceExternalTrafficPolicyCluster))

// Update the Gateway to use the custom GatewayParameters
gwName := types.NamespacedName{Name: gw.Name, Namespace: gw.Namespace}
err = s.testInstallation.ClusterContext.Client.Get(s.ctx, gwName, gw)
Expand Down Expand Up @@ -181,6 +185,11 @@ func (s *testingSuite) TestProvisionResourcesUpdatedWithValidParameters() {
// the GatewayParameters modification should cause the deployer to re-run and update the
// deployment to have 2 replicas
s.testInstallation.Assertions.EventuallyRunningReplicas(s.ctx, proxyDeployment.ObjectMeta, gomega.Equal(2))

// modify the external traffic policy in the GatewayParameters
s.patchGatewayParameters(gwParamsDefault.ObjectMeta, func(parameters *v1alpha1.GatewayParameters) {
parameters.Spec.Kube.Service.ExternalTrafficPolicy = ptr.To(corev1.ServiceExternalTrafficPolicyLocal)
})
}

func (s *testingSuite) TestProvisionResourcesNotUpdatedWithInvalidParameters() {
Expand Down

0 comments on commit ecaab37

Please sign in to comment.