Skip to content

Commit 94941a0

Browse files
committed
Allow -namespace-annotations to annotate namespaces created by the tests
Signed-off-by: [email protected]
1 parent ce5bf58 commit 94941a0

File tree

6 files changed

+64
-21
lines changed

6 files changed

+64
-21
lines changed

conformance/conformance_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func TestConformance(t *testing.T) {
5353
exemptFeatures := suite.ParseSupportedFeatures(*flags.ExemptFeatures)
5454
skipTests := suite.ParseSkipTests(*flags.SkipTests)
5555
namespaceLabels := suite.ParseNamespaceLabels(*flags.NamespaceLabels)
56+
namespaceAnnotations := suite.ParseNamespaceAnnotations(*flags.NamespaceAnnotations)
5657

5758
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]",
5859
*flags.GatewayClassName, *flags.CleanupBaseResources, *flags.ShowDebug, *flags.EnableAllSupportedFeatures, *flags.SupportedFeatures, *flags.ExemptFeatures)
@@ -70,6 +71,7 @@ func TestConformance(t *testing.T) {
7071
ExemptFeatures: exemptFeatures,
7172
EnableAllSupportedFeatures: *flags.EnableAllSupportedFeatures,
7273
NamespaceLabels: namespaceLabels,
74+
NamespaceAnnotations: namespaceAnnotations,
7375
SkipTests: skipTests,
7476
})
7577
cSuite.Setup(t)

conformance/experimental_conformance_test.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,16 @@ import (
3939
)
4040

4141
var (
42-
cfg *rest.Config
43-
k8sClientset *kubernetes.Clientset
44-
mgrClient client.Client
45-
supportedFeatures sets.Set[suite.SupportedFeature]
46-
exemptFeatures sets.Set[suite.SupportedFeature]
47-
namespaceLabels map[string]string
48-
implementation *confv1a1.Implementation
49-
conformanceProfiles sets.Set[suite.ConformanceProfileName]
50-
skipTests []string
42+
cfg *rest.Config
43+
k8sClientset *kubernetes.Clientset
44+
mgrClient client.Client
45+
supportedFeatures sets.Set[suite.SupportedFeature]
46+
exemptFeatures sets.Set[suite.SupportedFeature]
47+
namespaceLabels map[string]string
48+
namespaceAnnotations map[string]string
49+
implementation *confv1a1.Implementation
50+
conformanceProfiles sets.Set[suite.ConformanceProfileName]
51+
skipTests []string
5152
)
5253

5354
func TestExperimentalConformance(t *testing.T) {
@@ -73,6 +74,7 @@ func TestExperimentalConformance(t *testing.T) {
7374
exemptFeatures = suite.ParseSupportedFeatures(*flags.ExemptFeatures)
7475
skipTests = suite.ParseSkipTests(*flags.SkipTests)
7576
namespaceLabels = suite.ParseNamespaceLabels(*flags.NamespaceLabels)
77+
namespaceAnnotations = suite.ParseNamespaceAnnotations(*flags.NamespaceAnnotations)
7678

7779
// experimental conformance flags
7880
conformanceProfiles = suite.ParseConformanceProfiles(*flags.ConformanceProfiles)
@@ -115,6 +117,7 @@ func testExperimentalConformance(t *testing.T) {
115117
ExemptFeatures: exemptFeatures,
116118
EnableAllSupportedFeatures: *flags.EnableAllSupportedFeatures,
117119
NamespaceLabels: namespaceLabels,
120+
NamespaceAnnotations: namespaceAnnotations,
118121
SkipTests: skipTests,
119122
},
120123
Implementation: *implementation,

conformance/utils/flags/flags.go

+1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ var (
3232
ExemptFeatures = flag.String("exempt-features", "", "Exempt Features excluded from conformance tests suites")
3333
EnableAllSupportedFeatures = flag.Bool("all-features", false, "Whether to enable all supported features for conformance tests")
3434
NamespaceLabels = flag.String("namespace-labels", "", "Comma-separated list of name=value labels to add to test namespaces")
35+
NamespaceAnnotations = flag.String("namespace-annotations", "", "Comma-separated list of name=value annotations to add to test namespaces")
3536
)

conformance/utils/kubernetes/apply.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ import (
4040
// Applier prepares manifests depending on the available options and applies
4141
// them to the Kubernetes cluster.
4242
type Applier struct {
43-
NamespaceLabels map[string]string
43+
NamespaceLabels map[string]string
44+
NamespaceAnnotations map[string]string
4445

4546
// GatewayClass will be used as the spec.gatewayClassName when applying Gateway resources
4647
GatewayClass string
@@ -82,6 +83,23 @@ func (a Applier) prepareNamespace(t *testing.T, uObj *unstructured.Unstructured)
8283
err = unstructured.SetNestedStringMap(uObj.Object, labels, "metadata", "labels")
8384
}
8485
require.NoErrorf(t, err, "error setting labels on Namespace %s", uObj.GetName())
86+
87+
annotations, _, err := unstructured.NestedStringMap(uObj.Object, "metadata", "annotations")
88+
require.NoErrorf(t, err, "error getting annotations on Namespace %s", uObj.GetName())
89+
90+
for k, v := range a.NamespaceAnnotations {
91+
if annotations == nil {
92+
annotations = map[string]string{}
93+
}
94+
95+
annotations[k] = v
96+
}
97+
98+
// SetNestedStringMap converts nil to an empty map
99+
if annotations != nil {
100+
err = unstructured.SetNestedStringMap(uObj.Object, annotations, "metadata", "annotations")
101+
}
102+
require.NoErrorf(t, err, "error setting annotations on Namespace %s", uObj.GetName())
85103
}
86104

87105
// prepareResources uses the options from an Applier to tweak resources given by

conformance/utils/suite/experimental_suite.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ func NewExperimentalConformanceTestSuite(s ExperimentalConformanceOptions) (*Exp
159159
BaseManifests: s.BaseManifests,
160160
MeshManifests: s.MeshManifests,
161161
Applier: kubernetes.Applier{
162-
NamespaceLabels: s.NamespaceLabels,
162+
NamespaceLabels: s.NamespaceLabels,
163+
NamespaceAnnotations: s.NamespaceAnnotations,
163164
},
164165
SupportedFeatures: s.SupportedFeatures,
165166
TimeoutConfig: s.TimeoutConfig,

conformance/utils/suite/suite.go

+28-10
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,16 @@ type ConformanceTestSuite struct {
5555

5656
// Options can be used to initialize a ConformanceTestSuite.
5757
type Options struct {
58-
Client client.Client
59-
Clientset clientset.Interface
60-
RestConfig *rest.Config
61-
GatewayClassName string
62-
Debug bool
63-
RoundTripper roundtripper.RoundTripper
64-
BaseManifests string
65-
MeshManifests string
66-
NamespaceLabels map[string]string
58+
Client client.Client
59+
Clientset clientset.Interface
60+
RestConfig *rest.Config
61+
GatewayClassName string
62+
Debug bool
63+
RoundTripper roundtripper.RoundTripper
64+
BaseManifests string
65+
MeshManifests string
66+
NamespaceLabels map[string]string
67+
NamespaceAnnotations map[string]string
6768

6869
// CleanupBaseResources indicates whether or not the base test
6970
// resources such as Gateways should be cleaned up after the run.
@@ -118,7 +119,8 @@ func New(s Options) *ConformanceTestSuite {
118119
BaseManifests: s.BaseManifests,
119120
MeshManifests: s.MeshManifests,
120121
Applier: kubernetes.Applier{
121-
NamespaceLabels: s.NamespaceLabels,
122+
NamespaceLabels: s.NamespaceLabels,
123+
NamespaceAnnotations: s.NamespaceAnnotations,
122124
},
123125
SupportedFeatures: s.SupportedFeatures,
124126
TimeoutConfig: s.TimeoutConfig,
@@ -261,6 +263,22 @@ func ParseNamespaceLabels(f string) map[string]string {
261263
return res
262264
}
263265

266+
// ParseNamespaceAnnotations parses flag arguments and converts the string to
267+
// map[string]string containing annotation key/value pairs.
268+
func ParseNamespaceAnnotations(f string) map[string]string {
269+
if f == "" {
270+
return nil
271+
}
272+
res := map[string]string{}
273+
for _, kv := range strings.Split(f, ",") {
274+
parts := strings.Split(kv, "=")
275+
if len(parts) == 2 {
276+
res[parts[0]] = parts[1]
277+
}
278+
}
279+
return res
280+
}
281+
264282
// ParseSkipTests parses flag arguments and converts the string to
265283
// []string containing the tests to be skipped.
266284
func ParseSkipTests(t string) []string {

0 commit comments

Comments
 (0)