Skip to content
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

feat: make multiple controllers handle different ApisixRoute CRDs #593

Merged
merged 28 commits into from Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b2c23fd
feat: make multiple controllers handle different ApisixRoute CRDs #578
Donghui0 Jul 15, 2021
aba19cc
feat: Add IngressClass support for custom resources. #592
Donghui0 Jul 30, 2021
16edb2d
fix variable name
Donghui0 Aug 17, 2021
7799f25
use IngressClassName instead of ingressClass
Donghui0 Sep 2, 2021
731ac47
fix version logic
Donghui0 Sep 2, 2021
eff3156
feat: make multiple controllers handle different ApisixRoute CRDs #578
Donghui0 Jul 15, 2021
7515f64
feat: Add IngressClass support for custom resources. #592
Donghui0 Jul 30, 2021
ed4f0e7
fix variable name
Donghui0 Aug 17, 2021
d0d773f
use IngressClassName instead of ingressClass
Donghui0 Sep 2, 2021
eedc377
fix version logic
Donghui0 Sep 2, 2021
7defa15
fix: merge conflict
Donghui0 Mar 29, 2022
89edb94
fix: merge
Donghui0 Mar 29, 2022
528884a
Merge branch 'apache:master' into feat-routeclass
Donghui0 Jun 13, 2022
1990930
Merge branch 'apache:master' into feat-routeclass
Donghui0 Jun 28, 2022
c75dcc4
fix: v2 version
Donghui0 Jul 1, 2022
e37e950
feat: make multiple controllers handle different ApisixRoute CRDs #578
Donghui0 Jul 15, 2021
6d715a7
fix: v2 version
Donghui0 Jul 1, 2022
6de8065
fix: merge master
Donghui0 Feb 6, 2023
9fd5573
fix: config desc
Donghui0 Feb 6, 2023
7477a1d
Merge branch 'master' of https://github.com/apache/apisix-ingress-con…
tao12345666333 Feb 17, 2023
b7b7d34
fix: ApisixRoute support IngressClass
Donghui0 Mar 9, 2023
b7bde68
fix: remove v2beta3 crd change
Donghui0 Mar 10, 2023
086d872
fix: remove v2beta3 ingressClass prop
Donghui0 Mar 10, 2023
f57c047
fix: merge conflict
Donghui0 Mar 17, 2023
1c418f2
fix: e2e test
Donghui0 Mar 17, 2023
19e9220
Merge remote-tracking branch 'upstream/master' into feat-routeclass
AlinsRan Mar 22, 2023
08350e7
update go.mod go.sum
AlinsRan Mar 22, 2023
c8456e2
fix e2e apisixroute config
AlinsRan Mar 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/ingress/ingress.go
Expand Up @@ -143,7 +143,7 @@ the apisix cluster and others are created`,
cmd.PersistentFlags().StringVar(&cfg.Kubernetes.Kubeconfig, "kubeconfig", "", "Kubernetes configuration file (by default in-cluster configuration will be used)")
cmd.PersistentFlags().DurationVar(&cfg.Kubernetes.ResyncInterval.Duration, "resync-interval", time.Minute, "the controller resync (with Kubernetes) interval, the minimum resync interval is 30s")
cmd.PersistentFlags().StringSliceVar(&cfg.Kubernetes.AppNamespaces, "app-namespace", []string{config.NamespaceAll}, "namespaces that controller will watch for resources")
cmd.PersistentFlags().StringVar(&cfg.Kubernetes.IngressClass, "ingress-class", config.IngressClass, "the class of an Ingress object is set using the field IngressClassName in Kubernetes clusters version v1.18.0 or higher or the annotation \"kubernetes.io/ingress.class\" (deprecated)")
cmd.PersistentFlags().StringVar(&cfg.Kubernetes.IngressClass, "ingress-class", config.IngressClass, "the class of an Ingress object is set using the field IngressClassName in Kubernetes clusters version v1.18.0 or higher or the annotation \"kubernetes.io/ingress.class\" (deprecated). a wildcard '*' listen all ApisixRoutes or Ingress")
cmd.PersistentFlags().StringVar(&cfg.Kubernetes.ElectionID, "election-id", config.IngressAPISIXLeader, "election id used for campaign the controller leader")
cmd.PersistentFlags().StringVar(&cfg.Kubernetes.IngressVersion, "ingress-version", config.IngressNetworkingV1, "the supported ingress api group version, can be \"networking/v1beta1\", \"networking/v1\" (for Kubernetes version v1.19.0 or higher) and \"extensions/v1beta1\"")
cmd.PersistentFlags().StringVar(&cfg.Kubernetes.ApisixRouteVersion, "apisix-route-version", config.ApisixRouteV2alpha1, "the supported apisixroute api group version, can be \"apisix.apache.org/v1\" or \"apisix.apache.org/v2alpha1\"")
Expand Down
44 changes: 43 additions & 1 deletion pkg/ingress/apisix_route.go
Expand Up @@ -292,6 +292,26 @@ func (c *apisixRouteController) handleSyncErr(obj interface{}, errOrigin error)
c.workqueue.AddRateLimited(obj)
}

func (c *apisixRouteController) isApisixRouteEffective(ar kube.ApisixRoute) bool {
var class string

if ar.GroupVersion() == kube.ApisixRouteV1 {
class = ar.V1().Spec.IngressClass
} else if ar.GroupVersion() == kube.ApisixRouteV2alpha1 {
class = ar.V2alpha1().Spec.IngressClass
} else if ar.GroupVersion() == kube.ApisixRouteV2beta1 {
class = ar.V2beta1().Spec.IngressClass
} else {
class = c.controller.cfg.Kubernetes.IngressClass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the else won't be entered, once the logic here was executed, then there are some programming faults occurred, we may just:

  1. Panic, or
  2. log the error and just return false.

}

if c.controller.cfg.Kubernetes.IngressClass == "*" || c.controller.cfg.Kubernetes.IngressClass == class {
return true
}

return false
}

func (c *apisixRouteController) onAdd(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
if err != nil {
Expand All @@ -301,10 +321,19 @@ func (c *apisixRouteController) onAdd(obj interface{}) {
if !c.controller.namespaceWatching(key) {
return
}

ar := kube.MustNewApisixRoute(obj)

if !c.isApisixRouteEffective(ar) {
log.Debugw("ignore noneffective ApisixRoute add event",
zap.Any("object", obj),
)
return
}

log.Debugw("ApisixRoute add event arrived",
zap.Any("object", obj))

ar := kube.MustNewApisixRoute(obj)
c.workqueue.AddRateLimited(&types.Event{
Type: types.EventAdd,
Object: kube.ApisixRouteEvent{
Expand All @@ -328,6 +357,13 @@ func (c *apisixRouteController) onUpdate(oldObj, newObj interface{}) {
if !c.controller.namespaceWatching(key) {
return
}
if !c.isApisixRouteEffective(curr) {
log.Debugw("ignore noneffective ApisixRoute update event arrived",
zap.Any("new object", curr),
zap.Any("old object", prev),
)
return
}
log.Debugw("ApisixRoute update event arrived",
zap.Any("new object", curr),
zap.Any("old object", prev),
Expand Down Expand Up @@ -359,6 +395,12 @@ func (c *apisixRouteController) onDelete(obj interface{}) {
if !c.controller.namespaceWatching(key) {
return
}
if !c.isApisixRouteEffective(ar) {
log.Debugw("ignore noneffective ApisixRoute delete event arrived",
zap.Any("final state", ar),
)
return
}
log.Debugw("ApisixRoute delete event arrived",
zap.Any("final state", ar),
)
Expand Down
3 changes: 3 additions & 0 deletions pkg/ingress/ingress.go
Expand Up @@ -304,6 +304,9 @@ func (c *ingressController) isIngressEffective(ing kube.Ingress) bool {
ic *string
ica string
)
if c.controller.cfg.Kubernetes.IngressClass == "*" {
return true
}
if ing.GroupVersion() == kube.IngressV1 {
ic = ing.V1().Spec.IngressClassName
ica = ing.V1().GetAnnotations()[_ingressKey]
Expand Down
3 changes: 2 additions & 1 deletion pkg/kube/apisix/apis/config/v1/types.go
Expand Up @@ -39,7 +39,8 @@ type ApisixRoute struct {

// ApisixRouteSpec is the spec definition for ApisixRouteSpec.
type ApisixRouteSpec struct {
Rules []Rule `json:"rules,omitempty" yaml:"rules,omitempty"`
IngressClass string `json:"ingressClass,omitempty" yaml:"ingressClass,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rules []Rule `json:"rules,omitempty" yaml:"rules,omitempty"`
}

// Rule represents a single route rule in ApisixRoute.
Expand Down
5 changes: 3 additions & 2 deletions pkg/kube/apisix/apis/config/v2alpha1/types.go
Expand Up @@ -78,8 +78,9 @@ type ApisixStatus struct {

// ApisixRouteSpec is the spec definition for ApisixRouteSpec.
type ApisixRouteSpec struct {
HTTP []*ApisixRouteHTTP `json:"http,omitempty" yaml:"http,omitempty"`
TCP []*ApisixRouteTCP `json:"tcp,omitempty" yaml:"tcp,omitempty"`
IngressClass string `json:"ingressClass,omitempty" yaml:"ingressClass,omitempty"`
HTTP []*ApisixRouteHTTP `json:"http,omitempty" yaml:"http,omitempty"`
TCP []*ApisixRouteTCP `json:"tcp,omitempty" yaml:"tcp,omitempty"`
}

// ApisixRouteHTTP represents a single route in for HTTP traffic.
Expand Down
5 changes: 3 additions & 2 deletions pkg/kube/apisix/apis/config/v2beta1/types.go
Expand Up @@ -41,8 +41,9 @@ type ApisixStatus struct {

// ApisixRouteSpec is the spec definition for ApisixRouteSpec.
type ApisixRouteSpec struct {
HTTP []ApisixRouteHTTP `json:"http,omitempty" yaml:"http,omitempty"`
Stream []ApisixRouteStream `json:"stream,omitempty" yaml:"stream,omitempty"`
IngressClass string `json:"ingressClass,omitempty" yaml:"ingressClass,omitempty"`
HTTP []ApisixRouteHTTP `json:"http,omitempty" yaml:"http,omitempty"`
Stream []ApisixRouteStream `json:"stream,omitempty" yaml:"stream,omitempty"`
}

// ApisixRouteHTTP represents a single route in for HTTP traffic.
Expand Down
2 changes: 2 additions & 0 deletions samples/deploy/crd/v1beta1/ApisixRoute.yaml
Expand Up @@ -72,6 +72,8 @@ spec:
- required: ["tcp"]
- required: ["stream"]
properties:
ingressClass:
type: string
http:
type: array
minItems: 1
Expand Down