Skip to content

Commit

Permalink
Add namespace alias for string for validation purpose
Browse files Browse the repository at this point in the history
Signed-off-by: Lubron Zhan <lubronzhan@gmail.com>
  • Loading branch information
lubronzhan committed Jan 26, 2024
1 parent 4455855 commit c8666ad
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 14 deletions.
24 changes: 22 additions & 2 deletions apis/projectcontour/v1alpha1/contourdeployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ type ContourDeploymentSpec struct {
ResourceLabels map[string]string `json:"resourceLabels,omitempty"`
}

// Namespace refers to a Kubernetes namespace. It must be a RFC 1123 label.
//
// This validation is based off of the corresponding Kubernetes validation:
// https://github.com/kubernetes/apimachinery/blob/02cfb53916346d085a6c6c7c66f882e3c6b0eca6/pkg/util/validation/validation.go#L187
//
// This is used for Namespace name validation here:
// https://github.com/kubernetes/apimachinery/blob/02cfb53916346d085a6c6c7c66f882e3c6b0eca6/pkg/api/validation/generic.go#L63
//
// Valid values include:
//
// * "example"
//
// Invalid values include:
//
// * "example.com" - "." is an invalid character
//
// +kubebuilder:validation:Pattern=`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=63
type Namespace string

// ContourSettings contains settings for the Contour part of the installation,
// i.e. the xDS server/control plane and associated resources.
type ContourSettings struct {
Expand Down Expand Up @@ -127,10 +148,9 @@ type ContourSettings struct {
// to only watch this subset of namespaces.
// +optional
// +kubebuilder:validation:Type=array
// +kubebuilder:validation:Items=string
// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:MaxItems=42
WatchNamespaces []string `json:"watchNamespaces,omitempty"`
WatchNamespaces []Namespace `json:"watchNamespaces,omitempty"`
}

// DeploymentSettings contains settings for Deployment resources.
Expand Down
22 changes: 22 additions & 0 deletions apis/projectcontour/v1alpha1/contourdeployment_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright Project Contour Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v1alpha1

func NamespacesToStrings(ns []Namespace) []string {
res := make([]string, len(ns))
for i, n := range ns {
res[i] = string(n)
}
return res
}
46 changes: 46 additions & 0 deletions apis/projectcontour/v1alpha1/contourdeployment_helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright Project Contour Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v1alpha1

import (
"reflect"
"testing"
)

func TestDesiredRoleBindingInNamespace(t *testing.T) {
testCases := []struct {
description string
namespaces []Namespace
expectStrings []string
}{
{
description: "namespace 1",
namespaces: []Namespace{},
expectStrings: []string{},
},
{
description: "namespace 2",
namespaces: []Namespace{"ns1", "ns2"},
expectStrings: []string{"ns1", "ns2"},
},
}

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
if !reflect.DeepEqual(NamespacesToStrings(tc.namespaces), tc.expectStrings) {
t.Errorf("expect converted strings %v is the same as %v", NamespacesToStrings(tc.namespaces), tc.expectStrings)
}
})
}
}
2 changes: 1 addition & 1 deletion apis/projectcontour/v1alpha1/zz_generated.deepcopy.go

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

13 changes: 13 additions & 0 deletions examples/contour/01-crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,19 @@ spec:
WatchNamespaces is an array of namespaces. Setting it will instruct the contour instance
to only watch this subset of namespaces.
items:
description: |-
Namespace refers to a Kubernetes namespace. It must be a RFC 1123 label.
This validation is based off of the corresponding Kubernetes validation:
https://github.com/kubernetes/apimachinery/blob/02cfb53916346d085a6c6c7c66f882e3c6b0eca6/pkg/util/validation/validation.go#L187
This is used for Namespace name validation here:
https://github.com/kubernetes/apimachinery/blob/02cfb53916346d085a6c6c7c66f882e3c6b0eca6/pkg/api/validation/generic.go#L63
Valid values include:
* "example"
Invalid values include:
* "example.com" - "." is an invalid character
maxLength: 63
minLength: 1
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
type: string
maxItems: 42
minItems: 1
Expand Down
13 changes: 13 additions & 0 deletions examples/render/contour-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,19 @@ spec:
WatchNamespaces is an array of namespaces. Setting it will instruct the contour instance
to only watch this subset of namespaces.
items:
description: |-
Namespace refers to a Kubernetes namespace. It must be a RFC 1123 label.
This validation is based off of the corresponding Kubernetes validation:
https://github.com/kubernetes/apimachinery/blob/02cfb53916346d085a6c6c7c66f882e3c6b0eca6/pkg/util/validation/validation.go#L187
This is used for Namespace name validation here:
https://github.com/kubernetes/apimachinery/blob/02cfb53916346d085a6c6c7c66f882e3c6b0eca6/pkg/api/validation/generic.go#L63
Valid values include:
* "example"
Invalid values include:
* "example.com" - "." is an invalid character
maxLength: 63
minLength: 1
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
type: string
maxItems: 42
minItems: 1
Expand Down
13 changes: 13 additions & 0 deletions examples/render/contour-gateway-provisioner.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1636,6 +1636,19 @@ spec:
WatchNamespaces is an array of namespaces. Setting it will instruct the contour instance
to only watch this subset of namespaces.
items:
description: |-
Namespace refers to a Kubernetes namespace. It must be a RFC 1123 label.
This validation is based off of the corresponding Kubernetes validation:
https://github.com/kubernetes/apimachinery/blob/02cfb53916346d085a6c6c7c66f882e3c6b0eca6/pkg/util/validation/validation.go#L187
This is used for Namespace name validation here:
https://github.com/kubernetes/apimachinery/blob/02cfb53916346d085a6c6c7c66f882e3c6b0eca6/pkg/api/validation/generic.go#L63
Valid values include:
* "example"
Invalid values include:
* "example.com" - "." is an invalid character
maxLength: 63
minLength: 1
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
type: string
maxItems: 42
minItems: 1
Expand Down
13 changes: 13 additions & 0 deletions examples/render/contour-gateway.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,19 @@ spec:
WatchNamespaces is an array of namespaces. Setting it will instruct the contour instance
to only watch this subset of namespaces.
items:
description: |-
Namespace refers to a Kubernetes namespace. It must be a RFC 1123 label.
This validation is based off of the corresponding Kubernetes validation:
https://github.com/kubernetes/apimachinery/blob/02cfb53916346d085a6c6c7c66f882e3c6b0eca6/pkg/util/validation/validation.go#L187
This is used for Namespace name validation here:
https://github.com/kubernetes/apimachinery/blob/02cfb53916346d085a6c6c7c66f882e3c6b0eca6/pkg/api/validation/generic.go#L63
Valid values include:
* "example"
Invalid values include:
* "example.com" - "." is an invalid character
maxLength: 63
minLength: 1
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
type: string
maxItems: 42
minItems: 1
Expand Down
13 changes: 13 additions & 0 deletions examples/render/contour.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,19 @@ spec:
WatchNamespaces is an array of namespaces. Setting it will instruct the contour instance
to only watch this subset of namespaces.
items:
description: |-
Namespace refers to a Kubernetes namespace. It must be a RFC 1123 label.
This validation is based off of the corresponding Kubernetes validation:
https://github.com/kubernetes/apimachinery/blob/02cfb53916346d085a6c6c7c66f882e3c6b0eca6/pkg/util/validation/validation.go#L187
This is used for Namespace name validation here:
https://github.com/kubernetes/apimachinery/blob/02cfb53916346d085a6c6c7c66f882e3c6b0eca6/pkg/api/validation/generic.go#L63
Valid values include:
* "example"
Invalid values include:
* "example.com" - "." is an invalid character
maxLength: 63
minLength: 1
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
type: string
maxItems: 42
minItems: 1
Expand Down
2 changes: 1 addition & 1 deletion internal/provisioner/controller/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (r *gatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

contourModel.Spec.KubernetesLogLevel = contourParams.KubernetesLogLevel

contourModel.Spec.WatchNamespaces = contourParams.WatchNamespaces
contourModel.Spec.WatchNamespaces = contour_api_v1alpha1.NamespacesToStrings(contourParams.WatchNamespaces)

if contourParams.Deployment != nil &&
contourParams.Deployment.Strategy != nil {
Expand Down
7 changes: 1 addition & 6 deletions internal/provisioner/objects/deployment/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,9 @@ func TestDesiredDeploymentWhenSettingWatchNamespaces(t *testing.T) {
},
{
description: "single valid namespace",
namespaces: []string{"ns1", "ns2"},
namespaces: []string{"ns1"},
expectArgExist: true,
},
{
description: "include empty namespace",
namespaces: []string{"ns1", ""},
expectArgExist: false,
},
}

for _, tc := range testCases {
Expand Down
25 changes: 24 additions & 1 deletion site/content/docs/main/config/api-reference.html
Original file line number Diff line number Diff line change
Expand Up @@ -6252,7 +6252,9 @@ <h3 id="projectcontour.io/v1alpha1.ContourSettings">ContourSettings
<code>watchNamespaces</code>
<br>
<em>
[]string
<a href="#projectcontour.io/v1alpha1.Namespace">
[]Namespace
</a>
</em>
</td>
<td>
Expand Down Expand Up @@ -8094,6 +8096,27 @@ <h3 id="projectcontour.io/v1alpha1.MetricsTLS">MetricsTLS
</tr>
</tbody>
</table>
<h3 id="projectcontour.io/v1alpha1.Namespace">Namespace
(<code>string</code> alias)</p></h3>
<p>
(<em>Appears on:</em>
<a href="#projectcontour.io/v1alpha1.ContourSettings">ContourSettings</a>)
</p>
<p>
<p>Namespace refers to a Kubernetes namespace. It must be a RFC 1123 label.</p>
<p>This validation is based off of the corresponding Kubernetes validation:
<a href="https://github.com/kubernetes/apimachinery/blob/02cfb53916346d085a6c6c7c66f882e3c6b0eca6/pkg/util/validation/validation.go#L187">https://github.com/kubernetes/apimachinery/blob/02cfb53916346d085a6c6c7c66f882e3c6b0eca6/pkg/util/validation/validation.go#L187</a></p>
<p>This is used for Namespace name validation here:
<a href="https://github.com/kubernetes/apimachinery/blob/02cfb53916346d085a6c6c7c66f882e3c6b0eca6/pkg/api/validation/generic.go#L63">https://github.com/kubernetes/apimachinery/blob/02cfb53916346d085a6c6c7c66f882e3c6b0eca6/pkg/api/validation/generic.go#L63</a></p>
<p>Valid values include:</p>
<ul>
<li>&ldquo;example&rdquo;</li>
</ul>
<p>Invalid values include:</p>
<ul>
<li>&ldquo;example.com&rdquo; - &ldquo;.&rdquo; is an invalid character</li>
</ul>
</p>
<h3 id="projectcontour.io/v1alpha1.NamespacedName">NamespacedName
</h3>
<p>
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/provisioner/provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ var _ = Describe("Gateway provisioner", func() {
Spec: contour_api_v1alpha1.ContourDeploymentSpec{
RuntimeSettings: contourDeploymentRuntimeSettings(),
Contour: &contour_api_v1alpha1.ContourSettings{
WatchNamespaces: []string{"testns-1", "testns-2"},
WatchNamespaces: []contour_api_v1alpha1.Namespace{"testns-1", "testns-2"},
},
},
}
Expand Down Expand Up @@ -572,7 +572,7 @@ var _ = Describe("Gateway provisioner", func() {
gateway, ok := f.CreateGatewayAndWaitFor(gateway, func(gw *gatewayapi_v1beta1.Gateway) bool {
return e2e.GatewayProgrammed(gw) && e2e.GatewayHasAddress(gw)
})
require.True(f.T(), ok)
require.True(f.T(), ok, fmt.Sprintf("gateway is %v", gateway))
type testObj struct {
expectReconcile bool
namespace string
Expand Down Expand Up @@ -626,7 +626,7 @@ var _ = Describe("Gateway provisioner", func() {
By(fmt.Sprintf("Expect namespace %s to be watched by contour", t.namespace))
hr, ok := f.CreateHTTPRouteAndWaitFor(route, e2e.HTTPRouteAccepted)
By(fmt.Sprintf("Expect httproute under namespace %s is accepted", t.namespace))
require.True(f.T(), ok, fmt.Sprintf("httproute's is %v", hr))
require.True(f.T(), ok, fmt.Sprintf("httproute is %v", hr))
res, ok := f.HTTP.RequestUntil(&e2e.HTTPRequestOpts{
OverrideURL: "http://" + net.JoinHostPort(gateway.Status.Addresses[0].Value, "80"),
Host: string(route.Spec.Hostnames[0]),
Expand Down

0 comments on commit c8666ad

Please sign in to comment.