Skip to content

Commit

Permalink
worker: use local (root) command arguments instead of global (Persist…
Browse files Browse the repository at this point in the history
…ent) arguments

Signed-off-by: Marques Johansson <marques@packet.com>
  • Loading branch information
displague committed Sep 28, 2020
1 parent f63786e commit dc2b493
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 42 deletions.
76 changes: 38 additions & 38 deletions cmd/tink-worker/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"context"
"fmt"
"os"
"strings"
"time"

Expand All @@ -19,42 +18,36 @@ import (
)

const (
retryIntervalDefault = 3
retryCountDefault = 3
defaultMaxFileSize int64 = 10485760 //10MB ~= 10485760Bytes
defaultRetryInterval = 3
defaultRetryCount = 3
defaultMaxFileSize int64 = 10 * 1024 * 1024 //10MB
defaultTimeoutMinutes = 60
)

// NewRootCommand creates a new Tink Worker Cobra root command
func NewRootCommand(version string, logger log.Logger) *cobra.Command {
must := func(err error) {
if err != nil {
logger.Fatal(err)
}
}

rootCmd := &cobra.Command{
Use: "tink-worker",
Short: "Tink Worker",
Version: version,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
viper, err := createViper()
PreRunE: func(cmd *cobra.Command, args []string) error {
viper, err := createViper(logger)
if err != nil {
return err
}
return applyViper(viper, cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
retryInterval, _ := cmd.PersistentFlags().GetDuration("retry-interval")
retries, _ := cmd.PersistentFlags().GetInt("retries")
retryInterval, _ := cmd.Flags().GetDuration("retry-interval")
retries, _ := cmd.Flags().GetInt("retries")
// TODO(displague) is log-level no longer useful?
// logLevel, _ := cmd.PersistentFlags().GetString("log-level")
workerID, _ := cmd.PersistentFlags().GetString("id")
maxFileSize, _ := cmd.PersistentFlags().GetInt64("max-file-size")
timeOut, _ := cmd.PersistentFlags().GetDuration("timeout")
user, _ := cmd.PersistentFlags().GetString("registry-username")
pwd, _ := cmd.PersistentFlags().GetString("registry-password")
registry, _ := cmd.PersistentFlags().GetString("docker-registry")
// logLevel, _ := cmd.Flags().GetString("log-level")
workerID, _ := cmd.Flags().GetString("id")
maxFileSize, _ := cmd.Flags().GetInt64("max-file-size")
timeOut, _ := cmd.Flags().GetDuration("timeout")
user, _ := cmd.Flags().GetString("registry-username")
pwd, _ := cmd.Flags().GetString("registry-password")
registry, _ := cmd.Flags().GetString("docker-registry")

logger.With("version", version).Info("starting")
if setupErr := client.Setup(); setupErr != nil {
Expand Down Expand Up @@ -85,50 +78,57 @@ func NewRootCommand(version string, logger log.Logger) *cobra.Command {
},
}

rootCmd.PersistentFlags().Duration("retry-interval", retryIntervalDefault, "Retry interval in seconds")
rootCmd.Flags().Duration("retry-interval", defaultRetryInterval, "Retry interval in seconds")

rootCmd.PersistentFlags().Duration("timeout", time.Duration(defaultTimeoutMinutes*time.Minute), "Max duration to wait for worker to complete")
rootCmd.Flags().Duration("timeout", time.Duration(defaultTimeoutMinutes*time.Minute), "Max duration to wait for worker to complete")

rootCmd.PersistentFlags().Int("max-retry", retryCountDefault, "Maximum number of retries to attempt")
rootCmd.Flags().Int("max-retry", defaultRetryCount, "Maximum number of retries to attempt")

rootCmd.PersistentFlags().Int64("max-file-size", defaultMaxFileSize, "Maximum file size in bytes")
rootCmd.Flags().Int64("max-file-size", defaultMaxFileSize, "Maximum file size in bytes")

// rootCmd.PersistentFlags().String("log-level", "info", "Sets the worker log level (panic, fatal, error, warn, info, debug, trace)")
// rootCmd.Flags().String("log-level", "info", "Sets the worker log level (panic, fatal, error, warn, info, debug, trace)")

must := func(err error) {
if err != nil {
logger.Fatal(err)
}
}

rootCmd.PersistentFlags().StringP("id", "i", "", "Sets the worker id")
must(rootCmd.MarkPersistentFlagRequired("id"))
rootCmd.Flags().StringP("id", "i", "", "Sets the worker id")
must(rootCmd.MarkFlagRequired("id"))

rootCmd.PersistentFlags().StringP("docker-registry", "r", "", "Sets the Docker registry")
must(rootCmd.MarkPersistentFlagRequired("docker-registry"))
rootCmd.Flags().StringP("docker-registry", "r", "", "Sets the Docker registry")
must(rootCmd.MarkFlagRequired("docker-registry"))

rootCmd.PersistentFlags().StringP("registry-username", "u", "", "Sets the registry username")
must(rootCmd.MarkPersistentFlagRequired("registry-username"))
rootCmd.Flags().StringP("registry-username", "u", "", "Sets the registry username")
must(rootCmd.MarkFlagRequired("registry-username"))

rootCmd.PersistentFlags().StringP("registry-password", "p", "", "Sets the registry-password")
must(rootCmd.MarkPersistentFlagRequired("registry-password"))
rootCmd.Flags().StringP("registry-password", "p", "", "Sets the registry-password")
must(rootCmd.MarkFlagRequired("registry-password"))

return rootCmd
}

// createViper creates a Viper object configured to read in configuration files
// (from various paths with content type specific filename extensions) and load
// environment variables that start with TINK_WORKER.
func createViper() (*viper.Viper, error) {
func createViper(logger log.Logger) (*viper.Viper, error) {
v := viper.New()
v.AutomaticEnv()
v.SetConfigName("tink-worker")
v.AddConfigPath("/etc/tinkerbell")
v.AddConfigPath(".")
v.SetEnvPrefix("TINK_WORKER")
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

// If a config file is found, read it in.
if err := v.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
logger.With("configFile", v.ConfigFileUsed()).Error(err, "could not load config file")
return nil, err
}
logger.Info("no config file found")
} else {
fmt.Fprintln(os.Stderr, "Using config file:", v.ConfigFileUsed())
logger.With("configFile", v.ConfigFileUsed()).Info("loaded config file")
}

return v, nil
Expand All @@ -137,7 +137,7 @@ func createViper() (*viper.Viper, error) {
func applyViper(v *viper.Viper, cmd *cobra.Command) error {
errors := []error{}

cmd.PersistentFlags().VisitAll(func(f *pflag.Flag) {
cmd.Flags().VisitAll(func(f *pflag.Flag) {
if !f.Changed && v.IsSet(f.Name) {
val := v.Get(f.Name)
if err := cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val)); err != nil {
Expand Down
1 change: 0 additions & 1 deletion cmd/tink-worker/internal/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
)

const (
errContextClosed = "failed to wait for container, context closed"
errCreateContainer = "failed to create container"
errFailedToWait = "failed to wait for completion of action"
errFailedToRunCmd = "failed to run on-timeout command"
Expand Down
3 changes: 1 addition & 2 deletions cmd/tink-worker/internal/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ func (w *Worker) execute(ctx context.Context, wfID string, action *pb.WorkflowAc
l := w.logger.With("workflowID", wfID, "workerID", action.GetWorkerId(), "actionName", action.GetName(), "actionImage", action.GetImage())

cli := w.registryClient
err := w.regConn.pullImage(ctx, cli, action.GetImage())
if err != nil {
if err := w.regConn.pullImage(ctx, cli, action.GetImage()); err != nil {
return pb.ActionState_ACTION_IN_PROGRESS, errors.Wrap(err, "DOCKER PULL")
}
id, err := w.createContainer(ctx, action.Command, wfID, action)
Expand Down
3 changes: 2 additions & 1 deletion http-server/http_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"net/http"
tt "text/template"

// nolint:staticcheck SA1019 We will do it later
// nolint:staticcheck
// SA1019 We will do it later
"github.com/golang/protobuf/jsonpb"

"github.com/tinkerbell/tink/protos/template"
Expand Down

0 comments on commit dc2b493

Please sign in to comment.