Skip to content

Commit

Permalink
feat: add support for Shared Policy Groups
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiiiel committed Jan 15, 2025
1 parent e135e34 commit 9718ea1
Show file tree
Hide file tree
Showing 28 changed files with 1,716 additions and 11 deletions.
68 changes: 68 additions & 0 deletions api/model/policygroups/sharedpolicygroups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (C) 2015 The Gravitee team (http://gravitee.io)
//
// 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.

// +kubebuilder:object:generate=true
package policygroups

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

// +kubebuilder:validation:Enum=MESSAGE;PROXY;NATIVE;
type ApiType string

// +kubebuilder:validation:Enum=REQUEST;RESPONSE;INTERACT;CONNECT;PUBLISH;SUBSCRIBE;
type FlowPhase string

type SharedPolicyGroup struct {
// CrossID to export SharedPolicyGroup into different environments
CrossID *string `json:"crossId,omitempty"`
// SharedPolicyGroup name
// +kubebuilder:validation:Required
Name *string `json:"name"`
// SharedPolicyGroup description
Description *string `json:"description,omitempty"`
// SharedPolicyGroup prerequisite Message
PrerequisiteMessage *string `json:"prerequisiteMessage,omitempty"`
// Specify the SharedPolicyGroup ApiType
// +kubebuilder:validation:Required
ApiType *ApiType `json:"apiType"`
// SharedPolicyGroup phase (REQUEST;RESPONSE;INTERACT;CONNECT;PUBLISH;SUBSCRIBE)
// +kubebuilder:validation:Required
Phase *FlowPhase `json:"phase"`
// SharedPolicyGroup Steps
Steps []*Step `json:"steps,omitempty"`
// SharedPolicyGroup LifecycleState (UNDEPLOYED;DEPLOYED;PENDING)
}

type Step struct {
// +kubebuilder:default:=true
// Indicate if this FlowStep is enabled or not
Enabled bool `json:"enabled"`
// FlowStep policy
// +kubebuilder:validation:Optional
Policy *string `json:"policy,omitempty"`
// FlowStep name
// +kubebuilder:validation:Optional
Name *string `json:"name,omitempty"`
// FlowStep description
// +kubebuilder:validation:Optional
Description *string `json:"description,omitempty"`
// FlowStep configuration is a map of arbitrary key-values
// +kubebuilder:validation:Optional
Configuration *utils.GenericStringMap `json:"configuration,omitempty"`
// FlowStep condition
// +kubebuilder:validation:Optional
Condition *string `json:"condition,omitempty"`
}
35 changes: 35 additions & 0 deletions api/model/policygroups/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (C) 2015 The Gravitee team (http://gravitee.io)
//
// 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 policygroups

import (
"github.com/gravitee-io/gravitee-kubernetes-operator/api/model/status"
"github.com/gravitee-io/gravitee-kubernetes-operator/internal/core"
)

type Status struct {
// The organization ID, if a management context has been defined to sync with an APIM instance
OrgID string `json:"organizationId,omitempty"`
// The environment ID, if a management context has been defined to sync with an APIM instance
EnvID string `json:"environmentId,omitempty"`
// The Cross ID is used to identify an SharedPolicyGroup that has been promoted from one environment to another.
CrossID string `json:"crossId,omitempty"`
// The processing status of the SharedPolicyGroup.
// The value is `Completed` if the sync with APIM succeeded, Failed otherwise.
ProcessingStatus core.ProcessingStatus `json:"processingStatus,omitempty"`
// When SharedPolicyGroup has been created regardless of errors, this field is
// used to persist the error message encountered during admission
Errors status.Errors `json:"errors,omitempty"`
}
134 changes: 134 additions & 0 deletions api/model/policygroups/zz_generated.deepcopy.go

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

151 changes: 151 additions & 0 deletions api/v1alpha1/sharedpolicygroups_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* 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 (
"fmt"

"github.com/gravitee-io/gravitee-kubernetes-operator/api/model/policygroups"
"github.com/gravitee-io/gravitee-kubernetes-operator/api/model/refs"
"github.com/gravitee-io/gravitee-kubernetes-operator/internal/core"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/gravitee-io/gravitee-kubernetes-operator/internal/hash"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var _ core.SharedPolicyGroupObject = &SharedPolicyGroup{}

// SharedPolicyGroupSpec
// +kubebuilder:object:generate=true
type SharedPolicyGroupSpec struct {
*policygroups.SharedPolicyGroup `json:",inline"`
// +kubebuilder:validation:Required
Context *refs.NamespacedName `json:"contextRef"`
}

// Hash implements custom.Spec.
func (spec *SharedPolicyGroupSpec) Hash() string {
return hash.Calculate(spec)
}

// SharedPolicyGroupSpecStatus defines the observed state of an API Context.
type SharedPolicyGroupSpecStatus struct {
policygroups.Status `json:",inline"`
}

// SharedPolicyGroup
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:scope=Cluster
// +kubebuilder:printcolumn:name="name",type=string,JSONPath=`.spec.name`
// +kubebuilder:printcolumn:name="description",type=string,JSONPath=`.spec.description`
// +kubebuilder:printcolumn:name="apiType",type=string,JSONPath=`.spec.apiType`
// +kubebuilder:resource:shortName=sharedpolicygroups
// +kubebuilder:storageversion
type SharedPolicyGroup struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec SharedPolicyGroupSpec `json:"spec,omitempty"`
Status SharedPolicyGroupSpecStatus `json:"status,omitempty"`
}

// SharedPolicyGroupList contains a list of shared policy groups.
// +kubebuilder:object:root=true
type SharedPolicyGroupList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []SharedPolicyGroup `json:"items"`
}

func (s *SharedPolicyGroup) IsBeingDeleted() bool {
return !s.ObjectMeta.DeletionTimestamp.IsZero()
}

func (s *SharedPolicyGroup) GetSpec() core.Spec {
return &s.Spec
}

func (s *SharedPolicyGroup) GetStatus() core.Status {
return &s.Status
}

func (s *SharedPolicyGroup) GetRef() core.ObjectRef {
return &refs.NamespacedName{
Name: s.Name,
Namespace: s.Namespace,
}
}

func (s *SharedPolicyGroup) ContextRef() core.ObjectRef {
return s.Spec.Context
}

func (s *SharedPolicyGroup) HasContext() bool {
return s.Spec.Context != nil
}

func (s *SharedPolicyGroup) GetID() string {
return s.Status.CrossID
}

func (s *SharedPolicyGroup) PopulateIDs(context core.ContextModel) {
if s.Status.CrossID != "" {
s.Spec.CrossID = &s.Status.CrossID
}
}

func (s *SharedPolicyGroup) GetOrgID() string {
return s.Status.OrgID
}

func (s *SharedPolicyGroup) GetEnvID() string {
return s.Status.EnvID
}

func (s *SharedPolicyGroupSpecStatus) SetProcessingStatus(status core.ProcessingStatus) {
s.Status.ProcessingStatus = status
}

func (s *SharedPolicyGroupSpecStatus) IsFailed() bool {
return s.ProcessingStatus == core.ProcessingStatusFailed
}

func (s *SharedPolicyGroupSpecStatus) DeepCopyFrom(obj client.Object) error {
switch t := obj.(type) {
case *SharedPolicyGroup:
t.Status.DeepCopyInto(s)
return nil
default:
return fmt.Errorf("unknown type %T", t)
}
}

func (s *SharedPolicyGroupSpecStatus) DeepCopyTo(obj client.Object) error {
switch t := obj.(type) {
case *SharedPolicyGroup:
s.DeepCopyInto(&t.Status)
return nil
default:
return fmt.Errorf("unknown type %T", t)
}
}

func init() {
SchemeBuilder.Register(&SharedPolicyGroup{}, &SharedPolicyGroupList{})
}
Loading

0 comments on commit 9718ea1

Please sign in to comment.