Skip to content

Commit 7c1edfb

Browse files
committed
Remove unnecessary pointers
Signed-off-by: Dale Haiducek <[email protected]>
1 parent f58da0e commit 7c1edfb

File tree

6 files changed

+55
-55
lines changed

6 files changed

+55
-55
lines changed

internal/patches.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (m *manifestPatcher) Validate() error {
9090

9191
// Apply defaults on the patches
9292
for i := range m.patches {
93-
err := setPatchDefaults(apiVersion, kind, name, namespace, &m.patches[i])
93+
err := setPatchDefaults(apiVersion, kind, name, namespace, m.patches[i])
9494
if err != nil {
9595
return err
9696
}
@@ -102,56 +102,56 @@ func (m *manifestPatcher) Validate() error {
102102
// setPatchDefaults is a helper function for Validate that sets any missing values on the patches
103103
// that can be derived. An error is returned if a patch is in an invalid format.
104104
func setPatchDefaults(
105-
apiVersion, kind, name, namespace string, patch *map[string]interface{},
105+
apiVersion, kind, name, namespace string, patch map[string]interface{},
106106
) error {
107107
errTemplate := `failed to retrieve the "%s" field from the manifest of name "` + name +
108108
`"` + ` and kind "` + kind + `": %v`
109109
setErrTemplate := `failed to set the "%s" field on the patch from the manifest of name "` +
110110
name + `"` + ` and kind "` + kind + `": %v`
111111

112-
patchAPIVersion, _, err := unstructured.NestedString(*patch, "apiVersion")
112+
patchAPIVersion, _, err := unstructured.NestedString(patch, "apiVersion")
113113
if err != nil {
114114
return fmt.Errorf(errTemplate, "apiVersion", err)
115115
}
116116

117117
if patchAPIVersion == "" {
118-
err = unstructured.SetNestedField(*patch, apiVersion, "apiVersion")
118+
err = unstructured.SetNestedField(patch, apiVersion, "apiVersion")
119119
if err != nil {
120120
return fmt.Errorf(setErrTemplate, "apiVersion", err)
121121
}
122122
}
123123

124-
patchKind, _, err := unstructured.NestedString(*patch, "kind")
124+
patchKind, _, err := unstructured.NestedString(patch, "kind")
125125
if err != nil {
126126
return fmt.Errorf(errTemplate, "kind", err)
127127
}
128128

129129
if patchKind == "" {
130-
err = unstructured.SetNestedField(*patch, kind, "kind")
130+
err = unstructured.SetNestedField(patch, kind, "kind")
131131
if err != nil {
132132
return fmt.Errorf(setErrTemplate, "kind", err)
133133
}
134134
}
135135

136-
patchName, _, err := unstructured.NestedString(*patch, "metadata", "name")
136+
patchName, _, err := unstructured.NestedString(patch, "metadata", "name")
137137
if err != nil {
138138
return fmt.Errorf(errTemplate, "metadata.name", err)
139139
}
140140

141141
if patchName == "" {
142-
err = unstructured.SetNestedField(*patch, name, "metadata", "name")
142+
err = unstructured.SetNestedField(patch, name, "metadata", "name")
143143
if err != nil {
144144
return fmt.Errorf(setErrTemplate, "metadata.name", err)
145145
}
146146
}
147147

148-
patchNamespace, _, err := unstructured.NestedString(*patch, "metadata", "namespace")
148+
patchNamespace, _, err := unstructured.NestedString(patch, "metadata", "namespace")
149149
if err != nil {
150150
return fmt.Errorf(errTemplate, "metadata.namespace", err)
151151
}
152152

153153
if patchNamespace == "" {
154-
err = unstructured.SetNestedField(*patch, namespace, "metadata", "namespace")
154+
err = unstructured.SetNestedField(patch, namespace, "metadata", "namespace")
155155
if err != nil {
156156
return fmt.Errorf(setErrTemplate, "metadata.namespace", err)
157157
}
@@ -163,7 +163,7 @@ func setPatchDefaults(
163163
// ApplyPatches applies the Kustomize patches on the input manifests using Kustomize and returns
164164
// the patched manifests. An error is returned if the patches can't be applied. This should be
165165
// run after the Validate method.
166-
func (m *manifestPatcher) ApplyPatches() (*[]map[string]interface{}, error) {
166+
func (m *manifestPatcher) ApplyPatches() ([]map[string]interface{}, error) {
167167
kustomizeDir := "kustomize"
168168

169169
// Create the file system in memory with the Kustomize YAML files

internal/patches_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,12 @@ func TestApplyPatches(t *testing.T) {
244244

245245
assertEqual(t, err, nil)
246246

247-
patchedManifest1 := (*patchedManifests)[0]
247+
patchedManifest1 := patchedManifests[0]
248248
_, found, _ := unstructured.NestedStringMap(patchedManifest1, "metadata", "labels")
249249

250250
assertEqual(t, found, false)
251251

252-
patchedManifest2 := (*patchedManifests)[1]
252+
patchedManifest2 := patchedManifests[1]
253253
labels, found, _ := unstructured.NestedStringMap(patchedManifest2, "metadata", "labels")
254254

255255
assertEqual(t, found, true)

internal/plugin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,7 @@ func (p *Plugin) getPlcFromPath(plcPath string) (string, map[string]interface{},
12171217
var name string
12181218
var placement map[string]interface{}
12191219

1220-
for _, manifest := range *manifests {
1220+
for _, manifest := range manifests {
12211221
kind, _, _ := unstructured.NestedString(manifest, "kind")
12221222
if kind != placementRuleKind && kind != placementKind {
12231223
continue

internal/plugin_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2384,9 +2384,9 @@ func TestGenerateEvaluationInterval(t *testing.T) {
23842384
t.Fatal(err.Error())
23852385
}
23862386

2387-
assertEqual(t, len(*generatedManifests), 9)
2387+
assertEqual(t, len(generatedManifests), 9)
23882388

2389-
for _, manifest := range *generatedManifests {
2389+
for _, manifest := range generatedManifests {
23902390
kind, _ := manifest["kind"].(string)
23912391
if kind != "Policy" {
23922392
continue
@@ -2478,7 +2478,7 @@ func TestCreatePolicyWithConfigPolicyAnnotations(t *testing.T) {
24782478
t.Fatal(err.Error())
24792479
}
24802480
// nolint: forcetypeassert
2481-
spec := (*policyManifests)[0]["spec"].(map[string]interface{})
2481+
spec := policyManifests[0]["spec"].(map[string]interface{})
24822482
policyTemplates := spec["policy-templates"].([]interface{})
24832483
// nolint: forcetypeassert
24842484
configPolicy := policyTemplates[0].(map[string]interface{})["objectDefinition"].(map[string]interface{})
@@ -2586,7 +2586,7 @@ func TestCreatePolicyWithNamespaceSelector(t *testing.T) {
25862586
t.Fatal(err.Error())
25872587
}
25882588
// nolint: forcetypeassert
2589-
spec := (*policyManifests)[0]["spec"].(map[string]interface{})
2589+
spec := policyManifests[0]["spec"].(map[string]interface{})
25902590
policyTemplates := spec["policy-templates"].([]interface{})
25912591
// nolint: forcetypeassert
25922592
configPolicy := policyTemplates[0].(map[string]interface{})["objectDefinition"].(map[string]interface{})

internal/utils.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ func getManifests(policyConf *types.PolicyConfig) ([][]map[string]interface{}, e
7777
return nil, err
7878
}
7979

80-
if len(*manifestFile) == 0 {
80+
if len(manifestFile) == 0 {
8181
continue
8282
}
8383
// Allowing replace the original manifest metadata.name and/or metadata.namespace if it is a single
8484
// yaml structure in the manifest path
85-
if len(*manifestFile) == 1 && len(manifest.Patches) == 1 {
85+
if len(manifestFile) == 1 && len(manifest.Patches) == 1 {
8686
if patchMetadata, ok := manifest.Patches[0]["metadata"].(map[string]interface{}); ok {
87-
if metadata, ok := (*manifestFile)[0]["metadata"].(map[string]interface{}); ok {
87+
if metadata, ok := manifestFile[0]["metadata"].(map[string]interface{}); ok {
8888
name, ok := patchMetadata["name"].(string)
8989
if ok && name != "" {
9090
metadata["name"] = name
@@ -93,16 +93,16 @@ func getManifests(policyConf *types.PolicyConfig) ([][]map[string]interface{}, e
9393
if ok && namespace != "" {
9494
metadata["namespace"] = namespace
9595
}
96-
(*manifestFile)[0]["metadata"] = metadata
96+
manifestFile[0]["metadata"] = metadata
9797
}
9898
}
9999
}
100100

101-
manifestFiles = append(manifestFiles, *manifestFile...)
101+
manifestFiles = append(manifestFiles, manifestFile...)
102102
}
103103

104104
for _, manifestPath := range manifestPaths {
105-
var manifestFile *[]map[string]interface{}
105+
var manifestFile []map[string]interface{}
106106
var err error
107107

108108
if hasKustomize[manifestPath] {
@@ -115,11 +115,11 @@ func getManifests(policyConf *types.PolicyConfig) ([][]map[string]interface{}, e
115115
return nil, err
116116
}
117117

118-
if len(*manifestFile) == 0 {
118+
if len(manifestFile) == 0 {
119119
continue
120120
}
121121

122-
manifestFiles = append(manifestFiles, *manifestFile...)
122+
manifestFiles = append(manifestFiles, manifestFile...)
123123
}
124124

125125
if len(manifest.Patches) > 0 {
@@ -136,7 +136,7 @@ func getManifests(policyConf *types.PolicyConfig) ([][]map[string]interface{}, e
136136
return nil, fmt.Errorf(errTemplate, manifest.Path, err)
137137
}
138138

139-
manifestFiles = *patchedFiles
139+
manifestFiles = patchedFiles
140140
}
141141

142142
manifests = append(manifests, manifestFiles)
@@ -207,10 +207,10 @@ func getPolicyTemplates(policyConf *types.PolicyConfig) ([]map[string]map[string
207207
policyTemplate := buildPolicyTemplate(
208208
policyConf,
209209
len(policyTemplates)+1,
210-
&[]map[string]interface{}{objTemplate},
211-
policyConf.Manifests[i].ConfigurationPolicyOptions,
210+
[]map[string]interface{}{objTemplate},
211+
&policyConf.Manifests[i].ConfigurationPolicyOptions,
212212
)
213-
policyTemplates = append(policyTemplates, *policyTemplate)
213+
policyTemplates = append(policyTemplates, policyTemplate)
214214
}
215215
}
216216
}
@@ -227,15 +227,15 @@ func getPolicyTemplates(policyConf *types.PolicyConfig) ([]map[string]map[string
227227
policyTemplate := buildPolicyTemplate(
228228
policyConf,
229229
1,
230-
&objectTemplates,
231-
policyConf.ConfigurationPolicyOptions,
230+
objectTemplates,
231+
&policyConf.ConfigurationPolicyOptions,
232232
)
233-
policyTemplates = append(policyTemplates, *policyTemplate)
233+
policyTemplates = append(policyTemplates, policyTemplate)
234234
}
235235

236236
// check the enabled expanders and add additional policy templates
237237
for _, manifestGroup := range manifestGroups {
238-
expandedPolicyTemplates := handleExpanders(manifestGroup, policyConf)
238+
expandedPolicyTemplates := handleExpanders(manifestGroup, *policyConf)
239239
policyTemplates = append(policyTemplates, expandedPolicyTemplates...)
240240
}
241241

@@ -263,7 +263,7 @@ func isPolicyTypeManifest(manifest map[string]interface{}) (bool, error) {
263263

264264
// setNamespaceSelector sets the namespace selector, if set, on the input policy template.
265265
func setNamespaceSelector(
266-
policyConf types.ConfigurationPolicyOptions,
266+
policyConf *types.ConfigurationPolicyOptions,
267267
policyTemplate map[string]map[string]interface{},
268268
) {
269269
selector := policyConf.NamespaceSelector
@@ -277,7 +277,7 @@ func setNamespaceSelector(
277277
}
278278

279279
// processKustomizeDir runs a provided directory through Kustomize in order to generate the manifests within it.
280-
func processKustomizeDir(path string) (*[]map[string]interface{}, error) {
280+
func processKustomizeDir(path string) ([]map[string]interface{}, error) {
281281
k := krusty.MakeKustomizer(krusty.MakeDefaultOptions())
282282

283283
resourceMap, err := k.Run(filesys.MakeFsOnDisk(), path)
@@ -304,9 +304,9 @@ func processKustomizeDir(path string) (*[]map[string]interface{}, error) {
304304
func buildPolicyTemplate(
305305
policyConf *types.PolicyConfig,
306306
policyNum int,
307-
objectTemplates *[]map[string]interface{},
308-
configPolicyOptionsOverrides types.ConfigurationPolicyOptions,
309-
) *map[string]map[string]interface{} {
307+
objectTemplates []map[string]interface{},
308+
configPolicyOptionsOverrides *types.ConfigurationPolicyOptions,
309+
) map[string]map[string]interface{} {
310310
var name string
311311
if policyNum > 1 {
312312
name = fmt.Sprintf("%s%d", policyConf.Name, policyNum)
@@ -322,15 +322,15 @@ func buildPolicyTemplate(
322322
"name": name,
323323
},
324324
"spec": map[string]interface{}{
325-
"object-templates": *objectTemplates,
325+
"object-templates": objectTemplates,
326326
"remediationAction": policyConf.RemediationAction,
327327
"severity": policyConf.Severity,
328328
},
329329
},
330330
}
331331

332332
// Set NamespaceSelector with policy configuration
333-
setNamespaceSelector(policyConf.ConfigurationPolicyOptions, policyTemplate)
333+
setNamespaceSelector(&policyConf.ConfigurationPolicyOptions, policyTemplate)
334334

335335
if len(policyConf.ConfigurationPolicyAnnotations) > 0 {
336336
metadata := policyTemplate["objectDefinition"]["metadata"].(map[string]interface{})
@@ -373,19 +373,19 @@ func buildPolicyTemplate(
373373
configSpec["severity"] = configPolicyOptionsOverrides.Severity
374374
}
375375

376-
return &policyTemplate
376+
return policyTemplate
377377
}
378378

379379
// handleExpanders will go through all the enabled expanders and generate additional
380380
// policy templates to include in the policy.
381381
func handleExpanders(
382-
manifests []map[string]interface{}, policyConf *types.PolicyConfig,
382+
manifests []map[string]interface{}, policyConf types.PolicyConfig,
383383
) []map[string]map[string]interface{} {
384384
policyTemplates := []map[string]map[string]interface{}{}
385385

386386
for _, expander := range expanders.GetExpanders() {
387387
for _, m := range manifests {
388-
if expander.Enabled(policyConf) && expander.CanHandle(m) {
388+
if expander.Enabled(&policyConf) && expander.CanHandle(m) {
389389
expandedPolicyTemplates := expander.Expand(m, policyConf.Severity)
390390
policyTemplates = append(policyTemplates, expandedPolicyTemplates...)
391391
}
@@ -399,7 +399,7 @@ func handleExpanders(
399399
// a slice in order to account for multiple YAML documents in the same file.
400400
// If the file cannot be decoded or each document is not a map, an error will
401401
// be returned.
402-
func unmarshalManifestFile(manifestPath string) (*[]map[string]interface{}, error) {
402+
func unmarshalManifestFile(manifestPath string) ([]map[string]interface{}, error) {
403403
// #nosec G304
404404
manifestBytes, err := ioutil.ReadFile(manifestPath)
405405
if err != nil {
@@ -417,7 +417,7 @@ func unmarshalManifestFile(manifestPath string) (*[]map[string]interface{}, erro
417417
// unmarshalManifestBytes unmarshals the input bytes slice of an object manifest/definition file
418418
// into a slice of maps in order to account for multiple YAML documents in the bytes slice. If each
419419
// document is not a map, an error will be returned.
420-
func unmarshalManifestBytes(manifestBytes []byte) (*[]map[string]interface{}, error) {
420+
func unmarshalManifestBytes(manifestBytes []byte) ([]map[string]interface{}, error) {
421421
yamlDocs := []map[string]interface{}{}
422422
d := yaml.NewDecoder(bytes.NewReader(manifestBytes))
423423

@@ -445,7 +445,7 @@ func unmarshalManifestBytes(manifestBytes []byte) (*[]map[string]interface{}, er
445445
}
446446
}
447447

448-
return &yamlDocs, nil
448+
return yamlDocs, nil
449449
}
450450

451451
// verifyManifestPath verifies that the manifest path is in the directory tree under baseDirectory.

internal/utils_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,12 +1110,12 @@ data:
11101110
t.Fatalf("Failed to unmarshal the YAML content, got: %v", err)
11111111
}
11121112

1113-
assertEqual(t, len(*manifests), 2)
1113+
assertEqual(t, len(manifests), 2)
11141114

1115-
name1, _, _ := unstructured.NestedString((*manifests)[0], "metadata", "name")
1115+
name1, _, _ := unstructured.NestedString((manifests)[0], "metadata", "name")
11161116
assertEqual(t, name1, "my-configmap")
11171117

1118-
name2, _, _ := unstructured.NestedString((*manifests)[1], "metadata", "name")
1118+
name2, _, _ := unstructured.NestedString((manifests)[1], "metadata", "name")
11191119
assertEqual(t, name2, "my-configmap2")
11201120
}
11211121

@@ -1155,12 +1155,12 @@ data:
11551155
t.Fatalf("Failed to unmarshal the YAML content, got: %v", err)
11561156
}
11571157

1158-
assertEqual(t, len(*manifests), 2)
1158+
assertEqual(t, len(manifests), 2)
11591159

1160-
name1, _, _ := unstructured.NestedString((*manifests)[0], "metadata", "name")
1160+
name1, _, _ := unstructured.NestedString((manifests)[0], "metadata", "name")
11611161
assertEqual(t, name1, "my-configmap")
11621162

1163-
name2, _, _ := unstructured.NestedString((*manifests)[1], "metadata", "name")
1163+
name2, _, _ := unstructured.NestedString((manifests)[1], "metadata", "name")
11641164
assertEqual(t, name2, "my-configmap2")
11651165
}
11661166

@@ -1409,9 +1409,9 @@ data:
14091409
t.Fatalf(fmt.Sprintf("Unexpected error: %s", err))
14101410
}
14111411

1412-
assertEqual(t, len(*manifests), 3)
1412+
assertEqual(t, len(manifests), 3)
14131413

1414-
for _, manifest := range *manifests {
1414+
for _, manifest := range manifests {
14151415
if metadata, ok := manifest["metadata"]; ok {
14161416
ns := metadata.(map[string]interface{})["namespace"]
14171417
assertEqual(t, ns, "kustomize-test")

0 commit comments

Comments
 (0)