Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ resources:
kind: GatewayProxy
path: github.com/api7/api7-ingress-controller/api/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
namespaced: true
domain: github.com
group: gateway.apisix.io
kind: HTTPRoutePolicy
path: github.com/api7/api7-ingress-controller/api/v1alpha1
version: v1alpha1
version: "3"
125 changes: 125 additions & 0 deletions api/v1alpha1/httproutepolicy_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
Copyright 2024.
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 (
"encoding/json"
"errors"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
)

// HTTPRoutePolicySpec defines the desired state of HTTPRoutePolicy.
type HTTPRoutePolicySpec struct {
// TargetRef identifies an API object (enum: HTTPRoute, Ingress) to apply HTTPRoutePolicy to.
//
// target references.
// +listType=map
// +listMapKey=group
// +listMapKey=kind
// +listMapKey=name
// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:MaxItems=16
TargetRefs []gatewayv1alpha2.LocalPolicyTargetReferenceWithSectionName `json:"targetRefs"`

Policy HTTPRoutePolicySpecPolicy `json:"policy"`
}

// +kubebuilder:object:root=true

// HTTPRoutePolicy is the Schema for the httproutepolicies API.
type HTTPRoutePolicy struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec HTTPRoutePolicySpec `json:"spec,omitempty"`
}

// +kubebuilder:object:root=true

// HTTPRoutePolicyList contains a list of HTTPRoutePolicy.
type HTTPRoutePolicyList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []HTTPRoutePolicy `json:"items"`
}

type HTTPRoutePolicySpecPolicy struct {
Priority *int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
Vars Vars `json:"vars,omitempty" yaml:"vars,omitempty"`
}

// Vars represents the route match expressions of APISIX.
type Vars [][]StringOrSlice

// UnmarshalJSON implements json.Unmarshaler interface.
// lua-cjson doesn't distinguish empty array and table,
// and by default empty array will be encoded as '{}'.
// We have to maintain the compatibility.
func (vars *Vars) UnmarshalJSON(p []byte) error {
if p[0] == '{' {
if len(p) != 2 {
return errors.New("unexpected non-empty object")
}
return nil
}
var data [][]StringOrSlice
if err := json.Unmarshal(p, &data); err != nil {
return err
}
*vars = data
return nil
}

// StringOrSlice represents a string or a string slice.
// TODO Do not use interface{} to avoid the reflection overheads.
type StringOrSlice struct {
StrVal string `json:"-"`
SliceVal []string `json:"-"`
}

func (s *StringOrSlice) MarshalJSON() ([]byte, error) {
var (
p []byte
err error
)
if s.SliceVal != nil {
p, err = json.Marshal(s.SliceVal)
} else {
p, err = json.Marshal(s.StrVal)
}
return p, err
}

func (s *StringOrSlice) UnmarshalJSON(p []byte) error {
var err error

if len(p) == 0 {
return errors.New("empty object")
}
if p[0] == '[' {
err = json.Unmarshal(p, &s.SliceVal)
} else {
err = json.Unmarshal(p, &s.StrVal)
}
return err
}

func init() {
SchemeBuilder.Register(&HTTPRoutePolicy{}, &HTTPRoutePolicyList{})
}
161 changes: 161 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

1 change: 1 addition & 0 deletions config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ resources:
- bases/gateway.apisix.io_gatewayproxies.yaml
- bases/gateway.apisix.io_consumers.yaml
- bases/gateway.apisix.io_backendtrafficpolicies.yaml
- bases/gateway.apisix.io_httproutepolicies.yaml
# +kubebuilder:scaffold:crdkustomizeresource

patches:
Expand Down
8 changes: 8 additions & 0 deletions config/rbac/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ resources:
- metrics_auth_role.yaml
- metrics_auth_role_binding.yaml
- metrics_reader_role.yaml
# For each CRD, "Admin", "Editor" and "Viewer" roles are scaffolded by
# default, aiding admins in cluster management. Those roles are
# not used by the {{ .ProjectName }} itself. You can comment the following lines
# if you do not want those helpers be installed with your Project.
- httproutepolicy_admin_role.yaml
- httproutepolicy_editor_role.yaml
- httproutepolicy_viewer_role.yaml

8 changes: 8 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ rules:
- get
- list
- watch
- apiGroups:
- gateway.apisix.io
resources:
- httproutepolicies
verbs:
- get
- list
- watch
- apiGroups:
- gateway.apisix.io
resources:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ require (
go.uber.org/zap v1.27.0
golang.org/x/net v0.28.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/gorm v1.25.11
helm.sh/helm/v3 v3.15.4
k8s.io/api v0.31.1
Expand Down Expand Up @@ -213,6 +212,7 @@ require (
gopkg.in/fsnotify.v1 v1.4.7 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiserver v0.31.1 // indirect
k8s.io/cli-runtime v0.30.3 // indirect
k8s.io/component-base v0.31.1 // indirect
Expand Down
1 change: 1 addition & 0 deletions internal/manager/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
// +kubebuilder:rbac:groups=gateway.apisix.io,resources=consumers/status,verbs=get;update
// +kubebuilder:rbac:groups=gateway.apisix.io,resources=backendtrafficpolicies,verbs=get;list;watch
// +kubebuilder:rbac:groups=gateway.apisix.io,resources=backendtrafficpolicies/status,verbs=get;update
// +kubebuilder:rbac:groups=gateway.apisix.io,resources=httproutepolicies,verbs=get;list;watch

// GatewayAPI
// +kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gatewayclasses,verbs=get;list;watch;update
Expand Down
Loading