Skip to content

Commit 2f5dca7

Browse files
authored
Merge branch 'master' into patch-1
2 parents 570fb5c + 187db91 commit 2f5dca7

35 files changed

+375
-58
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ _ := $(foreach exec,$(REQ_BINS), \
5757

5858

5959
## Clean, build and pack
60-
all: build release-artifacts
60+
all: clean build release-artifacts
6161
.PHONY: all
6262

6363
## Prints list of tasks

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ Usage of ./kubent:
127127
- *`-x, --context`*
128128
Select context from kubeconfig file (`current-context` from the file is used by default).
129129

130-
- *`k, --kubeconfig`*
130+
- *`-k, --kubeconfig`*
131131
Path to kubeconfig file to use. This takes precedence over `KUBECONFIG` environment variable, which is also supported
132-
and can contain multiple paths, and default `~.kube/config`.
132+
and can contain multiple paths, and default `~/.kube/config`.
133133

134134
- *`-t, --target-version`*
135-
`Kubent` will try to detect K8S cluster version and display only relevant findings. This flag allows to override this
135+
`kubent` will try to detect K8S cluster version and display only relevant findings. This flag allows to override this
136136
version for scenarios like use in CI with the file collector only, when detection from an actual cluster is not possible.
137137
Expected format is `major.minor[.patch]`, e.g. `1.16` or `1.16.3`.
138138

@@ -223,7 +223,7 @@ Otherwise there's `Makefile`
223223
```sh
224224
$ make
225225
make
226-
all Cean, build and pack
226+
all Clean, build and pack
227227
help Prints list of tasks
228228
build Build binary
229229
generate Go generate

cmd/kubent/main.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"flag"
56
"fmt"
67
"io"
@@ -96,11 +97,13 @@ func getServerVersion(cv *judge.Version, collectors []collector.Collector) (*jud
9697
}
9798

9899
func main() {
100+
ctx := context.Background()
99101
exitCode := EXIT_CODE_FAIL_GENERIC
100102

101103
configureGlobalLogging()
102104

103-
config, err := config.NewFromFlags()
105+
config, ctx, err := config.NewFromFlags(ctx)
106+
104107
if err != nil {
105108
log.Fatal().Err(err).Msg("failed to parse config flags")
106109
}
@@ -156,7 +159,7 @@ func main() {
156159
log.Fatal().Err(err).Str("name", "Rego").Msg("Failed to filter results")
157160
}
158161

159-
err = outputResults(results, config.Output, config.OutputFile)
162+
err = outputResults(results, config.Output, config.OutputFile, ctx)
160163
if err != nil {
161164
log.Fatal().Err(err).Msgf("Failed to output results")
162165
}
@@ -180,14 +183,14 @@ func configureGlobalLogging() {
180183
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
181184
}
182185

183-
func outputResults(results []judge.Result, outputType string, outputFile string) error {
186+
func outputResults(results []judge.Result, outputType string, outputFile string, ctx context.Context) error {
184187
printer, err := printer.NewPrinter(outputType, outputFile)
185188
if err != nil {
186189
return fmt.Errorf("failed to create printer: %v", err)
187190
}
188191
defer printer.Close()
189192

190-
err = printer.Print(results)
193+
err = printer.Print(results, ctx)
191194
if err != nil {
192195
return fmt.Errorf("failed to print results: %v", err)
193196
}

cmd/kubent/main_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/base64"
67
"encoding/json"
78
"errors"
@@ -13,6 +14,7 @@ import (
1314

1415
"github.com/doitintl/kube-no-trouble/pkg/collector"
1516
"github.com/doitintl/kube-no-trouble/pkg/config"
17+
ctxKey "github.com/doitintl/kube-no-trouble/pkg/context"
1618
"github.com/doitintl/kube-no-trouble/pkg/judge"
1719

1820
"github.com/rs/zerolog"
@@ -108,6 +110,7 @@ func TestMainExitCodes(t *testing.T) {
108110
defer os.RemoveAll(tmpDir)
109111

110112
expectedJsonOutput, _ := os.ReadFile(filepath.Join(FIXTURES_DIR, "expected-json-output.json"))
113+
expectedJsonOutputLabels, _ := os.ReadFile(filepath.Join(FIXTURES_DIR, "expected-json-output-labels.json"))
111114
helm3FlagDisabled := "--helm3=false"
112115
clusterFlagDisabled := "--cluster=false"
113116
testCases := []struct {
@@ -121,6 +124,7 @@ func TestMainExitCodes(t *testing.T) {
121124
{"success", []string{clusterFlagDisabled, helm3FlagDisabled}, 0, "", "", false},
122125
{"errorBadFlag", []string{"-c=not-boolean"}, 2, "", "", false},
123126
{"successFound", []string{"-o=json", clusterFlagDisabled, helm3FlagDisabled, "-f=" + filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")}, 0, string(expectedJsonOutput), "", false},
127+
{"successFoundWithLabels", []string{"--labels=true", "-o=json", clusterFlagDisabled, helm3FlagDisabled, "-f=" + filepath.Join(FIXTURES_DIR, "deployment-v1beta1-labels.yaml")}, 0, string(expectedJsonOutputLabels), "", false},
124128
{"exitErrorFlagNone", []string{clusterFlagDisabled, helm3FlagDisabled, "-e"}, 0, "", "", false},
125129
{"exitErrorFlagFound", []string{clusterFlagDisabled, helm3FlagDisabled, "-e", "-f=" + filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")}, 200, "", "", false},
126130
{"version short flag set", []string{"-v"}, 0, "", "", false},
@@ -131,6 +135,7 @@ func TestMainExitCodes(t *testing.T) {
131135
{"json-file", []string{"-o=json", clusterFlagDisabled, helm3FlagDisabled, "-f=" + filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")}, 0, "", filepath.Join(tmpDir, "json-file.out"), false},
132136
{"text-file", []string{"-o=text", clusterFlagDisabled, helm3FlagDisabled, "-f=" + filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")}, 0, "", filepath.Join(tmpDir, "text-file.out"), false},
133137
{"json-stdout", []string{"-o=json", clusterFlagDisabled, helm3FlagDisabled, "-f=" + filepath.Join(FIXTURES_DIR, "deployment-v1beta1.yaml")}, 0, string(expectedJsonOutput), "-", false},
138+
{"json-stdout-with-labels", []string{"--labels=true", "-o=json", clusterFlagDisabled, helm3FlagDisabled, "-f=" + filepath.Join(FIXTURES_DIR, "deployment-v1beta1-labels.yaml")}, 0, string(expectedJsonOutputLabels), "-", false},
134139
{"error-bad-file", []string{clusterFlagDisabled, helm3FlagDisabled}, 1, "", "/this/dir/is/unlikely/to/exist", false},
135140
{"no-3rdparty-output", []string{clusterFlagDisabled, helm3FlagDisabled, "-l=disabled"}, 0, "", "", true},
136141
}
@@ -286,9 +291,12 @@ func Test_outputResults(t *testing.T) {
286291
{"bad-new-printer-file", args{testResults, "text", "/unlikely/to/exist/dir"}, true},
287292
}
288293

294+
labelsFlag := false
295+
ctx := context.WithValue(context.Background(), ctxKey.LABELS_CTX_KEY, &labelsFlag)
296+
289297
for _, tt := range tests {
290298
t.Run(tt.name, func(t *testing.T) {
291-
if err := outputResults(tt.args.results, tt.args.outputType, tt.args.outputFile); (err != nil) != tt.wantErr {
299+
if err := outputResults(tt.args.results, tt.args.outputType, tt.args.outputFile, ctx); (err != nil) != tt.wantErr {
292300
t.Errorf("unexpected error - got: %v, wantErr: %v", err, tt.wantErr)
293301
}
294302
})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: apps/v1beta1
2+
kind: Deployment
3+
metadata:
4+
name: nginx-deployment-old
5+
labels:
6+
app: nginx
7+
spec:
8+
replicas: 3
9+
selector:
10+
matchLabels:
11+
app: nginx
12+
template:
13+
metadata:
14+
labels:
15+
app: nginx
16+
spec:
17+
containers:
18+
- name: nginx
19+
image: nginx:1.14.2
20+
ports:
21+
- containerPort: 80

fixtures/deployment-v1beta1.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ apiVersion: apps/v1beta1
22
kind: Deployment
33
metadata:
44
name: nginx-deployment-old
5-
labels:
6-
app: nginx
75
spec:
86
replicas: 3
97
selector:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"Name": "nginx-deployment-old",
4+
"Namespace": "\u003cundefined\u003e",
5+
"Kind": "Deployment",
6+
"ApiVersion": "apps/v1beta1",
7+
"RuleSet": "Deprecated APIs removed in 1.16",
8+
"ReplaceWith": "apps/v1",
9+
"Since": "1.9.0",
10+
"Labels": {
11+
"app": "nginx"
12+
}
13+
}
14+
]

fixtures/expected-json-output.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"ApiVersion": "apps/v1beta1",
77
"RuleSet": "Deprecated APIs removed in 1.16",
88
"ReplaceWith": "apps/v1",
9-
"Since": "1.9.0"
9+
"Since": "1.9.0",
10+
"Labels": {}
1011
}
1112
]

pkg/collector/file_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestFileCollectorGet(t *testing.T) {
5252
t.Errorf("Expected to get %d, got %d", len(tc.expected), len(manifests))
5353
}
5454

55-
for i, _ := range manifests {
55+
for i := range manifests {
5656
if manifests[i]["kind"] != tc.expected[i] {
5757
t.Errorf("Expected to get %s, instead got: %s", tc.expected[i], manifests[i]["kind"])
5858
}

pkg/config/config.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"io/fs"
@@ -9,14 +10,20 @@ import (
910
"strings"
1011
"unicode"
1112

13+
ctxKey "github.com/doitintl/kube-no-trouble/pkg/context"
1214
"github.com/doitintl/kube-no-trouble/pkg/judge"
13-
"github.com/doitintl/kube-no-trouble/pkg/printer"
1415
"k8s.io/client-go/tools/clientcmd"
1516

1617
"github.com/rs/zerolog"
1718
flag "github.com/spf13/pflag"
1819
)
1920

21+
const (
22+
JSON = "json"
23+
TEXT = "text"
24+
CSV = "csv"
25+
)
26+
2027
type Config struct {
2128
AdditionalKinds []string
2229
AdditionalAnnotations []string
@@ -33,12 +40,14 @@ type Config struct {
3340
KubentVersion bool
3441
}
3542

36-
func NewFromFlags() (*Config, error) {
43+
func NewFromFlags(ctx context.Context) (*Config, context.Context, error) {
3744
config := Config{
3845
LogLevel: ZeroLogLevel(zerolog.InfoLevel),
3946
TargetVersion: &judge.Version{},
4047
}
4148

49+
var labels bool
50+
4251
flag.StringSliceVarP(&config.AdditionalKinds, "additional-kind", "a", []string{}, "additional kinds of resources to report in Kind.version.group.com format")
4352
flag.StringSliceVarP(&config.AdditionalAnnotations, "additional-annotation", "A", []string{}, "additional annotations that should be checked to determine the last applied config")
4453
flag.BoolVarP(&config.Cluster, "cluster", "c", true, "enable Cluster collector")
@@ -52,19 +61,22 @@ func NewFromFlags() (*Config, error) {
5261
flag.StringVarP(&config.OutputFile, "output-file", "O", "-", "output file, use - for stdout")
5362
flag.VarP(&config.LogLevel, "log-level", "l", "set log level (trace, debug, info, warn, error, fatal, panic, disabled)")
5463
flag.VarP(config.TargetVersion, "target-version", "t", "target K8s version in SemVer format (autodetected by default)")
64+
flag.BoolVar(&labels, "labels", false, "print resource labels")
5565

5666
flag.Parse()
5767

58-
if _, err := printer.ParsePrinter(config.Output); err != nil {
59-
return nil, fmt.Errorf("failed to validate argument output: %w", err)
68+
newContext := context.WithValue(ctx, ctxKey.LABELS_CTX_KEY, &labels)
69+
70+
if !isValidOutputFormat(config.Output) {
71+
return nil, nil, fmt.Errorf("failed to validate argument output: %s", config.Output)
6072
}
6173

6274
if err := validateOutputFile(config.OutputFile); err != nil {
63-
return nil, fmt.Errorf("failed to validate argument output-file: %w", err)
75+
return nil, nil, fmt.Errorf("failed to validate argument output-file: %w", err)
6476
}
6577

6678
if err := validateAdditionalResources(config.AdditionalKinds); err != nil {
67-
return nil, fmt.Errorf("failed to validate arguments: %w", err)
79+
return nil, nil, fmt.Errorf("failed to validate arguments: %w", err)
6880
}
6981

7082
// This is a little ugly, but I think preferred to implementing
@@ -74,7 +86,18 @@ func NewFromFlags() (*Config, error) {
7486
config.TargetVersion = nil
7587
}
7688

77-
return &config, nil
89+
return &config, newContext, nil
90+
}
91+
92+
// Previuosly this was handled by a printer.go ParsePrinter function
93+
// but we need to avoid cycle imports in order to inject the additional flags
94+
func isValidOutputFormat(format string) bool {
95+
switch format {
96+
case JSON, TEXT, CSV:
97+
return true
98+
default:
99+
return false
100+
}
78101
}
79102

80103
// validateAdditionalResources check that all resources are provided in full form

0 commit comments

Comments
 (0)