Skip to content

gko 286: apim validation #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions api/model/api/v2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ package v2

import (
"github.com/gravitee-io/gravitee-kubernetes-operator/api/model/api/base"
"github.com/gravitee-io/gravitee-kubernetes-operator/pkg/types/k8s/custom"
)

var _ custom.ApiDefinition = &Api{}

type Api struct {
*base.ApiBase `json:",inline"`
// +kubebuilder:validation:Required
Expand Down Expand Up @@ -69,6 +72,15 @@ type Api struct {
ExecutionMode string `json:"execution_mode,omitempty"`
}

func (api *Api) GetDefinitionVersion() custom.ApiDefinitionVersion {
return custom.ApiV2
}

// TODO implement when v2 admission handles paths
func (api *Api) GetContextPaths() ([]string, error) {
return make([]string, 0), nil
}

const (
ModeFullyManaged = "fully_managed"
OriginKubernetes = "kubernetes"
Expand Down
49 changes: 49 additions & 0 deletions api/model/api/v4/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
package v4

import (
"fmt"
"net/url"
"strings"

"github.com/gravitee-io/gravitee-kubernetes-operator/api/model/api/base"
"github.com/gravitee-io/gravitee-kubernetes-operator/pkg/types/k8s/custom"
)

// +kubebuilder:validation:Enum=PROXY;MESSAGE;
Expand All @@ -25,6 +30,8 @@ type ApiType string
// +kubebuilder:validation:Enum=PUBLISHED;UNPUBLISHED;
type ApiV4LifecycleState string

var _ custom.ApiDefinition = &Api{}

type Api struct {
*base.ApiBase `json:",inline"`
// +kubebuilder:default:=`V4`
Expand Down Expand Up @@ -185,3 +192,45 @@ func (api *Api) getGatewayDefinitionEndpointGroups() []*EndpointGroup {
}
return endpointGroups
}

func (api *Api) GetDefinitionVersion() custom.ApiDefinitionVersion {
return custom.ApiV4
}

func (api *Api) GetContextPaths() ([]string, error) {
paths := make([]string, 0)
for _, l := range api.Listeners {
for _, s := range parseListener(l) {
p, err := url.Parse(s)
if err != nil {
return paths, err
}
paths = append(paths, p.String())
}
}
return paths, nil
}

func parseListener(l Listener) []string {
if l == nil {
return []string{}
}

switch t := l.(type) {
case *GenericListener:
return parseListener(t.ToListener())
case *HttpListener:
{
paths := make([]string, 0)
for _, path := range t.Paths {
p := fmt.Sprintf("%s/%s", path.Host, path.Path)
paths = append(paths, strings.ReplaceAll(p, "//", "/"))
}
return paths
}
case *TCPListener:
return t.Hosts
}

return []string{}
}
18 changes: 17 additions & 1 deletion api/model/api/v4/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,28 @@

package v4

import "github.com/gravitee-io/gravitee-kubernetes-operator/api/model/api/base"
import (
"github.com/gravitee-io/gravitee-kubernetes-operator/api/model/api/base"
)

type Status struct {
base.Status `json:",inline"`
// This field is used to store the list of plans that have been created
// for the API definition if a management context has been defined
// to sync the API with an APIM instance
Plans map[string]string `json:"plans,omitempty"`
// When API has been created regardless of errors, this field is
// used to persist the error message encountered during admission
Errors Errors `json:"errors,omitempty"`
}

type Errors struct {
// warning errors do not block object reconciliation,
// most of the time because the value is ignored or defaulted
// when the API gets synced with APIM
Warning []string `json:"warning,omitempty"`
// severe errors do not pass admission and will block reconcile
// hence, this field should always be during the admission phase
// and is very unlikely to be persisted in the status
Severe []string `json:"severe,omitempty"`
}
26 changes: 26 additions & 0 deletions api/model/api/v4/zz_generated.deepcopy.go

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

73 changes: 73 additions & 0 deletions api/model/management/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ package management

import (
"github.com/gravitee-io/gravitee-kubernetes-operator/api/model/refs"
"github.com/gravitee-io/gravitee-kubernetes-operator/pkg/types/k8s/custom"
)

var _ custom.Auth = &Auth{}
var _ custom.BasicAuth = &BasicAuth{}
var _ custom.Context = &Context{}

type Context struct {
// The URL of a management API instance
// +kubebuilder:validation:Pattern=`^http(s?):\/\/.+$`
Expand All @@ -36,6 +41,31 @@ type Context struct {
Auth *Auth `json:"auth"`
}

// GetAuth implements custom.Context.
func (c *Context) GetAuth() custom.Auth {
return c.Auth
}

// GetEnv implements custom.Context.
func (c *Context) GetEnv() string {
return c.EnvId
}

// GetOrg implements custom.Context.
func (c *Context) GetOrg() string {
return c.OrgId
}

// GetSecretRef implements custom.Context.
func (c *Context) GetSecretRef() custom.ResourceRef {
return c.Auth.SecretRef
}

// GetURL implements custom.Context.
func (c *Context) GetURL() string {
return c.BaseUrl
}

type Auth struct {
// The bearer token used to authenticate against the API Management instance
// (must be generated from an admin account)
Expand All @@ -46,13 +76,56 @@ type Auth struct {
SecretRef *refs.NamespacedName `json:"secretRef,omitempty"`
}

// GetBearerToken implements custom.Auth.
func (in *Auth) GetBearerToken() string {
return in.BearerToken
}

// HasCredentials implements custom.Auth.
func (in *Auth) HasCredentials() bool {
return in.Credentials != nil
}

// GetCredentials implements custom.Auth.
func (in *Auth) GetCredentials() custom.BasicAuth {
return in.Credentials
}

// GetSecretRef implements custom.Auth.
func (in *Auth) GetSecretRef() custom.ResourceRef {
return in.SecretRef
}

// SetCredentials implements custom.Auth.
func (in *Auth) SetCredentials(username string, password string) {
in.Credentials = &BasicAuth{
Username: username,
Password: password,
}
}

// SetToken implements custom.Auth.
func (in *Auth) SetToken(token string) {
in.BearerToken = token
}

type BasicAuth struct {
// +kubebuilder:validation:Required
Username string `json:"username,omitempty"`
// +kubebuilder:validation:Required
Password string `json:"password,omitempty"`
}

// GetPassword implements custom.BasicAuth.
func (in *BasicAuth) GetPassword() string {
return in.Password
}

// GetUsername implements custom.BasicAuth.
func (in *BasicAuth) GetUsername() string {
return in.Username
}

func (c *Context) HasAuthentication() bool {
return c.Auth != nil
}
Expand Down
Loading