Skip to content

Commit ead60b8

Browse files
authored
minor test fixes + equals on policy ir (#10715)
1 parent 882bb99 commit ead60b8

File tree

7 files changed

+138
-28
lines changed

7 files changed

+138
-28
lines changed

internal/kgateway/controller/start.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ type SetupOpts struct {
5151

5252
// static set of global Settings
5353
GlobalSettings *settings.Settings
54+
55+
PprofBindAddress string
56+
HealthProbeBindAddress string
57+
MetricsBindAddress string
5458
}
5559

5660
var setupLog = ctrl.Log.WithName("setup")
@@ -103,11 +107,11 @@ func NewControllerBuilder(ctx context.Context, cfg StartConfig) (*ControllerBuil
103107
mgrOpts := ctrl.Options{
104108
BaseContext: func() context.Context { return ctx },
105109
Scheme: scheme,
106-
PprofBindAddress: "127.0.0.1:9099",
110+
PprofBindAddress: cfg.SetupOpts.PprofBindAddress,
107111
// if you change the port here, also change the port "health" in the helmchart.
108-
HealthProbeBindAddress: ":9093",
112+
HealthProbeBindAddress: cfg.SetupOpts.HealthProbeBindAddress,
109113
Metrics: metricsserver.Options{
110-
BindAddress: ":9092",
114+
BindAddress: cfg.SetupOpts.MetricsBindAddress,
111115
},
112116
Controller: config.Controller{
113117
// see https://github.com/kubernetes-sigs/controller-runtime/issues/2937

internal/kgateway/ir/iface.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (c PolicyWrapper) Equals(in PolicyWrapper) bool {
131131
return false
132132
}
133133

134-
return versionEquals(c.Policy, in.Policy)
134+
return versionEquals(c.Policy, in.Policy) && c.PolicyIR.Equals(in.PolicyIR)
135135
}
136136

137137
var (

internal/kgateway/krtcollections/uniqueclients_test.go

+87-8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,84 @@ func TestUniqueClients(t *testing.T) {
7575
})),
7676
),
7777
},
78+
{
79+
name: "two UCCs",
80+
inputs: []any{
81+
&corev1.Pod{
82+
TypeMeta: metav1.TypeMeta{},
83+
ObjectMeta: metav1.ObjectMeta{
84+
Name: "podname",
85+
Namespace: "ns",
86+
Labels: map[string]string{"a": "b"},
87+
},
88+
Spec: corev1.PodSpec{
89+
NodeName: "node",
90+
},
91+
},
92+
&corev1.Node{
93+
ObjectMeta: metav1.ObjectMeta{
94+
Name: "node",
95+
Labels: map[string]string{
96+
corev1.LabelTopologyRegion: "region",
97+
corev1.LabelTopologyZone: "zone",
98+
},
99+
},
100+
},
101+
&corev1.Pod{
102+
TypeMeta: metav1.TypeMeta{},
103+
ObjectMeta: metav1.ObjectMeta{
104+
Name: "podname2",
105+
Namespace: "ns",
106+
Labels: map[string]string{"a": "b"},
107+
},
108+
Spec: corev1.PodSpec{
109+
NodeName: "node2",
110+
},
111+
},
112+
&corev1.Node{
113+
ObjectMeta: metav1.ObjectMeta{
114+
Name: "node2",
115+
Labels: map[string]string{
116+
corev1.LabelTopologyRegion: "region2",
117+
corev1.LabelTopologyZone: "zone2",
118+
},
119+
},
120+
},
121+
},
122+
requests: []*envoy_service_discovery_v3.DiscoveryRequest{
123+
{
124+
Node: &corev3.Node{
125+
Id: "podname.ns",
126+
Metadata: &structpb.Struct{
127+
Fields: map[string]*structpb.Value{
128+
xds.RoleKey: structpb.NewStringValue(wellknown.GatewayApiProxyValue + "~best-proxy-role"),
129+
},
130+
},
131+
},
132+
},
133+
{
134+
Node: &corev3.Node{
135+
Id: "podname2.ns",
136+
Metadata: &structpb.Struct{
137+
Fields: map[string]*structpb.Value{
138+
xds.RoleKey: structpb.NewStringValue(wellknown.GatewayApiProxyValue + "~best-proxy-role"),
139+
},
140+
},
141+
},
142+
},
143+
},
144+
result: sets.New(
145+
fmt.Sprintf("kgateway-kube-gateway-api~best-proxy-role~%d~ns", utils.HashLabels(map[string]string{
146+
corev1.LabelTopologyRegion: "region",
147+
corev1.LabelTopologyZone: "zone",
148+
"a": "b",
149+
})), fmt.Sprintf("kgateway-kube-gateway-api~best-proxy-role~%d~ns", utils.HashLabels(map[string]string{
150+
corev1.LabelTopologyRegion: "region2",
151+
corev1.LabelTopologyZone: "zone2",
152+
"a": "b",
153+
})),
154+
),
155+
},
78156
{
79157
name: "no-pods",
80158
inputs: nil,
@@ -140,16 +218,17 @@ func TestUniqueClients(t *testing.T) {
140218
g.Expect(names).To(Equal(tc.result))
141219

142220
for i := range tc.requests {
143-
for j := 0; j < 10; j++ {
144-
g.Expect(ucc.List()).To(HaveLen(len(allUcc) - i))
221+
for j := 0; j < 9; j++ {
145222
cb.OnStreamClosed(int64(i*10+j), nil)
146223
}
147-
// FIXME: this assertion will be reworked; the goal here is to test jitter
148-
// that as clients disconnect we don't impact other UCCs etc.
149-
// g.Eventually(func() []ir.UniqlyConnectedClient {
150-
// allUcc = ucc.List()
151-
// return allUcc
152-
// }, "1s").Should(HaveLen(len(tc.result)))
224+
}
225+
226+
g.Expect(ucc.List()).Should(HaveLen(len(tc.result)))
227+
228+
for i := range tc.requests {
229+
j := 9
230+
g.Eventually(ucc.List).Should(HaveLen(len(allUcc) - i))
231+
cb.OnStreamClosed(int64(i*10+j), nil)
153232
}
154233

155234
// as events happens async, eventually after all clients disconnect all UCCs should be removed

internal/kgateway/setup/controlplane.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@ func NewControlPlane(
2929
if err != nil {
3030
return nil, err
3131
}
32-
return NewControlPlaneWithListener(ctx, lis, callbacks)
32+
snapshotCache, grpcServer := NewControlPlaneWithListener(ctx, lis, callbacks)
33+
go func() {
34+
<-ctx.Done()
35+
grpcServer.GracefulStop()
36+
}()
37+
return snapshotCache, err
3338
}
3439

3540
func NewControlPlaneWithListener(ctx context.Context,
3641
lis net.Listener,
37-
callbacks xdsserver.Callbacks) (envoycache.SnapshotCache, error) {
42+
callbacks xdsserver.Callbacks) (envoycache.SnapshotCache, *grpc.Server) {
3843
logger := contextutils.LoggerFrom(ctx).Desugar()
3944
serverOpts := []grpc.ServerOption{
4045
grpc.StreamInterceptor(
@@ -61,10 +66,6 @@ func NewControlPlaneWithListener(ctx context.Context,
6166
envoy_service_discovery_v3.RegisterAggregatedDiscoveryServiceServer(grpcServer, xdsServer)
6267

6368
go grpcServer.Serve(lis)
64-
go func() {
65-
<-ctx.Done()
66-
grpcServer.GracefulStop()
67-
}()
6869

69-
return snapshotCache, nil
70+
return snapshotCache, grpcServer
7071
}

internal/kgateway/setup/ggv2setup.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,13 @@ func StartGGv2(
7373
}
7474

7575
setupOpts := &controller.SetupOpts{
76-
Cache: cache,
77-
KrtDebugger: new(krt.DebugHandler),
78-
ExtraGatewayClasses: extraGwClasses,
79-
GlobalSettings: st,
76+
Cache: cache,
77+
KrtDebugger: new(krt.DebugHandler),
78+
ExtraGatewayClasses: extraGwClasses,
79+
GlobalSettings: st,
80+
PprofBindAddress: "127.0.0.1:9099",
81+
HealthProbeBindAddress: ":9093",
82+
MetricsBindAddress: ":9092",
8083
}
8184

8285
restConfig := ctrl.GetConfigOrDie()

internal/kgateway/setup/ggv2setup_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,8 @@ func TestScenarios(t *testing.T) {
178178
t.Fatalf("can't listen %v", err)
179179
}
180180
xdsPort := lis.Addr().(*net.TCPAddr).Port
181-
snapCache, err := ggv2setup.NewControlPlaneWithListener(ctx, lis, uniqueClientCallbacks)
182-
if err != nil {
183-
t.Fatalf("can't listen %v", err)
184-
}
181+
snapCache, grpcServer := ggv2setup.NewControlPlaneWithListener(ctx, lis, uniqueClientCallbacks)
182+
t.Cleanup(func() { grpcServer.Stop() })
185183

186184
st, err := settings.BuildSettings()
187185
if err != nil {

internal/kgateway/setup/testdata/setupyaml/setup.yaml

+26-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,29 @@ apiVersion: gateway.kgateway.dev/v1alpha1
1515
metadata:
1616
name: kgateway
1717
spec:
18-
kube: {}
18+
kube:
19+
deployment:
20+
replicas: 1
21+
envoyContainer:
22+
image:
23+
registry: ghcr.io/kgateway-dev
24+
repository: envoy-wrapper
25+
tag: v0.0.1
26+
pullPolicy: IfNotPresent
27+
securityContext:
28+
allowPrivilegeEscalation: false
29+
capabilities:
30+
add:
31+
- NET_BIND_SERVICE
32+
drop:
33+
- ALL
34+
readOnlyRootFilesystem: true
35+
runAsNonRoot: true
36+
runAsUser: 10101
37+
service:
38+
type: LoadBalancer
39+
stats:
40+
enableStatsRoute: true
41+
enabled: true
42+
routePrefixRewrite: /stats/prometheus
43+
statsRoutePrefixRewrite: /stats

0 commit comments

Comments
 (0)