From 37d10c8ebbe12c54b9aad4229653d7fcf7f1e65c Mon Sep 17 00:00:00 2001 From: SteveRuble Date: Wed, 25 Aug 2021 08:31:45 -0400 Subject: [PATCH] fix: improve jira and vault commands --- cmd/release.go | 83 ++++++++++++++++++++++++++++-------- cmd/vault.go | 61 +++++++++++++++++++++++--- pkg/actions/suite_test.go | 5 +++ pkg/bosun/platform.go | 4 +- pkg/command/command_value.go | 1 + pkg/jira/jira.go | 25 ----------- pkg/values/value_set.go | 4 ++ 7 files changed, 134 insertions(+), 49 deletions(-) diff --git a/cmd/release.go b/cmd/release.go index 6eeebbf..fa33350 100644 --- a/cmd/release.go +++ b/cmd/release.go @@ -9,6 +9,7 @@ import ( "github.com/naveego/bosun/pkg/command" "github.com/naveego/bosun/pkg/semver" "github.com/naveego/bosun/pkg/util" + "github.com/naveego/bosun/pkg/util/stringsn" "github.com/olekukonko/tablewriter" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -174,7 +175,6 @@ var releaseShowPreviousCmd = addCommand(releaseCmd, &cobra.Command{ }, }) - var releaseDotCmd = addCommand(releaseCmd, &cobra.Command{ Use: "dot", Short: "Prints a dot diagram of the release.", @@ -348,6 +348,59 @@ var releaseAddCmd = addCommand(releaseCmd, &cobra.Command{ cmd.Flags().String(ArgReleaseAddBump, "none", "The version bump to apply to the app.") }, withFilteringFlags) +var releaseReloadCmd = addCommand(releaseCmd, &cobra.Command{ + Use: "reload {stable|unstable} [apps...]", + Args: cobra.MinimumNArgs(1), + Short: "Reloads an app (or all apps) into a release from the location declared in the manifest.", + RunE: func(cmd *cobra.Command, args []string) error { + b := MustGetBosun() + + p, err := b.GetCurrentPlatform() + if err != nil { + return err + } + + r := mustGetRelease(p, args[0], bosun.SlotUnstable, bosun.SlotStable) + + requestedApps := args[1:] + + ctx := b.NewContext() + + for appName, appMetadata := range r.AppMetadata { + + if len(requestedApps) > 0 && !stringsn.Contains(appName, requestedApps) { + ctx.Log().Info("Skipping app %s because it wasn't requested.", appName) + continue + } + ctx.Log().Info("Reloading app %s.", appName) + + app, appErr := b.ProvideApp(bosun.AppProviderRequest{ + Name: appName, + Branch: appMetadata.Branch, + }) + + if appErr != nil { + ctx.Log().WithError(err).Warnf("Couldn't provide app %s from branch %s", appName, appMetadata.Branch) + } + + manifest, appErr := app.GetManifest(ctx) + if appErr != nil { + ctx.Log().WithError(err).Warnf("Couldn't get manifest from app %s from branch %s", appName, appMetadata.Branch) + } + + appErr = r.AddOrReplaceApp(manifest, false) + if appErr != nil { + ctx.Log().WithError(err).Warnf("Couldn't add app to release for app %s from branch %s", appName, appMetadata.Branch) + } + } + + err = p.Save(b.NewContext()) + return err + }, +}, func(cmd *cobra.Command) { + cmd.Flags().String(ArgReleaseAddBranch, "", "The branch to add the app from (defaults to the branch pattern for the slot).") + cmd.Flags().String(ArgReleaseAddBump, "none", "The version bump to apply to the app.") +}, withFilteringFlags) const ( ArgReleaseAddBranch = "branch" @@ -588,8 +641,6 @@ only those apps will be deployed. Otherwise, all apps in the release will be dep withFilteringFlags, withValueSetFlags) - - var releaseUpdateCmd = addCommand(releaseCmd, &cobra.Command{ Use: "update {stable|unstable} [apps...]", Short: "Updates the release with the correct values from the apps in it.", @@ -666,29 +717,28 @@ var releaseChangelogCmd = addCommand(releaseCmd, &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { return errors.New("not implemented") -/* viper.BindPFlags(cmd.Flags()) + /* viper.BindPFlags(cmd.Flags()) - b := MustGetBosun() - ctx := b.NewContext() + b := MustGetBosun() + ctx := b.NewContext() - p, err := b.GetCurrentPlatform() - if err != nil { - return err - } + p, err := b.GetCurrentPlatform() + if err != nil { + return err + } - err = p.CommitCurrentRelease(ctx) - if err != nil { - return err - } + err = p.CommitCurrentRelease(ctx) + if err != nil { + return err + } - return nil*/ + return nil*/ }, }, withFilteringFlags) const ArgReleaseSkipValidate = "skip-validation" const ArgReleaseRecycle = "recycle" - func diffStrings(a, b string) []difflib.DiffRecord { left := strings.Split(a, "\n") right := strings.Split(b, "\n") @@ -706,4 +756,3 @@ func renderDiff(diff difflib.DiffRecord) string { } panic(fmt.Sprintf("invalid delta %v", diff.Delta)) } - diff --git a/cmd/vault.go b/cmd/vault.go index 1249cba..1cf2a36 100644 --- a/cmd/vault.go +++ b/cmd/vault.go @@ -19,12 +19,14 @@ import ( "github.com/fatih/color" "github.com/google/uuid" "github.com/naveego/bosun/pkg/core" + "github.com/naveego/bosun/pkg/kube/kubeclient" "github.com/naveego/bosun/pkg/templating" "github.com/naveego/bosun/pkg/vault" "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/viper" "gopkg.in/yaml.v2" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "os/user" "strings" "time" @@ -228,7 +230,6 @@ var vaultUnsealCmd = &cobra.Command{ SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) error { - namespace := "default" if len(args) > 0 { namespace = args[0] @@ -249,7 +250,6 @@ var vaultUnsealCmd = &cobra.Command{ core.Log.Infof("Unsealing vault in namespace %q", namespace) - initializer := vault.VaultInitializer{ Client: vaultClient, VaultNamespace: namespace, @@ -268,7 +268,6 @@ var vaultInstallJoseCmd = addCommand(vaultCmd, &cobra.Command{ SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) error { - namespace := "default" if len(args) > 0 { namespace = args[0] @@ -289,7 +288,6 @@ var vaultInstallJoseCmd = addCommand(vaultCmd, &cobra.Command{ core.Log.Infof("Installing jose in namespace %q", namespace) - initializer := vault.VaultInitializer{ Client: vaultClient, VaultNamespace: namespace, @@ -405,6 +403,57 @@ var vaultJWTCmd = &cobra.Command{ }, } +var vaultK8sRootTokenCmd = addCommand(vaultCmd, &cobra.Command{ + Use: "get-root-token-from-k8s", + Short: "Tries to pull the root token from the conventional location in k8s. This will not return an error if it fails.", + Long: ``, + Example: "vault init-dev", + SilenceUsage: true, + RunE: func(cmd *cobra.Command, args []string) error { + + err := func() error { + + cluster := viper.GetString(ArgGlobalCluster) + + k8sClient, err := kubeclient.GetKubeClientWithContext("", cluster) + + if err != nil { + return err + } + + namespace := viper.GetString(ArgVaultNamespace) + + secret, err := k8sClient.CoreV1().Secrets(namespace).Get("vault-root-token", v1.GetOptions{}) + if err != nil { + return errors.Wrap(err, "couldn't get vault-root-token secret") + } + + encodedToken, hasToken := secret.Data["root"] + if !hasToken { + return errors.Wrap(err, "secret vault-root-token did not contain root token") + } + + fmt.Println(string(encodedToken)) + + return nil + }() + + if err != nil { + if viper.GetBool(argSuppressErrors) { + core.Log.Warnf("Error getting root token from k8s: %s", err) + return nil + } + return err + } + + return nil + }, +}, func(cmd *cobra.Command) { + + cmd.Flags().Bool(argSuppressErrors, false, "Don't return errors if something fails.") + cmd.Flags().String(ArgVaultNamespace, "default", "The namespace vault is deployed into.") +}) + const ( ArgVaultAddr = "vault-addr" ArgVaultToken = "vault-token" @@ -417,7 +466,9 @@ const ( ArgVaultSecretOverwrite = "overwrite" ArgVaultSecretDefault = "default" ArgVaultNamespace = "vault-namespace" - ArgVaultCluster = "cluster" + + argK8sNamespace = "namespace" + argSuppressErrors = "no-errors" ) func init() { diff --git a/pkg/actions/suite_test.go b/pkg/actions/suite_test.go index d708941..56139e7 100644 --- a/pkg/actions/suite_test.go +++ b/pkg/actions/suite_test.go @@ -4,6 +4,7 @@ import ( "context" "github.com/naveego/bosun/pkg/actions" "github.com/naveego/bosun/pkg/cli" + "github.com/naveego/bosun/pkg/command" "github.com/naveego/bosun/pkg/core" "github.com/naveego/bosun/pkg/ioc" "github.com/naveego/bosun/pkg/templating" @@ -46,6 +47,10 @@ type TestActionContext struct { log *logrus.Entry } +func (t TestActionContext) GetWorkspaceCommand(name string, hint string) *command.CommandValue { + panic("implement me") +} + func (t TestActionContext) ResolvePath(path string, expansions ...string) string { panic("implement me") } diff --git a/pkg/bosun/platform.go b/pkg/bosun/platform.go index 4803b20..fa13687 100644 --- a/pkg/bosun/platform.go +++ b/pkg/bosun/platform.go @@ -663,7 +663,7 @@ func (p *Platform) CommitPlan(ctx BosunContext) (*ReleaseManifest, error) { if err != nil { return nil, err } - currentProvider := NewReleaseManifestAppProvider(currentRelease) + // currentProvider := NewReleaseManifestAppProvider(currentRelease) plan, err := currentRelease.GetPlan() if err != nil { return nil, err @@ -765,7 +765,7 @@ func (p *Platform) CommitPlan(ctx BosunContext) (*ReleaseManifest, error) { case SlotStable: log.Infof("App %q has already been added to this release.", appName) - app, getAppErr = currentProvider.ProvideApp(AppProviderRequest{Name: appName}) + app, getAppErr = previousProvider.ProvideApp(AppProviderRequest{Name: appName}) if getAppErr != nil { return nil, getAppErr } diff --git a/pkg/command/command_value.go b/pkg/command/command_value.go index 85411d4..8a82049 100644 --- a/pkg/command/command_value.go +++ b/pkg/command/command_value.go @@ -14,6 +14,7 @@ type CommandValue struct { OS map[string]*CommandValue `yaml:"os,omitempty" json:"os,omitempty"` WorkspaceCommand string `yaml:"workspaceCommand,omitempty"` WorkspaceCommandHint string `yaml:"workspaceCommandHint,omitempty"` + Disabled bool `yaml:"disabled"` resolvedValue string } diff --git a/pkg/jira/jira.go b/pkg/jira/jira.go index c3913a0..48acaf7 100644 --- a/pkg/jira/jira.go +++ b/pkg/jira/jira.go @@ -170,31 +170,6 @@ func (c *Client) HandleEvent(event *stories.ValidatedEvent) error { } func (c *Client) handleBranchCreated(event *stories.ValidatedEvent, payload stories.EventBranchCreated, story *jira.Issue) (error, bool) { - subtaskName := SubtaskName(event.Issue(), payload.Branch) - var subtask = &jira.Issue{ - Fields: &jira.IssueFields{ - Parent: &jira.Parent{Key: story.Key, ID: story.ID}, - Project: story.Fields.Project, - Summary: subtaskName.String(), - Type: jira.IssueType{ID: "5"}, - Assignee: &jira.User{AccountID: c.AccountID}, - Description: fmt.Sprintf(`This subtask tracks development in the %s repo. - -The branch is %s - -Link: %s -`, event.Issue().RepoRef.String(), payload.Branch, event.URL()), - }, - } - var err error - var res *jira.Response - - subtask, res, err = c.jira.Issue.Create(subtask) - if err != nil { - return errors.Wrapf(detailedErr(res, err), "create subtask documenting branch %q for story with key %q, id %q", subtaskName, story.Key, story.ID), true - } - - err = c.doTransition(subtask.ID, c.transitions.InDevelopment) return nil, false } diff --git a/pkg/values/value_set.go b/pkg/values/value_set.go index 0736204..0c2ac8d 100644 --- a/pkg/values/value_set.go +++ b/pkg/values/value_set.go @@ -286,6 +286,10 @@ func (v ValueSet) WithDynamicValuesResolved(ctx command.ExecutionContext) (Value for k, value := range out.Dynamic { + if value.Disabled { + continue + } + if value.Script != "" { ctx.Log().Debugf("Resolving dynamic value %q using script:\n %s", k, value.Script) }