Skip to content

Commit

Permalink
Merge pull request #898 from jcmoraisjr/jm-check-gateway-default
Browse files Browse the repository at this point in the history
Check by default if gateway api crds are installed
  • Loading branch information
jcmoraisjr authored Feb 4, 2022
2 parents 2a2e41e + c7ee930 commit 98b8a99
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
7 changes: 5 additions & 2 deletions docs/content/en/docs/configuration/command-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,8 +607,11 @@ changes in one single shot. The default value is `200ms`.

Since v0.13

Enables Gateway API watch and parse. The Gateway API CRDs should be installed before enable
this option. See also the Gateway API configuration [doc]({{% relref "gateway-api" %}}).
Enables Gateway API watch and parse. This option is enabled by default since v0.14 and controller
will start the listener only if the Gateway API CRDs are found. Add `--watch-gateway=false`
option to instruct the controller to not try to listen to the CRDs. The controller should be
restarted if the CRDs are installed after starting the controller. See also the Gateway API
configuration [doc]({{% relref "gateway-api" %}}).

---

Expand Down
11 changes: 1 addition & 10 deletions docs/content/en/docs/configuration/gateway-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The following steps configure the Kubernetes cluster and HAProxy Ingress to read

* Manually install the Gateway API CRDs, see the Gateway API [documentation](https://gateway-api.sigs.k8s.io/v1alpha1/guides/getting-started/#installing-gateway-api-crds-manually)
* ... or simply `kubectl kustomize "github.com/kubernetes-sigs/gateway-api/config/crd?ref=v0.3.0" | kubectl apply -f -`
* Add the controller's [`--watch-gateway`]({{% relref "command-line#watch-gateway" %}}) command-line option
* Start (or restart) the controller

See below the [getting started steps](#getting-started).

Expand Down Expand Up @@ -58,15 +58,6 @@ kubectl --namespace default create deployment echoserver --image k8s.gcr.io/echo
kubectl --namespace default expose deployment echoserver --port=8080
```

Add the [`--watch-gateway`]({{% relref "command-line#watch-gateway" %}}) command-line option in the `haproxy-ingress-values.yaml` file and [`helm upgrade ...`]({{% relref "/docs/getting-started#installation" %}}) the controller (or simply edit the deployment):

```yaml
controller:
...
extraArgs:
watch-gateway: "true"
```
A GatewayClass enables Gateways to be read and parsed by HAProxy Ingress. Create a GatewayClass with the following content:

```yaml
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/ingress/controller/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ new /path, e.g., controller-class=staging will make this controller look for
declare neither the kubernetes.io/ingress.class annotation nor the
<ingress>.spec.ingressClassName field. Defaults to false`)

watchGateway = flags.Bool("watch-gateway", false,
watchGateway = flags.Bool("watch-gateway", true,
`Watch and parse resources from the Gateway API`)

masterWorker = flags.Bool("master-worker", false,
Expand Down
43 changes: 31 additions & 12 deletions pkg/controller/listers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
gateway "sigs.k8s.io/gateway-api/apis/v1alpha1"
"sigs.k8s.io/gateway-api/pkg/client/clientset/versioned"
informersgateway "sigs.k8s.io/gateway-api/pkg/client/informers/externalversions"
informersgatewayv1alpha1 "sigs.k8s.io/gateway-api/pkg/client/informers/externalversions/apis/v1alpha1"
listersgateway "sigs.k8s.io/gateway-api/pkg/client/listers/apis/v1alpha1"
Expand Down Expand Up @@ -136,20 +137,38 @@ func createListers(
}

if watchGateway {
var option informersgateway.SharedInformerOption
if clusterWatch {
option = informersgateway.WithTweakListOptions(nil)
gv := "networking.x-k8s.io/v1alpha1"
hasAPI := false
if client, ok := client.(versioned.Interface); ok {
resources, err := client.Discovery().ServerResourcesForGroupVersion(gv)
if err == nil && resources != nil {
names := map[string]bool{}
for _, r := range resources.APIResources {
names[r.SingularName] = true
}
hasAPI =
names["gatewayclass"] && names["gateway"] && names["httproute"] &&
names["tlsroute"] && names["tcproute"] && names["udproute"] && names["backendpolicy"]
}
}
if hasAPI {
var option informersgateway.SharedInformerOption
if clusterWatch {
option = informersgateway.WithTweakListOptions(nil)
} else {
option = informersgateway.WithNamespace(watchNamespace)
}
informer := informersgateway.NewSharedInformerFactoryWithOptions(client, resync, option)
l.createGatewayLister(informer.Networking().V1alpha1().Gateways())
l.createGatewayClassLister(informer.Networking().V1alpha1().GatewayClasses())
l.createHTTPRouteLister(informer.Networking().V1alpha1().HTTPRoutes())
l.createTLSRouteLister(informer.Networking().V1alpha1().TLSRoutes())
l.createTCPRouteLister(informer.Networking().V1alpha1().TCPRoutes())
l.createUDPRouteLister(informer.Networking().V1alpha1().UDPRoutes())
l.createBackendPolicyLister(informer.Networking().V1alpha1().BackendPolicies())
} else {
option = informersgateway.WithNamespace(watchNamespace)
l.logger.Warn("gateway API '%s' was not found, skipping", gv)
}
informer := informersgateway.NewSharedInformerFactoryWithOptions(client, resync, option)
l.createGatewayLister(informer.Networking().V1alpha1().Gateways())
l.createGatewayClassLister(informer.Networking().V1alpha1().GatewayClasses())
l.createHTTPRouteLister(informer.Networking().V1alpha1().HTTPRoutes())
l.createTLSRouteLister(informer.Networking().V1alpha1().TLSRoutes())
l.createTCPRouteLister(informer.Networking().V1alpha1().TCPRoutes())
l.createUDPRouteLister(informer.Networking().V1alpha1().UDPRoutes())
l.createBackendPolicyLister(informer.Networking().V1alpha1().BackendPolicies())
}

return l
Expand Down

0 comments on commit 98b8a99

Please sign in to comment.