Skip to content

Commit d4e5a91

Browse files
committed
PS-1530: Viper -> Kong, rework controller entrypoint
1 parent 4beedd0 commit d4e5a91

File tree

11 files changed

+675
-575
lines changed

11 files changed

+675
-575
lines changed

cmd/controller/controller.go

Lines changed: 136 additions & 272 deletions
Large diffs are not rendered by default.

cmd/controller/controller_test.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package controller_test
22

33
import (
4+
"os"
45
"testing"
56
"time"
67

78
"github.com/buildkite/agent-stack-k8s/v2/cmd/controller"
89
"github.com/buildkite/agent-stack-k8s/v2/internal/controller/config"
910
"github.com/google/go-cmp/cmp"
10-
"github.com/spf13/cobra"
1111
"github.com/stretchr/testify/require"
1212
corev1 "k8s.io/api/core/v1"
1313
"k8s.io/apimachinery/pkg/api/resource"
@@ -163,22 +163,21 @@ func TestReadAndParseConfig(t *testing.T) {
163163
},
164164
}
165165

166-
// These need to be unset to as it is set in CI which pollutes the test environment
167-
t.Setenv("INTEGRATION_TEST_BUILDKITE_TOKEN", "")
168-
t.Setenv("IMAGE", "")
169-
t.Setenv("NAMESPACE", "")
170-
t.Setenv("AGENT_TOKEN_SECRET", "")
171-
172-
cmd := &cobra.Command{}
173-
controller.AddConfigFlags(cmd)
174-
v, err := controller.ReadConfigFromFileArgsAndEnv(cmd, []string{})
175-
require.NoError(t, err)
176-
177-
// We need to read the config file from the test
178-
v.SetConfigFile("../../examples/config.yaml")
179-
require.NoError(t, v.ReadInConfig())
166+
// These need to be unset as they are set in CI or .envrc which pollutes the test environment
167+
// Note: We must unset (not set to empty) because Kong will try to parse empty strings
168+
for _, env := range []string{
169+
"INTEGRATION_TEST_BUILDKITE_TOKEN",
170+
"BUILDKITE_TOKEN",
171+
"IMAGE",
172+
"NAMESPACE",
173+
"AGENT_TOKEN_SECRET",
174+
"IMAGE_PULL_BACKOFF_GRACE_PERIOD",
175+
} {
176+
t.Setenv(env, "") // t.Setenv actually unsets when value is empty in cleanup
177+
os.Unsetenv(env)
178+
}
180179

181-
actual, err := controller.ParseAndValidateConfig(v)
180+
actual, err := controller.LoadAndValidateConfig("../../examples/config.yaml")
182181
require.NoError(t, err)
183182

184183
if diff := cmp.Diff(*actual, expected); diff != "" {

cmd/controller/flags.go

Lines changed: 457 additions & 178 deletions
Large diffs are not rendered by default.

cmd/linter/linter.go

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import (
99
"log"
1010
"os"
1111

12+
"github.com/alecthomas/kong"
1213
"github.com/buildkite/agent-stack-k8s/v2/internal/controller/scheduler"
1314
"github.com/buildkite/go-buildkite/v3/buildkite"
14-
"github.com/go-playground/validator/v10"
15-
"github.com/spf13/cobra"
1615
"github.com/xeipuuv/gojsonschema"
1716
"sigs.k8s.io/yaml"
1817
)
@@ -25,39 +24,34 @@ const (
2524
//go:embed schema.json
2625
var schema string
2726

28-
type Options struct {
29-
File string `validate:"required,file"`
27+
// CLI represents the lint subcommand for Kong.
28+
type CLI struct {
29+
File string `kong:"arg,required,help='Path to the pipeline file'"`
3030
}
3131

32-
func (o *Options) AddFlags(cmd *cobra.Command) {
33-
cmd.Flags().StringVarP(&o.File, "file", "f", "", "path to the pipeline file, or {-} for stdin")
34-
}
35-
36-
func (o *Options) Validate() error {
37-
return validator.New().Struct(o)
38-
}
39-
40-
func New() *cobra.Command {
41-
o := &Options{}
42-
43-
cmd := &cobra.Command{
44-
Use: "lint",
45-
Short: "A tool for linting Buildkite pipelines",
46-
SilenceUsage: true,
47-
RunE: func(cmd *cobra.Command, args []string) error {
48-
if err := o.Validate(); err != nil {
49-
return fmt.Errorf("failed to validate config: %w", err)
50-
}
51-
return Lint(cmd.Context(), o)
52-
},
32+
// Run parses CLI arguments and executes the lint command.
33+
func Run() error {
34+
cli := &CLI{}
35+
parser, err := kong.New(
36+
cli,
37+
kong.Name("agent-stack-k8s lint"),
38+
kong.Description("A tool for linting Buildkite pipelines"),
39+
kong.UsageOnError(),
40+
)
41+
if err != nil {
42+
return err
5343
}
54-
o.AddFlags(cmd)
55-
56-
return cmd
44+
_, err = parser.Parse(os.Args[2:])
45+
if err != nil {
46+
parser.FatalIfErrorf(err)
47+
return err
48+
}
49+
return Lint(context.Background(), cli.File)
5750
}
5851

59-
func Lint(ctx context.Context, options *Options) error {
60-
contents, err := os.ReadFile(options.File)
52+
// Lint validates a Buildkite pipeline file against the schema.
53+
func Lint(ctx context.Context, file string) error {
54+
contents, err := os.ReadFile(file)
6155
if err != nil {
6256
return fmt.Errorf("failed to open file: %w", err)
6357
}
@@ -105,7 +99,7 @@ func Lint(ctx context.Context, options *Options) error {
10599
if err := schemaLoader.AddSchema(k8sSchema, gojsonschema.NewReferenceLoader(k8sSchema)); err != nil {
106100
return fmt.Errorf("failed to add kubernetes schema: %w", err)
107101
}
108-
schema, err := schemaLoader.Compile(gojsonschema.NewStringLoader(schema))
102+
compiledSchema, err := schemaLoader.Compile(gojsonschema.NewStringLoader(schema))
109103
if err != nil {
110104
return fmt.Errorf("failed to compile schemas: %w", err)
111105
}
@@ -115,7 +109,7 @@ func Lint(ctx context.Context, options *Options) error {
115109
}
116110
documentLoader := gojsonschema.NewBytesLoader(bs)
117111

118-
result, err := schema.Validate(documentLoader)
112+
result, err := compiledSchema.Validate(documentLoader)
119113
if err != nil {
120114
return fmt.Errorf("failed to validate: %w", err)
121115
}

cmd/version/version.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
11
package version
22

33
import (
4-
"context"
54
"fmt"
6-
"io"
75
"os"
86

97
"github.com/buildkite/agent-stack-k8s/v2/internal/version"
10-
"github.com/spf13/cobra"
118
)
129

13-
func New() *cobra.Command {
14-
return &cobra.Command{
15-
Use: "version",
16-
Short: "Prints the version",
17-
SilenceUsage: true,
18-
RunE: func(cmd *cobra.Command, args []string) error {
19-
return Version(cmd.Context(), os.Stdout)
20-
},
21-
}
22-
}
23-
24-
func Version(ctx context.Context, out io.WriteCloser) error {
25-
fmt.Fprintf(out, "%s\n", version.Version())
10+
// Run prints the version to stdout.
11+
func Run() error {
12+
fmt.Fprintf(os.Stdout, "%s\n", version.Version())
2613
return nil
2714
}

go.mod

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.25.4
44

55
require (
66
github.com/Khan/genqlient v0.8.1
7+
github.com/alecthomas/kong v1.13.0
78
github.com/buildkite/agent/v3 v3.115.2
89
github.com/buildkite/go-buildkite/v3 v3.13.0
910
github.com/buildkite/roko v1.4.0
@@ -12,13 +13,10 @@ require (
1213
github.com/go-playground/locales v0.14.1
1314
github.com/go-playground/universal-translator v0.18.1
1415
github.com/go-playground/validator/v10 v10.30.1
15-
github.com/go-viper/mapstructure/v2 v2.4.0
1616
github.com/google/uuid v1.6.0
1717
github.com/jedib0t/go-pretty/v6 v6.7.8
1818
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
1919
github.com/lmittmann/tint v1.1.2
20-
github.com/spf13/cobra v1.10.2
21-
github.com/spf13/viper v1.21.0
2220
github.com/xeipuuv/gojsonschema v1.2.0
2321
gotest.tools/gotestsum v1.13.0
2422
k8s.io/api v0.35.0
@@ -38,6 +36,7 @@ require (
3836
github.com/aws/aws-sdk-go-v2/service/s3 v1.93.2 // indirect
3937
github.com/aws/aws-sdk-go-v2/service/signin v1.0.4 // indirect
4038
github.com/buildkite/zstash v0.7.0 // indirect
39+
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
4140
github.com/klauspost/compress v1.18.2 // indirect
4241
github.com/saracen/zipextra v0.0.0-20250129175152-f1aa42d25216 // indirect
4342
github.com/wolfeidau/quickzip v1.0.2 // indirect
@@ -139,7 +138,6 @@ require (
139138
github.com/gowebpki/jcs v1.0.1 // indirect
140139
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.3 // indirect
141140
github.com/hashicorp/go-version v1.7.0 // indirect
142-
github.com/inconshreveable/mousetrap v1.1.0 // indirect
143141
github.com/josharian/intern v1.0.0 // indirect
144142
github.com/json-iterator/go v1.1.12 // indirect
145143
github.com/kylelemons/godebug v1.1.0 // indirect
@@ -163,7 +161,6 @@ require (
163161
github.com/opentracing/opentracing-go v1.2.0 // indirect
164162
github.com/outcaste-io/ristretto v0.2.3 // indirect
165163
github.com/pborman/uuid v1.2.1 // indirect
166-
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
167164
github.com/philhofer/fwd v1.2.0 // indirect
168165
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
169166
github.com/pkg/errors v0.9.1 // indirect
@@ -180,16 +177,11 @@ require (
180177
github.com/qri-io/jsonschema v0.2.1 // indirect
181178
github.com/rivo/uniseg v0.4.7 // indirect
182179
github.com/russross/blackfriday/v2 v2.1.0 // indirect
183-
github.com/sagikazarmark/locafero v0.11.0 // indirect
184180
github.com/secure-systems-lab/go-securesystemslib v0.9.1 // indirect
185181
github.com/segmentio/asm v1.2.1 // indirect
186182
github.com/shirou/gopsutil/v4 v4.25.9 // indirect
187-
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
188-
github.com/spf13/afero v1.15.0 // indirect
189-
github.com/spf13/cast v1.10.0 // indirect
190-
github.com/spf13/pflag v1.0.10
183+
github.com/spf13/pflag v1.0.10 // indirect
191184
github.com/stretchr/testify v1.11.1
192-
github.com/subosito/gotenv v1.6.0 // indirect
193185
github.com/theckman/httpforwarded v0.4.0 // indirect
194186
github.com/tinylib/msgp v1.4.0 // indirect
195187
github.com/tklauser/go-sysconf v0.3.15 // indirect

go.sum

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERo
6666
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
6767
github.com/agnivade/levenshtein v1.2.1 h1:EHBY3UOn1gwdy/VbFwgo4cxecRznFk7fKWN1KOX7eoM=
6868
github.com/agnivade/levenshtein v1.2.1/go.mod h1:QVVI16kDrtSuwcpd0p1+xMC6Z/VfhtCyDIjcwga4/DU=
69+
github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0=
70+
github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
71+
github.com/alecthomas/kong v1.13.0 h1:5e/7XC3ugvhP1DQBmTS+WuHtCbcv44hsohMgcvVxSrA=
72+
github.com/alecthomas/kong v1.13.0/go.mod h1:wrlbXem1CWqUV5Vbmss5ISYhsVPkBb1Yo7YKJghju2I=
73+
github.com/alecthomas/repr v0.5.2 h1:SU73FTI9D1P5UNtvseffFSGmdNci/O6RsqzeXJtP0Qs=
74+
github.com/alecthomas/repr v0.5.2/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
6975
github.com/alexflint/go-arg v1.5.1 h1:nBuWUCpuRy0snAG+uIJ6N0UvYxpxA0/ghA/AaHxlT8Y=
7076
github.com/alexflint/go-arg v1.5.1/go.mod h1:A7vTJzvjoaSTypg4biM5uYNTkJ27SkNTArtYXnlqVO8=
7177
github.com/alexflint/go-scalar v1.2.0 h1:WR7JPKkeNpnYIOfHRa7ivM21aWAdHD0gEWHCx+WQBRw=
@@ -157,7 +163,6 @@ github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UF
157163
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
158164
github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575 h1:kHaBemcxl8o/pQ5VM1c8PVE1PubbNx3mjUr09OqWGCs=
159165
github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575/go.mod h1:9d6lWj8KzO/fd/NrVaLscBKmPigpZpn5YawRPw+e3Yo=
160-
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
161166
github.com/cpuguy83/go-md2man/v2 v2.0.7 h1:zbFlGlXEAKlwXpmvle3d8Oe3YnkKIK4xSRTd3sHPnBo=
162167
github.com/cpuguy83/go-md2man/v2 v2.0.7/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
163168
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
@@ -193,8 +198,6 @@ github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
193198
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
194199
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
195200
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
196-
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
197-
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
198201
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
199202
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
200203
github.com/fxamacker/cbor/v2 v2.9.0 h1:NpKPmjDBgUfBms6tr6JZkTHtfFGcMKsw3eGcmD/sapM=
@@ -279,8 +282,8 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.3 h1:NmZ1PKzSTQbuGHw9DGPFomqkkLW
279282
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.3/go.mod h1:zQrxl1YP88HQlA6i9c63DSVPFklWpGX4OWAc9bFuaH4=
280283
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
281284
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
282-
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
283-
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
285+
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
286+
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
284287
github.com/jedib0t/go-pretty/v6 v6.7.8 h1:BVYrDy5DPBA3Qn9ICT+PokP9cvCv1KaHv2i+Hc8sr5o=
285288
github.com/jedib0t/go-pretty/v6 v6.7.8/go.mod h1:YwC5CE4fJ1HFUDeivSV1r//AmANFHyqczZk+U6BDALU=
286289
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
@@ -356,8 +359,6 @@ github.com/outcaste-io/ristretto v0.2.3 h1:AK4zt/fJ76kjlYObOeNwh4T3asEuaCmp26pOv
356359
github.com/outcaste-io/ristretto v0.2.3/go.mod h1:W8HywhmtlopSB1jeMg3JtdIhf+DYkLAr0VN/s4+MHac=
357360
github.com/pborman/uuid v1.2.1 h1:+ZZIw58t/ozdjRaXh/3awHfmWRbzYxJoAdNJxe/3pvw=
358361
github.com/pborman/uuid v1.2.1/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
359-
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
360-
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
361362
github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM=
362363
github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
363364
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
@@ -396,8 +397,6 @@ github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0t
396397
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
397398
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
398399
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
399-
github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc=
400-
github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik=
401400
github.com/saracen/zipextra v0.0.0-20250129175152-f1aa42d25216 h1:8zyjtFyKi5NJySVOJRiHmSN1vl6qugQ5n9C4X7WyY3U=
402401
github.com/saracen/zipextra v0.0.0-20250129175152-f1aa42d25216/go.mod h1:hnzuad9d2wdd3z8fC6UouHQK5qZxqv3F/E6MMzXc7q0=
403402
github.com/secure-systems-lab/go-securesystemslib v0.9.1 h1:nZZaNz4DiERIQguNy0cL5qTdn9lR8XKHf4RUyG1Sx3g=
@@ -410,21 +409,10 @@ github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NF
410409
github.com/shirou/gopsutil/v4 v4.25.9 h1:JImNpf6gCVhKgZhtaAHJ0serfFGtlfIlSC08eaKdTrU=
411410
github.com/shirou/gopsutil/v4 v4.25.9/go.mod h1:gxIxoC+7nQRwUl/xNhutXlD8lq+jxTgpIkEf3rADHL8=
412411
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
413-
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw=
414-
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8/go.mod h1:3n1Cwaq1E1/1lhQhtRK2ts/ZwZEhjcQeJQ1RuC6Q/8U=
415412
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
416413
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
417-
github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I=
418-
github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg=
419-
github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY=
420-
github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
421-
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
422-
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
423-
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
424414
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
425415
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
426-
github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU=
427-
github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY=
428416
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
429417
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
430418
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
@@ -441,8 +429,6 @@ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl
441429
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
442430
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
443431
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
444-
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
445-
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
446432
github.com/theckman/httpforwarded v0.4.0 h1:N55vGJT+6ojTnLY3LQCNliJC4TW0P0Pkeys1G1WpX2w=
447433
github.com/theckman/httpforwarded v0.4.0/go.mod h1:GVkFynv6FJreNbgH/bpOU9ITDZ7a5WuzdNCtIMI1pVI=
448434
github.com/tinylib/msgp v1.4.0 h1:SYOeDRiydzOw9kSiwdYp9UcBgPFtLU2WDHaJXyHruf8=

internal/controller/config/command_params.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package config
22

33
import (
4+
"encoding/json"
45
"fmt"
5-
"reflect"
66
"slices"
77
"strings"
88

@@ -129,23 +129,16 @@ var AllowedInterposers = []Interposer{
129129
// BUILDKITE_COMMAND.
130130
type Interposer string
131131

132-
var interposerType = reflect.TypeOf(InterposerBuildkite)
133-
134-
// StringToInterposer implements a [mapstructure.DecodeHookFunc] for decoding
135-
// a string into CmdInterposer.
136-
func StringToInterposer(from, to reflect.Type, data any) (any, error) {
137-
if from.Kind() != reflect.String {
138-
return data, nil
139-
}
140-
if to != interposerType {
141-
return data, nil
142-
}
143-
interp := Interposer(data.(string))
144-
if interp == "" {
145-
return "", nil
132+
// UnmarshalJSON validates that the interposer value is one of the allowed values.
133+
func (i *Interposer) UnmarshalJSON(data []byte) error {
134+
var s string
135+
if err := json.Unmarshal(data, &s); err != nil {
136+
return err
146137
}
138+
interp := Interposer(s)
147139
if !slices.Contains(AllowedInterposers, interp) {
148-
return data, fmt.Errorf("invalid command interposer %q", interp)
140+
return fmt.Errorf("invalid interposer %q, must be one of: %v", s, AllowedInterposers)
149141
}
150-
return interp, nil
142+
*i = interp
143+
return nil
151144
}

internal/controller/config/config.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ const (
3737

3838
var DefaultAgentImage = "ghcr.io/buildkite/agent:" + version.Version()
3939

40-
// viper requires mapstructure struct tags, but the k8s types only have json struct tags.
41-
// mapstructure (the module) supports switching the struct tag to "json", viper does not. So we have
42-
// to have the `mapstructure` tag for viper and the `json` tag is used by the mapstructure!
40+
// Config holds the controller configuration.
4341
type Config struct {
4442
Debug bool `json:"debug"`
4543

0 commit comments

Comments
 (0)