Skip to content

Commit

Permalink
Add --ingress-class flag to openfaas-ingress
Browse files Browse the repository at this point in the history
Allows users of traefik or other ingress controllers to set the
value they need through a flag.

Documented via flag and unit test updated.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed Nov 25, 2019
1 parent 010c4de commit f59225a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
21 changes: 15 additions & 6 deletions pkg/cmd/openfaas_ingress_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
type InputData struct {
IngressDomain string
CertmanagerEmail string
IngressClass string
}

func makeInstallOpenFaaSIngress() *cobra.Command {
Expand All @@ -30,16 +31,22 @@ func makeInstallOpenFaaSIngress() *cobra.Command {

openfaasIngress.Flags().StringP("domain", "d", "", "Custom Ingress Domain")
openfaasIngress.Flags().StringP("email", "e", "", "Letsencrypt Email")
openfaasIngress.Flags().String("ingress-class", "nginx", "Ingress class to be used such as nginx or traefik")

openfaasIngress.RunE = func(command *cobra.Command, args []string) error {

email, _ := command.Flags().GetString("email")
domain, _ := command.Flags().GetString("domain")
ingressClass, _ := command.Flags().GetString("ingress-class")

if email == "" || domain == "" {
return errors.New("both --email and --domain flags should be set and not empty, please set these values")
}

if ingressClass == "" {
return errors.New("--ingress-class must be set")
}

kubeConfigPath := getDefaultKubeconfig()

if command.Flags().Changed("kubeconfig") {
Expand All @@ -48,7 +55,7 @@ func makeInstallOpenFaaSIngress() *cobra.Command {

fmt.Printf("Using kubeconfig: %s\n", kubeConfigPath)

yamlBytes, templateErr := buildYaml(domain, email)
yamlBytes, templateErr := buildYAML(domain, email, ingressClass)
if templateErr != nil {
log.Print("Unable to install the application. Could not build the templated yaml file for the resources")
return templateErr
Expand Down Expand Up @@ -137,8 +144,8 @@ func writeTempFile(input []byte) (string, error) {
return filename, nil
}

func buildYaml(domain string, email string) ([]byte, error) {
tmpl, err := template.New("yaml").Parse(yamlTemplate)
func buildYAML(domain, email, ingressClass string) ([]byte, error) {
tmpl, err := template.New("yaml").Parse(ingressYamlTemplate)

if err != nil {
return nil, err
Expand All @@ -147,7 +154,9 @@ func buildYaml(domain string, email string) ([]byte, error) {
inputData := InputData{
IngressDomain: domain,
CertmanagerEmail: email,
IngressClass: ingressClass,
}

var tpl bytes.Buffer

err = tmpl.Execute(&tpl, inputData)
Expand All @@ -159,15 +168,15 @@ func buildYaml(domain string, email string) ([]byte, error) {
return tpl.Bytes(), nil
}

var yamlTemplate = `
var ingressYamlTemplate = `
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: openfaas-gateway
namespace: openfaas
annotations:
cert-manager.io/cluster-issuer: letsencrypt-prod
kubernetes.io/ingress.class: nginx
kubernetes.io/ingress.class: {{.IngressClass}}
spec:
rules:
- host: {{.IngressDomain}}
Expand Down Expand Up @@ -195,4 +204,4 @@ spec:
solvers:
- http01:
ingress:
class: nginx`
class: {{.IngressClass}}`
8 changes: 4 additions & 4 deletions pkg/cmd/openfaas_ingress_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"testing"
)

func Test_build_yaml_returns_correct_substitutions(t *testing.T) {
templBytes, _ := buildYaml("openfaas.subdomain.example.com", "[email protected]")
func Test_buildYAML_SubsitutesDomainEmailAndIngress(t *testing.T) {
templBytes, _ := buildYAML("openfaas.subdomain.example.com", "[email protected]", "traefik")

got := string(templBytes)
if want != got {
Expand All @@ -24,7 +24,7 @@ metadata:
namespace: openfaas
annotations:
cert-manager.io/cluster-issuer: letsencrypt-prod
kubernetes.io/ingress.class: nginx
kubernetes.io/ingress.class: traefik
spec:
rules:
- host: openfaas.subdomain.example.com
Expand Down Expand Up @@ -52,7 +52,7 @@ spec:
solvers:
- http01:
ingress:
class: nginx`
class: traefik`

func Test_writeTempFile_writes_to_tmp(t *testing.T) {
var want = "some input string"
Expand Down

0 comments on commit f59225a

Please sign in to comment.