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 17 commits
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
4 changes: 2 additions & 2 deletions cluster/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type Client interface {
tsq remotecommand.TerminalSizeQueue) (ctypes.ExecResult, error)

// ConnectHostnameToDeployment Connect a given hostname to a deployment
ConnectHostnameToDeployment(ctx context.Context, directive ctypes.ConnectHostnameToDeploymentDirective) error
ConnectHostnameToDeployment(ctx context.Context, directive ctypes.ConnectHostnameToDeploymentDirective, sslEnabled bool) error
// RemoveHostnameFromDeployment Remove a given hostname from a deployment
RemoveHostnameFromDeployment(ctx context.Context, hostname string, leaseID mtypes.LeaseID, allowMissing bool) error

Expand Down Expand Up @@ -415,7 +415,7 @@ func (c *nullClient) GetHostnameDeploymentConnections(_ context.Context) ([]ctyp
return nil, errNotImplemented
}

func (c *nullClient) ConnectHostnameToDeployment(_ context.Context, _ ctypes.ConnectHostnameToDeploymentDirective) error {
func (c *nullClient) ConnectHostnameToDeployment(_ context.Context, _ ctypes.ConnectHostnameToDeploymentDirective, _ bool) error {
return errNotImplemented
}

Expand Down
4 changes: 3 additions & 1 deletion cluster/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type client struct {
ns string
log log.Logger
kubeContentConfig *restclient.Config
cfg ClientConfig
}

func (c *client) String() string {
Expand All @@ -64,7 +65,7 @@ func (c *client) String() string {

// NewClient returns new Kubernetes Client instance with provided logger, host and ns. Returns error in-case of failure
// configPath may be the empty string
func NewClient(ctx context.Context, log log.Logger, ns string, configPath string) (Client, error) {
func NewClient(ctx context.Context, log log.Logger, ns string, configPath string, ccfg ClientConfig) (Client, error) {
config, err := clientcommon.OpenKubeConfig(configPath, log)
if err != nil {
return nil, errors.Wrap(err, "kube: error building config flags")
Expand Down Expand Up @@ -98,6 +99,7 @@ func NewClient(ctx context.Context, log log.Logger, ns string, configPath string
ns: ns,
log: log.With("client", "kube"),
kubeContentConfig: config,
cfg: ccfg,
}, nil
}

Expand Down
29 changes: 25 additions & 4 deletions cluster/kube/client_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ import (

const (
akashIngressClassName = "akash-ingress-class"
root = "nginx.ingress.kubernetes.io"
certManager = "cert-manager.io"
)

func kubeNginxIngressAnnotations(directive ctypes.ConnectHostnameToDeploymentDirective) map[string]string {
func (c *client) kubeNginxIngressAnnotations(directive ctypes.ConnectHostnameToDeploymentDirective) map[string]string {
// For kubernetes/ingress-nginx
// https://github.com/kubernetes/ingress-nginx
const root = "nginx.ingress.kubernetes.io"

readTimeout := math.Ceil(float64(directive.ReadTimeout) / 1000.0)
sendTimeout := math.Ceil(float64(directive.SendTimeout) / 1000.0)
Expand Down Expand Up @@ -66,11 +67,20 @@ func kubeNginxIngressAnnotations(directive ctypes.ConnectHostnameToDeploymentDir
}
}

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

result[fmt.Sprintf("%s/proxy-next-upstream", root)] = strBuilder.String()
return result
}

func (c *client) ConnectHostnameToDeployment(ctx context.Context, directive ctypes.ConnectHostnameToDeploymentDirective) error {
func (c *client) ConnectHostnameToDeployment(ctx context.Context, directive ctypes.ConnectHostnameToDeploymentDirective, tlsEnabled bool) error {
ingressName := directive.Hostname
ns := builder.LidNS(directive.LeaseID)
rules := ingressRules(directive.Hostname, directive.ServiceName, directive.ServicePort)
Expand All @@ -82,16 +92,27 @@ func (c *client) ConnectHostnameToDeployment(ctx context.Context, directive ctyp
labels[builder.AkashManagedLabelName] = "true"
builder.AppendLeaseLabels(directive.LeaseID, labels)

var tls []netv1.IngressTLS
if tlsEnabled {
tls = []netv1.IngressTLS{
{
Hosts: []string{directive.Hostname},
SecretName: fmt.Sprintf("%s-tls", ingressName),
},
}
}

ingressClassName := akashIngressClassName
obj := &netv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: ingressName,
Labels: labels,
Annotations: kubeNginxIngressAnnotations(directive),
Annotations: c.kubeNginxIngressAnnotations(directive),
},
Spec: netv1.IngressSpec{
IngressClassName: &ingressClassName,
Rules: rules,
TLS: tls,
},
}

Expand Down
15 changes: 15 additions & 0 deletions cluster/kube/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package kube

const (
issuer = "issuer"
clusterIssuer = "cluster-issuer"
)

type ClientConfig struct {
Ssl Ssl
}

type Ssl struct {
IssuerType string
IssuerName string
}
2 changes: 1 addition & 1 deletion cluster/kube/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestDeploy(t *testing.T) {
require.NoError(t, err)

log := testutil.Logger(t)
client, err := NewClient(ctx, log, "lease", "")
client, err := NewClient(ctx, log, "lease", "", ClientConfig{})
require.NoError(t, err)

ctx = context.WithValue(ctx, builder.SettingsKey, builder.NewDefaultSettings())
Expand Down
4 changes: 2 additions & 2 deletions cluster/kube/k8s_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestNewClientNSNotFound(t *testing.T) {

ctx := context.WithValue(context.Background(), builder.SettingsKey, settings)

ac, err := NewClient(ctx, atestutil.Logger(t), ns, providerflags.KubeConfigDefaultPath)
ac, err := NewClient(ctx, atestutil.Logger(t), ns, providerflags.KubeConfigDefaultPath, ClientConfig{})
require.True(t, kubeErrors.IsNotFound(err))
require.Nil(t, ac)
}
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestNewClient(t *testing.T) {
}, metav1.CreateOptions{})
require.NoError(t, err)

ac, err := NewClient(ctx, atestutil.Logger(t), ns, providerflags.KubeConfigDefaultPath)
ac, err := NewClient(ctx, atestutil.Logger(t), ns, providerflags.KubeConfigDefaultPath, ClientConfig{})

require.NoError(t, err)

Expand Down
21 changes: 11 additions & 10 deletions cluster/mocks/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion cmd/provider-services/cmd/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ const (
FlagWebRefreshInterval = "web-refresh-interval"
FlagRetryDelay = "retry-delay"

FlagKubeConfig = "kubeconfig"
FlagKubeConfig = "kubeconfig"
FlagSslEnabled = "ssl"
FlagSslIssuerType = "ssl-issuer-type"
FlagSslIssuerName = "ssl-issuer-name"
)
16 changes: 15 additions & 1 deletion cmd/provider-services/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@ func RunCmd() *cobra.Command {
return nil
}

cmd.Flags().Bool(providerflags.FlagSslEnabled, false, "enable issuing of SSL certificates on the provider's ingress controller. defaults to false")
if err := viper.BindPFlag(providerflags.FlagSslEnabled, cmd.Flags().Lookup(providerflags.FlagSslEnabled)); err != nil {
return nil
}

return cmd
}

Expand Down Expand Up @@ -760,7 +765,16 @@ func createClusterClient(ctx context.Context, log log.Logger, _ *cobra.Command,
if ns == "" {
return nil, fmt.Errorf("%w: --%s required", errInvalidConfig, providerflags.FlagK8sManifestNS)
}
return kube.NewClient(ctx, log, ns, configPath)

var sslCfg kube.Ssl
if viper.GetBool(providerflags.FlagSslEnabled) {
sslCfg = kube.Ssl{
IssuerName: viper.GetString(providerflags.FlagSslIssuerName),
IssuerType: viper.GetString(providerflags.FlagSslIssuerType),
}
}
ccfg := kube.ClientConfig{Ssl: sslCfg}
return kube.NewClient(ctx, log, ns, configPath, ccfg)
}

func showErrorToUser(err error) error {
Expand Down
10 changes: 7 additions & 3 deletions operator/hostnameoperator/hostname_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ func (op *hostnameOperator) applyAddOrUpdateEvent(ctx context.Context, ev ctypes
if shouldConnect {
op.log.Debug("Updating ingress")
// Update or create the existing ingress
err = op.client.ConnectHostnameToDeployment(ctx, directive)
err = op.client.ConnectHostnameToDeployment(ctx, directive, op.isSslEnabled())
}
} else {
op.log.Debug("Swapping ingress to new deployment")
Expand All @@ -398,7 +398,7 @@ func (op *hostnameOperator) applyAddOrUpdateEvent(ctx context.Context, ev ctypes
if err == nil {
// Remove the current entry, if the next action succeeds then it gets inserted below
delete(op.hostnames, ev.GetHostname())
err = op.client.ConnectHostnameToDeployment(ctx, directive)
err = op.client.ConnectHostnameToDeployment(ctx, directive, op.isSslEnabled())
}
}

Expand Down Expand Up @@ -445,7 +445,7 @@ func doHostnameOperator(cmd *cobra.Command) error {
logger := operatorcommon.OpenLogger().With("op", "hostname")
logger.Info("HTTP listening", "address", listenAddr)

client, err := clusterClient.NewClient(cmd.Context(), logger, ns, configPath)
client, err := clusterClient.NewClient(cmd.Context(), logger, ns, configPath, config.ClientConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -502,3 +502,7 @@ func Cmd() *cobra.Command {

return cmd
}

func (op *hostnameOperator) isSslEnabled() bool {
return op.cfg.ClientConfig.Ssl != clusterClient.Ssl{}
}
8 changes: 4 additions & 4 deletions operator/hostnameoperator/hostname_operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func TestHostnameOperatorApplyAdd(t *testing.T) {
}
s.client.On("GetManifestGroup", mock.Anything, leaseID).Return(true, mg, nil)
directive := buildDirective(ev, serviceExpose) // result tested in other unit tests
s.client.On("ConnectHostnameToDeployment", mock.Anything, directive).Return(nil)
s.client.On("ConnectHostnameToDeployment", mock.Anything, directive, mock.Anything).Return(nil)

managed := grabManagedHostnames(t, s.op.server.GetRouter().ServeHTTP)
require.Empty(t, managed)
Expand Down Expand Up @@ -511,7 +511,7 @@ func TestHostnameOperatorApplyAddMultipleServices(t *testing.T) {
}
s.client.On("GetManifestGroup", mock.Anything, leaseID).Return(true, mg, nil)
directive := buildDirective(ev, serviceExpose) // result tested in other unit tests
s.client.On("ConnectHostnameToDeployment", mock.Anything, directive).Return(nil)
s.client.On("ConnectHostnameToDeployment", mock.Anything, directive, mock.Anything).Return(nil)

err := s.op.applyEvent(s.ctx, ev)
require.NoError(t, err)
Expand Down Expand Up @@ -596,9 +596,9 @@ func TestHostnameOperatorApplyUpdate(t *testing.T) {
s.client.On("GetManifestGroup", mock.Anything, secondLeaseID).Return(true, mg2, nil)

directive := buildDirective(ev, serviceExpose) // result tested in other unit tests
s.client.On("ConnectHostnameToDeployment", mock.Anything, directive).Return(nil)
s.client.On("ConnectHostnameToDeployment", mock.Anything, directive, mock.Anything).Return(nil)
secondDirective := buildDirective(secondEv, secondServiceExpose) // result tested in other unit tests
s.client.On("ConnectHostnameToDeployment", mock.Anything, secondDirective).Return(nil)
s.client.On("ConnectHostnameToDeployment", mock.Anything, secondDirective, mock.Anything).Return(nil)

s.client.On("RemoveHostnameFromDeployment", mock.Anything, hostname, leaseID, false).Return(nil)

Expand Down
8 changes: 4 additions & 4 deletions operator/ipoperator/ip_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,13 +579,13 @@ func doIPOperator(cmd *cobra.Command) error {
poolName := viper.GetString(flagMetalLbPoolName)
logger := operatorcommon.OpenLogger().With("operator", "ip")

opcfg := operatorcommon.GetOperatorConfigFromViper()
_, err := sdk.AccAddressFromBech32(opcfg.ProviderAddress)
config := operatorcommon.GetOperatorConfigFromViper()
_, err := sdk.AccAddressFromBech32(config.ProviderAddress)
if err != nil {
return fmt.Errorf("%w: provider address must valid bech32", err)
}

client, err := clusterClient.NewClient(cmd.Context(), logger, ns, configPath)
client, err := clusterClient.NewClient(cmd.Context(), logger, ns, configPath, config.ClientConfig)
if err != nil {
return err
}
Expand All @@ -603,7 +603,7 @@ func doIPOperator(cmd *cobra.Command) error {
logger.Info("clients", "kube", client, "metallb", mllbc)
logger.Info("HTTP listening", "address", listenAddr)

op, err := newIPOperator(logger, client, opcfg, operatorcommon.IgnoreListConfigFromViper(), mllbc)
op, err := newIPOperator(logger, client, config, operatorcommon.IgnoreListConfigFromViper(), mllbc)
if err != nil {
return err
}
Expand Down
12 changes: 12 additions & 0 deletions operator/operatorcommon/operator_config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package operatorcommon

import (
"github.com/akash-network/provider/cluster/kube"
"time"

"github.com/spf13/viper"
Expand All @@ -13,13 +14,24 @@ type OperatorConfig struct {
WebRefreshInterval time.Duration
RetryDelay time.Duration
ProviderAddress string
ClientConfig kube.ClientConfig
}

func GetOperatorConfigFromViper() OperatorConfig {
var sslCfg kube.Ssl
if viper.GetBool(providerflags.FlagSslEnabled) {
sslCfg = kube.Ssl{
IssuerName: viper.GetString(providerflags.FlagSslIssuerName),
IssuerType: viper.GetString(providerflags.FlagSslIssuerType),
}
}
ccfg := kube.ClientConfig{Ssl: sslCfg}

return OperatorConfig{
PruneInterval: viper.GetDuration(providerflags.FlagPruneInterval),
WebRefreshInterval: viper.GetDuration(providerflags.FlagWebRefreshInterval),
RetryDelay: viper.GetDuration(providerflags.FlagRetryDelay),
ProviderAddress: viper.GetString(flagProviderAddress),
ClientConfig: ccfg,
}
}