From 5e2524de86374ea006ce206724bd378f2f07c83d Mon Sep 17 00:00:00 2001 From: Vinicyus Macedo <7549205+vinicyusmacedo@users.noreply.github.com> Date: Mon, 21 Oct 2019 10:56:27 -0300 Subject: [PATCH] Added ingress class annotation (#616) * Added ingress class annotation --- helm/README.md | 3 +++ helm/chart/teresa/templates/deployment.yaml | 2 ++ helm/chart/teresa/values.yaml | 1 + pkg/server/deploy/deploy.go | 4 ++-- pkg/server/deploy/deploy_test.go | 2 +- pkg/server/deploy/handlers.go | 1 + pkg/server/k8s/client.go | 19 +++++++++++++++---- 7 files changed, 25 insertions(+), 7 deletions(-) diff --git a/helm/README.md b/helm/README.md index 495ea60ee..6977c3e89 100644 --- a/helm/README.md +++ b/helm/README.md @@ -110,6 +110,9 @@ Parameter | Description | Default `rbac.enabled` | If true, this configure teresa deployment to use rbac, for now it will use the `cluster-admin` role | `false` `apps.ingress` | If true, teresa will create a ingress when expose the app | `false` `apps.service_type` | The type used to create the app server | `LoadBalancer` +`apps.ingress_class` | The ingress class to be used | `` +`slugstore.image`| The image to be used for the slugstore | `""` +`slugbuilder.image`| The image to be used for the slugbuilder | `""` Specify each parameter using the `--set key=value[,key=value]` argument to `helm install`. For example, diff --git a/helm/chart/teresa/templates/deployment.yaml b/helm/chart/teresa/templates/deployment.yaml index 324f62a2e..8e81a445b 100644 --- a/helm/chart/teresa/templates/deployment.yaml +++ b/helm/chart/teresa/templates/deployment.yaml @@ -147,6 +147,8 @@ spec: value: {{ .Values.apps.ingress | quote}} - name: TERESA_DEPLOY_DEFAULT_SERVICE_TYPE value: {{ .Values.apps.service_type }} + - name: TERESA_DEPLOY_INGRESS_CLASS + value: {{ .Values.apps.ingress_class }} volumeMounts: {{- if .Values.tls.crt }} - mountPath: /etc/teresa diff --git a/helm/chart/teresa/values.yaml b/helm/chart/teresa/values.yaml index 669967f99..86fde6c1d 100644 --- a/helm/chart/teresa/values.yaml +++ b/helm/chart/teresa/values.yaml @@ -43,6 +43,7 @@ rbac: apps: ingress: false service_type: LoadBalancer + ingress_class: slugstore: image: slugbuilder: diff --git a/pkg/server/deploy/deploy.go b/pkg/server/deploy/deploy.go index a33a970e0..ceea7e305 100644 --- a/pkg/server/deploy/deploy.go +++ b/pkg/server/deploy/deploy.go @@ -38,7 +38,7 @@ type Operations interface { type K8sOperations interface { CreateOrUpdateDeploy(deploySpec *spec.Deploy) error CreateOrUpdateCronJob(cronJobSpec *spec.CronJob) error - ExposeDeploy(namespace, name, svcType, portName string, vHosts []string, reserveStaticIp bool, w io.Writer) error + ExposeDeploy(namespace, name, svcType, portName string, vHosts []string, reserveStaticIp bool, ingressClass string, w io.Writer) error ReplicaSetListByLabel(namespace, label, value string) ([]*ReplicaSetListItem, error) DeployRollbackToRevision(namespace, name, revision string) error CreateOrUpdateConfigMap(namespace, name string, data map[string]string) error @@ -234,7 +234,7 @@ func (ops *DeployOperations) exposeApp(a *app.App, w io.Writer) error { } svcType := ops.serviceType(a) vHosts := strings.Split(a.VirtualHost, ",") - if err := ops.k8s.ExposeDeploy(a.Name, a.Name, svcType, a.Protocol, vHosts, a.ReserveStaticIp, w); err != nil { + if err := ops.k8s.ExposeDeploy(a.Name, a.Name, svcType, a.Protocol, vHosts, a.ReserveStaticIp, ops.opts.IngressClass, w); err != nil { return err } if a.ReserveStaticIp { diff --git a/pkg/server/deploy/deploy_test.go b/pkg/server/deploy/deploy_test.go index ed543552d..05a1f73db 100644 --- a/pkg/server/deploy/deploy_test.go +++ b/pkg/server/deploy/deploy_test.go @@ -62,7 +62,7 @@ func (f *fakeK8sOperations) CreateOrUpdateCronJob(cronJobSpec *spec.CronJob) err return f.createCronJobReturn } -func (f *fakeK8sOperations) ExposeDeploy(namespace, name, svcType, portName string, vHosts []string, reserveStaticIp bool, w io.Writer) error { +func (f *fakeK8sOperations) ExposeDeploy(namespace, name, svcType, portName string, vHosts []string, reserveStaticIp bool, ingressClass string, w io.Writer) error { f.exposeDeployWasCalled = true return nil } diff --git a/pkg/server/deploy/handlers.go b/pkg/server/deploy/handlers.go index d94cf3ba0..abcb23cef 100644 --- a/pkg/server/deploy/handlers.go +++ b/pkg/server/deploy/handlers.go @@ -26,6 +26,7 @@ type Options struct { BuildLimitMemory string `split_words:"true" default:"1Gi"` DefaultServiceType string `split_words:"true" default:"LoadBalancer"` CloudSQLProxyImage string `split_words:"true" default:"gcr.io/cloudsql-docker/gce-proxy:1.11"` + IngressClass string `split_words:"true" default:""` } type Service struct { diff --git a/pkg/server/k8s/client.go b/pkg/server/k8s/client.go index 76acf12a6..260e44200 100644 --- a/pkg/server/k8s/client.go +++ b/pkg/server/k8s/client.go @@ -623,14 +623,25 @@ func (k *Client) HasIngress(namespace, appName string) (bool, error) { return true, nil } -func (k *Client) createIngress(namespace, appName string, vHosts []string, reserveStaticIp bool) error { +func (k *Client) createIngress(namespace, appName string, vHosts []string, reserveStaticIp bool, ingressClass string) error { kc, err := k.buildClient() if err != nil { return err } igsSpec := ingressSpec(namespace, appName, vHosts, reserveStaticIp) _, err = kc.ExtensionsV1beta1().Ingresses(namespace).Create(igsSpec) - return errors.Wrap(err, "create ingress failed") + if err != nil { + return errors.Wrap(err, "create ingress failed") + } + if ingressClass != "" { + if err = k.SetIngressAnnotations( + namespace, appName, + map[string]string{"kubernetes.io/ingress.class": ingressClass}, + ); err != nil { + return errors.Wrap(err, "create ingress failed") + } + } + return nil } func (k *Client) UpdateIngress(namespace, name string, vHosts []string, reserveStaticIp bool) error { @@ -688,7 +699,7 @@ func (c *Client) IngressAnnotations(namespace, ingName string) (map[string]strin } // ExposeDeploy creates a service and/or a ingress if needed -func (k *Client) ExposeDeploy(namespace, appName, svcType, portName string, vHosts []string, reserveStaticIp bool, w io.Writer) error { +func (k *Client) ExposeDeploy(namespace, appName, svcType, portName string, vHosts []string, reserveStaticIp bool, ingressClass string, w io.Writer) error { hasSrv, err := k.hasService(namespace, appName) if err != nil { return err @@ -710,7 +721,7 @@ func (k *Client) ExposeDeploy(namespace, appName, svcType, portName string, vHos } if !hasIgs { fmt.Fprintln(w, "Creating ingress") - if err := k.createIngress(namespace, appName, vHosts, reserveStaticIp); err != nil { + if err := k.createIngress(namespace, appName, vHosts, reserveStaticIp, ingressClass); err != nil { return err } }