2019-12-09 17:56:02 +00:00
|
|
|
package helm
|
|
|
|
|
2019-12-23 22:44:59 +00:00
|
|
|
import (
|
2019-12-24 19:08:09 +00:00
|
|
|
"fmt"
|
2019-12-23 22:44:59 +00:00
|
|
|
"github.com/kelseyhightower/envconfig"
|
2019-12-24 19:08:09 +00:00
|
|
|
"io"
|
2019-12-28 00:18:10 +00:00
|
|
|
"regexp"
|
2019-12-23 22:44:59 +00:00
|
|
|
)
|
2019-12-09 17:56:02 +00:00
|
|
|
|
2019-12-28 00:18:10 +00:00
|
|
|
var justNumbers = regexp.MustCompile(`^\d+$`)
|
|
|
|
|
2019-12-23 22:03:51 +00:00
|
|
|
// The Config struct captures the `settings` and `environment` blocks in the application's drone
|
2019-12-09 18:52:41 +00:00
|
|
|
// config. Configuration in drone's `settings` block arrives as uppercase env vars matching the
|
2019-12-24 00:45:09 +00:00
|
|
|
// config key, prefixed with `PLUGIN_`. Config from the `environment` block is uppercased, but does
|
2019-12-31 17:28:42 +00:00
|
|
|
// not have the `PLUGIN_` prefix.
|
2019-12-09 17:56:02 +00:00
|
|
|
type Config struct {
|
|
|
|
// Configuration for drone-helm itself
|
2019-12-23 22:06:05 +00:00
|
|
|
Command string `envconfig:"HELM_COMMAND"` // Helm command to run
|
|
|
|
DroneEvent string `envconfig:"DRONE_BUILD_EVENT"` // Drone event that invoked this plugin.
|
|
|
|
UpdateDependencies bool `split_words:"true"` // Call `helm dependency update` before the main command
|
2019-12-30 19:57:19 +00:00
|
|
|
AddRepos []string `envconfig:"HELM_REPOS"` // Call `helm repo add` before the main command
|
2019-12-23 22:06:05 +00:00
|
|
|
Debug bool `` // Generate debug output and pass --debug to all helm commands
|
|
|
|
Values string `` // Argument to pass to --set in applicable helm commands
|
|
|
|
StringValues string `split_words:"true"` // Argument to pass to --set-string in applicable helm commands
|
|
|
|
ValuesFiles []string `split_words:"true"` // Arguments to pass to --values in applicable helm commands
|
|
|
|
Namespace string `` // Kubernetes namespace for all helm commands
|
|
|
|
KubeToken string `envconfig:"KUBERNETES_TOKEN"` // Kubernetes authentication token to put in .kube/config
|
|
|
|
SkipTLSVerify bool `envconfig:"SKIP_TLS_VERIFY"` // Put insecure-skip-tls-verify in .kube/config
|
|
|
|
Certificate string `envconfig:"KUBERNETES_CERTIFICATE"` // The Kubernetes cluster CA's self-signed certificate (must be base64-encoded)
|
|
|
|
APIServer string `envconfig:"API_SERVER"` // The Kubernetes cluster's API endpoint
|
2019-12-23 23:31:40 +00:00
|
|
|
ServiceAccount string `split_words:"true"` // Account to use for connecting to the Kubernetes cluster
|
2019-12-23 22:06:05 +00:00
|
|
|
ChartVersion string `split_words:"true"` // Specific chart version to use in `helm upgrade`
|
|
|
|
DryRun bool `split_words:"true"` // Pass --dry-run to applicable helm commands
|
|
|
|
Wait bool `` // Pass --wait to applicable helm commands
|
|
|
|
ReuseValues bool `split_words:"true"` // Pass --reuse-values to `helm upgrade`
|
|
|
|
Timeout string `` // Argument to pass to --timeout in applicable helm commands
|
|
|
|
Chart string `` // Chart argument to use in applicable helm commands
|
|
|
|
Release string `` // Release argument to use in applicable helm commands
|
|
|
|
Force bool `` // Pass --force to applicable helm commands
|
2019-12-24 19:08:09 +00:00
|
|
|
|
|
|
|
Stdout io.Writer `ignored:"true"`
|
|
|
|
Stderr io.Writer `ignored:"true"`
|
2019-12-09 17:56:02 +00:00
|
|
|
}
|
2019-12-23 22:44:59 +00:00
|
|
|
|
2019-12-24 17:34:38 +00:00
|
|
|
// NewConfig creates a Config and reads environment variables into it, accounting for several possible formats.
|
2019-12-24 19:08:09 +00:00
|
|
|
func NewConfig(stdout, stderr io.Writer) (*Config, error) {
|
|
|
|
cfg := Config{
|
|
|
|
Stdout: stdout,
|
|
|
|
Stderr: stderr,
|
|
|
|
}
|
2019-12-24 17:34:38 +00:00
|
|
|
if err := envconfig.Process("plugin", &cfg); err != nil {
|
|
|
|
return nil, err
|
2019-12-23 23:31:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 17:34:38 +00:00
|
|
|
if err := envconfig.Process("", &cfg); err != nil {
|
|
|
|
return nil, err
|
2019-12-23 23:31:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 00:18:10 +00:00
|
|
|
if justNumbers.MatchString(cfg.Timeout) {
|
|
|
|
cfg.Timeout = fmt.Sprintf("%ss", cfg.Timeout)
|
|
|
|
}
|
|
|
|
|
2019-12-24 19:08:09 +00:00
|
|
|
if cfg.Debug && cfg.Stderr != nil {
|
|
|
|
cfg.logDebug()
|
|
|
|
}
|
|
|
|
|
2019-12-24 17:34:38 +00:00
|
|
|
return &cfg, nil
|
2019-12-23 22:44:59 +00:00
|
|
|
}
|
2019-12-24 19:08:09 +00:00
|
|
|
|
|
|
|
func (cfg Config) logDebug() {
|
|
|
|
if cfg.KubeToken != "" {
|
|
|
|
cfg.KubeToken = "(redacted)"
|
|
|
|
}
|
|
|
|
fmt.Fprintf(cfg.Stderr, "Generated config: %+v\n", cfg)
|
|
|
|
}
|