Skip to content

Commit

Permalink
Add ingress class support for Contour
Browse files Browse the repository at this point in the history
Add `-ingress-class` command flag. When set, the specified class is used to annotate the generated HTTPProxy objects.
  • Loading branch information
stefanprodan committed May 14, 2020
1 parent a6b8d19 commit ae479a2
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
8 changes: 5 additions & 3 deletions cmd/flagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var (
meshProvider string
selectorLabels string
ingressAnnotationsPrefix string
ingressClass string
enableLeaderElection bool
leaderElectionNamespace string
enableConfigTracking bool
Expand All @@ -77,9 +78,10 @@ func init() {
flag.BoolVar(&zapReplaceGlobals, "zap-replace-globals", false, "Whether to change the logging level of the global zap logger.")
flag.StringVar(&zapEncoding, "zap-encoding", "json", "Zap logger encoding.")
flag.StringVar(&namespace, "namespace", "", "Namespace that flagger would watch canary object.")
flag.StringVar(&meshProvider, "mesh-provider", "istio", "Service mesh provider, can be istio, linkerd, appmesh, supergloo, nginx or smi.")
flag.StringVar(&meshProvider, "mesh-provider", "istio", "Service mesh provider, can be istio, linkerd, appmesh, contour, gloo or nginx.")
flag.StringVar(&selectorLabels, "selector-labels", "app,name,app.kubernetes.io/name", "List of pod labels that Flagger uses to create pod selectors.")
flag.StringVar(&ingressAnnotationsPrefix, "ingress-annotations-prefix", "nginx.ingress.kubernetes.io", "Annotations prefix for ingresses.")
flag.StringVar(&ingressAnnotationsPrefix, "ingress-annotations-prefix", "nginx.ingress.kubernetes.io", "Annotations prefix for NGINX ingresses.")
flag.StringVar(&ingressClass, "ingress-class", "", "Ingress class used for annotating HTTPProxy objects.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false, "Enable leader election.")
flag.StringVar(&leaderElectionNamespace, "leader-election-namespace", "kube-system", "Namespace used to create the leader election config map.")
flag.BoolVar(&enableConfigTracking, "enable-config-tracking", true, "Enable secrets and configmaps tracking.")
Expand Down Expand Up @@ -169,7 +171,7 @@ func main() {
// start HTTP server
go server.ListenAndServe(port, 3*time.Second, logger, stopCh)

routerFactory := router.NewFactory(cfg, kubeClient, flaggerClient, ingressAnnotationsPrefix, logger, meshClient)
routerFactory := router.NewFactory(cfg, kubeClient, flaggerClient, ingressAnnotationsPrefix, ingressClass, logger, meshClient)

var configTracker canary.Tracker
if enableConfigTracking {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/scheduler_daemonset_fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func newDaemonSetFixture(c *flaggerv1.Canary) daemonSetFixture {
}

// init router
rf := router.NewFactory(nil, kubeClient, flaggerClient, "annotationsPrefix", logger, flaggerClient)
rf := router.NewFactory(nil, kubeClient, flaggerClient, "annotationsPrefix", "", logger, flaggerClient)

// init observer
observerFactory, _ := observers.NewFactory("fake")
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/scheduler_deployment_fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func newDeploymentFixture(c *flaggerv1.Canary) fixture {
}

// init router
rf := router.NewFactory(nil, kubeClient, flaggerClient, "annotationsPrefix", logger, flaggerClient)
rf := router.NewFactory(nil, kubeClient, flaggerClient, "annotationsPrefix", "", logger, flaggerClient)

// init observer
observerFactory, _ := observers.NewFactory("fake")
Expand Down
9 changes: 9 additions & 0 deletions pkg/router/contour.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ type ContourRouter struct {
contourClient clientset.Interface
flaggerClient clientset.Interface
logger *zap.SugaredLogger
ingressClass string
}

// Reconcile creates or updates the HTTP proxy
func (cr *ContourRouter) Reconcile(canary *flaggerv1.Canary) error {
annotation := "projectcontour.io/ingress.class"

apexName, primaryName, canaryName := canary.GetServiceNames()

newSpec := contourv1.HTTPProxySpec{
Expand Down Expand Up @@ -151,6 +154,12 @@ func (cr *ContourRouter) Reconcile(canary *flaggerv1.Canary) error {
},
}

if cr.ingressClass != "" {
proxy.Annotations = map[string]string{
annotation: cr.ingressClass,
}
}

_, err = cr.contourClient.ProjectcontourV1().HTTPProxies(canary.Namespace).Create(context.TODO(), proxy, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("HTTPProxy %s.%s create error: %w", apexName, canary.Namespace, err)
Expand Down
2 changes: 2 additions & 0 deletions pkg/router/contour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestContourRouter_Reconcile(t *testing.T) {
flaggerClient: mocks.flaggerClient,
contourClient: mocks.meshClient,
kubeClient: mocks.kubeClient,
ingressClass: "contour",
}

// init
Expand All @@ -31,6 +32,7 @@ func TestContourRouter_Reconcile(t *testing.T) {
require.Len(t, services, 2)
assert.Equal(t, uint32(100), services[0].Weight)
assert.Equal(t, uint32(0), services[1].Weight)
assert.Equal(t, "contour", proxy.Annotations["projectcontour.io/ingress.class"])

// test update
cd, err := mocks.flaggerClient.FlaggerV1beta1().Canaries("default").Get(context.TODO(), "podinfo", metav1.GetOptions{})
Expand Down
3 changes: 3 additions & 0 deletions pkg/router/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ type Factory struct {
meshClient clientset.Interface
flaggerClient clientset.Interface
ingressAnnotationsPrefix string
ingressClass string
logger *zap.SugaredLogger
}

func NewFactory(kubeConfig *restclient.Config, kubeClient kubernetes.Interface,
flaggerClient clientset.Interface,
ingressAnnotationsPrefix string,
ingressClass string,
logger *zap.SugaredLogger,
meshClient clientset.Interface) *Factory {
return &Factory{
Expand All @@ -31,6 +33,7 @@ func NewFactory(kubeConfig *restclient.Config, kubeClient kubernetes.Interface,
kubeClient: kubeClient,
flaggerClient: flaggerClient,
ingressAnnotationsPrefix: ingressAnnotationsPrefix,
ingressClass: ingressClass,
logger: logger,
}
}
Expand Down

0 comments on commit ae479a2

Please sign in to comment.