Skip to content

Commit 95a3369

Browse files
authored
Add indent option (#39)
Signed-off-by: Anders Eknert <[email protected]>
1 parent 1db2939 commit 95a3369

File tree

6 files changed

+83
-29
lines changed

6 files changed

+83
-29
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## [Unreleased]
7+
## [0.4.0] - 2025-01-21
8+
9+
### Added
10+
11+
* `--indent` option
12+
13+
### Changed
14+
15+
* Bump all dependencies to the latest versions (Kubernetes v0.32.1)
816

917
## [0.3.0] - 2022-06-07
1018
### Changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ $ kubectl get service gatekeeper-webhook-service -o yaml | kube-review create --
166166
| `--action` | string | create | Type of operation to apply in admission review (create, update, delete, connect) |
167167
| `--as` | string | kube-review | Name of user or service account for userInfo attributes |
168168
| `--as-group` | string | none | Name of group this user or service account belongs to. May be repeated for multiple groups |
169+
| `--indent` | int | 2 | Number of spaces to indent JSON output |
169170

170171
The `action` provided has the following effects on the produced `AdmissionReview` object:
171172

cmd/main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type parameters struct {
1414
action string
1515
as string
1616
groups []string
17+
indent uint8
1718
}
1819

1920
//nolint:gochecknoglobals
@@ -59,7 +60,7 @@ webhooks`,
5960
}
6061
}
6162

62-
req, err := admission.CreateAdmissionReviewRequest(input, params.action, params.as, params.groups)
63+
req, err := admission.CreateAdmissionReviewRequest(input, params.action, params.as, params.groups, params.indent)
6364
if err != nil {
6465
log.Fatal(err)
6566
}
@@ -97,6 +98,12 @@ func Execute() {
9798
[]string{},
9899
"Group(s) of user (may be repeated) (default: empty)",
99100
)
101+
rootCmd.PersistentFlags().Uint8Var(
102+
&params.indent,
103+
"indent",
104+
2,
105+
"Number of spaces to indent JSON output (default: 2)",
106+
)
100107
rootCmd.AddCommand(createCmd)
101108
rootCmd.AddCommand(versionCmd)
102109

pkg/admission/review.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"strings"
78

89
admissionv1 "k8s.io/api/admission/v1"
910
v1 "k8s.io/api/authentication/v1"
@@ -16,7 +17,13 @@ import (
1617
"k8s.io/client-go/kubernetes/scheme"
1718
)
1819

19-
func CreateAdmissionReviewRequest(input []byte, action string, username string, groups []string) ([]byte, error) {
20+
func CreateAdmissionReviewRequest(
21+
input []byte,
22+
action string,
23+
username string,
24+
groups []string,
25+
indent uint8,
26+
) ([]byte, error) {
2027
operation, err := actionToOperation(action)
2128
if err != nil {
2229
return nil, err
@@ -27,7 +34,7 @@ func CreateAdmissionReviewRequest(input []byte, action string, username string,
2734
object, kind, err := decode(input, nil, nil)
2835
if err != nil {
2936
// Failure to decode, likely due to unrecognized type, try unstructured
30-
return fromUnstructured(input, operation, username, groups)
37+
return fromUnstructured(input, operation, username, groups, indent)
3138
}
3239

3340
unstructured, err := runtime.DefaultUnstructuredConverter.ToUnstructured(object)
@@ -42,6 +49,7 @@ func CreateAdmissionReviewRequest(input []byte, action string, username string,
4249
getUserInfo(username, groups),
4350
getNewObject(object, *operation),
4451
getOldObject(object, *operation),
52+
indent,
4553
)
4654
}
4755

@@ -50,6 +58,7 @@ func fromUnstructured(
5058
operation *admissionv1.Operation,
5159
username string,
5260
groups []string,
61+
indent uint8,
5362
) ([]byte, error) {
5463
var object any
5564

@@ -87,7 +96,7 @@ func fromUnstructured(
8796
newObject := getUnknownRaw(&unknown, *operation)
8897
oldObject := getOldUnknownRaw(&unknown, *operation)
8998

90-
return createAdmissionRequest(unstructured, *kind, operation, userInfo, newObject, oldObject)
99+
return createAdmissionRequest(unstructured, *kind, operation, userInfo, newObject, oldObject, indent)
91100
}
92101

93102
func createAdmissionRequest(
@@ -96,6 +105,7 @@ func createAdmissionRequest(
96105
operation *admissionv1.Operation,
97106
user v1.UserInfo,
98107
object, oldObject runtime.RawExtension,
108+
indent uint8,
99109
) ([]byte, error) {
100110
dryRun := true
101111

@@ -130,9 +140,20 @@ func createAdmissionRequest(
130140
Request: admissionRequest,
131141
}
132142

133-
requestJSON, err := json.MarshalIndent(&admissionReview, "", " ")
134-
if err != nil {
135-
return nil, fmt.Errorf("failed encoding object to JSON %w", err)
143+
var requestJSON []byte
144+
145+
var err error
146+
147+
if indent == 0 {
148+
requestJSON, err = json.Marshal(&admissionReview)
149+
if err != nil {
150+
return nil, fmt.Errorf("failed encoding object to JSON %w", err)
151+
}
152+
} else {
153+
requestJSON, err = json.MarshalIndent(&admissionReview, "", strings.Repeat(" ", int(indent)))
154+
if err != nil {
155+
return nil, fmt.Errorf("failed encoding object to JSON %w", err)
156+
}
136157
}
137158

138159
return requestJSON, nil

pkg/admission/review_test.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package admission
22

33
import (
44
"encoding/json"
5+
"os"
56
"testing"
67

78
v1 "k8s.io/api/admission/v1"
@@ -10,29 +11,14 @@ import (
1011
func TestBasicReview(t *testing.T) {
1112
t.Parallel()
1213

13-
manifest := `apiVersion: apps/v1
14-
kind: Deployment
15-
metadata:
16-
name: nginx
17-
labels:
18-
app: nginx
19-
spec:
20-
selector:
21-
matchLabels:
22-
app: nginx
23-
template:
24-
metadata:
25-
labels:
26-
app: nginx
27-
spec:
28-
containers:
29-
- image: nginx
30-
name: nginx
31-
ports:
32-
- containerPort: 8080`
14+
manifest := mustReadFileString(t, "testdata/in.yaml")
3315

3416
reviewBytes, err := CreateAdmissionReviewRequest(
35-
[]byte(manifest), "create", "kube-review", []string{"system:masters"},
17+
[]byte(manifest),
18+
"create",
19+
"kube-review",
20+
[]string{"system:masters"},
21+
2,
3622
)
3723
if err != nil {
3824
t.Fatal(err)
@@ -45,3 +31,14 @@ spec:
4531
t.Fatal(err)
4632
}
4733
}
34+
35+
func mustReadFileString(t *testing.T, path string) string {
36+
t.Helper()
37+
38+
data, err := os.ReadFile(path)
39+
if err != nil {
40+
t.Fatal(err)
41+
}
42+
43+
return string(data)
44+
}

pkg/admission/testdata/in.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: nginx
5+
labels:
6+
app: nginx
7+
spec:
8+
selector:
9+
matchLabels:
10+
app: nginx
11+
template:
12+
metadata:
13+
labels:
14+
app: nginx
15+
spec:
16+
containers:
17+
- image: nginx
18+
name: nginx
19+
ports:
20+
- containerPort: 8080

0 commit comments

Comments
 (0)