Skip to content

Commit

Permalink
Migrate Service and Ingress to client-go dynamic client
Browse files Browse the repository at this point in the history
This is further refactoring for #2219.

Moves Service and Ingress to the client-go dynamic client, leaving only Secrets and Endpoints not handled by the new InformerSet. Secrets need namespace handling, and Endpoints need a separate handler.

Signed-off-by: Nick Young <[email protected]>
  • Loading branch information
Nick Young committed Mar 24, 2020
1 parent fb5056e commit f9a74b1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
5 changes: 1 addition & 4 deletions cmd/contour/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,7 @@ func doServe(log logrus.FieldLogger, ctx *serveContext) error {
informerSyncList.RegisterInformer(inf, dynamicHandler)
}

// We need to register this separately for now, as the core stuff uses a non-dynamic InformerFactory.
informerSyncList.RegisterInformer(informerFactory.Core().V1().Services().Informer(), dynamicHandler)
informerSyncList.RegisterInformer(informerFactory.Networking().V1beta1().Ingresses().Informer(), dynamicHandler)

// TODO(youngnick): Move this logic out to internal/k8s/informers.go somehow.
// Add informers for each root-ingressroute namespaces
for _, factory := range namespacedInformerFactories {
informerSyncList.RegisterInformer(factory.Core().V1().Secrets().Informer(), dynamicHandler)
Expand Down
24 changes: 24 additions & 0 deletions internal/k8s/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import (
ingressroutev1 "github.com/projectcontour/contour/apis/contour/v1beta1"
projectcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/api/networking/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/cache"
serviceapis "sigs.k8s.io/service-apis/api/v1alpha1"
)
Expand Down Expand Up @@ -91,6 +94,16 @@ func NewUnstructuredConverter() (*UnstructuredConverter, error) {
projectcontour.AddKnownTypes(uc.scheme)
ingressroutev1.AddKnownTypes(uc.scheme)

// Add the core types we need
if err := scheme.AddToScheme(uc.scheme); err != nil {
return nil, err
}

// Setup converter to understand Ingress types
if err := v1beta1.AddToScheme(uc.scheme); err != nil {
return nil, err
}

// The kubebuilder tools' contract here is different, yay.
if err := serviceapis.AddToScheme(uc.scheme); err != nil {
return nil, err
Expand All @@ -107,6 +120,17 @@ func (c *UnstructuredConverter) Convert(obj interface{}) (interface{}, error) {
return obj, nil
}
switch unstructured.GetKind() {
case "Ingress":
// TODO(youngnick): Have this check for v1 or v1beta1
// Currently we have no support for v1.
// unstructured.GetObjectKind().GroupVersionKind() will probably be useful.
i := &v1beta1.Ingress{}
err := c.scheme.Convert(obj, i, nil)
return i, err
case "Service":
s := &v1.Service{}
err := c.scheme.Convert(obj, s, nil)
return s, err
case "HTTPProxy":
proxy := &projectcontour.HTTPProxy{}
err := c.scheme.Convert(obj, proxy, nil)
Expand Down
4 changes: 4 additions & 0 deletions internal/k8s/informers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import (
projectcontour "github.com/projectcontour/contour/apis/projectcontour/v1"

ingressroutev1 "github.com/projectcontour/contour/apis/contour/v1beta1"
"k8s.io/api/networking/v1beta1"

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

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic/dynamicinformer"
"k8s.io/client-go/tools/cache"
Expand All @@ -41,6 +43,8 @@ func DefaultInformerSet(inffactory dynamicinformer.DynamicSharedInformerFactory,
ingressroutev1.TLSCertificateDelegationGVR,
projectcontour.HTTPProxyGVR,
projectcontour.TLSCertificateDelegationGVR,
corev1.SchemeGroupVersion.WithResource("services"),
v1beta1.SchemeGroupVersion.WithResource("ingresses"),
}

// TODO(youngnick): Remove this boolean once we have autodetection of available types (Further work on #2219).
Expand Down

0 comments on commit f9a74b1

Please sign in to comment.