Skip to content

Commit 32c9832

Browse files
authored
Merge pull request #1537 from kubernetes-sigs/goconst
Enable and fix `goconst` linter
2 parents accffba + b6c89f9 commit 32c9832

19 files changed

+81
-65
lines changed

.golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ linters:
3232
- gocheckcompilerdirectives
3333
- gochecksumtype
3434
- gocognit
35+
- goconst
3536
- gocritic
3637
- gocyclo
3738
- godot
@@ -99,7 +100,6 @@ linters:
99100
# - funlen
100101
# - gochecknoglobals
101102
# - gochecknoinits
102-
# - goconst
103103
# - gosec
104104
# - ireturn
105105
# - lll

cmd/crictl/container.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ import (
3737
internalapi "k8s.io/cri-api/pkg/apis"
3838
pb "k8s.io/cri-api/pkg/apis/runtime/v1"
3939
"k8s.io/kubelet/pkg/types"
40+
41+
"sigs.k8s.io/cri-tools/pkg/framework"
4042
)
4143

4244
type containerByCreated []*pb.Container
@@ -580,7 +582,7 @@ var listContainersCommand = &cli.Command{
580582
Name: "output",
581583
Aliases: []string{"o"},
582584
Usage: "Output format, One of: json|yaml|table",
583-
Value: "table",
585+
Value: outputTypeTable,
584586
},
585587
&cli.BoolFlag{
586588
Name: "all",
@@ -889,7 +891,7 @@ func UpdateContainerResources(client internalapi.RuntimeService, id string, opts
889891
request := &pb.UpdateContainerResourcesRequest{
890892
ContainerId: id,
891893
}
892-
if goruntime.GOOS != "windows" {
894+
if goruntime.GOOS != framework.OSWindows {
893895
request.Linux = &pb.LinuxContainerResources{
894896
CpuPeriod: opts.CPUPeriod,
895897
CpuQuota: opts.CPUQuota,
@@ -1010,7 +1012,7 @@ func marshalContainerStatus(cs *pb.ContainerStatus) (string, error) {
10101012
func containerStatus(client internalapi.RuntimeService, ids []string, output, tmplStr string, quiet bool) error {
10111013
verbose := !(quiet)
10121014
if output == "" { // default to json output
1013-
output = "json"
1015+
output = outputTypeJSON
10141016
}
10151017
if len(ids) == 0 {
10161018
return errors.New("ID cannot be empty")
@@ -1036,7 +1038,7 @@ func containerStatus(client internalapi.RuntimeService, ids []string, output, tm
10361038
return fmt.Errorf("marshal container status: %w", err)
10371039
}
10381040

1039-
if output == "table" {
1041+
if output == outputTypeTable {
10401042
outputContainerStatusTable(r, verbose)
10411043
} else {
10421044
statuses = append(statuses, statusData{json: statusJSON, info: r.Info})
@@ -1138,11 +1140,11 @@ func ListContainers(runtimeClient internalapi.RuntimeService, imageClient intern
11381140
r = getContainersList(r, opts)
11391141

11401142
switch opts.output {
1141-
case "json":
1143+
case outputTypeJSON:
11421144
return outputProtobufObjAsJSON(&pb.ListContainersResponse{Containers: r})
1143-
case "yaml":
1145+
case outputTypeYAML:
11441146
return outputProtobufObjAsYAML(&pb.ListContainersResponse{Containers: r})
1145-
case "table":
1147+
case outputTypeTable:
11461148
// continue; output will be generated after the switch block ends.
11471149
default:
11481150
return fmt.Errorf("unsupported output format %q", opts.output)

cmd/crictl/container_stats.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ func (d containerStatsDisplayer) displayStats(ctx context.Context, client intern
164164
return err
165165
}
166166
switch d.opts.output {
167-
case "json":
167+
case outputTypeJSON:
168168
return outputProtobufObjAsJSON(r)
169-
case "yaml":
169+
case outputTypeYAML:
170170
return outputProtobufObjAsYAML(r)
171171
}
172172
oldStats := make(map[string]*pb.ContainerStats)

cmd/crictl/events.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var eventsCommand = &cli.Command{
3737
&cli.StringFlag{
3838
Name: "output",
3939
Aliases: []string{"o"},
40-
Value: "json",
40+
Value: outputTypeJSON,
4141
Usage: "Output format, One of: json|yaml|go-template",
4242
},
4343
&cli.StringFlag{
@@ -51,11 +51,11 @@ var eventsCommand = &cli.Command{
5151
}
5252

5353
switch format := c.String("output"); format {
54-
case "json", "yaml":
54+
case outputTypeJSON, outputTypeYAML:
5555
if c.String("template") != "" {
5656
return fmt.Errorf("template can't be used with %q format", format)
5757
}
58-
case "go-template":
58+
case outputTypeGoTemplate:
5959
if err := validateTemplate(c.String(("template"))); err != nil {
6060
return fmt.Errorf("failed to parse go-template: %w", err)
6161
}

cmd/crictl/image.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ var listImageCommand = &cli.Command{
210210
}
211211

212212
switch c.String("output") {
213-
case "json":
213+
case outputTypeJSON:
214214
return outputProtobufObjAsJSON(r)
215-
case "yaml":
215+
case outputTypeYAML:
216216
return outputProtobufObjAsYAML(r)
217217
}
218218

@@ -320,7 +320,7 @@ var imageStatusCommand = &cli.Command{
320320
verbose := !(c.Bool("quiet"))
321321
output := c.String("output")
322322
if output == "" { // default to json output
323-
output = "json"
323+
output = outputTypeJSON
324324
}
325325
tmplStr := c.String("template")
326326

@@ -342,7 +342,7 @@ var imageStatusCommand = &cli.Command{
342342
return fmt.Errorf("marshal status to JSON for %q: %w", id, err)
343343
}
344344

345-
if output == "table" {
345+
if output == outputTypeTable {
346346
outputImageStatusTable(r, verbose)
347347
} else {
348348
statuses = append(statuses, statusData{json: statusJSON, info: r.Info})
@@ -528,7 +528,7 @@ var imageFsInfoCommand = &cli.Command{
528528

529529
output := c.String("output")
530530
if output == "" { // default to json output
531-
output = "json"
531+
output = outputTypeJSON
532532
}
533533
tmplStr := c.String("template")
534534

@@ -541,7 +541,7 @@ var imageFsInfoCommand = &cli.Command{
541541
return fmt.Errorf("marshal filesystem info to json: %w", err)
542542
}
543543

544-
if output == "table" {
544+
if output == outputTypeTable {
545545
ouputImageFsInfoTable(r)
546546
} else {
547547
return outputStatusData([]statusData{{json: status}}, output, tmplStr)

cmd/crictl/info.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var runtimeStatusCommand = &cli.Command{
3636
&cli.StringFlag{
3737
Name: "output",
3838
Aliases: []string{"o"},
39-
Value: "json",
39+
Value: outputTypeJSON,
4040
Usage: "Output format, One of: json|yaml|go-template",
4141
},
4242
&cli.BoolFlag{

cmd/crictl/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"k8s.io/klog/v2"
3838

3939
"sigs.k8s.io/cri-tools/pkg/common"
40+
"sigs.k8s.io/cri-tools/pkg/framework"
4041
"sigs.k8s.io/cri-tools/pkg/tracing"
4142
"sigs.k8s.io/cri-tools/pkg/version"
4243
)
@@ -165,7 +166,7 @@ func getTimeout(timeDuration time.Duration) time.Duration {
165166
return timeDuration
166167
}
167168

168-
if runtime.GOOS == "windows" {
169+
if runtime.GOOS == framework.OSWindows {
169170
return defaultTimeoutWindows
170171
}
171172

cmd/crictl/pod_metrics.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ var podMetricsCommand = &cli.Command{
6666
}
6767

6868
switch opts.output {
69-
case "json", "yaml", "":
69+
case outputTypeJSON, outputTypeYAML, "":
7070
default:
7171
return cli.ShowSubcommandHelp(c)
7272
}
@@ -103,9 +103,9 @@ func (p *podMetricsDisplayer) displayPodMetrics(
103103

104104
response := &pb.ListPodSandboxMetricsResponse{PodMetrics: metrics}
105105
switch p.opts.output {
106-
case "json", "":
106+
case outputTypeJSON, "":
107107
return outputProtobufObjAsJSON(response)
108-
case "yaml":
108+
case outputTypeYAML:
109109
return outputProtobufObjAsYAML(response)
110110
}
111111
return nil

cmd/crictl/pod_stats.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ func (d *podStatsDisplayer) displayPodStats(
158158

159159
response := &pb.ListPodSandboxStatsResponse{Stats: stats}
160160
switch d.opts.output {
161-
case "json":
161+
case outputTypeJSON:
162162
return outputProtobufObjAsJSON(response)
163-
case "yaml":
163+
case outputTypeYAML:
164164
return outputProtobufObjAsYAML(response)
165165
}
166166

cmd/crictl/sandbox.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ var listPodCommand = &cli.Command{
287287
Name: "output",
288288
Aliases: []string{"o"},
289289
Usage: "Output format, One of: json|yaml|table",
290-
Value: "table",
290+
Value: outputTypeTable,
291291
},
292292
&cli.BoolFlag{
293293
Name: "latest",
@@ -406,7 +406,7 @@ func marshalPodSandboxStatus(ps *pb.PodSandboxStatus) (string, error) {
406406
func podSandboxStatus(client internalapi.RuntimeService, ids []string, output string, quiet bool, tmplStr string) error {
407407
verbose := !(quiet)
408408
if output == "" { // default to json output
409-
output = "json"
409+
output = outputTypeJSON
410410
}
411411
if len(ids) == 0 {
412412
return errors.New("ID cannot be empty")
@@ -433,7 +433,7 @@ func podSandboxStatus(client internalapi.RuntimeService, ids []string, output st
433433
return fmt.Errorf("marshal pod sandbox status: %w", err)
434434
}
435435

436-
if output == "table" {
436+
if output == outputTypeTable {
437437
outputPodSandboxStatusTable(r, verbose)
438438
} else {
439439
statuses = append(statuses, statusData{json: statusJSON, info: r.Info})
@@ -523,11 +523,11 @@ func ListPodSandboxes(client internalapi.RuntimeService, opts *listOptions) erro
523523
r = getSandboxesList(r, opts)
524524

525525
switch opts.output {
526-
case "json":
526+
case outputTypeJSON:
527527
return outputProtobufObjAsJSON(&pb.ListPodSandboxResponse{Items: r})
528-
case "yaml":
528+
case outputTypeYAML:
529529
return outputProtobufObjAsYAML(&pb.ListPodSandboxResponse{Items: r})
530-
case "table":
530+
case outputTypeTable:
531531
// continue; output will be generated after the switch block ends.
532532
default:
533533
return fmt.Errorf("unsupported output format %q", opts.output)

cmd/crictl/templates.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ func builtinTmplFuncs() template.FuncMap {
3232
l := cases.Lower(language.Und)
3333
u := cases.Upper(language.Und)
3434
return template.FuncMap{
35-
"json": jsonBuiltinTmplFunc,
36-
"title": t.String,
37-
"lower": l.String,
38-
"upper": u.String,
35+
outputTypeJSON: jsonBuiltinTmplFunc,
36+
"title": t.String,
37+
"lower": l.String,
38+
"upper": u.String,
3939
}
4040
}
4141

cmd/crictl/util.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ import (
4444
const (
4545
// truncatedImageIDLen is the truncated length of imageID.
4646
truncatedIDLen = 13
47+
48+
outputTypeJSON = "json"
49+
outputTypeYAML = "yaml"
50+
outputTypeTable = "table"
51+
outputTypeGoTemplate = "go-template"
4752
)
4853

4954
var (
@@ -348,19 +353,19 @@ func outputStatusData(statuses []statusData, format, tmplStr string) (err error)
348353
}
349354

350355
switch format {
351-
case "yaml":
356+
case outputTypeYAML:
352357
yamlInfo, err := yaml.JSONToYAML(jsonResult)
353358
if err != nil {
354359
return fmt.Errorf("JSON result to YAML: %w", err)
355360
}
356361
fmt.Println(string(yamlInfo))
357-
case "json":
362+
case outputTypeJSON:
358363
var output bytes.Buffer
359364
if err := json.Indent(&output, jsonResult, "", " "); err != nil {
360365
return fmt.Errorf("indent JSON result: %w", err)
361366
}
362367
fmt.Println(output.String())
363-
case "go-template":
368+
case outputTypeGoTemplate:
364369
output, err := tmplExecuteRawJSON(tmplStr, string(jsonResult))
365370
if err != nil {
366371
return fmt.Errorf("execute template: %w", err)
@@ -375,17 +380,17 @@ func outputStatusData(statuses []statusData, format, tmplStr string) (err error)
375380

376381
func outputEvent(event protoiface.MessageV1, format, tmplStr string) error {
377382
switch format {
378-
case "yaml":
383+
case outputTypeYAML:
379384
err := outputProtobufObjAsYAML(event)
380385
if err != nil {
381386
return err
382387
}
383-
case "json":
388+
case outputTypeJSON:
384389
err := outputProtobufObjAsJSON(event)
385390
if err != nil {
386391
return err
387392
}
388-
case "go-template":
393+
case outputTypeGoTemplate:
389394
jsonEvent, err := protobufObjectToJSON(event)
390395
if err != nil {
391396
return err
@@ -440,7 +445,7 @@ func marshalMapInOrder(m map[string]interface{}, t interface{}) (string, error)
440445

441446
// jsonFieldFromTag gets json field name from field tag.
442447
func jsonFieldFromTag(tag reflect.StructTag) string {
443-
field := strings.Split(tag.Get("json"), ",")[0]
448+
field := strings.Split(tag.Get(outputTypeJSON), ",")[0]
444449
for _, f := range strings.Split(tag.Get("protobuf"), ",") {
445450
if !strings.HasPrefix(f, "json=") {
446451
continue

cmd/crictl/util_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -117,39 +117,39 @@ func TestOutputStatusData(t *testing.T) {
117117
status: statusResponse,
118118
handlers: handlerResponse,
119119
info: map[string]string{"key1": `{"foo": "bar"}`, "key2": `{"bar": "baz"}`},
120-
format: "yaml",
120+
format: outputTypeYAML,
121121
tmplStr: "",
122122
expectedOut: "key1:\n foo: bar\nkey2:\n bar: baz\nruntimeHandlers:\n- features:\n recursive_read_only_mounts: true\n name: runc\n- features:\n recursive_read_only_mounts: true\n user_namespaces: true\n name: crun\nstatus:\n conditions:\n - message: no network config found in C:\\Program Files\n reason: NetworkPluginNotReady\n status: false\n type: NetworkReady",
123123
},
124124
{
125125
name: "YAML format with empty status response",
126126
status: emptyResponse,
127127
handlers: handlerResponse,
128-
format: "yaml",
128+
format: outputTypeYAML,
129129
tmplStr: "",
130130
expectedOut: "runtimeHandlers:\n- features:\n recursive_read_only_mounts: true\n name: runc\n- features:\n recursive_read_only_mounts: true\n user_namespaces: true\n name: crun",
131131
},
132132
{
133133
name: "YAML format with empty handlers response",
134134
status: statusResponse,
135135
handlers: emptyResponse,
136-
format: "yaml",
136+
format: outputTypeYAML,
137137
tmplStr: "",
138138
expectedOut: "status:\n conditions:\n - message: no network config found in C:\\Program Files\n reason: NetworkPluginNotReady\n status: false\n type: NetworkReady",
139139
},
140140
{
141141
name: "JSON format",
142142
status: statusResponse,
143143
handlers: handlerResponse,
144-
format: "json",
144+
format: outputTypeJSON,
145145
tmplStr: "",
146146
expectedOut: "{\n \"runtimeHandlers\": [\n {\n \"features\": {\n \"recursive_read_only_mounts\": true\n },\n \"name\": \"runc\"\n },\n {\n \"features\": {\n \"recursive_read_only_mounts\": true,\n \"user_namespaces\": true\n },\n \"name\": \"crun\"\n }\n ],\n \"status\": {\n \"conditions\": [\n {\n \"message\": \"no network config found in C:\\\\Program Files\",\n \"reason\": \"NetworkPluginNotReady\",\n \"status\": false,\n \"type\": \"NetworkReady\"\n }\n ]\n }\n}",
147147
},
148148
{
149149
name: "Go template format",
150150
status: statusResponse,
151151
handlers: handlerResponse,
152-
format: "go-template",
152+
format: outputTypeGoTemplate,
153153
tmplStr: `NetworkReady: {{ (index .status.conditions 0).status }}`,
154154
expectedOut: "NetworkReady: false",
155155
},

0 commit comments

Comments
 (0)