Skip to content

Commit da9a313

Browse files
authored
fix(cli): allow specifying empty provisioner tag set with --provisioner-tag="-" (#18205)
Relates to #17818 Note: due to limitations in `cobra/serpent` I ended up having to use `-` to signify absence of provisioner tags. This value is not a valid key-value pair and thus not a valid tag.
1 parent b0d23ca commit da9a313

File tree

4 files changed

+117
-19
lines changed

4 files changed

+117
-19
lines changed

cli/templatepush.go

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"os"
1010
"path/filepath"
11+
"slices"
1112
"strings"
1213
"time"
1314

@@ -80,6 +81,46 @@ func (r *RootCmd) templatePush() *serpent.Command {
8081
createTemplate = true
8182
}
8283

84+
var tags map[string]string
85+
// Passing --provisioner-tag="-" allows the user to clear all provisioner tags.
86+
if len(provisionerTags) == 1 && strings.TrimSpace(provisionerTags[0]) == "-" {
87+
cliui.Warn(inv.Stderr, "Not reusing provisioner tags from the previous template version.")
88+
tags = map[string]string{}
89+
} else {
90+
tags, err = ParseProvisionerTags(provisionerTags)
91+
if err != nil {
92+
return err
93+
}
94+
95+
// If user hasn't provided new provisioner tags, inherit ones from the active template version.
96+
if len(tags) == 0 && template.ActiveVersionID != uuid.Nil {
97+
templateVersion, err := client.TemplateVersion(inv.Context(), template.ActiveVersionID)
98+
if err != nil {
99+
return err
100+
}
101+
tags = templateVersion.Job.Tags
102+
cliui.Info(inv.Stderr, "Re-using provisioner tags from the active template version.")
103+
cliui.Info(inv.Stderr, "Tip: You can override these tags by passing "+cliui.Code(`--provisioner-tag="key=value"`)+".")
104+
cliui.Info(inv.Stderr, " You can also clear all provisioner tags by passing "+cliui.Code(`--provisioner-tag="-"`)+".")
105+
}
106+
}
107+
108+
{ // For clarity, display provisioner tags to the user.
109+
var tmp []string
110+
for k, v := range tags {
111+
if k == provisionersdk.TagScope || k == provisionersdk.TagOwner {
112+
continue
113+
}
114+
tmp = append(tmp, fmt.Sprintf("%s=%q", k, v))
115+
}
116+
slices.Sort(tmp)
117+
tagStr := strings.Join(tmp, " ")
118+
if len(tmp) == 0 {
119+
tagStr = "<none>"
120+
}
121+
cliui.Info(inv.Stderr, "Provisioner tags: "+cliui.Code(tagStr))
122+
}
123+
83124
err = uploadFlags.checkForLockfile(inv)
84125
if err != nil {
85126
return xerrors.Errorf("check for lockfile: %w", err)
@@ -104,21 +145,6 @@ func (r *RootCmd) templatePush() *serpent.Command {
104145
return err
105146
}
106147

107-
tags, err := ParseProvisionerTags(provisionerTags)
108-
if err != nil {
109-
return err
110-
}
111-
112-
// If user hasn't provided new provisioner tags, inherit ones from the active template version.
113-
if len(tags) == 0 && template.ActiveVersionID != uuid.Nil {
114-
templateVersion, err := client.TemplateVersion(inv.Context(), template.ActiveVersionID)
115-
if err != nil {
116-
return err
117-
}
118-
tags = templateVersion.Job.Tags
119-
inv.Logger.Info(inv.Context(), "reusing existing provisioner tags", "tags", tags)
120-
}
121-
122148
userVariableValues, err := codersdk.ParseUserVariableValues(
123149
varsFiles,
124150
variablesFile,
@@ -214,7 +240,7 @@ func (r *RootCmd) templatePush() *serpent.Command {
214240
},
215241
{
216242
Flag: "provisioner-tag",
217-
Description: "Specify a set of tags to target provisioner daemons.",
243+
Description: "Specify a set of tags to target provisioner daemons. If you do not specify any tags, the tags from the active template version will be reused, if available. To remove existing tags, use --provisioner-tag=\"-\".",
218244
Value: serpent.StringArrayOf(&provisionerTags),
219245
},
220246
{

cli/templatepush_test.go

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ func TestTemplatePush(t *testing.T) {
602602
templateVersion = coderdtest.AwaitTemplateVersionJobCompleted(t, client, templateVersion.ID)
603603
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, templateVersion.ID)
604604

605-
// Push new template version without provisioner tags. CLI should reuse tags from the previous version.
605+
// Push new template version with different provisioner tags.
606606
source := clitest.CreateTemplateVersionSource(t, &echo.Responses{
607607
Parse: echo.ParseComplete,
608608
ProvisionApply: echo.ApplyComplete,
@@ -639,6 +639,75 @@ func TestTemplatePush(t *testing.T) {
639639
require.EqualValues(t, map[string]string{"foobar": "foobaz", "owner": "", "scope": "organization"}, templateVersion.Job.Tags)
640640
})
641641

642+
t.Run("DeleteTags", func(t *testing.T) {
643+
t.Parallel()
644+
645+
ctx := testutil.Context(t, testutil.WaitLong)
646+
647+
// Start the first provisioner with no tags.
648+
client, provisionerDocker, api := coderdtest.NewWithAPI(t, &coderdtest.Options{
649+
IncludeProvisionerDaemon: true,
650+
ProvisionerDaemonTags: map[string]string{},
651+
})
652+
defer provisionerDocker.Close()
653+
654+
// Start the second provisioner with a tag set.
655+
provisionerFoobar := coderdtest.NewTaggedProvisionerDaemon(t, api, "provisioner-foobar", map[string]string{
656+
"foobar": "foobaz",
657+
})
658+
defer provisionerFoobar.Close()
659+
660+
owner := coderdtest.CreateFirstUser(t, client)
661+
templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin())
662+
663+
// Create the template with initial tagged template version.
664+
templateVersion := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil, func(ctvr *codersdk.CreateTemplateVersionRequest) {
665+
ctvr.ProvisionerTags = map[string]string{
666+
"foobar": "foobaz",
667+
}
668+
})
669+
templateVersion = coderdtest.AwaitTemplateVersionJobCompleted(t, client, templateVersion.ID)
670+
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, templateVersion.ID)
671+
672+
// Stop the tagged provisioner daemon.
673+
provisionerFoobar.Close()
674+
675+
// Push new template version with no provisioner tags.
676+
source := clitest.CreateTemplateVersionSource(t, &echo.Responses{
677+
Parse: echo.ParseComplete,
678+
ProvisionApply: echo.ApplyComplete,
679+
})
680+
inv, root := clitest.New(t, "templates", "push", template.Name, "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--name", template.Name, "--provisioner-tag=\"-\"")
681+
clitest.SetupConfig(t, templateAdmin, root)
682+
pty := ptytest.New(t).Attach(inv)
683+
684+
execDone := make(chan error)
685+
go func() {
686+
execDone <- inv.WithContext(ctx).Run()
687+
}()
688+
689+
matches := []struct {
690+
match string
691+
write string
692+
}{
693+
{match: "Upload", write: "yes"},
694+
}
695+
for _, m := range matches {
696+
pty.ExpectMatch(m.match)
697+
pty.WriteLine(m.write)
698+
}
699+
700+
require.NoError(t, <-execDone)
701+
702+
// Verify template version tags
703+
template, err := client.Template(ctx, template.ID)
704+
require.NoError(t, err)
705+
706+
templateVersion, err = client.TemplateVersion(ctx, template.ActiveVersionID)
707+
require.NoError(t, err)
708+
require.EqualValues(t, map[string]string{"owner": "", "scope": "organization"}, templateVersion.Job.Tags)
709+
})
710+
642711
t.Run("DoNotChangeTags", func(t *testing.T) {
643712
t.Parallel()
644713

cli/testdata/coder_templates_push_--help.golden

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ OPTIONS:
3333
generated if not provided.
3434

3535
--provisioner-tag string-array
36-
Specify a set of tags to target provisioner daemons.
36+
Specify a set of tags to target provisioner daemons. If you do not
37+
specify any tags, the tags from the active template version will be
38+
reused, if available. To remove existing tags, use
39+
--provisioner-tag="-".
3740

3841
--var string-array
3942
Alias of --variable.

docs/reference/cli/templates_push.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)