Skip to content

Commit 13f3912

Browse files
committed
Refactor and update types and references
This commit refactors the `common` package by moving the `Vars` and `StringOrSlice` types to the `adc` package. It also updates the `TargetReference` type to use `LocalPolicyTargetReferenceWithSectionName` from `gateway-api` and simplifies the CRD documentation. Additionally, it adjusts the import paths and type references in the `httproute.go` file to reflect these changes.
1 parent e4b2523 commit 13f3912

File tree

7 files changed

+94
-289
lines changed

7 files changed

+94
-289
lines changed

api/adc/types.go

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"time"
1111

1212
"github.com/incubator4/go-resty-expr/expr"
13-
14-
"github.com/api7/api7-ingress-controller/api/common"
1513
)
1614

1715
const (
@@ -115,16 +113,16 @@ type Service struct {
115113
type Route struct {
116114
Metadata `json:",inline" yaml:",inline"`
117115

118-
EnableWebsocket *bool `json:"enable_websocket,omitempty" yaml:"enable_websocket,omitempty"`
119-
FilterFunc string `json:"filter_func,omitempty" yaml:"filter_func,omitempty"`
120-
Hosts []string `json:"hosts,omitempty" yaml:"hosts,omitempty"`
121-
Methods []string `json:"methods,omitempty" yaml:"methods,omitempty"`
122-
Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
123-
Priority *int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
124-
RemoteAddrs []string `json:"remote_addrs,omitempty" yaml:"remote_addrs,omitempty"`
125-
Timeout *Timeout `json:"timeout,omitempty" yaml:"timeout,omitempty"`
126-
Uris []string `json:"uris" yaml:"uris"`
127-
Vars common.Vars `json:"vars,omitempty" yaml:"vars,omitempty"`
116+
EnableWebsocket *bool `json:"enable_websocket,omitempty" yaml:"enable_websocket,omitempty"`
117+
FilterFunc string `json:"filter_func,omitempty" yaml:"filter_func,omitempty"`
118+
Hosts []string `json:"hosts,omitempty" yaml:"hosts,omitempty"`
119+
Methods []string `json:"methods,omitempty" yaml:"methods,omitempty"`
120+
Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
121+
Priority *int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
122+
RemoteAddrs []string `json:"remote_addrs,omitempty" yaml:"remote_addrs,omitempty"`
123+
Timeout *Timeout `json:"timeout,omitempty" yaml:"timeout,omitempty"`
124+
Uris []string `json:"uris" yaml:"uris"`
125+
Vars Vars `json:"vars,omitempty" yaml:"vars,omitempty"`
128126
}
129127

130128
type Timeout struct {
@@ -502,3 +500,49 @@ type ResponseData struct {
502500
Value map[string]any `json:"value"`
503501
ErrorMsg string `json:"error_msg"`
504502
}
503+
504+
// Vars represents the route match expressions of APISIX.
505+
type Vars [][]StringOrSlice
506+
507+
// UnmarshalJSON implements json.Unmarshaler interface.
508+
// lua-cjson doesn't distinguish empty array and table,
509+
// and by default empty array will be encoded as '{}'.
510+
// We have to maintain the compatibility.
511+
func (vars *Vars) UnmarshalJSON(p []byte) error {
512+
if p[0] == '{' {
513+
if len(p) != 2 {
514+
return errors.New("unexpected non-empty object")
515+
}
516+
return nil
517+
}
518+
var data [][]StringOrSlice
519+
if err := json.Unmarshal(p, &data); err != nil {
520+
return err
521+
}
522+
*vars = data
523+
return nil
524+
}
525+
526+
// StringOrSlice represents a string or a string slice.
527+
// TODO Do not use interface{} to avoid the reflection overheads.
528+
type StringOrSlice struct {
529+
StrVal string `json:"-"`
530+
SliceVal []StringOrSlice `json:"-"`
531+
}
532+
533+
func (s *StringOrSlice) MarshalJSON() ([]byte, error) {
534+
if s.SliceVal != nil {
535+
return json.Marshal(s.SliceVal)
536+
}
537+
return json.Marshal(s.StrVal)
538+
}
539+
540+
func (s *StringOrSlice) UnmarshalJSON(p []byte) error {
541+
if len(p) == 0 {
542+
return errors.New("empty object")
543+
}
544+
if p[0] == '[' {
545+
return json.Unmarshal(p, &s.SliceVal)
546+
}
547+
return json.Unmarshal(p, &s.StrVal)
548+
}

api/common/types.go

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

api/common/zz_generated.deepcopy.go

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

api/v1alpha1/httproutepolicy_types.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package v1alpha1
1919
import (
2020
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22-
v1 "sigs.k8s.io/gateway-api/apis/v1"
2322
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
2423
)
2524

@@ -34,22 +33,12 @@ type HTTPRoutePolicySpec struct {
3433
// +listMapKey=name
3534
// +kubebuilder:validation:MinItems=1
3635
// +kubebuilder:validation:MaxItems=16
37-
TargetRefs []TargetReference `json:"targetRefs"`
36+
TargetRefs []gatewayv1alpha2.LocalPolicyTargetReferenceWithSectionName `json:"targetRefs"`
3837

3938
Priority *int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
4039
Vars []apiextensionsv1.JSON `json:"vars,omitempty" yaml:"vars,omitempty"`
4140
}
4241

43-
type TargetReference struct {
44-
Group v1.Group `json:"group"`
45-
Kind v1.Kind `json:"kind"`
46-
// +optional
47-
Namespace *v1.Namespace `json:"namespace,omitempty"`
48-
Name v1.ObjectName `json:"name"`
49-
// +optional
50-
SectionName *v1.SectionName `json:"sectionName,omitempty"`
51-
}
52-
5342
// +kubebuilder:object:root=true
5443
// +kubebuilder:subresource:status
5544

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)