Skip to content

Commit

Permalink
feat: add logic for ApisixRoute v2beta1 (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
gxthrj authored Jul 10, 2021
1 parent a754f69 commit 1c17b41
Show file tree
Hide file tree
Showing 19 changed files with 535 additions and 130 deletions.
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const (
ApisixRouteV1 = "apisix.apache.org/v1"
// ApisixRouteV2alpha1 represents apisixroute.apisix.apache.org/v2alpha1
ApisixRouteV2alpha1 = "apisix.apache.org/v2alpha1"
// ApisixRouteV2beta1 represents apisixroute.apisix.apache.org/v2beta1
ApisixRouteV2beta1 = "apisix.apache.org/v2beta1"

_minimalResyncInterval = 30 * time.Second
)
Expand Down
61 changes: 46 additions & 15 deletions pkg/ingress/apisix_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,14 @@ func (c *apisixRouteController) sync(ctx context.Context, ev *types.Event) error
ar kube.ApisixRoute
tctx *translation.TranslateContext
)
if obj.GroupVersion == kube.ApisixRouteV1 {
switch obj.GroupVersion {
case kube.ApisixRouteV1:
ar, err = c.controller.apisixRouteLister.V1(namespace, name)
} else {
case kube.ApisixRouteV2alpha1:
ar, err = c.controller.apisixRouteLister.V2alpha1(namespace, name)
case kube.ApisixRouteV2beta1:
ar, err = c.controller.apisixRouteLister.V2beta1(namespace, name)
}

if err != nil {
if !k8serrors.IsNotFound(err) {
log.Errorw("failed to get ApisixRoute",
Expand Down Expand Up @@ -129,7 +131,9 @@ func (c *apisixRouteController) sync(ctx context.Context, ev *types.Event) error
}
ar = ev.Tombstone.(kube.ApisixRoute)
}
if obj.GroupVersion == kube.ApisixRouteV1 {
//
switch obj.GroupVersion {
case kube.ApisixRouteV1:
tctx, err = c.controller.translator.TranslateRouteV1(ar.V1())
if err != nil {
log.Errorw("failed to translate ApisixRoute v1",
Expand All @@ -138,7 +142,7 @@ func (c *apisixRouteController) sync(ctx context.Context, ev *types.Event) error
)
return err
}
} else {
case kube.ApisixRouteV2alpha1:
if ev.Type != types.EventDelete {
tctx, err = c.controller.translator.TranslateRouteV2alpha1(ar.V2alpha1())
} else {
Expand All @@ -154,6 +158,19 @@ func (c *apisixRouteController) sync(ctx context.Context, ev *types.Event) error
)
return err
}
case kube.ApisixRouteV2beta1:
if ev.Type != types.EventDelete {
tctx, err = c.controller.translator.TranslateRouteV2beta1(ar.V2beta1())
} else {
tctx, err = c.controller.translator.TranslateRouteV2beta1NotStrictly(ar.V2beta1())
}
if err != nil {
log.Errorw("failed to translate ApisixRoute v2beta1",
zap.Error(err),
zap.Any("object", ar),
)
return err
}
}

log.Debugw("translated ApisixRoute",
Expand All @@ -180,18 +197,21 @@ func (c *apisixRouteController) sync(ctx context.Context, ev *types.Event) error
added = m
} else {
var oldCtx *translation.TranslateContext
if obj.GroupVersion == kube.ApisixRouteV1 {
switch obj.GroupVersion {
case kube.ApisixRouteV1:
oldCtx, err = c.controller.translator.TranslateRouteV1(obj.OldObject.V1())
} else {
case kube.ApisixRouteV2alpha1:
oldCtx, err = c.controller.translator.TranslateRouteV2alpha1(obj.OldObject.V2alpha1())
case kube.ApisixRouteV2beta1:
oldCtx, err = c.controller.translator.TranslateRouteV2beta1(obj.OldObject.V2beta1())
}
if err != nil {
log.Errorw("failed to translate old ApisixRoute v2alpha1",
log.Errorw("failed to translate old ApisixRoute",
zap.String("version", obj.GroupVersion),
zap.String("event", "update"),
zap.Error(err),
zap.Any("ApisixRoute", ar),
)

return err
}

Expand All @@ -215,19 +235,26 @@ func (c *apisixRouteController) handleSyncErr(obj interface{}, errOrigin error)
return
}
var ar kube.ApisixRoute
if event.GroupVersion == kube.ApisixRouteV1 {
switch event.GroupVersion {
case kube.ApisixRouteV1:
ar, errLocal = c.controller.apisixRouteLister.V1(namespace, name)
} else {
case kube.ApisixRouteV2alpha1:
ar, errLocal = c.controller.apisixRouteLister.V2alpha1(namespace, name)
case kube.ApisixRouteV2beta1:
ar, errLocal = c.controller.apisixRouteLister.V2beta1(namespace, name)
}
if errOrigin == nil {
if ev.Type != types.EventDelete {
if errLocal == nil {
if ar.GroupVersion() == kube.ApisixRouteV1 {
switch ar.GroupVersion() {
case kube.ApisixRouteV1:
c.controller.recorderEvent(ar.V1(), v1.EventTypeNormal, _resourceSynced, nil)
} else if ar.GroupVersion() == kube.ApisixRouteV2alpha1 {
case kube.ApisixRouteV2alpha1:
c.controller.recorderEvent(ar.V2alpha1(), v1.EventTypeNormal, _resourceSynced, nil)
c.controller.recordStatus(ar.V2alpha1(), _resourceSynced, nil, metav1.ConditionTrue)
case kube.ApisixRouteV2beta1:
c.controller.recorderEvent(ar.V2beta1(), v1.EventTypeNormal, _resourceSynced, nil)
c.controller.recordStatus(ar.V2beta1(), _resourceSynced, nil, metav1.ConditionTrue)
}
} else {
log.Errorw("failed list ApisixRoute",
Expand All @@ -245,11 +272,15 @@ func (c *apisixRouteController) handleSyncErr(obj interface{}, errOrigin error)
zap.Error(errOrigin),
)
if errLocal == nil {
if ar.GroupVersion() == kube.ApisixRouteV1 {
switch ar.GroupVersion() {
case kube.ApisixRouteV1:
c.controller.recorderEvent(ar.V1(), v1.EventTypeWarning, _resourceSyncAborted, errOrigin)
} else if ar.GroupVersion() == kube.ApisixRouteV2alpha1 {
case kube.ApisixRouteV2alpha1:
c.controller.recorderEvent(ar.V2alpha1(), v1.EventTypeWarning, _resourceSyncAborted, errOrigin)
c.controller.recordStatus(ar.V2alpha1(), _resourceSyncAborted, errOrigin, metav1.ConditionFalse)
case kube.ApisixRouteV2beta1:
c.controller.recorderEvent(ar.V2beta1(), v1.EventTypeWarning, _resourceSyncAborted, errOrigin)
c.controller.recordStatus(ar.V2beta1(), _resourceSyncAborted, errOrigin, metav1.ConditionFalse)
}
} else {
log.Errorw("failed list ApisixRoute",
Expand Down
10 changes: 7 additions & 3 deletions pkg/ingress/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func (c *Controller) initWhenStartLeading() {
c.apisixRouteLister = kube.NewApisixRouteLister(
apisixFactory.Apisix().V1().ApisixRoutes().Lister(),
apisixFactory.Apisix().V2alpha1().ApisixRoutes().Lister(),
apisixFactory.Apisix().V2beta1().ApisixRoutes().Lister(),
)
c.apisixUpstreamLister = apisixFactory.Apisix().V1().ApisixUpstreams().Lister()
c.apisixTlsLister = apisixFactory.Apisix().V1().ApisixTlses().Lister()
Expand All @@ -217,10 +218,13 @@ func (c *Controller) initWhenStartLeading() {
} else {
ingressInformer = kubeFactory.Extensions().V1beta1().Ingresses().Informer()
}
if c.cfg.Kubernetes.ApisixRouteVersion == config.ApisixRouteV2alpha1 {
apisixRouteInformer = apisixFactory.Apisix().V2alpha1().ApisixRoutes().Informer()
} else {
switch c.cfg.Kubernetes.ApisixRouteVersion {
case config.ApisixRouteV1:
apisixRouteInformer = apisixFactory.Apisix().V1().ApisixRoutes().Informer()
case config.ApisixRouteV2alpha1:
apisixRouteInformer = apisixFactory.Apisix().V2alpha1().ApisixRoutes().Informer()
case config.ApisixRouteV2beta1:
apisixRouteInformer = apisixFactory.Apisix().V2beta1().ApisixRoutes().Informer()
}

c.podInformer = kubeFactory.Core().V1().Pods().Informer()
Expand Down
2 changes: 1 addition & 1 deletion pkg/ingress/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c *podController) onAdd(obj interface{}) {
return
}
log.Debugw("pod add event arrived",
zap.Any("object", obj),
zap.String("obj.key", key),
)
pod := obj.(*corev1.Pod)
if err := c.controller.podCache.Add(pod); err != nil {
Expand Down
51 changes: 8 additions & 43 deletions pkg/kube/apisix/apis/config/v2beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v2alpha1"
)

// +genclient
Expand Down Expand Up @@ -53,14 +55,14 @@ type ApisixRouteHTTP struct {
Priority int `json:"priority,omitempty" yaml:"priority,omitempty"`
Match ApisixRouteHTTPMatch `json:"match,omitempty" yaml:"match,omitempty"`
// Deprecated: Backend will be removed in the future, use Backends instead.
Backend ApisixRouteHTTPBackend `json:"backend" yaml:"backend"`
Backend v2alpha1.ApisixRouteHTTPBackend `json:"backend" yaml:"backend"`
// Backends represents potential backends to proxy after the route
// rule matched. When number of backends are more than one, traffic-split
// plugin in APISIX will be used to split traffic based on the backend weight.
Backends []ApisixRouteHTTPBackend `json:"backends" yaml:"backends"`
Websocket bool `json:"websocket" yaml:"websocket"`
Plugins []ApisixRouteHTTPPlugin `json:"plugins,omitempty" yaml:"plugins,omitempty"`
Authentication ApisixRouteAuthentication `json:"authentication,omitempty" yaml:"authentication,omitempty"`
Backends []v2alpha1.ApisixRouteHTTPBackend `json:"backends" yaml:"backends"`
Websocket bool `json:"websocket" yaml:"websocket"`
Plugins []ApisixRouteHTTPPlugin `json:"plugins,omitempty" yaml:"plugins,omitempty"`
Authentication ApisixRouteAuthentication `json:"authentication,omitempty" yaml:"authentication,omitempty"`
}

// ApisixRouteHTTPMatch represents the match condition for hitting this route.
Expand Down Expand Up @@ -90,25 +92,7 @@ type ApisixRouteHTTPMatch struct {
// value:
// - "127.0.0.1"
// - "10.0.5.11"
NginxVars []ApisixRouteHTTPMatchExpr `json:"exprs,omitempty" yaml:"exprs,omitempty"`
}

// ApisixRouteHTTPMatchExpr represents a binary route match expression .
type ApisixRouteHTTPMatchExpr struct {
// Subject is the expression subject, it can
// be any string composed by literals and nginx
// vars.
Subject ApisixRouteHTTPMatchExprSubject `json:"subject" yaml:"subject"`
// Op is the operator.
Op string `json:"op" yaml:"op"`
// Set is an array type object of the expression.
// It should be used when the Op is "in" or "not_in";
Set []string `json:"set" yaml:"set"`
// Value is the normal type object for the expression,
// it should be used when the Op is not "in" and "not_in".
// Set and Value are exclusive so only of them can be set
// in the same time.
Value string `json:"value" yaml:"value"`
NginxVars []v2alpha1.ApisixRouteHTTPMatchExpr `json:"exprs,omitempty" yaml:"exprs,omitempty"`
}

// ApisixRouteHTTPMatchExprSubject describes the route match expression subject.
Expand All @@ -122,25 +106,6 @@ type ApisixRouteHTTPMatchExprSubject struct {
Name string `json:"name" yaml:"name"`
}

// ApisixRouteHTTPBackend represents a HTTP backend (a Kuberentes Service).
type ApisixRouteHTTPBackend struct {
// The name (short) of the service, note cross namespace is forbidden,
// so be sure the ApisixRoute and Service are in the same namespace.
ServiceName string `json:"serviceName" yaml:"serviceName"`
// The service port, could be the name or the port number.
ServicePort intstr.IntOrString `json:"servicePort" yaml:"servicePort"`
// The resolve granularity, can be "endpoints" or "service",
// when set to "endpoints", the pod ips will be used; other
// wise, the service ClusterIP or ExternalIP will be used,
// default is endpoints.
ResolveGranularity string `json:"resolveGranularity" yaml:"resolveGranularity"`
// Weight of this backend.
Weight int `json:"weight" yaml:"weight"`
// Subset specifies a subset for the target Service. The subset should be pre-defined
// in ApisixUpstream about this service.
Subset string `json:"subset" yaml:"subset"`
}

// ApisixRouteHTTPPlugin represents an APISIX plugin.
type ApisixRouteHTTPPlugin struct {
// The plugin name.
Expand Down
50 changes: 7 additions & 43 deletions pkg/kube/apisix/apis/config/v2beta1/zz_generated.deepcopy.go

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

Loading

0 comments on commit 1c17b41

Please sign in to comment.