Skip to content

Commit 123fe3c

Browse files
authored
Add conformance test flag to set implementation labels on mesh namespaces (#1957)
* Add labels to mesh namespaces * add doc * remove MeshNamespaceLabels * one more * update doc * tweak wording * add unit tests
1 parent 85a8adf commit 123fe3c

File tree

7 files changed

+117
-21
lines changed

7 files changed

+117
-21
lines changed

conformance/conformance_test.go

+5-17
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ limitations under the License.
1818
package conformance_test
1919

2020
import (
21-
"strings"
2221
"testing"
2322

2423
"sigs.k8s.io/gateway-api/apis/v1alpha2"
@@ -27,7 +26,6 @@ import (
2726
"sigs.k8s.io/gateway-api/conformance/utils/flags"
2827
"sigs.k8s.io/gateway-api/conformance/utils/suite"
2928

30-
"k8s.io/apimachinery/pkg/util/sets"
3129
"k8s.io/client-go/kubernetes"
3230
_ "k8s.io/client-go/plugin/pkg/client/auth"
3331
"k8s.io/client-go/rest"
@@ -52,8 +50,10 @@ func TestConformance(t *testing.T) {
5250
v1alpha2.AddToScheme(client.Scheme())
5351
v1beta1.AddToScheme(client.Scheme())
5452

55-
supportedFeatures := parseSupportedFeatures(*flags.SupportedFeatures)
56-
exemptFeatures := parseSupportedFeatures(*flags.ExemptFeatures)
53+
supportedFeatures := suite.ParseSupportedFeatures(*flags.SupportedFeatures)
54+
exemptFeatures := suite.ParseSupportedFeatures(*flags.ExemptFeatures)
55+
56+
namespaceLabels := suite.ParseNamespaceLabels(*flags.NamespaceLabels)
5757

5858
t.Logf("Running conformance tests with %s GatewayClass\n cleanup: %t\n debug: %t\n enable all features: %t \n supported features: [%v]\n exempt features: [%v]",
5959
*flags.GatewayClassName, *flags.CleanupBaseResources, *flags.ShowDebug, *flags.EnableAllSupportedFeatures, *flags.SupportedFeatures, *flags.ExemptFeatures)
@@ -68,21 +68,9 @@ func TestConformance(t *testing.T) {
6868
SupportedFeatures: supportedFeatures,
6969
ExemptFeatures: exemptFeatures,
7070
EnableAllSupportedFeatures: *flags.EnableAllSupportedFeatures,
71+
NamespaceLabels: namespaceLabels,
7172
})
7273
cSuite.Setup(t)
7374

7475
cSuite.Run(t, tests.ConformanceTests)
7576
}
76-
77-
// parseSupportedFeatures parses flag arguments and converts the string to
78-
// sets.Set[suite.SupportedFeature]
79-
func parseSupportedFeatures(f string) sets.Set[suite.SupportedFeature] {
80-
if f == "" {
81-
return nil
82-
}
83-
res := sets.Set[suite.SupportedFeature]{}
84-
for _, value := range strings.Split(f, ",") {
85-
res.Insert(suite.SupportedFeature(value))
86-
}
87-
return res
88-
}

conformance/mesh/manifests.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ kind: Namespace
44
metadata:
55
name: gateway-conformance-mesh
66
labels:
7-
gateway-conformance: infra
7+
gateway-conformance: mesh
88
---
99

1010
apiVersion: apps/v1

conformance/utils/flags/flags.go

+1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ var (
3030
SupportedFeatures = flag.String("supported-features", "", "Supported features included in conformance tests suites")
3131
ExemptFeatures = flag.String("exempt-features", "", "Exempt Features excluded from conformance tests suites")
3232
EnableAllSupportedFeatures = flag.Bool("all-features", false, "Whether to enable all supported features for conformance tests")
33+
NamespaceLabels = flag.String("namespace-labels", "", "Comma-separated list of name=value labels to add to test namespaces")
3334
)

conformance/utils/kubernetes/apply.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ func (a Applier) prepareGatewayClass(t *testing.T, uObj *unstructured.Unstructur
6565
}
6666

6767
// prepareNamespace adjusts the Namespace labels.
68-
func prepareNamespace(t *testing.T, uObj *unstructured.Unstructured, namespaceLabels map[string]string) {
68+
func (a Applier) prepareNamespace(t *testing.T, uObj *unstructured.Unstructured) {
6969
labels, _, err := unstructured.NestedStringMap(uObj.Object, "metadata", "labels")
7070
require.NoErrorf(t, err, "error getting labels on Namespace %s", uObj.GetName())
7171

72-
for k, v := range namespaceLabels {
72+
for k, v := range a.NamespaceLabels {
7373
if labels == nil {
7474
labels = map[string]string{}
7575
}
@@ -109,7 +109,7 @@ func (a Applier) prepareResources(t *testing.T, decoder *yaml.YAMLOrJSONDecoder)
109109
}
110110

111111
if uObj.GetKind() == "Namespace" && uObj.GetObjectKind().GroupVersionKind().Group == "" {
112-
prepareNamespace(t, &uObj, a.NamespaceLabels)
112+
a.prepareNamespace(t, &uObj)
113113
}
114114

115115
resources = append(resources, uObj)

conformance/utils/suite/suite.go

+30
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package suite
1818

1919
import (
2020
"embed"
21+
"strings"
2122
"testing"
2223

2324
"k8s.io/apimachinery/pkg/util/sets"
@@ -224,3 +225,32 @@ func (test *ConformanceTest) Run(t *testing.T, suite *ConformanceTestSuite) {
224225

225226
test.Test(t, suite)
226227
}
228+
229+
// ParseSupportedFeatures parses flag arguments and converts the string to
230+
// sets.Set[suite.SupportedFeature]
231+
func ParseSupportedFeatures(f string) sets.Set[SupportedFeature] {
232+
if f == "" {
233+
return nil
234+
}
235+
res := sets.Set[SupportedFeature]{}
236+
for _, value := range strings.Split(f, ",") {
237+
res.Insert(SupportedFeature(value))
238+
}
239+
return res
240+
}
241+
242+
// ParseNamespaceLables parses flag arguments and converts the string to
243+
// map[string]string containing label key/value pairs.
244+
func ParseNamespaceLabels(f string) map[string]string {
245+
if f == "" {
246+
return nil
247+
}
248+
res := map[string]string{}
249+
for _, kv := range strings.Split(f, ",") {
250+
parts := strings.Split(kv, "=")
251+
if len(parts) == 2 {
252+
res[parts[0]] = parts[1]
253+
}
254+
}
255+
return res
256+
}

conformance/utils/suite/suite_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package suite
18+
19+
import (
20+
"reflect"
21+
"testing"
22+
23+
"k8s.io/apimachinery/pkg/util/sets"
24+
)
25+
26+
func TestParseSupportedFeatures(t *testing.T) {
27+
flags := []string{
28+
"",
29+
"a",
30+
"b,c,d",
31+
}
32+
33+
s1 := sets.Set[SupportedFeature]{}
34+
s1.Insert(SupportedFeature("a"))
35+
s2 := sets.Set[SupportedFeature]{}
36+
s2.Insert(SupportedFeature("b"))
37+
s2.Insert(SupportedFeature("c"))
38+
s2.Insert(SupportedFeature("d"))
39+
features := []sets.Set[SupportedFeature]{nil, s1, s2}
40+
41+
for i, f := range flags {
42+
expect := features[i]
43+
got := ParseSupportedFeatures(f)
44+
if !reflect.DeepEqual(got, expect) {
45+
t.Errorf("Unexpected features from flags '%s', expected: %v, got: %v", f, expect, got)
46+
}
47+
}
48+
}
49+
50+
func TestParseNamespaceLabels(t *testing.T) {
51+
flags := []string{
52+
"",
53+
"a=b",
54+
"b=c,d=e,f=g",
55+
}
56+
labels := []map[string]string{
57+
nil,
58+
{"a": "b"},
59+
{"b": "c", "d": "e", "f": "g"},
60+
}
61+
62+
for i, f := range flags {
63+
expect := labels[i]
64+
got := ParseNamespaceLabels(f)
65+
if !reflect.DeepEqual(got, expect) {
66+
t.Errorf("Unexpected labels from flags '%s', expected: %v, got: %v", f, expect, got)
67+
}
68+
}
69+
}

site-src/concepts/conformance.md

+8
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ feature and any relevant API features, e.g:
126126
go test ./conformance/... -args -supported-features=Mesh,Gateway,HTTPRoute
127127
```
128128

129+
#### Namespace Labels
130+
131+
If labels are needed on namespaces used for testing, you can use
132+
the `-namespace-labels` flag to pass one or more `name=value` labels to
133+
set on the test namespaces. For mesh testing, this flag can be used if
134+
an implementation requires labels on namespaces that host mesh workloads,
135+
for example, to enable sidecar injection.
136+
129137
#### Excluding Tests
130138

131139
The `Gateway` and `ReferenceGrant` features are enabled by default.

0 commit comments

Comments
 (0)