Skip to content

Commit 4b62950

Browse files
committed
feat: make context ref namespace optional
From now on the contest ref namespace will default to the resource namespace if not specified. see https://gravitee.atlassian.net/browse/GKO-320
1 parent 6c16b00 commit 4b62950

12 files changed

+275
-10
lines changed

api/model/refs/namespace.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,46 @@ import (
2020
"k8s.io/apimachinery/pkg/types"
2121
)
2222

23-
var _ custom.ResourceRef = NamespacedName{}
23+
var _ custom.ResourceRef = &NamespacedName{}
2424

2525
type NamespacedName struct {
2626
Name string `json:"name"`
2727
Namespace string `json:"namespace,omitempty"`
2828
}
2929

30+
// SetNamespace implements custom.ResourceRef.
31+
func (n *NamespacedName) SetNamespace(ns string) {
32+
n.Namespace = ns
33+
}
34+
35+
// IsMissingNamespace implements custom.ResourceRef.
36+
func (n *NamespacedName) IsMissingNamespace() bool {
37+
return !n.HasNameSpace()
38+
}
39+
40+
// GetName implements custom.ResourceRef.
41+
func (n *NamespacedName) GetName() string {
42+
return n.Name
43+
}
44+
45+
// GetNamespace implements custom.ResourceRef.
46+
func (n *NamespacedName) GetNamespace() string {
47+
return n.Namespace
48+
}
49+
50+
// HasNameSpace implements custom.ResourceRef.
51+
func (n *NamespacedName) HasNameSpace() bool {
52+
return n.Namespace != ""
53+
}
54+
3055
func NewNamespacedName(namespace, name string) NamespacedName {
3156
return NamespacedName{Namespace: namespace, Name: name}
3257
}
3358

34-
func (n NamespacedName) NamespacedName() types.NamespacedName {
59+
func (n *NamespacedName) NamespacedName() types.NamespacedName {
3560
return types.NamespacedName{Namespace: n.Namespace, Name: n.Name}
3661
}
3762

38-
func (n NamespacedName) String() string {
63+
func (n *NamespacedName) String() string {
3964
return n.Namespace + "/" + n.Name
4065
}

api/v1alpha1/apiv2definition_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ func (api *ApiDefinition) GetOrGenerateCrossID() string {
133133
return uuid.FromStrings(api.GetNamespacedName().String())
134134
}
135135

136-
func (api *ApiDefinition) GetNamespacedName() refs.NamespacedName {
137-
return refs.NamespacedName{Namespace: api.Namespace, Name: api.Name}
136+
func (api *ApiDefinition) GetNamespacedName() *refs.NamespacedName {
137+
return &refs.NamespacedName{Namespace: api.Namespace, Name: api.Name}
138138
}
139139

140140
func (spec *ApiDefinitionV2Spec) SetDefinitionContext() {

api/v1alpha1/apiv2definition_webhook.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package v1alpha1
1818

1919
import (
20+
commonMutate "github.com/gravitee-io/gravitee-kubernetes-operator/internal/admission/common/mutate"
2021
runtime "k8s.io/apimachinery/pkg/runtime"
2122
ctrl "sigs.k8s.io/controller-runtime"
2223
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -32,7 +33,9 @@ func (api *ApiDefinition) SetupWebhookWithManager(mgr ctrl.Manager) error {
3233
Complete()
3334
}
3435

35-
func (api *ApiDefinition) Default() {}
36+
func (api *ApiDefinition) Default() {
37+
commonMutate.SetDefaults(api)
38+
}
3639

3740
func (api *ApiDefinition) ValidateCreate() (admission.Warnings, error) {
3841
return admission.Warnings{}, nil

api/v1alpha1/apiv4definition_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ func (api *ApiV4Definition) Version() custom.ApiDefinitionVersion {
214214
return custom.ApiV4
215215
}
216216

217-
func (api *ApiV4Definition) GetNamespacedName() refs.NamespacedName {
218-
return refs.NamespacedName{Namespace: api.Namespace, Name: api.Name}
217+
func (api *ApiV4Definition) GetNamespacedName() *refs.NamespacedName {
218+
return &refs.NamespacedName{Namespace: api.Namespace, Name: api.Name}
219219
}
220220

221221
func (api *ApiV4Definition) GetObjectMeta() *metav1.ObjectMeta {

api/v1alpha1/apiv4definition_webhook.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"sigs.k8s.io/controller-runtime/pkg/client"
2828

2929
v4 "github.com/gravitee-io/gravitee-kubernetes-operator/api/model/api/v4"
30+
commonMutate "github.com/gravitee-io/gravitee-kubernetes-operator/internal/admission/common/mutate"
3031
"github.com/gravitee-io/gravitee-kubernetes-operator/internal/k8s"
3132
runtime "k8s.io/apimachinery/pkg/runtime"
3233
ctrl "sigs.k8s.io/controller-runtime"
@@ -43,7 +44,9 @@ func (api *ApiV4Definition) SetupWebhookWithManager(mgr ctrl.Manager) error {
4344
Complete()
4445
}
4546

46-
func (api *ApiV4Definition) Default() {}
47+
func (api *ApiV4Definition) Default() {
48+
commonMutate.SetDefaults(api)
49+
}
4750

4851
func (api *ApiV4Definition) ValidateCreate() (admission.Warnings, error) {
4952
return validateApi(api)

api/v1alpha1/application_types.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,20 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20+
"fmt"
21+
2022
"github.com/gravitee-io/gravitee-kubernetes-operator/api/model/application"
2123
"github.com/gravitee-io/gravitee-kubernetes-operator/api/model/refs"
24+
"github.com/gravitee-io/gravitee-kubernetes-operator/internal/hash"
2225
"github.com/gravitee-io/gravitee-kubernetes-operator/pkg/types/k8s/custom"
2326
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"sigs.k8s.io/controller-runtime/pkg/client"
2428
)
2529

30+
var _ custom.ContextAwareResource = &Application{}
31+
var _ custom.Spec = &ApplicationSpec{}
32+
var _ custom.Status = &ApplicationStatus{}
33+
2634
// Application is the main resource handled by the Kubernetes Operator
2735
// +kubebuilder:object:generate=true
2836
type ApplicationSpec struct {
@@ -57,6 +65,16 @@ type Application struct {
5765
Status ApplicationStatus `json:"status,omitempty"`
5866
}
5967

68+
// GetSpec implements custom.Resource.
69+
func (app *Application) GetSpec() custom.Spec {
70+
return &app.Spec
71+
}
72+
73+
// GetStatus implements custom.Resource.
74+
func (app *Application) GetStatus() custom.Status {
75+
return &app.Status
76+
}
77+
6078
// +kubebuilder:object:root=true
6179
type ApplicationList struct {
6280
metav1.TypeMeta `json:",inline"`
@@ -71,3 +89,53 @@ func (app *Application) IsBeingDeleted() bool {
7189
func init() {
7290
SchemeBuilder.Register(&Application{}, &ApplicationList{})
7391
}
92+
93+
func (app *Application) ContextRef() custom.ResourceRef {
94+
return app.Spec.Context
95+
}
96+
97+
func (app *Application) HasContext() bool {
98+
return app.Spec.Context != nil
99+
}
100+
101+
func (app *Application) ID() string {
102+
return app.Status.ID
103+
}
104+
105+
func (app *Application) DeepCopyResource() custom.Resource {
106+
return app.DeepCopy()
107+
}
108+
109+
func (spec *ApplicationSpec) Hash() string {
110+
return hash.Calculate(spec)
111+
}
112+
113+
func (s *ApplicationStatus) DeepCopyFrom(obj client.Object) error {
114+
switch t := obj.(type) {
115+
case *Application:
116+
t.Status.DeepCopyInto(s)
117+
default:
118+
return fmt.Errorf("unknown type %T", t)
119+
}
120+
121+
return nil
122+
}
123+
124+
func (s *ApplicationStatus) DeepCopyTo(api client.Object) error {
125+
switch t := api.(type) {
126+
case *Application:
127+
s.DeepCopyInto(&t.Status)
128+
default:
129+
return fmt.Errorf("unknown type %T", t)
130+
}
131+
132+
return nil
133+
}
134+
135+
func (s *ApplicationStatus) SetObservedGeneration(g int64) {
136+
s.ObservedGeneration = g
137+
}
138+
139+
func (s *ApplicationStatus) SetProcessingStatus(status custom.ProcessingStatus) {
140+
s.Status = status
141+
}

api/v1alpha1/application_webhook.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package v1alpha1
1818

1919
import (
20+
commonMutate "github.com/gravitee-io/gravitee-kubernetes-operator/internal/admission/common/mutate"
2021
runtime "k8s.io/apimachinery/pkg/runtime"
2122
ctrl "sigs.k8s.io/controller-runtime"
2223
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -32,7 +33,9 @@ func (app *Application) SetupWebhookWithManager(mgr ctrl.Manager) error {
3233
Complete()
3334
}
3435

35-
func (app *Application) Default() {}
36+
func (app *Application) Default() {
37+
commonMutate.SetDefaults(app)
38+
}
3639

3740
func (app *Application) ValidateCreate() (admission.Warnings, error) {
3841
return admission.Warnings{}, nil
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (C) 2015 The Gravitee team (http://gravitee.io)
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package mutate
16+
17+
import "github.com/gravitee-io/gravitee-kubernetes-operator/pkg/types/k8s/custom"
18+
19+
func SetDefaults(ctxAware custom.ContextAwareResource) {
20+
if ctxAware.HasContext() && ctxAware.ContextRef().IsMissingNamespace() {
21+
ctxAware.ContextRef().SetNamespace(ctxAware.GetNamespace())
22+
}
23+
}

pkg/types/k8s/custom/custom.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@ type ContextAwareResource interface {
7676
type ResourceRef interface {
7777
fmt.Stringer
7878
NamespacedName() types.NamespacedName
79+
GetName() string
80+
GetNamespace() string
81+
HasNameSpace() bool
82+
IsMissingNamespace() bool
83+
SetNamespace(ns string)
7984
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (C) 2015 The Gravitee team (http://gravitee.io)
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package admissionwebhook
16+
17+
import (
18+
"github.com/gravitee-io/gravitee-kubernetes-operator/test/internal/integration/constants"
19+
"github.com/gravitee-io/gravitee-kubernetes-operator/test/internal/integration/fixture"
20+
"github.com/gravitee-io/gravitee-kubernetes-operator/test/internal/integration/labels"
21+
. "github.com/onsi/ginkgo/v2"
22+
. "github.com/onsi/gomega"
23+
)
24+
25+
var _ = Describe("Mutate", labels.WithContext, func() {
26+
It("should set context namespace to api namespace if empty", func() {
27+
fixtures := fixture.
28+
Builder().
29+
WithAPI(constants.ApiWithContextFile).
30+
WithContext(constants.ContextWithCredentialsFile).
31+
Build()
32+
33+
By("removing namespace from context reference")
34+
35+
fixtures.API.Spec.Context.Namespace = ""
36+
37+
By("applying defaults")
38+
39+
Expect(fixtures.API.Namespace).ToNot(BeEmpty())
40+
41+
fixtures.API.Default()
42+
43+
Expect(fixtures.API.Spec.Context.Namespace).To(Equal(fixtures.API.Namespace))
44+
})
45+
})

0 commit comments

Comments
 (0)