Skip to content

Commit 921e351

Browse files
authored
chore: upgrade go version, add tests and comments, and fix arg names (#396)
1 parent ffc38fe commit 921e351

File tree

23 files changed

+235
-70
lines changed

23 files changed

+235
-70
lines changed

.golangci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
version: "2"
12
run:
23
tests: false
34

@@ -6,10 +7,10 @@ linters:
67
- bodyclose
78
- durationcheck
89
- errcheck
9-
- gosimple
1010
- govet
1111
- ineffassign
1212
- staticcheck
13+
- sloglint
1314
- unparam
1415
- unused
1516
- usestdlibvars

.tool-versions

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
earthly 0.8.15
2-
golang 1.22.7
3-
golangci-lint 1.63.4
2+
golang 1.24.2
3+
golangci-lint 2.1.1
44
helm 3.16.3
55
helm-cr 1.6.1
66
helm-ct 3.11.0

cmd/flags_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,77 @@ func TestStringUsages(t *testing.T) {
5151
})
5252
}
5353
}
54+
55+
func TestCombine(t *testing.T) {
56+
tests := map[string]struct {
57+
dst DocOpt[any]
58+
src DocOpt[any]
59+
expected DocOpt[any]
60+
}{
61+
"combine choices": {
62+
dst: DocOpt[any]{},
63+
src: DocOpt[any]{
64+
choices: []string{"choice1", "choice2"},
65+
},
66+
expected: DocOpt[any]{
67+
choices: []string{"choice1", "choice2"},
68+
},
69+
},
70+
"combine default value": {
71+
dst: DocOpt[any]{},
72+
src: DocOpt[any]{
73+
defaultValue: ptr[any]("default"),
74+
},
75+
expected: DocOpt[any]{
76+
defaultValue: ptr[any]("default"),
77+
},
78+
},
79+
"combine shorthand": {
80+
dst: DocOpt[any]{},
81+
src: DocOpt[any]{
82+
shorthand: ptr("s"),
83+
},
84+
expected: DocOpt[any]{
85+
shorthand: ptr("s"),
86+
},
87+
},
88+
"combine all fields": {
89+
dst: DocOpt[any]{},
90+
src: DocOpt[any]{
91+
choices: []string{"choice1", "choice2"},
92+
defaultValue: ptr[any]("default"),
93+
shorthand: ptr("s"),
94+
},
95+
expected: DocOpt[any]{
96+
choices: []string{"choice1", "choice2"},
97+
defaultValue: ptr[any]("default"),
98+
shorthand: ptr("s"),
99+
},
100+
},
101+
"preserve existing dst values when src is empty": {
102+
dst: DocOpt[any]{
103+
choices: []string{"existing"},
104+
defaultValue: ptr[any]("existing"),
105+
shorthand: ptr("e"),
106+
},
107+
src: DocOpt[any]{},
108+
expected: DocOpt[any]{
109+
choices: []string{"existing"},
110+
defaultValue: ptr[any]("existing"),
111+
shorthand: ptr("e"),
112+
},
113+
},
114+
}
115+
116+
for testName, test := range tests {
117+
t.Run(testName, func(t *testing.T) {
118+
combine(&test.dst, test.src)
119+
assert.Equal(t, test.expected, test.dst)
120+
})
121+
}
122+
}
123+
124+
// Helper function to create pointers for test values
125+
func ptr[T any](v T) *T {
126+
return &v
127+
}

cmd/locations.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import (
1616
"github.com/zapier/kubechecks/pkg/git"
1717
)
1818

19+
// processLocations processes a list of locations, which can be either file paths or Git repository URLs.
20+
// For Git URLs, it clones the repository and updates the location to point to the local clone.
21+
// For file paths, it leaves them unchanged.
22+
// The function returns an error if any Git repository fails to clone.
1923
func processLocations(ctx context.Context, ctr container.Container, locations []string) error {
2024
for index, location := range locations {
2125
if newLocation, err := maybeCloneGitUrl(ctx, ctr.RepoManager, ctr.Config.RepoRefreshInterval, location, ctr.VcsClient.Username(), ctr.Config.RepoShallowClone); err != nil {

cmd/process.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import (
77
"github.com/argoproj/argo-cd/v2/common"
88
"github.com/rs/zerolog/log"
99
"github.com/spf13/cobra"
10-
"github.com/zapier/kubechecks/pkg/container"
1110

11+
"github.com/zapier/kubechecks/pkg"
1212
"github.com/zapier/kubechecks/pkg/config"
13+
"github.com/zapier/kubechecks/pkg/container"
1314
"github.com/zapier/kubechecks/pkg/server"
1415
)
1516

@@ -24,9 +25,7 @@ var processCmd = &cobra.Command{
2425
if err != nil {
2526
log.Fatal().Err(err).Msg("fail to create ssh data dir")
2627
}
27-
defer func() {
28-
os.RemoveAll(tempPath)
29-
}()
28+
defer pkg.WithErrorLogging(func() error { return os.RemoveAll(tempPath) }, "failed to remove temp directory")
3029

3130
// symlink local ssh known hosts to argocd ssh known hosts
3231
homeDir, err := os.UserHomeDir()

cmd/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func init() {
108108
int64Flag(flags, "max-queue-size", "Size of app diff check queue.",
109109
newInt64Opts().
110110
withDefault(1024))
111-
int64Flag(flags, "max-concurrenct-checks", "Number of concurrent checks to run.",
111+
int64Flag(flags, "max-concurrent-checks", "Number of concurrent checks to run.",
112112
newInt64Opts().
113113
withDefault(32))
114114
boolFlag(flags, "enable-hooks-renderer", "Render hooks.", newBoolOpts().withDefault(true))
@@ -125,7 +125,7 @@ func init() {
125125
stringFlag(flags, "identifier", "Identifier for the kubechecks instance. Used to differentiate between multiple kubechecks instances.",
126126
newStringOpts().
127127
withDefault(""))
128-
stringFlag(flags, "generated-store", "URL for the kubepug generated store.",
128+
stringFlag(flags, "kubepug-generated-store", "URL for the kubepug generated store.",
129129
newStringOpts().
130130
withDefault("https://kubepug.xyz/data/data.json"))
131131

docs/usage.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,17 @@ The full list of supported environment variables is described below:
5151
|`KUBECHECKS_ENABLE_PREUPGRADE`|Enable preupgrade checks.|`true`|
5252
|`KUBECHECKS_ENSURE_WEBHOOKS`|Ensure that webhooks are created in repositories referenced by argo.|`false`|
5353
|`KUBECHECKS_FALLBACK_K8S_VERSION`|Fallback target Kubernetes version for schema / upgrade checks.|`1.23.0`|
54-
|`KUBECHECKS_GENERATED_STORE`|URL for the kubepug generated store.|`https://kubepug.xyz/data/data.json`|
5554
|`KUBECHECKS_GITHUB_APP_ID`|Github App ID.|`0`|
5655
|`KUBECHECKS_GITHUB_INSTALLATION_ID`|Github Installation ID.|`0`|
5756
|`KUBECHECKS_GITHUB_PRIVATE_KEY`|Github App Private Key.||
5857
|`KUBECHECKS_IDENTIFIER`|Identifier for the kubechecks instance. Used to differentiate between multiple kubechecks instances.||
58+
|`KUBECHECKS_KUBEPUG_GENERATED_STORE`|URL for the kubepug generated store.|`https://kubepug.xyz/data/data.json`|
5959
|`KUBECHECKS_KUBERNETES_CLUSTERID`|Kubernetes Cluster ID, must be specified if kubernetes-type is eks.||
6060
|`KUBECHECKS_KUBERNETES_CONFIG`|Path to your kubernetes config file, used to monitor applications.||
6161
|`KUBECHECKS_KUBERNETES_TYPE`|Kubernetes Type One of eks, or local.|`local`|
6262
|`KUBECHECKS_LABEL_FILTER`|(Optional) If set, The label that must be set on an MR (as "kubechecks:<value>") for kubechecks to process the merge request webhook.||
6363
|`KUBECHECKS_LOG_LEVEL`|Set the log output level. One of error, warn, info, debug, trace.|`info`|
64-
|`KUBECHECKS_MAX_CONCURRENCT_CHECKS`|Number of concurrent checks to run.|`32`|
64+
|`KUBECHECKS_MAX_CONCURRENT_CHECKS`|Number of concurrent checks to run.|`32`|
6565
|`KUBECHECKS_MAX_QUEUE_SIZE`|Size of app diff check queue.|`1024`|
6666
|`KUBECHECKS_MONITOR_ALL_APPLICATIONS`|Monitor all applications in argocd automatically.|`true`|
6767
|`KUBECHECKS_OPENAI_API_TOKEN`|OpenAI API Token.||

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module github.com/zapier/kubechecks
22

3-
go 1.22.0
4-
5-
toolchain go1.22.7
3+
go 1.24
64

75
require (
86
github.com/argoproj/argo-cd/v2 v2.13.1

pkg/appdir/app_directory.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,21 @@ import (
88
"github.com/rs/zerolog/log"
99
)
1010

11+
// AppDirectory manages the mapping between applications and their associated directories and files.
12+
// It provides functionality to track which applications are affected by changes in specific directories or files,
13+
// and maintains a collection of Argo CD applications.
1114
type AppDirectory struct {
12-
appDirs map[string][]string // directory -> array of app names
13-
appFiles map[string][]string // file path -> array of app names
15+
// appDirs maps directory paths to the names of applications that use those directories.
16+
// This is used to quickly identify which applications are affected when changes occur in a directory.
17+
appDirs map[string][]string
1418

15-
appsMap map[string]v1alpha1.Application // app name -> app stub
19+
// appFiles maps file paths to the names of applications that use those files.
20+
// This is used to quickly identify which applications are affected when specific files change.
21+
appFiles map[string][]string
22+
23+
// appsMap stores the full Argo CD application definitions, indexed by application name.
24+
// This serves as the source of truth for application configurations.
25+
appsMap map[string]v1alpha1.Application
1626
}
1727

1828
func NewAppDirectory() *AppDirectory {

pkg/appdir/appset_directory.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,26 @@ import (
88

99
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
1010
"github.com/rs/zerolog/log"
11+
"github.com/zapier/kubechecks/pkg"
1112
"github.com/zapier/kubechecks/pkg/git"
1213
"sigs.k8s.io/yaml"
1314
)
1415

16+
// AppSetDirectory manages the mapping between ApplicationSets and their associated directories and files.
17+
// It provides functionality to track which ApplicationSets are affected by changes in specific directories or files,
18+
// and maintains a collection of Argo CD ApplicationSets.
1519
type AppSetDirectory struct {
16-
appSetDirs map[string][]string // directory -> array of app names
17-
appSetFiles map[string][]string // file path -> array of app names
20+
// appSetDirs maps directory paths to the names of ApplicationSets that use those directories.
21+
// This is used to quickly identify which ApplicationSets are affected when changes occur in a directory.
22+
appSetDirs map[string][]string
1823

19-
appSetsMap map[string]v1alpha1.ApplicationSet // app name -> app stub
24+
// appSetFiles maps file paths to the names of ApplicationSets that use those files.
25+
// This is used to quickly identify which ApplicationSets are affected when specific files change.
26+
appSetFiles map[string][]string
27+
28+
// appSetsMap stores the full Argo CD ApplicationSet definitions, indexed by ApplicationSet name.
29+
// This serves as the source of truth for ApplicationSet configurations.
30+
appSetsMap map[string]v1alpha1.ApplicationSet
2031
}
2132

2233
func NewAppSetDirectory() *AppSetDirectory {
@@ -179,11 +190,7 @@ func containsKindApplicationSet(path string) bool {
179190
log.Error().Err(err).Stack().Msgf("failed to open file %s: %v", path, err)
180191
return false
181192
}
182-
defer func() {
183-
if err := file.Close(); err != nil {
184-
log.Warn().Err(err).Stack().Msgf("failed to close file %s: %v", path, err)
185-
}
186-
}()
193+
defer pkg.WithErrorLogging(file.Close, "failed to close file")
187194

188195
scanner := bufio.NewScanner(file)
189196
for scanner.Scan() {

0 commit comments

Comments
 (0)