Skip to content

Commit 86d1768

Browse files
authored
Pass xds host/port to controller (#10688)
Signed-off-by: Jenny Shu <[email protected]>
1 parent 6b87df8 commit 86d1768

File tree

21 files changed

+135
-115
lines changed

21 files changed

+135
-115
lines changed

install/helm/kgateway/templates/deployment.yaml

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ spec:
3434
image: "{{ .Values.controller.image.registry | default .Values.image.registry }}/{{ .Values.controller.image.repository }}:{{ .Values.controller.image.tag | default .Values.image.tag | default .Chart.Version }}"
3535
imagePullPolicy: {{ .Values.controller.image.pullPolicy | default .Values.image.pullPolicy }}
3636
ports:
37-
- containerPort: {{ .Values.controller.service.ports.grpc | default 9977 }}
37+
- containerPort: {{ .Values.controller.service.ports.grpc }}
3838
name: grpc-xds
3939
protocol: TCP
4040
readinessProbe:
@@ -43,7 +43,7 @@ spec:
4343
periodSeconds: 10
4444
successThreshold: 1
4545
tcpSocket:
46-
port: 9977
46+
port: {{ .Values.controller.service.ports.grpc }}
4747
timeoutSeconds: 1
4848
env:
4949
- name: GOMEMLIMIT
@@ -58,6 +58,10 @@ spec:
5858
resource: limits.cpu
5959
- name: LOG_LEVEL
6060
value: {{ .Values.controller.logLevel | quote }}
61+
- name: KGW_XDS_SERVICE_NAME
62+
value: {{ include "kgateway.fullname" . }}
63+
- name: KGW_XDS_SERVICE_PORT
64+
value: {{ .Values.controller.service.ports.grpc | quote }}
6165
# TODO: Remove this once the cleanup is done. Required as the gloo-system
6266
# namespace is the default namespace and conformance will fail as a result.
6367
- name: POD_NAMESPACE

install/test/helm_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/kgateway-dev/kgateway/v2/internal/gateway/pkg/defaults"
4040
"github.com/kgateway-dev/kgateway/v2/pkg/utils/kubeutils"
4141
"github.com/kgateway-dev/kgateway/v2/test/gomega/matchers"
42+
"github.com/kgateway-dev/kgateway/v2/test/helpers"
4243
glootestutils "github.com/kgateway-dev/kgateway/v2/test/testutils"
4344
)
4445

@@ -5037,7 +5038,7 @@ metadata:
50375038
})
50385039

50395040
// make sure the resource requests and limits are set in the pod template
5040-
deploy := getStructuredDeployment(testManifest, kubeutils.GlooDeploymentName)
5041+
deploy := getStructuredDeployment(testManifest, helpers.DefaultKgatewayDeploymentName)
50415042
glooContainer := deploy.Spec.Template.Spec.Containers[0]
50425043
Expect(glooContainer.Resources).To(Equal(corev1.ResourceRequirements{
50435044
Limits: corev1.ResourceList{
@@ -5078,7 +5079,7 @@ metadata:
50785079
})
50795080

50805081
// make sure the resource requests are set in the pod template
5081-
deploy := getStructuredDeployment(testManifest, kubeutils.GlooDeploymentName)
5082+
deploy := getStructuredDeployment(testManifest, helpers.DefaultKgatewayDeploymentName)
50825083
glooContainer := deploy.Spec.Template.Spec.Containers[0]
50835084
Expect(glooContainer.Resources).To(Equal(corev1.ResourceRequirements{
50845085
Requests: corev1.ResourceList{

install/test/k8sgateway_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import (
1616
"github.com/kgateway-dev/kgateway/v2/api/v1alpha1"
1717
"github.com/kgateway-dev/kgateway/v2/install/utils/kuberesource"
1818
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/wellknown"
19-
"github.com/kgateway-dev/kgateway/v2/pkg/utils/kubeutils"
2019
"github.com/kgateway-dev/kgateway/v2/test/gomega/matchers"
20+
"github.com/kgateway-dev/kgateway/v2/test/helpers"
2121
glootestutils "github.com/kgateway-dev/kgateway/v2/test/testutils"
2222
)
2323

@@ -46,7 +46,7 @@ var _ = Describe("Kubernetes Gateway API integration", func() {
4646
})
4747

4848
It("relevant resources are rendered", func() {
49-
deployment := getDeployment(testManifest, namespace, kubeutils.GlooDeploymentName)
49+
deployment := getDeployment(testManifest, namespace, helpers.DefaultKgatewayDeploymentName)
5050
Expect(deployment.Spec.Template.Spec.Containers).To(HaveLen(1), "should have exactly 1 container")
5151

5252
// make sure the GatewayClass and RBAC resources exist (note, since they are all cluster-scoped, they do not have a namespace)
@@ -487,7 +487,7 @@ var _ = Describe("Kubernetes Gateway API integration", func() {
487487
})
488488

489489
It("relevant resources are not rendered", func() {
490-
deployment := getDeployment(testManifest, namespace, kubeutils.GlooDeploymentName)
490+
deployment := getDeployment(testManifest, namespace, helpers.DefaultKgatewayDeploymentName)
491491
Expect(deployment.Spec.Template.Spec.Containers).To(HaveLen(1), "should have exactly 1 container")
492492

493493
// the RBAC resources should not be rendered

internal/kgateway/controller/start.go

+26-28
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,21 @@ package controller
33
import (
44
"context"
55

6-
"k8s.io/client-go/rest"
7-
"k8s.io/utils/ptr"
8-
"sigs.k8s.io/controller-runtime/pkg/config"
9-
6+
envoycache "github.com/envoyproxy/go-control-plane/pkg/cache/v3"
107
"github.com/solo-io/go-utils/contextutils"
11-
12-
glooschemes "github.com/kgateway-dev/kgateway/v2/pkg/schemes"
13-
8+
uzap "go.uber.org/zap"
9+
istiokube "istio.io/istio/pkg/kube"
10+
"istio.io/istio/pkg/kube/krt"
11+
istiolog "istio.io/istio/pkg/log"
12+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1413
"k8s.io/apimachinery/pkg/util/sets"
14+
"k8s.io/client-go/rest"
15+
"k8s.io/utils/ptr"
1516
ctrl "sigs.k8s.io/controller-runtime"
16-
17+
"sigs.k8s.io/controller-runtime/pkg/config"
1718
"sigs.k8s.io/controller-runtime/pkg/healthz"
1819
czap "sigs.k8s.io/controller-runtime/pkg/log/zap"
1920
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
20-
21-
envoycache "github.com/envoyproxy/go-control-plane/pkg/cache/v3"
22-
uzap "go.uber.org/zap"
23-
istiokube "istio.io/istio/pkg/kube"
24-
"istio.io/istio/pkg/kube/krt"
25-
istiolog "istio.io/istio/pkg/log"
2621
apiv1 "sigs.k8s.io/gateway-api/apis/v1"
2722

2823
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/deployer"
@@ -37,6 +32,9 @@ import (
3732
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/utils/krtutil"
3833
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/wellknown"
3934
"github.com/kgateway-dev/kgateway/v2/pkg/client/clientset/versioned"
35+
glooschemes "github.com/kgateway-dev/kgateway/v2/pkg/schemes"
36+
"github.com/kgateway-dev/kgateway/v2/pkg/utils/kubeutils"
37+
"github.com/kgateway-dev/kgateway/v2/pkg/utils/namespaces"
4038
)
4139

4240
const (
@@ -51,8 +49,8 @@ type SetupOpts struct {
5149

5250
KrtDebugger *krt.DebugHandler
5351

54-
XdsHost string
55-
XdsPort int32
52+
// static set of global Settings
53+
GlobalSettings *settings.Settings
5654
}
5755

5856
var setupLog = ctrl.Log.WithName("setup")
@@ -81,7 +79,6 @@ type ControllerBuilder struct {
8179
cfg StartConfig
8280
mgr ctrl.Manager
8381
isOurGw func(gw *apiv1.Gateway) bool
84-
settings settings.Settings
8582
}
8683

8784
func NewControllerBuilder(ctx context.Context, cfg StartConfig) (*ControllerBuilder, error) {
@@ -139,6 +136,7 @@ func NewControllerBuilder(ctx context.Context, cfg StartConfig) (*ControllerBuil
139136
cfg.Client,
140137
cli,
141138
setupLog,
139+
*cfg.SetupOpts.GlobalSettings,
142140
)
143141
gwClasses := sets.New(append(cfg.SetupOpts.ExtraGatewayClasses, wellknown.GatewayClassName)...)
144142
isOurGw := func(gw *apiv1.Gateway) bool {
@@ -169,7 +167,6 @@ func NewControllerBuilder(ctx context.Context, cfg StartConfig) (*ControllerBuil
169167
cfg: cfg,
170168
mgr: mgr,
171169
isOurGw: isOurGw,
172-
settings: commoncol.Settings,
173170
}, nil
174171
}
175172

@@ -185,21 +182,22 @@ func pluginFactoryWithBuiltin(extraPlugins []extensionsplug.Plugin) extensions2.
185182
func (c *ControllerBuilder) Start(ctx context.Context) error {
186183
logger := contextutils.LoggerFrom(ctx).Desugar()
187184
logger.Info("starting gateway controller")
188-
// GetXdsAddress waits for gloo-edge to populate the xds address of the server.
189-
// in the future this logic may move here and be duplicated.
190-
xdsHost, xdsPort := c.cfg.SetupOpts.XdsHost, c.cfg.SetupOpts.XdsPort
191-
if xdsHost == "" {
192-
return ctx.Err()
193-
}
194185

195-
logger.Info("got xds address for deployer", uzap.String("xds_host", xdsHost), uzap.Int32("xds_port", xdsPort))
186+
globalSettings := c.cfg.SetupOpts.GlobalSettings
187+
188+
xdsHost := kubeutils.ServiceFQDN(metav1.ObjectMeta{
189+
Name: globalSettings.XdsServiceName,
190+
Namespace: namespaces.GetPodNamespace(),
191+
})
192+
xdsPort := globalSettings.XdsServicePort
193+
logger.Info("got xds address for deployer", uzap.String("xds_host", xdsHost), uzap.Uint32("xds_port", xdsPort))
196194

197-
integrationEnabled := c.settings.EnableIstioIntegration
195+
integrationEnabled := globalSettings.EnableIstioIntegration
198196

199197
// copy over relevant aws options (if any) from Settings
200198
var awsInfo *deployer.AwsInfo
201-
stsCluster := c.settings.StsClusterName
202-
stsUri := c.settings.StsUri
199+
stsCluster := globalSettings.StsClusterName
200+
stsUri := globalSettings.StsUri
203201
if stsCluster != "" && stsUri != "" {
204202
awsInfo = &deployer.AwsInfo{
205203
EnableServiceAccountCredentials: true,

internal/kgateway/deployer/deployer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type Deployer struct {
5353

5454
type ControlPlaneInfo struct {
5555
XdsHost string
56-
XdsPort int32
56+
XdsPort uint32
5757
}
5858

5959
type AwsInfo struct {

internal/kgateway/deployer/values.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ type helmServiceAccount struct {
101101
// to receive xds config updates
102102
type helmXds struct {
103103
Host *string `json:"host,omitempty"`
104-
Port *int32 `json:"port,omitempty"`
104+
Port *uint32 `json:"port,omitempty"`
105105
}
106106

107107
type helmAutoscaling struct {

internal/kgateway/extensions2/common/krt.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package common
22

33
import (
4-
"fmt"
5-
64
"github.com/go-logr/logr"
75
"istio.io/istio/pkg/kube"
86
istiokube "istio.io/istio/pkg/kube"
@@ -44,6 +42,7 @@ func NewCommonCollections(
4442
client istiokube.Client,
4543
ourClient versioned.Interface,
4644
logger logr.Logger,
45+
settings settings.Settings,
4746
) *CommonCollections {
4847
secretClient := kclient.New[*corev1.Secret](client)
4948
k8sSecretsRaw := krt.WrapClient(secretClient, krt.WithStop(krtOptions.Stop), krt.WithName("Secrets") /* no debug here - we don't want raw secrets printed*/)
@@ -67,18 +66,13 @@ func NewCommonCollections(
6766
refgrantsCol := krt.WrapClient(kclient.New[*gwv1beta1.ReferenceGrant](client), krtOptions.ToOptions("RefGrants")...)
6867
refgrants := krtcollections.NewRefGrantIndex(refgrantsCol)
6968

70-
st, err := settings.BuildSettings()
71-
if err != nil {
72-
logger.Error(err, "got err while parsing Settings from env")
73-
}
74-
logger.Info(fmt.Sprintf("got settings from env: %+v", *st))
7569
return &CommonCollections{
7670
OurClient: ourClient,
7771
Client: client,
7872
KrtOpts: krtOptions,
7973
Secrets: krtcollections.NewSecretIndex(secrets, refgrants),
8074
Pods: krtcollections.NewPodsCollection(client, krtOptions),
8175
RefGrants: refgrants,
82-
Settings: *st,
76+
Settings: settings,
8377
}
8478
}

internal/kgateway/extensions2/settings/settings.go

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ type Settings struct {
99
EnableAutoMtls bool `split_words:"true"`
1010
StsClusterName string `split_words:"true"`
1111
StsUri string `split_words:"true"`
12+
13+
// XdsServiceName is the name of the Kubernetes Service that serves xDS config.
14+
// It it assumed to be in the kgateway install namespace.
15+
XdsServiceName string `split_words:"true" default:"kgateway"`
16+
17+
// XdsServicePort is the port of the Kubernetes Service that serves xDS config.
18+
// This corresponds to the value of the `grpc-xds` port in the service.
19+
XdsServicePort uint32 `split_words:"true" default:"9977"`
1220
}
1321

1422
// BuildSettings returns a zero-valued Settings obj if error is encountered when parsing env

internal/kgateway/extensions2/settings/settings_test.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ func TestSettings(t *testing.T) {
2525
expectedErrorStr string
2626
}{
2727
{
28-
name: "defaults to empty values",
28+
name: "defaults to empty or default values",
2929
envVars: map[string]string{},
3030
expectedSettings: &settings.Settings{
3131
EnableIstioIntegration: false,
3232
EnableAutoMtls: false,
3333
StsClusterName: "",
3434
StsUri: "",
35+
XdsServiceName: "kgateway",
36+
XdsServicePort: 9977,
3537
},
3638
},
3739
{
@@ -41,12 +43,16 @@ func TestSettings(t *testing.T) {
4143
"KGW_ENABLE_AUTO_MTLS": "true",
4244
"KGW_STS_CLUSTER_NAME": "my-cluster",
4345
"KGW_STS_URI": "my.sts.uri",
46+
"KGW_XDS_SERVICE_NAME": "custom-svc",
47+
"KGW_XDS_SERVICE_PORT": "1234",
4448
},
4549
expectedSettings: &settings.Settings{
4650
EnableIstioIntegration: true,
4751
EnableAutoMtls: true,
4852
StsClusterName: "my-cluster",
4953
StsUri: "my.sts.uri",
54+
XdsServiceName: "custom-svc",
55+
XdsServicePort: 1234,
5056
},
5157
},
5258
{
@@ -56,6 +62,13 @@ func TestSettings(t *testing.T) {
5662
},
5763
expectedErrorStr: "invalid syntax",
5864
},
65+
{
66+
name: "errors on invalid port",
67+
envVars: map[string]string{
68+
"KGW_XDS_SERVICE_PORT": "a123",
69+
},
70+
expectedErrorStr: "invalid syntax",
71+
},
5972
{
6073
name: "ignores other env vars",
6174
envVars: map[string]string{
@@ -65,6 +78,8 @@ func TestSettings(t *testing.T) {
6578
},
6679
expectedSettings: &settings.Settings{
6780
EnableAutoMtls: true,
81+
XdsServiceName: "kgateway",
82+
XdsServicePort: 9977,
6883
},
6984
},
7085
}

0 commit comments

Comments
 (0)