Skip to content

Commit 38775ae

Browse files
committed
refactor: replace deprecated validator and defaulter interfaces
1 parent e70f3a2 commit 38775ae

File tree

69 files changed

+1467
-876
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1467
-876
lines changed

api/model/api/v2/api.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ package v2
1717

1818
import (
1919
"github.com/gravitee-io/gravitee-kubernetes-operator/api/model/api/base"
20+
"github.com/gravitee-io/gravitee-kubernetes-operator/pkg/types/k8s/custom"
2021
)
2122

23+
var _ custom.ApiDefinition = &Api{}
24+
2225
type Api struct {
2326
*base.ApiBase `json:",inline"`
2427
// +kubebuilder:validation:Required
@@ -69,6 +72,15 @@ type Api struct {
6972
ExecutionMode string `json:"execution_mode,omitempty"`
7073
}
7174

75+
func (api *Api) GetDefinitionVersion() custom.ApiDefinitionVersion {
76+
return custom.ApiV2
77+
}
78+
79+
// TODO implement when v2 admission handles paths
80+
func (api *Api) GetContextPaths() ([]string, error) {
81+
return make([]string, 0), nil
82+
}
83+
7284
const (
7385
ModeFullyManaged = "fully_managed"
7486
OriginKubernetes = "kubernetes"

api/model/api/v4/api.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
package v4
1717

1818
import (
19+
"fmt"
20+
"net/url"
21+
"strings"
22+
1923
"github.com/gravitee-io/gravitee-kubernetes-operator/api/model/api/base"
24+
"github.com/gravitee-io/gravitee-kubernetes-operator/pkg/types/k8s/custom"
2025
)
2126

2227
// +kubebuilder:validation:Enum=PROXY;MESSAGE;
@@ -25,6 +30,8 @@ type ApiType string
2530
// +kubebuilder:validation:Enum=PUBLISHED;UNPUBLISHED;
2631
type ApiV4LifecycleState string
2732

33+
var _ custom.ApiDefinition = &Api{}
34+
2835
type Api struct {
2936
*base.ApiBase `json:",inline"`
3037
// +kubebuilder:default:=`V4`
@@ -185,3 +192,45 @@ func (api *Api) getGatewayDefinitionEndpointGroups() []*EndpointGroup {
185192
}
186193
return endpointGroups
187194
}
195+
196+
func (api *Api) GetDefinitionVersion() custom.ApiDefinitionVersion {
197+
return custom.ApiV4
198+
}
199+
200+
func (api *Api) GetContextPaths() ([]string, error) {
201+
paths := make([]string, 0)
202+
for _, l := range api.Listeners {
203+
for _, s := range parseListener(l) {
204+
p, err := url.Parse(s)
205+
if err != nil {
206+
return paths, err
207+
}
208+
paths = append(paths, p.String())
209+
}
210+
}
211+
return paths, nil
212+
}
213+
214+
func parseListener(l Listener) []string {
215+
if l == nil {
216+
return []string{}
217+
}
218+
219+
switch t := l.(type) {
220+
case *GenericListener:
221+
return parseListener(t.ToListener())
222+
case *HttpListener:
223+
{
224+
paths := make([]string, 0)
225+
for _, path := range t.Paths {
226+
p := fmt.Sprintf("%s/%s", path.Host, path.Path)
227+
paths = append(paths, strings.ReplaceAll(p, "//", "/"))
228+
}
229+
return paths
230+
}
231+
case *TCPListener:
232+
return t.Hosts
233+
}
234+
235+
return []string{}
236+
}

api/model/management/context.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ package management
1717

1818
import (
1919
"github.com/gravitee-io/gravitee-kubernetes-operator/api/model/refs"
20+
"github.com/gravitee-io/gravitee-kubernetes-operator/pkg/types/k8s/custom"
2021
)
2122

23+
var _ custom.Auth = &Auth{}
24+
var _ custom.BasicAuth = &BasicAuth{}
25+
2226
type Context struct {
2327
// The URL of a management API instance
2428
// +kubebuilder:validation:Pattern=`^http(s?):\/\/.+$`
@@ -36,6 +40,31 @@ type Context struct {
3640
Auth *Auth `json:"auth"`
3741
}
3842

43+
// GetAuth implements custom.Context.
44+
func (c *Context) GetAuth() custom.Auth {
45+
return c.Auth
46+
}
47+
48+
// GetEnv implements custom.Context.
49+
func (c *Context) GetEnv() string {
50+
return c.EnvId
51+
}
52+
53+
// GetOrg implements custom.Context.
54+
func (c *Context) GetOrg() string {
55+
return c.OrgId
56+
}
57+
58+
// GetSecretRef implements custom.Context.
59+
func (c *Context) GetSecretRef() custom.ResourceRef {
60+
return c.Auth.SecretRef
61+
}
62+
63+
// GetURL implements custom.Context.
64+
func (c *Context) GetURL() string {
65+
return c.BaseUrl
66+
}
67+
3968
type Auth struct {
4069
// The bearer token used to authenticate against the API Management instance
4170
// (must be generated from an admin account)
@@ -46,13 +75,56 @@ type Auth struct {
4675
SecretRef *refs.NamespacedName `json:"secretRef,omitempty"`
4776
}
4877

78+
// GetBearerToken implements custom.Auth.
79+
func (in *Auth) GetBearerToken() string {
80+
return in.BearerToken
81+
}
82+
83+
// HasCredentials implements custom.Auth.
84+
func (in *Auth) HasCredentials() bool {
85+
return in.Credentials != nil
86+
}
87+
88+
// GetCredentials implements custom.Auth.
89+
func (in *Auth) GetCredentials() custom.BasicAuth {
90+
return in.Credentials
91+
}
92+
93+
// GetSecretRef implements custom.Auth.
94+
func (in *Auth) GetSecretRef() custom.ResourceRef {
95+
return in.SecretRef
96+
}
97+
98+
// SetCredentials implements custom.Auth.
99+
func (in *Auth) SetCredentials(username string, password string) {
100+
in.Credentials = &BasicAuth{
101+
Username: username,
102+
Password: password,
103+
}
104+
}
105+
106+
// SetToken implements custom.Auth.
107+
func (in *Auth) SetToken(token string) {
108+
in.BearerToken = token
109+
}
110+
49111
type BasicAuth struct {
50112
// +kubebuilder:validation:Required
51113
Username string `json:"username,omitempty"`
52114
// +kubebuilder:validation:Required
53115
Password string `json:"password,omitempty"`
54116
}
55117

118+
// GetPassword implements custom.BasicAuth.
119+
func (in *BasicAuth) GetPassword() string {
120+
return in.Password
121+
}
122+
123+
// GetUsername implements custom.BasicAuth.
124+
func (in *BasicAuth) GetUsername() string {
125+
return in.Username
126+
}
127+
56128
func (c *Context) HasAuthentication() bool {
57129
return c.Auth != nil
58130
}

api/v1alpha1/apiv2definition_types.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/gravitee-io/gravitee-kubernetes-operator/api/model/refs"
3030
"github.com/gravitee-io/gravitee-kubernetes-operator/internal/uuid"
3131
"github.com/gravitee-io/gravitee-kubernetes-operator/pkg/types/k8s/custom"
32-
"github.com/gravitee-io/gravitee-kubernetes-operator/pkg/types/list"
3332
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3433
"sigs.k8s.io/controller-runtime/pkg/client"
3534
)
@@ -61,7 +60,6 @@ type ApiDefinitionStatus struct {
6160
base.Status `json:",inline"`
6261
}
6362

64-
var _ list.Item = &ApiDefinition{}
6563
var _ custom.ApiDefinition = &ApiDefinition{}
6664
var _ custom.Status = &ApiDefinitionStatus{}
6765
var _ custom.Spec = &ApiDefinitionV2Spec{}
@@ -143,7 +141,7 @@ func (api *ApiDefinition) OrgID() string {
143141
return api.Status.OrgID
144142
}
145143

146-
func (api *ApiDefinition) Version() custom.ApiDefinitionVersion {
144+
func (api *ApiDefinition) GetDefinitionVersion() custom.ApiDefinitionVersion {
147145
return custom.ApiV2
148146
}
149147

@@ -167,6 +165,10 @@ func (api *ApiDefinition) HasContext() bool {
167165
return api.Spec.Context != nil
168166
}
169167

168+
func (api *ApiDefinition) GetContextPaths() ([]string, error) {
169+
return api.Spec.GetContextPaths()
170+
}
171+
170172
func (spec *ApiDefinitionV2Spec) Hash() string {
171173
return hash.Calculate(spec)
172174
}
@@ -201,8 +203,6 @@ func (s *ApiDefinitionStatus) DeepCopyTo(obj client.Object) error {
201203
return nil
202204
}
203205

204-
var _ list.Interface = &ApiDefinitionList{}
205-
206206
// ApiDefinitionList contains a list of ApiDefinition.
207207
// +kubebuilder:object:root=true
208208
type ApiDefinitionList struct {
@@ -211,14 +211,6 @@ type ApiDefinitionList struct {
211211
Items []ApiDefinition `json:"items"`
212212
}
213213

214-
func (l *ApiDefinitionList) GetItems() []list.Item {
215-
items := make([]list.Item, len(l.Items))
216-
for i := range l.Items {
217-
items[i] = &l.Items[i]
218-
}
219-
return items
220-
}
221-
222214
func init() {
223215
SchemeBuilder.Register(&ApiDefinition{}, &ApiDefinitionList{})
224216
}

api/v1alpha1/apiv2definition_webhook.go

Lines changed: 0 additions & 50 deletions
This file was deleted.

api/v1alpha1/apiv4definition_types.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@ import (
2222
"github.com/gravitee-io/gravitee-kubernetes-operator/internal/hash"
2323

2424
v4 "github.com/gravitee-io/gravitee-kubernetes-operator/api/model/api/v4"
25-
"github.com/gravitee-io/gravitee-kubernetes-operator/api/model/management"
2625
"github.com/gravitee-io/gravitee-kubernetes-operator/api/model/refs"
2726
"github.com/gravitee-io/gravitee-kubernetes-operator/internal/uuid"
2827
"github.com/gravitee-io/gravitee-kubernetes-operator/pkg/types/k8s/custom"
29-
"github.com/gravitee-io/gravitee-kubernetes-operator/pkg/types/list"
3028
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3129
"sigs.k8s.io/controller-runtime/pkg/client"
3230
)
@@ -43,7 +41,7 @@ type ApiV4DefinitionStatus struct {
4341
v4.Status `json:",inline"`
4442
}
4543

46-
var _ custom.ApiDefinition = &ApiV4Definition{}
44+
var _ custom.ApiDefinitionResource = &ApiV4Definition{}
4745
var _ custom.Status = &ApiDefinitionStatus{}
4846
var _ custom.Spec = &ApiDefinitionV2Spec{}
4947

@@ -78,7 +76,7 @@ func (api *ApiV4Definition) IsBeingDeleted() bool {
7876
// If the API is unknown, the ID is either given from the spec if given,
7977
// or generated from the API UID and the context key to ensure uniqueness
8078
// in case the API is replicated on a same APIM instance.
81-
func (api *ApiV4Definition) PickID(mCtx *management.Context) string {
79+
func (api *ApiV4Definition) PickID(mCtx custom.Context) string {
8280
if api.Status.ID != "" {
8381
return api.Status.ID
8482
}
@@ -88,7 +86,7 @@ func (api *ApiV4Definition) PickID(mCtx *management.Context) string {
8886
}
8987

9088
if mCtx != nil {
91-
return uuid.FromStrings(api.PickCrossID(), mCtx.OrgId, mCtx.EnvId)
89+
return uuid.FromStrings(api.PickCrossID(), mCtx.GetOrg(), mCtx.GetEnv())
9290
}
9391

9492
return string(api.UID)
@@ -202,6 +200,14 @@ func (api *ApiV4Definition) GetObjectMeta() *metav1.ObjectMeta {
202200
return &api.ObjectMeta
203201
}
204202

203+
func (api *ApiV4Definition) GetContextPaths() ([]string, error) {
204+
return api.Spec.GetContextPaths()
205+
}
206+
207+
func (api *ApiV4Definition) GetDefinitionVersion() custom.ApiDefinitionVersion {
208+
return custom.ApiV4
209+
}
210+
205211
func (spec *ApiV4DefinitionSpec) Hash() string {
206212
return hash.Calculate(spec)
207213
}
@@ -248,14 +254,6 @@ type ApiV4DefinitionList struct {
248254
Items []ApiV4Definition `json:"items"`
249255
}
250256

251-
func (l *ApiV4DefinitionList) GetItems() []list.Item {
252-
items := make([]list.Item, len(l.Items))
253-
for i := range l.Items {
254-
items[i] = &l.Items[i]
255-
}
256-
return items
257-
}
258-
259257
func init() {
260258
SchemeBuilder.Register(&ApiV4Definition{}, &ApiV4DefinitionList{})
261259
}

0 commit comments

Comments
 (0)