Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP feat: allow cert-manager annotations on ingress based on environment variables PT.2 #112

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cluster/kube/client_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ func (c *client) kubeNginxIngressAnnotations(directive ctypes.ConnectHostnameToD
}
}

switch c.cfg.issuerType {
switch c.cfg.ssl.issuerType {
case clusterIssuer:
result[fmt.Sprintf("%s/cluster-issuer", certManager)] = c.cfg.issuerName
result[fmt.Sprintf("%s/cluster-issuer", certManager)] = c.cfg.ssl.issuerName
break
case issuer:
result[fmt.Sprintf("%s/issuer", certManager)] = c.cfg.issuerName
result[fmt.Sprintf("%s/issuer", certManager)] = c.cfg.ssl.issuerName
break
}

Expand Down
35 changes: 25 additions & 10 deletions cluster/kube/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package kube

import (
"errors"
"fmt"
kubeclienterrors "github.com/akash-network/provider/cluster/kube/errors"
"github.com/pkg/errors"
"os"
)

Expand All @@ -11,27 +12,41 @@ const (
clusterIssuer = "cluster-issuer"
akashProviderIssuerTypeStr = "AKASH_PROVIDER_ISSUER_TYPE"
akashProviderIssuerNameStr = "AKASH_PROVIDER_ISSUER_NAME"
akashProviderSslEnabledStr = "AKASH_PROVIDER_SSL_ENABLED"
)

type clientConfig struct {
ssl ssl
}

type ssl struct {
issuerType string
issuerName string
}

// configFromEnv creates a new clientConfig from environment variables.
func configFromEnv() (*clientConfig, error) {
issuerType, ok := os.LookupEnv(akashProviderIssuerTypeStr)
if !ok || (issuerType != issuer && issuerType != clusterIssuer) {
return nil, errors.New(fmt.Sprintf("Invalid value for %s: %s", akashProviderIssuerTypeStr, issuerType))
}
sslEnabled := os.Getenv(akashProviderSslEnabledStr)
var sslCfg ssl

if sslEnabled != "" && sslEnabled != "0" {
cloud-j-luna marked this conversation as resolved.
Show resolved Hide resolved
issuerType, ok := os.LookupEnv(akashProviderIssuerTypeStr)
if !ok || (issuerType != issuer && issuerType != clusterIssuer) {
return nil, errors.Wrap(kubeclienterrors.ErrInternalError, fmt.Sprintf("Invalid value for %s: %s", akashProviderIssuerTypeStr, issuerType))
}

issuerName, ok := os.LookupEnv(akashProviderIssuerNameStr)
if !ok {
return nil, errors.Wrap(kubeclienterrors.ErrInternalError, fmt.Sprintf("Value for %s not set", akashProviderIssuerNameStr))
}

issuerName, ok := os.LookupEnv(akashProviderIssuerNameStr)
if !ok {
return nil, errors.New(fmt.Sprintf("Value for %s not set", akashProviderIssuerNameStr))
sslCfg = ssl{
issuerType: issuerType,
issuerName: issuerName,
}
}

return &clientConfig{
issuerType: issuerType,
issuerName: issuerName,
ssl: sslCfg,
}, nil
}
14 changes: 10 additions & 4 deletions cluster/kube/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,27 @@ func TestConfigFromEnv(t *testing.T) {
t.Run("should create if environment variables are set correctly", func(t *testing.T) {
os.Setenv(akashProviderIssuerTypeStr, "cluster-issuer")
os.Setenv(akashProviderIssuerNameStr, "letsencrypt")
os.Setenv(akashProviderSslEnabledStr, "1")
ccfg, err := configFromEnv()

if err != nil {
t.Fatalf("Did not expect an error: %s", err)
}

if ccfg.issuerType != "cluster-issuer" {
t.Errorf("Expected cluster-issuer, got %s", ccfg.issuerType)
if ccfg.ssl.issuerType != "cluster-issuer" {
t.Errorf("Expected cluster-issuer, got %s", ccfg.ssl.issuerType)
}

if ccfg.issuerName != "letsencrypt" {
t.Errorf("Expected letsencrypt, got %s", ccfg.issuerName)
if ccfg.ssl.issuerName != "letsencrypt" {
t.Errorf("Expected letsencrypt, got %s", ccfg.ssl.issuerName)
}
})

t.Run("should return error if type not set", func(t *testing.T) {
os.Clearenv()
os.Setenv(akashProviderIssuerNameStr, "letsencrypt")
os.Setenv(akashProviderSslEnabledStr, "1")

_, err := configFromEnv()

if err == nil {
Expand All @@ -37,6 +40,8 @@ func TestConfigFromEnv(t *testing.T) {
t.Run("should return error if name not set", func(t *testing.T) {
os.Clearenv()
os.Setenv(akashProviderIssuerTypeStr, "cluster-issuer")
os.Setenv(akashProviderSslEnabledStr, "1")

_, err := configFromEnv()

if err == nil {
Expand All @@ -48,6 +53,7 @@ func TestConfigFromEnv(t *testing.T) {
os.Clearenv()
os.Setenv(akashProviderIssuerTypeStr, "fake-issuer-type")
os.Setenv(akashProviderIssuerNameStr, "letsencrypt")
os.Setenv(akashProviderSslEnabledStr, "1")

_, err := configFromEnv()

Expand Down