Skip to content

Commit a0a8b1e

Browse files
edcdavidopenshift-merge-bot[bot]
authored andcommitted
Addressing second round of comments
Signed-off-by: David Elie-Dit-Cosaque <[email protected]>
1 parent cecc1d3 commit a0a8b1e

File tree

10 files changed

+55
-47
lines changed

10 files changed

+55
-47
lines changed

internal/patches.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,10 @@ import (
1616
"open-cluster-management.io/policy-generator-plugin/internal/types"
1717
)
1818

19-
const (
20-
localSchemaFileName = "schema.json"
21-
kustomizeDir = "kustomize"
22-
)
23-
24-
type KustomizeJSON struct {
25-
types.Filepath `json:"openapi,omitempty" yaml:"openapi,omitempty"`
26-
Patches []Patch `json:"patches" yaml:"patches"`
27-
Resources []string `json:"resources" yaml:"resources"`
19+
type kustomizeFile struct {
20+
OpenAPI types.Filepath `json:"openapi,omitempty" yaml:"openapi,omitempty"`
21+
Patches []Patch `json:"patches" yaml:"patches"`
22+
Resources []string `json:"resources" yaml:"resources"`
2823
}
2924

3025
type Patch struct {
@@ -183,19 +178,22 @@ func setPatchDefaults(
183178
// the patched manifests. An error is returned if the patches can't be applied. This should be
184179
// run after the Validate method.
185180
func (m *manifestPatcher) ApplyPatches() ([]map[string]interface{}, error) {
186-
kustomizeDir := "kustomize"
181+
const (
182+
localSchemaFileName = "schema.json"
183+
kustomizeDir = "kustomize"
184+
)
187185

188186
// Create the file system in memory with the Kustomize YAML files
189187
fSys := filesys.MakeFsInMemory()
190188

191-
err := InitializeInMemoryKustomizeDir(fSys, m.openAPI.Path)
189+
err := initializeInMemoryKustomizeDir(fSys, m.openAPI.Path, kustomizeDir, localSchemaFileName)
192190
if err != nil {
193191
return nil, fmt.Errorf("failed to initialize Kustomize dir: %w", err)
194192
}
195193

196-
kustomizationYAMLFile := KustomizeJSON{}
194+
kustomizationYAMLFile := kustomizeFile{}
197195
if m.openAPI.Path != "" {
198-
kustomizationYAMLFile.Filepath.Path = localSchemaFileName
196+
kustomizationYAMLFile.OpenAPI.Path = localSchemaFileName
199197
}
200198

201199
options := []struct {
@@ -271,7 +269,9 @@ func (m *manifestPatcher) ApplyPatches() ([]map[string]interface{}, error) {
271269
}
272270

273271
// Initializes the in-memory file system with base directory and open API schema
274-
func InitializeInMemoryKustomizeDir(fSys filesys.FileSystem, schema string) (err error) {
272+
func initializeInMemoryKustomizeDir(fSys filesys.FileSystem, schema,
273+
kustomizeDir, localSchemaFileName string,
274+
) (err error) {
275275
err = fSys.Mkdir(kustomizeDir)
276276
if err != nil {
277277
return fmt.Errorf("an unexpected error occurred when configuring Kustomize: %w", err)
@@ -282,12 +282,12 @@ func InitializeInMemoryKustomizeDir(fSys filesys.FileSystem, schema string) (err
282282

283283
schemaJSON, err := os.ReadFile(schema)
284284
if err != nil {
285-
return fmt.Errorf("unable to open file: %s, err: %w ", schema, err)
285+
return fmt.Errorf("error reading file %s: %w ", schema, err)
286286
}
287287

288288
err = fSys.WriteFile(path.Join(kustomizeDir, localSchemaFileName), schemaJSON)
289289
if err != nil {
290-
return fmt.Errorf("error writing schema, err: %w", err)
290+
return fmt.Errorf("error writing schema: %w", err)
291291
}
292292
}
293293

internal/patches_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ func TestApplyPatchesInvalidPatch(t *testing.T) {
303303
}
304304

305305
func TestInitializeInMemoryKustomizeDir(t *testing.T) {
306+
const (
307+
localSchemaFileName = "schema.json"
308+
kustomizeDir = "kustomize"
309+
)
310+
306311
tmpDir := t.TempDir()
307312
testSchemaPath := filepath.Join(tmpDir, "myschema.json")
308313

@@ -337,7 +342,8 @@ func TestInitializeInMemoryKustomizeDir(t *testing.T) {
337342

338343
for _, tt := range tests {
339344
t.Run(tt.name, func(t *testing.T) {
340-
if err := InitializeInMemoryKustomizeDir(tt.args.fSys, tt.args.schema); (err != nil) != tt.wantErr {
345+
if err := initializeInMemoryKustomizeDir(tt.args.fSys, tt.args.schema,
346+
kustomizeDir, localSchemaFileName); (err != nil) != tt.wantErr {
341347
t.Errorf("InitializeInMemoryKustomizeDir() error = %v, wantErr %v", err, tt.wantErr)
342348
}
343349
})

internal/plugin.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,11 +1030,18 @@ func (p *Plugin) assertValidConfig() error {
10301030
)
10311031
}
10321032

1033-
err = verifyManifestPath(p.baseDirectory, manifest.Path)
1033+
err = verifyFilePath(p.baseDirectory, manifest.Path, "manifest")
10341034
if err != nil {
10351035
return err
10361036
}
10371037

1038+
if manifest.OpenAPI.Path != "" {
1039+
err = verifyFilePath(p.baseDirectory, manifest.OpenAPI.Path, "openapi")
1040+
if err != nil {
1041+
return err
1042+
}
1043+
}
1044+
10381045
evalInterval := manifest.EvaluationInterval
10391046

10401047
// Verify that consolidated manifests fields match that of the policy configuration.

internal/plugin_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4445,7 +4445,7 @@ func TestOpenAPIListPatch(t *testing.T) {
44454445

44464446
// Set CR file path and OpenAPI path with temporary directory
44474447
p.Policies[0].Manifests[0].Path = crRelativePath
4448-
p.Policies[0].Manifests[0].Filepath.Path = openAPIRelativePath
4448+
p.Policies[0].Manifests[0].OpenAPI.Path = openAPIRelativePath
44494449

44504450
// Check configuration
44514451
if err := p.assertValidConfig(); err != nil {
@@ -4544,7 +4544,7 @@ func TestOpenAPIListUnkownFieldsPatch(t *testing.T) {
45444544

45454545
// Set CR file path and OpenAPI path with temporary directory
45464546
p.Policies[0].Manifests[0].Path = crRelativePath
4547-
p.Policies[0].Manifests[0].Filepath.Path = openAPIRelativePath
4547+
p.Policies[0].Manifests[0].OpenAPI.Path = openAPIRelativePath
45484548

45494549
// Check configuration
45504550
if err := p.assertValidConfig(); err != nil {

internal/testdata/OpenAPI/cr-files/cr2.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,3 @@ spec:
1515
LocalMaxHoldoverOffSet: 1500
1616
LocalHoldoverTimeout: 14400
1717
MaxInSpecOffset: 100
18-

internal/testdata/OpenAPI/openapi-schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@
5757
]
5858
}
5959
}
60-
}
60+
}

internal/testdata/OpenAPI/policy-generator2.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,3 @@ policies:
4343
pins: $e810_pins
4444
settings:
4545
LocalHoldoverTimeout: 14401
46-
openapi:
47-
path: /home/deliedit/dev/cnf-features-deploy/ztp/tools/pgt2acm/test/openapi-schema.json

internal/types/types.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,19 @@ type Manifest struct {
5353
Path string `json:"path,omitempty" yaml:"path,omitempty"`
5454
ExtraDependencies []PolicyDependency `json:"extraDependencies,omitempty" yaml:"extraDependencies,omitempty"`
5555
IgnorePending bool `json:"ignorePending,omitempty" yaml:"ignorePending,omitempty"`
56-
Filepath `json:"openapi,omitempty" yaml:"openapi,omitempty"`
56+
OpenAPI Filepath `json:"openapi,omitempty" yaml:"openapi,omitempty"`
5757
}
5858

5959
type Filepath struct {
60-
Path string `json:"path" yaml:"path"`
60+
Path string `json:"path,omitempty" yaml:"path,omitempty"`
6161
}
6262

63-
type (
64-
NamespaceSelector struct {
65-
Exclude []string `json:"exclude,omitempty" yaml:"exclude,omitempty"`
66-
Include []string `json:"include,omitempty" yaml:"include,omitempty"`
67-
MatchLabels *map[string]string `json:"matchLabels,omitempty" yaml:"matchLabels,omitempty"`
68-
MatchExpressions *[]metav1.LabelSelectorRequirement `json:"matchExpressions,omitempty" yaml:"matchExpressions,omitempty"`
69-
}
70-
)
63+
type NamespaceSelector struct {
64+
Exclude []string `json:"exclude,omitempty" yaml:"exclude,omitempty"`
65+
Include []string `json:"include,omitempty" yaml:"include,omitempty"`
66+
MatchLabels *map[string]string `json:"matchLabels,omitempty" yaml:"matchLabels,omitempty"`
67+
MatchExpressions *[]metav1.LabelSelectorRequirement `json:"matchExpressions,omitempty" yaml:"matchExpressions,omitempty"`
68+
}
7169

7270
// Define String() so that the LabelSelector is dereferenced in the logs
7371
func (t NamespaceSelector) String() string {

internal/utils.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func getManifests(policyConf *types.PolicyConfig) ([][]map[string]interface{}, e
123123
}
124124

125125
if len(manifest.Patches) > 0 {
126-
patcher := manifestPatcher{manifests: manifestFiles, patches: manifest.Patches, openAPI: manifest.Filepath}
126+
patcher := manifestPatcher{manifests: manifestFiles, patches: manifest.Patches, openAPI: manifest.OpenAPI}
127127
const errTemplate = `failed to process the manifest at "%s": %w`
128128

129129
err = patcher.Validate()
@@ -574,39 +574,39 @@ func unmarshalManifestBytes(manifestBytes []byte) ([]map[string]interface{}, err
574574
return yamlDocs, nil
575575
}
576576

577-
// verifyManifestPath verifies that the manifest path is in the directory tree under baseDirectory.
577+
// verifyFilePath verifies that the file path is in the directory tree under baseDirectory.
578578
// An error is returned if it is not or the paths couldn't be properly resolved.
579-
func verifyManifestPath(baseDirectory string, manifestPath string) error {
580-
absPath, err := filepath.Abs(manifestPath)
579+
func verifyFilePath(baseDirectory string, filePath, fileType string) error {
580+
absPath, err := filepath.Abs(filePath)
581581
if err != nil {
582-
return fmt.Errorf("could not resolve the manifest path %s to an absolute path", manifestPath)
582+
return fmt.Errorf("could not resolve the %s path %s to an absolute path", fileType, filePath)
583583
}
584584

585585
absPath, err = filepath.EvalSymlinks(absPath)
586586
if err != nil {
587-
return fmt.Errorf("could not resolve symlinks to the manifest path %s", manifestPath)
587+
return fmt.Errorf("could not resolve symlinks to the %s path %s", fileType, filePath)
588588
}
589589

590590
relPath, err := filepath.Rel(baseDirectory, absPath)
591591
if err != nil {
592592
return fmt.Errorf(
593-
"could not resolve the manifest path %s to a relative path from the kustomization.yaml file",
594-
manifestPath,
593+
"could not resolve the %s path %s to a relative path from the kustomization.yaml file",
594+
fileType, filePath,
595595
)
596596
}
597597

598598
if relPath == "." {
599599
return fmt.Errorf(
600-
"the manifest path %s may not refer to the same directory as the kustomization.yaml file",
601-
manifestPath,
600+
"the %s path %s may not refer to the same directory as the kustomization.yaml file",
601+
fileType, filePath,
602602
)
603603
}
604604

605605
parDir := ".." + string(filepath.Separator)
606606
if strings.HasPrefix(relPath, parDir) || relPath == ".." {
607607
return fmt.Errorf(
608-
"the manifest path %s is not in the same directory tree as the kustomization.yaml file",
609-
manifestPath,
608+
"the %s path %s is not in the same directory tree as the kustomization.yaml file",
609+
fileType, filePath,
610610
)
611611
}
612612

internal/utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1604,7 +1604,7 @@ func TestVerifyManifestPath(t *testing.T) {
16041604
t.Run(
16051605
"manifestPath="+test.ManifestPath,
16061606
func(t *testing.T) {
1607-
err := verifyManifestPath(workingDir, test.ManifestPath)
1607+
err := verifyFilePath(workingDir, test.ManifestPath, "manifest")
16081608
if err == nil {
16091609
assertEqual(t, "", test.ExpectedErrMsg)
16101610
} else {

0 commit comments

Comments
 (0)