Skip to content

Commit

Permalink
all: migrate to networking.k8s.io/v1beta1.Ingress
Browse files Browse the repository at this point in the history
Fixes projectcontour#1633

This PR changes Contour to use the networking/v1beta1.Ingress version of
the Ingress object. Contour uses this object internally and relies on
the API server to convert the older extensions/v1beta1 form to the current
networking/v1beta1 form.  Where this translation is not possible because
the cluster is too old, Contour performs this translation internally.

The catch is when relying on this translation mechanism, registering for
*both* extensions/v1beta1 and networking/v1beta1 causes objects that are
natively in the former format to appear twice. Thus, contour can only
subscribe to one of extensions/v1beta1 or networking/v1beta1. Thus
this PR provides a new flag, --use-extensions-v1beta1-ingress to
subscribe to the older form in pre 1.14 clusters.

Signed-off-by: Dave Cheney <dave@cheney.net>
  • Loading branch information
davecheney committed Oct 11, 2019
1 parent 71ae175 commit b0a268b
Show file tree
Hide file tree
Showing 31 changed files with 97 additions and 27 deletions.
13 changes: 12 additions & 1 deletion cmd/contour/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ func registerServe(app *kingpin.Application) (*kingpin.CmdClause, *serveContext)

serve.Flag("accesslog-format", "Format for Envoy access logs").StringVar(&ctx.AccessLogFormat)
serve.Flag("disable-leader-election", "Disable leader election mechanism").BoolVar(&ctx.DisableLeaderElection)

serve.Flag("use-extensions-v1beta1-ingress", "Subscribe to the deprecated extensions/v1beta1.Ingress type").BoolVar(&ctx.UseExtensionsV1beta1Ingress)
return serve, ctx
}

Expand Down Expand Up @@ -168,7 +170,16 @@ func doServe(log logrus.FieldLogger, ctx *serveContext) error {

// step 4. register our resource event handler with the k8s informers.
coreInformers.Core().V1().Services().Informer().AddEventHandler(eh)
coreInformers.Extensions().V1beta1().Ingresses().Informer().AddEventHandler(eh)

// After K8s 1.13 the API server will automatically translate extensions/v1beta1.Ingress objects
// to networking/v1beta1.Ingress objects so we should only listen for one type or the other.
// The default behavior is to listen for networking/v1beta1.Ingress objects and let the API server
// transparently upgrade the extensions version for us.
if ctx.UseExtensionsV1beta1Ingress {
coreInformers.Extensions().V1beta1().Ingresses().Informer().AddEventHandler(eh)
} else {
coreInformers.Networking().V1beta1().Ingresses().Informer().AddEventHandler(eh)
}
contourInformers.Contour().V1beta1().IngressRoutes().Informer().AddEventHandler(eh)
contourInformers.Contour().V1beta1().TLSCertificateDelegations().Informer().AddEventHandler(eh)
contourInformers.Projectcontour().V1().HTTPProxies().Informer().AddEventHandler(eh)
Expand Down
10 changes: 10 additions & 0 deletions cmd/contour/servecontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ type serveContext struct {

// RequestTimeout sets the client request timeout globally for Contour.
RequestTimeout time.Duration `yaml:"request-timeout,omitempty"`

// Should Contour fall back to registering an informer for the deprecated
// extensions/v1beta1.Ingress type.
// By default this value is false, meaning Contour will register an informer for
// networking/v1beta1.Ingress and expect the API server to rewrite extensions/v1beta1.Ingress
// objects transparently.
// If the value is true, Contour will register for extensions/v1beta1.Ingress type and do
// the rewrite itself.
UseExtensionsV1beta1Ingress bool `yaml:"-"`
}

// newServeContext returns a serveContext initialized to defaults.
Expand Down Expand Up @@ -153,6 +162,7 @@ func newServeContext() *serveContext {
Namespace: "projectcontour",
Name: "leader-elect",
},
UseExtensionsV1beta1Ingress: false,
}
}

Expand Down
8 changes: 8 additions & 0 deletions examples/contour/02-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ rules:
- get
- list
- watch
- apiGroups:
- "networking.k8s.io"
resources:
- ingresses
verbs:
- get
- list
- watch
- apiGroups: ["contour.heptio.com"]
resources: ["ingressroutes", "tlscertificatedelegations"]
verbs:
Expand Down
1 change: 1 addition & 0 deletions examples/contour/03-contour.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ spec:
- args:
- serve
- --incluster
- --use-extensions-v1beta1-ingress
- --xds-address=0.0.0.0
- --xds-port=8001
- --envoy-service-http-port=80
Expand Down
9 changes: 9 additions & 0 deletions examples/render/contour.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,14 @@ rules:
- get
- list
- watch
- apiGroups:
- "networking.k8s.io"
resources:
- ingresses
verbs:
- get
- list
- watch
- apiGroups: ["contour.heptio.com"]
resources: ["ingressroutes", "tlscertificatedelegations"]
verbs:
Expand Down Expand Up @@ -465,6 +473,7 @@ spec:
- args:
- serve
- --incluster
- --use-extensions-v1beta1-ingress
- --xds-address=0.0.0.0
- --xds-port=8001
- --envoy-service-http-port=80
Expand Down
2 changes: 1 addition & 1 deletion internal/contour/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/projectcontour/contour/internal/envoy"
"github.com/projectcontour/contour/internal/protobuf"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/contour/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/projectcontour/contour/internal/assert"
"github.com/projectcontour/contour/internal/envoy"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/contour/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/projectcontour/contour/internal/envoy"
"github.com/projectcontour/contour/internal/protobuf"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/contour/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/projectcontour/contour/internal/assert"
"github.com/projectcontour/contour/internal/dag"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/dag/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"time"

envoy_api_v2_auth "github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
)

// compatAnnotation checks the Object for the given annotation, first with the
Expand Down
2 changes: 1 addition & 1 deletion internal/dag/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

"github.com/projectcontour/contour/internal/assert"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/dag/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"strings"

v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
"k8s.io/apimachinery/pkg/util/intstr"

ingressroutev1 "github.com/projectcontour/contour/apis/contour/v1beta1"
Expand Down
2 changes: 1 addition & 1 deletion internal/dag/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
ingressroutev1 "github.com/projectcontour/contour/apis/contour/v1beta1"
projcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
Expand Down
33 changes: 32 additions & 1 deletion internal/dag/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
package dag

import (
"bytes"
"encoding/json"

v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
"k8s.io/client-go/tools/cache"

ingressroutev1 "github.com/projectcontour/contour/apis/contour/v1beta1"
Expand Down Expand Up @@ -105,6 +109,16 @@ func (kc *KubernetesCache) Insert(obj interface{}) bool {
}
kc.ingresses[m] = obj
return true
case *extensionsv1beta1.Ingress:
ingress := new(v1beta1.Ingress)
if err := transposeIngress(obj, ingress); err != nil {
om := obj.GetObjectMeta()
kc.WithField("name", om.GetName()).
WithField("namespace", om.GetNamespace()).
Error(err)
return false
}
return kc.Insert(ingress)
case *ingressroutev1.IngressRoute:
class := ingressClass(obj)
if class != "" && class != kc.ingressClass() {
Expand Down Expand Up @@ -183,6 +197,11 @@ func (kc *KubernetesCache) remove(obj interface{}) bool {
_, ok := kc.ingresses[m]
delete(kc.ingresses, m)
return ok
case *extensionsv1beta1.Ingress:
m := toMeta(obj)
_, ok := kc.ingresses[m]
delete(kc.ingresses, m)
return ok
case *ingressroutev1.IngressRoute:
m := toMeta(obj)
_, ok := kc.ingressroutes[m]
Expand Down Expand Up @@ -391,3 +410,15 @@ func (kc *KubernetesCache) secretTriggersRebuild(secret *v1.Secret) bool {

return false
}

// transposeIngress transposes extensionis/v1beta1.Ingress objects into
// networking/v1beta1.Ingress objects.
func transposeIngress(src *extensionsv1beta1.Ingress, dst *v1beta1.Ingress) error {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
if err := enc.Encode(src); err != nil {
return nil
}
dec := json.NewDecoder(&buf)
return dec.Decode(dst)
}
2 changes: 1 addition & 1 deletion internal/dag/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
projcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/dag/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

ingressroutev1 "github.com/projectcontour/contour/apis/contour/v1beta1"
projcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
)

func retryPolicy(rp *projcontour.RetryPolicy) *RetryPolicy {
Expand Down
2 changes: 1 addition & 1 deletion internal/dag/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
ingressroutev1 "github.com/projectcontour/contour/apis/contour/v1beta1"
projcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/internal/assert"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/dag/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
projcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/internal/assert"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/e2e/cds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/projectcontour/contour/internal/protobuf"
"google.golang.org/grpc"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/e2e/lds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"github.com/projectcontour/contour/internal/protobuf"
"google.golang.org/grpc"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/e2e/rds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/projectcontour/contour/internal/protobuf"
"google.golang.org/grpc"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/e2e/sds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/projectcontour/contour/internal/dag"
"github.com/projectcontour/contour/internal/envoy"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/featuretests/ingressclass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/projectcontour/contour/internal/contour"
"github.com/projectcontour/contour/internal/envoy"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/featuretests/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package featuretests

import (
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
"k8s.io/apimachinery/pkg/util/intstr"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/featuretests/retrypolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
projcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/internal/envoy"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/featuretests/timeoutpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/projectcontour/contour/internal/contour"
"github.com/projectcontour/contour/internal/envoy"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/featuretests/tlsprotocolversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
projcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/internal/envoy"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/featuretests/upstreamprotocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/featuretests/websockets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
projcontour "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/internal/envoy"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
Expand Down
2 changes: 1 addition & 1 deletion site/examples/kuard.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ spec:
sessionAffinity: None
type: ClusterIP
---
apiVersion: extensions/v1beta1
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: kuard
Expand Down

0 comments on commit b0a268b

Please sign in to comment.