Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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"
78 changes: 12 additions & 66 deletions api/adc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"time"

"github.com/incubator4/go-resty-expr/expr"

"github.com/api7/api7-ingress-controller/api/common"
)

const (
Expand Down Expand Up @@ -113,16 +115,16 @@ type Service struct {
type Route struct {
Metadata `json:",inline" yaml:",inline"`

EnableWebsocket *bool `json:"enable_websocket,omitempty" yaml:"enable_websocket,omitempty"`
FilterFunc string `json:"filter_func,omitempty" yaml:"filter_func,omitempty"`
Hosts []string `json:"hosts,omitempty" yaml:"hosts,omitempty"`
Methods []string `json:"methods,omitempty" yaml:"methods,omitempty"`
Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
Priority *int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
RemoteAddrs []string `json:"remote_addrs,omitempty" yaml:"remote_addrs,omitempty"`
Timeout *Timeout `json:"timeout,omitempty" yaml:"timeout,omitempty"`
Uris []string `json:"uris" yaml:"uris"`
Vars Vars `json:"vars,omitempty" yaml:"vars,omitempty"`
EnableWebsocket *bool `json:"enable_websocket,omitempty" yaml:"enable_websocket,omitempty"`
FilterFunc string `json:"filter_func,omitempty" yaml:"filter_func,omitempty"`
Hosts []string `json:"hosts,omitempty" yaml:"hosts,omitempty"`
Methods []string `json:"methods,omitempty" yaml:"methods,omitempty"`
Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
Priority *int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
RemoteAddrs []string `json:"remote_addrs,omitempty" yaml:"remote_addrs,omitempty"`
Timeout *Timeout `json:"timeout,omitempty" yaml:"timeout,omitempty"`
Uris []string `json:"uris" yaml:"uris"`
Vars common.Vars `json:"vars,omitempty" yaml:"vars,omitempty"`
}

type Timeout struct {
Expand Down Expand Up @@ -325,62 +327,6 @@ func (n *UpstreamNodes) UnmarshalJSON(p []byte) error {
return nil
}

// 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
}

// ComposeRouteName uses namespace, name and rule name to compose
// the route name.
func ComposeRouteName(namespace, name string, rule string) string {
Expand Down
64 changes: 64 additions & 0 deletions api/common/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// +kubebuilder:object:generate=true

package common

import (
"encoding/json"
"errors"
)

// 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
}
70 changes: 70 additions & 0 deletions api/common/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/v1alpha1/httproutepolicy_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
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 (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"

"github.com/api7/api7-ingress-controller/api/common"
)

// 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"`
Status gatewayv1alpha2.PolicyStatus `json:"status,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.
// +kubebuilder:object:generate=false
type Vars = common.Vars

func init() {
SchemeBuilder.Register(&HTTPRoutePolicy{}, &HTTPRoutePolicyList{})
}
Loading
Loading