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

chore: gateway-api v0.5.1 #1445

Merged
merged 6 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 38 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ unit-test:

### e2e-test: Run e2e test cases (in existing clusters directly)
.PHONY: e2e-test
e2e-test: ginkgo-check pack-images e2e-wolf-rbac
kubectl apply -k $(PWD)/samples/deploy/crd
kubectl apply -f $(PWD)/samples/deploy/gateway-api
e2e-test: ginkgo-check pack-images e2e-wolf-rbac install install-gateway-api
cd test/e2e \
&& go mod download \
&& export REGISTRY=$(REGISTRY) \
Expand All @@ -143,6 +141,16 @@ ifeq ("$(wildcard $(GINKGO))", "")
exit 1
endif

### install: Install CRDs into the K8s cluster.
.PHONY: install
install:
kubectl apply -k $(PWD)/samples/deploy/crd

### uninstall: Uninstall CRDs from the K8s cluster.
.PHONY: uninstall
uninstall:
kubectl delete -k $(PWD)/samples/deploy/crd

### kind-up: Launch a Kubernetes cluster with a image registry by Kind.
.PHONY: kind-up
kind-up:
Expand Down Expand Up @@ -258,3 +266,30 @@ kind-load-images:
$(REGISTRY)/test-backend:dev \
$(REGISTRY)/jmalloc/echo-server:dev \
$(REGISTRY)/busybox:dev


GATEWAY_API_VERSION ?= v0.5.1
GATEWAY_API_PACKAGE ?= sigs.k8s.io/gateway-api@$(GATEWAY_API_VERSION)
GATEWAY_API_CRDS_GO_MOD_PATH = $(shell go env GOPATH)/pkg/mod/$(GATEWAY_API_PACKAGE)
GATEWAY_API_CRDS_LOCAL_PATH = $(PWD)/samples/deploy/gateway-api/$(GATEWAY_API_VERSION)

.PHONY: go-mod-download-gateway-api
go-mod-download-gateway-api:
@go mod download $(GATEWAY_API_PACKAGE)

### install: Install Gateway API into the K8s cluster from go mod.
.PHONY: install-gateway-api
install-gateway-api: go-mod-download-gateway-api
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it's necessary since we have a local copy.

kubectl apply -k $(GATEWAY_API_CRDS_GO_MOD_PATH)/config/crd
kubectl apply -k $(GATEWAY_API_CRDS_GO_MOD_PATH)/config/crd/experimental
kubectl apply -f $(GATEWAY_API_CRDS_GO_MOD_PATH)/config/webhook

### install: Install Gateway API into the K8s cluster from repo.
.PHONY: install-gateway-api-local
install-gateway-api-local:
kubectl apply -f $(GATEWAY_API_CRDS_LOCAL_PATH)

### uninstall-gateway-api: Uninstall Gateway API from the K8s cluster.
.PHONY: uninstall-gateway-api
uninstall-gateway-api:
kubectl delete -f $(GATEWAY_API_CRDS_LOCAL_PATH)
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ require (
k8s.io/apimachinery v0.25.3
k8s.io/client-go v0.25.3
k8s.io/code-generator v0.25.3
sigs.k8s.io/gateway-api v0.4.0
sigs.k8s.io/gateway-api v0.5.1
sigs.k8s.io/yaml v1.3.0
)

require (
Expand Down Expand Up @@ -87,5 +88,4 @@ require (
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
389 changes: 3 additions & 386 deletions go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/kube/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package kube
import (
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
gatewayclientset "sigs.k8s.io/gateway-api/pkg/client/clientset/gateway/versioned"
gatewayexternalversions "sigs.k8s.io/gateway-api/pkg/client/informers/gateway/externalversions"
gatewayclientset "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned"
gatewayexternalversions "sigs.k8s.io/gateway-api/pkg/client/informers/externalversions"

"github.com/apache/apisix-ingress-controller/pkg/config"
clientset "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/client/clientset/versioned"
Expand Down
30 changes: 15 additions & 15 deletions pkg/providers/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/apache/apisix-ingress-controller/pkg/log"
"github.com/apache/apisix-ingress-controller/pkg/providers/utils"
Expand Down Expand Up @@ -120,7 +120,7 @@ func (c *gatewayController) sync(ctx context.Context, ev *types.Event) error {
)
return nil
}
gateway = ev.Tombstone.(*gatewayv1alpha2.Gateway)
gateway = ev.Tombstone.(*gatewayv1beta1.Gateway)

err = c.controller.RemoveListeners(gateway.Namespace, gateway.Name)
if err != nil {
Expand All @@ -129,7 +129,7 @@ func (c *gatewayController) sync(ctx context.Context, ev *types.Event) error {
} else {
if c.controller.HasGatewayClass(string(gateway.Spec.GatewayClassName)) {
// TODO: handle listeners
listeners, err := c.controller.translator.TranslateGatewayV1Alpha2(gateway)
listeners, err := c.controller.translator.TranslateGatewayV1beta1(gateway)
if err != nil {
return err
}
Expand All @@ -146,7 +146,7 @@ func (c *gatewayController) sync(ctx context.Context, ev *types.Event) error {
// At present, we choose to directly update `GatewayStatus.Addresses`
// to indicate that we have picked the Gateway resource.

c.recordStatus(gateway, string(gatewayv1alpha2.ListenerReasonReady), metav1.ConditionTrue, gateway.Generation)
c.recordStatus(gateway, string(gatewayv1beta1.ListenerReasonReady), metav1.ConditionTrue, gateway.Generation)
return nil
}

Expand Down Expand Up @@ -208,7 +208,7 @@ func (c *gatewayController) OnDelete(obj interface{}) {
return
}

gateway, ok := obj.(*gatewayv1alpha2.Gateway)
gateway, ok := obj.(*gatewayv1beta1.Gateway)
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
Expand All @@ -218,7 +218,7 @@ func (c *gatewayController) OnDelete(obj interface{}) {
)
return
}
gateway = tombstone.Obj.(*gatewayv1alpha2.Gateway)
gateway = tombstone.Obj.(*gatewayv1beta1.Gateway)
}

c.workqueue.Add(&types.Event{
Expand All @@ -229,11 +229,11 @@ func (c *gatewayController) OnDelete(obj interface{}) {
}

// recordStatus record resources status
func (c *gatewayController) recordStatus(v *gatewayv1alpha2.Gateway, reason string, status metav1.ConditionStatus, generation int64) {
func (c *gatewayController) recordStatus(v *gatewayv1beta1.Gateway, reason string, status metav1.ConditionStatus, generation int64) {
v = v.DeepCopy()

gatewayCondition := metav1.Condition{
Type: string(gatewayv1alpha2.ListenerConditionReady),
Type: string(gatewayv1beta1.ListenerConditionReady),
Reason: reason,
Status: status,
Message: "Gateway's status has been successfully updated",
Expand All @@ -255,7 +255,7 @@ func (c *gatewayController) recordStatus(v *gatewayv1alpha2.Gateway, reason stri
}

v.Status.Addresses = convLBIPToGatewayAddr(lbips)
if _, errRecord := c.controller.gatewayClient.GatewayV1alpha2().Gateways(v.Namespace).UpdateStatus(context.TODO(), v, metav1.UpdateOptions{}); errRecord != nil {
if _, errRecord := c.controller.gatewayClient.GatewayV1beta1().Gateways(v.Namespace).UpdateStatus(context.TODO(), v, metav1.UpdateOptions{}); errRecord != nil {
log.Errorw("failed to record status change for Gateway resource",
zap.Error(errRecord),
zap.String("name", v.Name),
Expand All @@ -265,24 +265,24 @@ func (c *gatewayController) recordStatus(v *gatewayv1alpha2.Gateway, reason stri
}

// convLBIPToGatewayAddr convert LoadBalancerIngress to GatewayAddress format
func convLBIPToGatewayAddr(lbips []corev1.LoadBalancerIngress) []gatewayv1alpha2.GatewayAddress {
var gas []gatewayv1alpha2.GatewayAddress
func convLBIPToGatewayAddr(lbips []corev1.LoadBalancerIngress) []gatewayv1beta1.GatewayAddress {
var gas []gatewayv1beta1.GatewayAddress

// In the definition, there is also an address type called NamedAddress,
// which we currently do not implement
HostnameAddressType := gatewayv1alpha2.HostnameAddressType
IPAddressType := gatewayv1alpha2.IPAddressType
HostnameAddressType := gatewayv1beta1.HostnameAddressType
IPAddressType := gatewayv1beta1.IPAddressType

for _, lbip := range lbips {
if v := lbip.Hostname; v != "" {
gas = append(gas, gatewayv1alpha2.GatewayAddress{
gas = append(gas, gatewayv1beta1.GatewayAddress{
Type: &HostnameAddressType,
Value: v,
})
}

if v := lbip.IP; v != "" {
gas = append(gas, gatewayv1alpha2.GatewayAddress{
gas = append(gas, gatewayv1beta1.GatewayAddress{
Type: &IPAddressType,
Value: v,
})
Expand Down
14 changes: 7 additions & 7 deletions pkg/providers/gateway/gateway_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/gateway-api/apis/v1alpha2"
"sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/apache/apisix-ingress-controller/pkg/log"
"github.com/apache/apisix-ingress-controller/pkg/types"
Expand Down Expand Up @@ -80,11 +80,11 @@ func (c *gatewayClassController) init() error {
return nil
}

func (c *gatewayClassController) markAsUpdated(gatewayClass *v1alpha2.GatewayClass) error {
func (c *gatewayClassController) markAsUpdated(gatewayClass *v1beta1.GatewayClass) error {
gc := gatewayClass.DeepCopy()

condition := metav1.Condition{
Type: string(v1alpha2.GatewayClassConditionStatusAccepted),
Type: string(v1beta1.GatewayClassConditionStatusAccepted),
Status: metav1.ConditionTrue,
Reason: "Updated",
Message: fmt.Sprintf("Updated by apisix-ingress-controller, sync at %v", time.Now()),
Expand Down Expand Up @@ -112,7 +112,7 @@ func (c *gatewayClassController) markAsUpdated(gatewayClass *v1alpha2.GatewayCla
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

_, err := c.controller.gatewayClient.GatewayV1alpha2().GatewayClasses().UpdateStatus(ctx, gc, metav1.UpdateOptions{})
_, err := c.controller.gatewayClient.GatewayV1beta1().GatewayClasses().UpdateStatus(ctx, gc, metav1.UpdateOptions{})
if err != nil {
log.Errorw("failed to update GatewayClass status",
zap.Error(err),
Expand Down Expand Up @@ -166,7 +166,7 @@ func (c *gatewayClassController) sync(ctx context.Context, ev *types.Event) erro
return c.markAsUpdated(gatewayClass)
}
} else if ev.Type == types.EventDelete {
c.controller.RemoveGatewayClass(ev.Tombstone.(*v1alpha2.GatewayClass).Name)
c.controller.RemoveGatewayClass(ev.Tombstone.(*v1beta1.GatewayClass).Name)
}

return nil
Expand Down Expand Up @@ -230,7 +230,7 @@ func (c *gatewayClassController) onDelete(obj interface{}) {
return
}

gatewayClass, ok := obj.(*v1alpha2.GatewayClass)
gatewayClass, ok := obj.(*v1beta1.GatewayClass)
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
Expand All @@ -240,7 +240,7 @@ func (c *gatewayClassController) onDelete(obj interface{}) {
)
return
}
gatewayClass = tombstone.Obj.(*v1alpha2.GatewayClass)
gatewayClass = tombstone.Obj.(*v1beta1.GatewayClass)
}

c.workqueue.Add(&types.Event{
Expand Down
14 changes: 7 additions & 7 deletions pkg/providers/gateway/gateway_httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/apache/apisix-ingress-controller/pkg/log"
"github.com/apache/apisix-ingress-controller/pkg/providers/translation"
Expand Down Expand Up @@ -120,10 +120,10 @@ func (c *gatewayHTTPRouteController) sync(ctx context.Context, ev *types.Event)
)
return nil
}
httpRoute = ev.Tombstone.(*gatewayv1alpha2.HTTPRoute)
httpRoute = ev.Tombstone.(*gatewayv1beta1.HTTPRoute)
}

tctx, err := c.controller.translator.TranslateGatewayHTTPRouteV1Alpha2(httpRoute)
tctx, err := c.controller.translator.TranslateGatewayHTTPRouteV1beta1(httpRoute)

if err != nil {
log.Errorw("failed to translate gateway HTTPRoute",
Expand Down Expand Up @@ -154,8 +154,8 @@ func (c *gatewayHTTPRouteController) sync(ctx context.Context, ev *types.Event)
added = m
} else {
var oldCtx *translation.TranslateContext
oldObj := ev.OldObject.(*gatewayv1alpha2.HTTPRoute)
oldCtx, err = c.controller.translator.TranslateGatewayHTTPRouteV1Alpha2(oldObj)
oldObj := ev.OldObject.(*gatewayv1beta1.HTTPRoute)
oldCtx, err = c.controller.translator.TranslateGatewayHTTPRouteV1beta1(oldObj)
if err != nil {
log.Errorw("failed to translate old HTTPRoute",
zap.String("version", oldObj.APIVersion),
Expand Down Expand Up @@ -222,8 +222,8 @@ func (c *gatewayHTTPRouteController) onAdd(obj interface{}) {
}

func (c *gatewayHTTPRouteController) onUpdate(oldObj, newObj interface{}) {
oldHTTPRoute := oldObj.(*gatewayv1alpha2.HTTPRoute)
newHTTPRoute := newObj.(*gatewayv1alpha2.HTTPRoute)
oldHTTPRoute := oldObj.(*gatewayv1beta1.HTTPRoute)
newHTTPRoute := newObj.(*gatewayv1beta1.HTTPRoute)
if oldHTTPRoute.ResourceVersion >= newHTTPRoute.ResourceVersion {
return
}
Expand Down
32 changes: 17 additions & 15 deletions pkg/providers/gateway/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayclientset "sigs.k8s.io/gateway-api/pkg/client/clientset/gateway/versioned"
gatewayexternalversions "sigs.k8s.io/gateway-api/pkg/client/informers/gateway/externalversions"
gatewaylistersv1alpha2 "sigs.k8s.io/gateway-api/pkg/client/listers/gateway/apis/v1alpha2"
gatewayclientset "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned"
gatewayexternalversions "sigs.k8s.io/gateway-api/pkg/client/informers/externalversions"
gatewaylistersv1alpha2 "sigs.k8s.io/gateway-api/pkg/client/listers/apis/v1alpha2"

gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
gatewaylistersv1beta1 "sigs.k8s.io/gateway-api/pkg/client/listers/apis/v1beta1"

"github.com/apache/apisix-ingress-controller/pkg/apisix"
"github.com/apache/apisix-ingress-controller/pkg/config"
Expand All @@ -54,7 +56,7 @@ type Provider struct {
listenersLock sync.RWMutex
// meta key ("ns/name") of Gateway -> section name -> ListenerConf
listeners map[string]map[string]*types.ListenerConf
portListeners map[gatewayv1alpha2.PortNumber]*types.ListenerConf
portListeners map[gatewayv1beta1.PortNumber]*types.ListenerConf

*ProviderOptions
gatewayClient gatewayclientset.Interface
Expand All @@ -63,15 +65,15 @@ type Provider struct {

gatewayController *gatewayController
gatewayInformer cache.SharedIndexInformer
gatewayLister gatewaylistersv1alpha2.GatewayLister
gatewayLister gatewaylistersv1beta1.GatewayLister

gatewayClassController *gatewayClassController
gatewayClassInformer cache.SharedIndexInformer
gatewayClassLister gatewaylistersv1alpha2.GatewayClassLister
gatewayClassLister gatewaylistersv1beta1.GatewayClassLister

gatewayHTTPRouteController *gatewayHTTPRouteController
gatewayHTTPRouteInformer cache.SharedIndexInformer
gatewayHTTPRouteLister gatewaylistersv1alpha2.HTTPRouteLister
gatewayHTTPRouteLister gatewaylistersv1beta1.HTTPRouteLister

gatewayTLSRouteController *gatewayTLSRouteController
gatewayTLSRouteInformer cache.SharedIndexInformer
Expand Down Expand Up @@ -118,7 +120,7 @@ func NewGatewayProvider(opts *ProviderOptions) (*Provider, error) {
gatewayClasses: make(map[string]struct{}),

listeners: make(map[string]map[string]*types.ListenerConf),
portListeners: make(map[gatewayv1alpha2.PortNumber]*types.ListenerConf),
portListeners: make(map[gatewayv1beta1.PortNumber]*types.ListenerConf),

ProviderOptions: opts,
gatewayClient: gatewayKubeClient,
Expand All @@ -130,14 +132,14 @@ func NewGatewayProvider(opts *ProviderOptions) (*Provider, error) {

gatewayFactory := gatewayexternalversions.NewSharedInformerFactory(p.gatewayClient, p.Cfg.Kubernetes.ResyncInterval.Duration)

p.gatewayLister = gatewayFactory.Gateway().V1alpha2().Gateways().Lister()
p.gatewayInformer = gatewayFactory.Gateway().V1alpha2().Gateways().Informer()
p.gatewayLister = gatewayFactory.Gateway().V1beta1().Gateways().Lister()
p.gatewayInformer = gatewayFactory.Gateway().V1beta1().Gateways().Informer()

p.gatewayClassLister = gatewayFactory.Gateway().V1alpha2().GatewayClasses().Lister()
p.gatewayClassInformer = gatewayFactory.Gateway().V1alpha2().GatewayClasses().Informer()
p.gatewayClassLister = gatewayFactory.Gateway().V1beta1().GatewayClasses().Lister()
p.gatewayClassInformer = gatewayFactory.Gateway().V1beta1().GatewayClasses().Informer()

p.gatewayHTTPRouteLister = gatewayFactory.Gateway().V1alpha2().HTTPRoutes().Lister()
p.gatewayHTTPRouteInformer = gatewayFactory.Gateway().V1alpha2().HTTPRoutes().Informer()
p.gatewayHTTPRouteLister = gatewayFactory.Gateway().V1beta1().HTTPRoutes().Lister()
p.gatewayHTTPRouteInformer = gatewayFactory.Gateway().V1beta1().HTTPRoutes().Informer()

p.gatewayTLSRouteLister = gatewayFactory.Gateway().V1alpha2().TLSRoutes().Lister()
p.gatewayTLSRouteInformer = gatewayFactory.Gateway().V1alpha2().TLSRoutes().Informer()
Expand Down
Loading