Skip to content

Commit

Permalink
Remove configuration file support
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpollet authored Oct 1, 2020
1 parent 9ccab4b commit 54faa0c
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 140 deletions.
18 changes: 9 additions & 9 deletions cmd/cleanup/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,36 @@ import (
)

// NewCmd builds a new Cleanup command.
func NewCmd(cConfig *cmd.CleanupConfiguration, loaders []cli.ResourceLoader) *cli.Command {
func NewCmd(config *cmd.CleanupConfiguration, loaders []cli.ResourceLoader) *cli.Command {
return &cli.Command{
Name: "cleanup",
Description: `Removes Traefik Mesh shadow services from a Kubernetes cluster.`,
Configuration: cConfig,
Configuration: config,
Run: func(_ []string) error {
return cleanupCommand(cConfig)
return cleanupCommand(config)
},
Resources: loaders,
}
}

func cleanupCommand(cConfig *cmd.CleanupConfiguration) error {
func cleanupCommand(config *cmd.CleanupConfiguration) error {
ctx := cmd.ContextWithSignal(context.Background())

logger, err := cmd.NewLogger(cConfig.LogFormat, cConfig.LogLevel)
logger, err := cmd.NewLogger(config.LogFormat, config.LogLevel)
if err != nil {
return fmt.Errorf("could not create logger: %w", err)
}

logger.Debug("Starting cleanup...")
logger.Debugf("Using masterURL: %q", cConfig.MasterURL)
logger.Debugf("Using kubeconfig: %q", cConfig.KubeConfig)
logger.Debugf("Using masterURL: %q", config.MasterURL)
logger.Debugf("Using kubeconfig: %q", config.KubeConfig)

clients, err := k8s.NewClient(logger, cConfig.MasterURL, cConfig.KubeConfig)
clients, err := k8s.NewClient(logger, config.MasterURL, config.KubeConfig)
if err != nil {
return fmt.Errorf("error building clients: %w", err)
}

c := cleanup.NewCleanup(logger, clients.KubernetesClient(), cConfig.Namespace)
c := cleanup.NewCleanup(logger, clients.KubernetesClient(), config.Namespace)

if err := c.CleanShadowServices(ctx); err != nil {
return fmt.Errorf("error encountered during cluster cleanup: %w", err)
Expand Down
4 changes: 0 additions & 4 deletions cmd/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

// TraefikMeshConfiguration wraps the static configuration and extra parameters.
type TraefikMeshConfiguration struct {
ConfigFile string `description:"Configuration file to use. If specified all other flags are ignored." export:"true"`
KubeConfig string `description:"Path to a kubeconfig. Only required if out-of-cluster." export:"true"`
MasterURL string `description:"The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster." export:"true"`
LogLevel string `description:"The log level." export:"true"`
Expand All @@ -26,7 +25,6 @@ type TraefikMeshConfiguration struct {
// NewTraefikMeshConfiguration creates a TraefikMeshConfiguration with default values.
func NewTraefikMeshConfiguration() *TraefikMeshConfiguration {
return &TraefikMeshConfiguration{
ConfigFile: "",
KubeConfig: os.Getenv("KUBECONFIG"),
LogLevel: "error",
LogFormat: "common",
Expand All @@ -43,7 +41,6 @@ func NewTraefikMeshConfiguration() *TraefikMeshConfiguration {

// PrepareConfiguration holds the configuration to prepare the cluster.
type PrepareConfiguration struct {
ConfigFile string `description:"Configuration file to use. If specified all other flags are ignored." export:"true"`
KubeConfig string `description:"Path to a kubeconfig. Only required if out-of-cluster." export:"true"`
MasterURL string `description:"The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster." export:"true"`
LogLevel string `description:"The log level." export:"true"`
Expand All @@ -66,7 +63,6 @@ func NewPrepareConfiguration() *PrepareConfiguration {

// CleanupConfiguration holds the configuration for the cleanup command.
type CleanupConfiguration struct {
ConfigFile string `description:"Configuration file to use. If specified all other flags are ignored." export:"true"`
KubeConfig string `description:"Path to a kubeconfig. Only required if out-of-cluster." export:"true"`
MasterURL string `description:"The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster." export:"true"`
Namespace string `description:"The namespace that Traefik Mesh is installed in." export:"true"`
Expand Down
45 changes: 45 additions & 0 deletions cmd/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cmd

import (
"fmt"
"os"
"strings"

"github.com/sirupsen/logrus"
"github.com/traefik/paerser/cli"
"github.com/traefik/paerser/env"
)

const (
meshPrefix = "MESH_"
traefikMeshPrefix = "TRAEFIK_MESH_"
)

// EnvLoader loads a configuration from all the environment variables.
type EnvLoader struct{}

// Load loads the command's configuration from the environment variables prefixed with "TRAEFIK_MESH_".
// As it is not possible to have a prefix with multiple "_" everything is normalized to "MESH_" under the hood for the decoding.
func (e *EnvLoader) Load(_ []string, cmd *cli.Command) (bool, error) {
logger := logrus.StandardLogger()
traefikMeshVars := env.FindPrefixedEnvVars(os.Environ(), traefikMeshPrefix, cmd.Configuration)

var meshVars []string

for _, v := range traefikMeshVars {
meshVars = append(meshVars, strings.Replace(v, traefikMeshPrefix, meshPrefix, 1))
}

if len(traefikMeshVars) == 0 {
return false, nil
}

if err := env.Decode(meshVars, meshPrefix, cmd.Configuration); err != nil {
logger.Debug("environment variables", strings.Join(meshVars, ", "))
return false, fmt.Errorf("failed to decode configuration from environment variables: %w ", err)
}

logger.Println("Configuration loaded from environment variables.")

return true, nil
}
105 changes: 0 additions & 105 deletions cmd/loaders.go

This file was deleted.

20 changes: 10 additions & 10 deletions cmd/mesh/mesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,37 @@ const (
)

func main() {
traefikMeshConfig := cmd.NewTraefikMeshConfiguration()
traefikMeshLoaders := []cli.ResourceLoader{&cmd.FileLoader{}, &cli.FlagLoader{}, &cmd.EnvLoader{}}
config := cmd.NewTraefikMeshConfiguration()
loaders := []cli.ResourceLoader{&cli.FlagLoader{}, &cmd.EnvLoader{}}

cmdTraefikMesh := &cli.Command{
traefikMeshCmd := &cli.Command{
Name: "traefik-mesh",
Description: `traefik-mesh`,
Configuration: traefikMeshConfig,
Resources: traefikMeshLoaders,
Configuration: config,
Resources: loaders,
Run: func(_ []string) error {
return traefikMeshCommand(traefikMeshConfig)
return traefikMeshCommand(config)
},
}

prepareConfig := cmd.NewPrepareConfiguration()
if err := cmdTraefikMesh.AddCommand(prepare.NewCmd(prepareConfig, traefikMeshLoaders)); err != nil {
if err := traefikMeshCmd.AddCommand(prepare.NewCmd(prepareConfig, loaders)); err != nil {
stdlog.Println(err)
os.Exit(1)
}

cleanupConfig := cmd.NewCleanupConfiguration()
if err := cmdTraefikMesh.AddCommand(cleanup.NewCmd(cleanupConfig, traefikMeshLoaders)); err != nil {
if err := traefikMeshCmd.AddCommand(cleanup.NewCmd(cleanupConfig, loaders)); err != nil {
stdlog.Println(err)
os.Exit(1)
}

if err := cmdTraefikMesh.AddCommand(version.NewCmd()); err != nil {
if err := traefikMeshCmd.AddCommand(version.NewCmd()); err != nil {
stdlog.Println(err)
os.Exit(1)
}

if err := cli.Execute(cmdTraefikMesh); err != nil {
if err := cli.Execute(traefikMeshCmd); err != nil {
stdlog.Println(err)
os.Exit(1)
}
Expand Down
24 changes: 12 additions & 12 deletions cmd/prepare/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,40 @@ import (
)

// NewCmd builds a new Prepare command.
func NewCmd(pConfig *cmd.PrepareConfiguration, loaders []cli.ResourceLoader) *cli.Command {
func NewCmd(config *cmd.PrepareConfiguration, loaders []cli.ResourceLoader) *cli.Command {
return &cli.Command{
Name: "prepare",
Description: `Prepare command.`,
Configuration: pConfig,
Configuration: config,
Run: func(_ []string) error {
return prepareCommand(pConfig)
return prepareCommand(config)
},
Resources: loaders,
}
}

func prepareCommand(pConfig *cmd.PrepareConfiguration) error {
func prepareCommand(config *cmd.PrepareConfiguration) error {
ctx := cmd.ContextWithSignal(context.Background())

log, err := cmd.NewLogger(pConfig.LogFormat, pConfig.LogLevel)
log, err := cmd.NewLogger(config.LogFormat, config.LogLevel)
if err != nil {
return fmt.Errorf("could not create logger: %w", err)
}

log.Debug("Starting prepare...")
log.Debugf("Using masterURL: %q", pConfig.MasterURL)
log.Debugf("Using kubeconfig: %q", pConfig.KubeConfig)
log.Debugf("Using masterURL: %q", config.MasterURL)
log.Debugf("Using kubeconfig: %q", config.KubeConfig)

client, err := k8s.NewClient(log, pConfig.MasterURL, pConfig.KubeConfig)
client, err := k8s.NewClient(log, config.MasterURL, config.KubeConfig)
if err != nil {
return fmt.Errorf("unable to create kubernetes client: %w", err)
}

dnsClient := dns.NewClient(log, client.KubernetesClient())

log.Debugf("ACL mode enabled: %t", pConfig.ACL)
log.Debugf("ACL mode enabled: %t", config.ACL)

if err = k8s.CheckSMIVersion(client.KubernetesClient(), pConfig.ACL); err != nil {
if err = k8s.CheckSMIVersion(client.KubernetesClient(), config.ACL); err != nil {
return fmt.Errorf("unsupported SMI version: %w", err)
}

Expand All @@ -58,11 +58,11 @@ func prepareCommand(pConfig *cmd.PrepareConfiguration) error {

switch dnsProvider {
case dns.CoreDNS:
if err := dnsClient.ConfigureCoreDNS(ctx, metav1.NamespaceSystem, pConfig.ClusterDomain, pConfig.Namespace); err != nil {
if err := dnsClient.ConfigureCoreDNS(ctx, metav1.NamespaceSystem, config.ClusterDomain, config.Namespace); err != nil {
return fmt.Errorf("unable to configure CoreDNS: %w", err)
}
case dns.KubeDNS:
if err := dnsClient.ConfigureKubeDNS(ctx, pConfig.ClusterDomain, pConfig.Namespace); err != nil {
if err := dnsClient.ConfigureKubeDNS(ctx, config.ClusterDomain, config.Namespace); err != nil {
return fmt.Errorf("unable to configure KubeDNS: %w", err)
}
}
Expand Down

0 comments on commit 54faa0c

Please sign in to comment.