Skip to content

Commit

Permalink
Move CRD informer list generation to k8s
Browse files Browse the repository at this point in the history
This is in preparation for adding in filtering by discovered CRD types.

Updates #2219

Signed-off-by: Nick Young <[email protected]>
  • Loading branch information
Nick Young committed Mar 18, 2020
1 parent a42493e commit b9aa0a9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 18 deletions.
25 changes: 7 additions & 18 deletions cmd/contour/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ import (
"syscall"
"time"

projectcontour "github.com/projectcontour/contour/apis/projectcontour/v1"

ingressroutev1 "github.com/projectcontour/contour/apis/contour/v1beta1"

serviceapis "sigs.k8s.io/service-apis/api/v1alpha1"

"github.com/projectcontour/contour/internal/contour"
"github.com/projectcontour/contour/internal/dag"
"github.com/projectcontour/contour/internal/debug"
Expand Down Expand Up @@ -206,22 +200,17 @@ func doServe(log logrus.FieldLogger, ctx *serveContext) error {
// using the SyncList to keep track of what to sync later.
var informerSyncList k8s.InformerSyncList

informerSyncList.Add(dynamicInformerFactory.ForResource(ingressroutev1.IngressRouteGVR).Informer()).AddEventHandler(dynamicHandler)
informerSyncList.Add(dynamicInformerFactory.ForResource(ingressroutev1.TLSCertificateDelegationGVR).Informer()).AddEventHandler(dynamicHandler)
informerSyncList.Add(dynamicInformerFactory.ForResource(projectcontour.HTTPProxyGVR).Informer()).AddEventHandler(dynamicHandler)
informerSyncList.Add(dynamicInformerFactory.ForResource(projectcontour.TLSCertificateDelegationGVR).Informer()).AddEventHandler(dynamicHandler)
// Build the default GVR to Informer lookup object
gvri := k8s.NewGVRInformerLookupDefaults(dynamicInformerFactory, ctx.UseExperimentalServiceAPITypes)

// Use the GVR to Informer lookup map to register all the required informers.
for _, inf := range gvri.Lookup {
informerSyncList.Add(inf).AddEventHandler(dynamicHandler)
}

informerSyncList.Add(informerFactory.Core().V1().Services().Informer()).AddEventHandler(dynamicHandler)
informerSyncList.Add(informerFactory.Networking().V1beta1().Ingresses().Informer()).AddEventHandler(dynamicHandler)

if ctx.UseExperimentalServiceAPITypes {
log.Info("Enabling Experimental Service APIs types")
informerSyncList.Add(dynamicInformerFactory.ForResource(serviceapis.GroupVersion.WithResource("gatewayclasses")).Informer()).AddEventHandler(dynamicHandler)
informerSyncList.Add(dynamicInformerFactory.ForResource(serviceapis.GroupVersion.WithResource("gateways")).Informer()).AddEventHandler(dynamicHandler)
informerSyncList.Add(dynamicInformerFactory.ForResource(serviceapis.GroupVersion.WithResource("httproutes")).Informer()).AddEventHandler(dynamicHandler)
informerSyncList.Add(dynamicInformerFactory.ForResource(serviceapis.GroupVersion.WithResource("tcproutes")).Informer()).AddEventHandler(dynamicHandler)
}

// Add informers for each root-ingressroute namespaces
for _, factory := range namespacedInformerFactories {
informerSyncList.Add(factory.Core().V1().Secrets().Informer()).AddEventHandler(dynamicHandler)
Expand Down
61 changes: 61 additions & 0 deletions internal/k8s/gvrautodetect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright © 2020 VMware
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package k8s

import (
projectcontour "github.com/projectcontour/contour/apis/projectcontour/v1"

ingressroutev1 "github.com/projectcontour/contour/apis/contour/v1beta1"

serviceapis "sigs.k8s.io/service-apis/api/v1alpha1"

"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/informers"
"k8s.io/client-go/tools/cache"
)

type GVRMap map[schema.GroupVersionResource]cache.SharedIndexInformer
type GVRInformer struct {
Lookup GVRMap
}

type ForResource interface {
ForResource(gvr schema.GroupVersionResource) informers.GenericInformer
}

func NewGVRInformerLookupDefaults(inffactory ForResource, serviceAPIs bool) GVRInformer {

var gvri GVRInformer

defaultGVRs := []schema.GroupVersionResource{
ingressroutev1.IngressRouteGVR,
ingressroutev1.TLSCertificateDelegationGVR,
projectcontour.HTTPProxyGVR,
projectcontour.TLSCertificateDelegationGVR,
}

if serviceAPIs {
defaultGVRs = append(defaultGVRs, serviceapis.GroupVersion.WithResource("gatewayclasses"))
defaultGVRs = append(defaultGVRs, serviceapis.GroupVersion.WithResource("gateways"))
defaultGVRs = append(defaultGVRs, serviceapis.GroupVersion.WithResource("httproutes"))
defaultGVRs = append(defaultGVRs, serviceapis.GroupVersion.WithResource("tcproutes"))
}

gvri.Lookup = make(GVRMap)

for _, gvr := range defaultGVRs {
gvri.Lookup[gvr] = inffactory.ForResource(gvr).Informer()
}
return gvri
}

0 comments on commit b9aa0a9

Please sign in to comment.