Skip to content

Commit

Permalink
Merge pull request #994 from jcmoraisjr/jm-gateway-api-v1beta1
Browse files Browse the repository at this point in the history
Add gateway version v1beta1
  • Loading branch information
jcmoraisjr authored Oct 29, 2023
2 parents a8be706 + 3bb2852 commit b5d1b92
Show file tree
Hide file tree
Showing 18 changed files with 2,015 additions and 119 deletions.
4 changes: 2 additions & 2 deletions docs/content/en/docs/configuration/gateway-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ description: >
The following steps configure the Kubernetes cluster and HAProxy Ingress to read and parse Gateway API resources:

* Manually install the Gateway API CRDs, see the Gateway API [documentation](https://gateway-api.sigs.k8s.io/guides/#installing-gateway-api)
* ... or simply `kubectl kustomize "github.com/kubernetes-sigs/gateway-api/config/crd?ref=v0.5.0" | kubectl apply -f -`
* ... or simply `kubectl kustomize "github.com/kubernetes-sigs/gateway-api/config/crd?ref=v0.7.1" | kubectl apply -f -`
* Start (or restart) the controller

See below the [getting started steps](#getting-started).
Expand Down Expand Up @@ -51,7 +51,7 @@ Add the following steps to the [Getting Started guide]({{% relref "/docs/getting

```
kubectl kustomize\
"github.com/kubernetes-sigs/gateway-api/config/crd?ref=v0.5.0" |\
"github.com/kubernetes-sigs/gateway-api/config/crd?ref=v0.7.1" |\
kubectl apply -f -
```

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ require (
k8s.io/apiserver v0.26.6
k8s.io/client-go v0.26.6
k8s.io/klog/v2 v2.100.1
k8s.io/utils v0.0.0-20230209194617-a36077c30491
k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5
sigs.k8s.io/controller-runtime v0.14.6
sigs.k8s.io/gateway-api v0.5.0
sigs.k8s.io/gateway-api v0.7.1
)

require (
Expand Down
10 changes: 5 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
Expand Down Expand Up @@ -364,12 +364,12 @@ k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg=
k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/kube-openapi v0.0.0-20230217203603-ff9a8e8fa21d h1:oFDpQ7FfzinCtrFOl4izwOWsdTprlS2A9IXBENMW0UA=
k8s.io/kube-openapi v0.0.0-20230217203603-ff9a8e8fa21d/go.mod h1:/BYxry62FuDzmI+i9B+X2pqfySRmSOW2ARmj5Zbqhj0=
k8s.io/utils v0.0.0-20230209194617-a36077c30491 h1:r0BAOLElQnnFhE/ApUsg3iHdVYYPBjNSSOMowRZxxsY=
k8s.io/utils v0.0.0-20230209194617-a36077c30491/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5 h1:kmDqav+P+/5e1i9tFfHq1qcF3sOrDp+YEkVDAHu7Jwk=
k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
sigs.k8s.io/controller-runtime v0.14.6 h1:oxstGVvXGNnMvY7TAESYk+lzr6S3V5VFxQ6d92KcwQA=
sigs.k8s.io/controller-runtime v0.14.6/go.mod h1:WqIdsAY6JBsjfc/CqO0CORmNtoCtE4S6qbPc9s68h+0=
sigs.k8s.io/gateway-api v0.5.0 h1:ze+k9fJqvmL8s1t3e4q1ST8RnN+f09dEv+gfacahlAE=
sigs.k8s.io/gateway-api v0.5.0/go.mod h1:x0AP6gugkFV8fC/oTlnOMU0pnmuzIR8LfIPRVUjxSqA=
sigs.k8s.io/gateway-api v0.7.1 h1:Tts2jeepVkPA5rVG/iO+S43s9n7Vp7jCDhZDQYtPigQ=
sigs.k8s.io/gateway-api v0.7.1/go.mod h1:Xv0+ZMxX0lu1nSSDIIPEfbVztgNZ+3cfiYrJsa2Ooso=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 h1:PRbqxJClWWYMNV1dhaG4NsibJbArud9kFxnAMREiWFE=
Expand Down
19 changes: 15 additions & 4 deletions pkg/controller/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
gwapiversioned "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned"

"github.com/jcmoraisjr/haproxy-ingress/pkg/utils"
Expand Down Expand Up @@ -384,6 +385,7 @@ define if ingress without class should be tracked.`)
scheme := runtime.NewScheme()
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(gatewayv1alpha2.AddToScheme(scheme))
utilruntime.Must(gatewayv1beta1.AddToScheme(scheme))

var kubeConfig *rest.Config
if *apiserverHost == "" {
Expand Down Expand Up @@ -478,13 +480,20 @@ define if ingress without class should be tracked.`)
configLog.Info("watching for Gateway API resources - --watch-gateway is true")
}

hasGateway := *watchGateway && configHasAPI(
hasGatewayA2 := *watchGateway && configHasAPI(
clientGateway.Discovery(),
gatewayv1alpha2.GroupVersion,
"gatewayclass", "gateway", "httproute")
if hasGateway {
if hasGatewayA2 {
configLog.Info("found custom resource definition for gateway API v1alpha2")
}
hasGatewayB1 := *watchGateway && configHasAPI(
clientGateway.Discovery(),
gatewayv1beta1.GroupVersion,
"gatewayclass", "gateway", "httproute")
if hasGatewayB1 {
configLog.Info("found custom resource definition for gateway API v1beta1")
}

if *enableEndpointSlicesAPI {
configLog.Info("watching endpointslices - --enable-endpointslices-api is true")
Expand Down Expand Up @@ -720,7 +729,8 @@ define if ingress without class should be tracked.`)
ElectionNamespace: podNamespace,
EnableEndpointSliceAPI: *enableEndpointSlicesAPI,
ForceNamespaceIsolation: *forceIsolation,
HasGateway: hasGateway,
HasGatewayA2: hasGatewayA2,
HasGatewayB1: hasGatewayB1,
HealthzAddr: healthz,
HealthzURL: *healthzURL,
IngressClass: *ingressClass,
Expand Down Expand Up @@ -931,7 +941,8 @@ type Config struct {
ElectionNamespace string
EnableEndpointSliceAPI bool
ForceNamespaceIsolation bool
HasGateway bool
HasGatewayA2 bool
HasGatewayB1 bool
HealthzAddr string
HealthzURL string
IngressClass string
Expand Down
58 changes: 36 additions & 22 deletions pkg/controller/legacy/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/jcmoraisjr/haproxy-ingress/pkg/acme"
cfile "github.com/jcmoraisjr/haproxy-ingress/pkg/common/file"
Expand Down Expand Up @@ -218,8 +219,9 @@ func (c *k8scache) hasGateway() bool {
}

var errGatewayA2Disabled = fmt.Errorf("Gateway API v1alpha2 wasn't initialized")
var errGatewayB1Disabled = fmt.Errorf("Legacy controller does not support Gateway API v1beta1")

func (c *k8scache) GetGatewayMap() (map[string]*gatewayv1alpha2.Gateway, error) {
func (c *k8scache) GetGatewayA2Map() (map[string]*gatewayv1alpha2.Gateway, error) {
if !c.hasGateway() {
return nil, errGatewayA2Disabled
}
Expand All @@ -236,13 +238,21 @@ func (c *k8scache) GetGatewayMap() (map[string]*gatewayv1alpha2.Gateway, error)
return validGwList, nil
}

func (c *k8scache) GetGatewayClass(className string) (*gatewayv1alpha2.GatewayClass, error) {
func (c *k8scache) GetGatewayB1Map() (map[string]*gatewayv1beta1.Gateway, error) {
return nil, errGatewayB1Disabled
}

func (c *k8scache) GetGatewayClassA2(className string) (*gatewayv1alpha2.GatewayClass, error) {
if !c.hasGateway() {
return nil, errGatewayA2Disabled
}
return c.listers.gatewayClassLister.Get(className)
}

func (c *k8scache) GetGatewayClassB1(className string) (*gatewayv1alpha2.GatewayClass, error) {
return nil, errGatewayB1Disabled
}

func buildLabelSelector(match map[string]string) (labels.Selector, error) {
list := make([]string, 0, len(match))
for k, v := range match {
Expand All @@ -251,13 +261,17 @@ func buildLabelSelector(match map[string]string) (labels.Selector, error) {
return labels.Parse(strings.Join(list, ","))
}

func (c *k8scache) GetHTTPRouteList() ([]*gatewayv1alpha2.HTTPRoute, error) {
func (c *k8scache) GetHTTPRouteA2List() ([]*gatewayv1alpha2.HTTPRoute, error) {
if !c.hasGateway() {
return nil, errGatewayA2Disabled
}
return c.listers.httpRouteLister.List(labels.Everything())
}

func (c *k8scache) GetHTTPRouteB1List() ([]*gatewayv1beta1.HTTPRoute, error) {
return nil, errGatewayB1Disabled
}

func (c *k8scache) GetService(defaultNamespace, serviceName string) (*api.Service, error) {
namespace, name, err := c.buildResourceName(defaultNamespace, "service", serviceName, c.dynamicConfig.CrossNamespaceServices)
if err != nil {
Expand Down Expand Up @@ -726,7 +740,7 @@ func (c *k8scache) IsValidIngressClass(ingressClass *networking.IngressClass) bo
// implements ListerEvents
func (c *k8scache) IsValidGateway(gw *gatewayv1alpha2.Gateway) bool {
className := gw.Spec.GatewayClassName
gwClass, err := c.GetGatewayClass(string(className))
gwClass, err := c.GetGatewayClassA2(string(className))
if err != nil {
c.logger.Warn("error reading GatewayClass v1alpha2 '%s': %v", className, err)
return false
Expand Down Expand Up @@ -778,17 +792,17 @@ func (c *k8scache) Notify(old, cur interface{}) {
}
case *gatewayv1alpha2.Gateway:
if cur == nil {
ch.GatewaysDel = append(ch.GatewaysDel, old)
ch.GatewaysA2Del = append(ch.GatewaysA2Del, old)
ch.NeedFullSync = true
}
case *gatewayv1alpha2.GatewayClass:
if cur == nil {
ch.GatewayClassesDel = append(ch.GatewayClassesDel, old)
ch.GatewayClassesA2Del = append(ch.GatewayClassesA2Del, old)
ch.NeedFullSync = true
}
case *gatewayv1alpha2.HTTPRoute:
if cur == nil {
ch.HTTPRoutesDel = append(ch.HTTPRoutesDel, old)
ch.HTTPRoutesA2Del = append(ch.HTTPRoutesA2Del, old)
ch.NeedFullSync = true
}
case *api.Service:
Expand Down Expand Up @@ -828,25 +842,25 @@ func (c *k8scache) Notify(old, cur interface{}) {
case *gatewayv1alpha2.Gateway:
gw := cur
if old == nil {
ch.GatewaysAdd = append(ch.GatewaysAdd, gw)
ch.GatewaysA2Add = append(ch.GatewaysA2Add, gw)
} else {
ch.GatewaysUpd = append(ch.GatewaysUpd, gw)
ch.GatewaysA2Upd = append(ch.GatewaysA2Upd, gw)
}
ch.NeedFullSync = true
case *gatewayv1alpha2.GatewayClass:
cls := cur
if old == nil {
ch.GatewayClassesAdd = append(ch.GatewayClassesAdd, cls)
ch.GatewayClassesA2Add = append(ch.GatewayClassesA2Add, cls)
} else {
ch.GatewayClassesUpd = append(ch.GatewayClassesUpd, cls)
ch.GatewayClassesA2Upd = append(ch.GatewayClassesA2Upd, cls)
}
ch.NeedFullSync = true
case *gatewayv1alpha2.HTTPRoute:
hr := cur
if old == nil {
ch.HTTPRoutesAdd = append(ch.HTTPRoutesAdd, hr)
ch.HTTPRoutesA2Add = append(ch.HTTPRoutesA2Add, hr)
} else {
ch.HTTPRoutesUpd = append(ch.HTTPRoutesUpd, hr)
ch.HTTPRoutesA2Upd = append(ch.HTTPRoutesA2Upd, hr)
}
ch.NeedFullSync = true
case *api.Endpoints:
Expand Down Expand Up @@ -945,31 +959,31 @@ func (c *k8scache) SwapChangedObjects() *convtypes.ChangedObjects {
for _, cls := range ch.IngressClassesAdd {
addChanges(convtypes.ResourceIngressClass, eventAdd, "", cls.Name)
}
for _, gw := range ch.GatewaysDel {
for _, gw := range ch.GatewaysA2Del {
addChanges(convtypes.ResourceGateway, eventDel, gw.Namespace, gw.Name)
}
for _, gw := range ch.GatewaysUpd {
for _, gw := range ch.GatewaysA2Upd {
addChanges(convtypes.ResourceGateway, eventUpdate, gw.Namespace, gw.Name)
}
for _, gw := range ch.GatewaysAdd {
for _, gw := range ch.GatewaysA2Add {
addChanges(convtypes.ResourceGateway, eventAdd, gw.Namespace, gw.Name)
}
for _, cls := range ch.GatewayClassesDel {
for _, cls := range ch.GatewayClassesA2Del {
addChanges(convtypes.ResourceGatewayClass, eventDel, "", cls.Name)
}
for _, cls := range ch.GatewayClassesUpd {
for _, cls := range ch.GatewayClassesA2Upd {
addChanges(convtypes.ResourceGatewayClass, eventUpdate, "", cls.Name)
}
for _, cls := range ch.GatewayClassesAdd {
for _, cls := range ch.GatewayClassesA2Add {
addChanges(convtypes.ResourceGatewayClass, eventAdd, "", cls.Name)
}
for _, hr := range ch.HTTPRoutesDel {
for _, hr := range ch.HTTPRoutesA2Del {
addChanges(convtypes.ResourceHTTPRoute, eventDel, hr.Namespace, hr.Name)
}
for _, hr := range ch.HTTPRoutesUpd {
for _, hr := range ch.HTTPRoutesA2Upd {
addChanges(convtypes.ResourceHTTPRoute, eventUpdate, hr.Namespace, hr.Name)
}
for _, hr := range ch.HTTPRoutesAdd {
for _, hr := range ch.HTTPRoutesA2Add {
addChanges(convtypes.ResourceHTTPRoute, eventAdd, hr.Namespace, hr.Name)
}
for _, ep := range ch.EndpointsNew {
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/legacy/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ func (hc *HAProxyController) configController() {
DisableKeywords: utils.Split(hc.cfg.DisableConfigKeywords, ","),
AcmeTrackTLSAnn: hc.cfg.AcmeTrackTLSAnn,
TrackInstances: hc.cfg.TrackOldInstances,
HasGateway: hc.cache.hasGateway(),
HasGatewayA2: hc.cache.hasGateway(),
HasGatewayB1: false,
EnableEPSlices: hc.cfg.EnableEndpointSlicesAPI,
}
}
Expand Down
Loading

0 comments on commit b5d1b92

Please sign in to comment.