Skip to content

Commit

Permalink
refactor: rename localFilesClient to localSchemasClient and don't con…
Browse files Browse the repository at this point in the history
…sider CRDs

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly committed May 31, 2023
1 parent 352a78c commit d5bd545
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 140 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (c *commandFlags) Run(cmd *cobra.Command, args []string) error {
openapiclient.PatchLoaderFromDirectory(nil, c.schemaPatchesDir),
openapiclient.NewComposite(
// consult local OpenAPI
openapiclient.NewLocalFiles(nil, c.localSchemasDir),
openapiclient.NewLocalSchemaFiles(nil, c.localSchemasDir),
// consult local CRDs
openapiclient.NewLocalCRDFiles(nil, c.localCRDsDir),
openapiclient.NewOverlay(
Expand Down
1 change: 0 additions & 1 deletion pkg/openapiclient/github_builtins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ func TestGitHubBuiltins(t *testing.T) {
t.Fatal(err)
}
fmt.Println(m)

}
138 changes: 0 additions & 138 deletions pkg/openapiclient/local_files.go

This file was deleted.

61 changes: 61 additions & 0 deletions pkg/openapiclient/local_schemas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package openapiclient

import (
"fmt"
"io/fs"
"path/filepath"
"strings"

"k8s.io/client-go/openapi"
"sigs.k8s.io/kubectl-validate/pkg/openapiclient/groupversion"
"sigs.k8s.io/kubectl-validate/pkg/utils"
)

// client which provides openapi read from files on disk
type localSchemasClient struct {
fs fs.FS
dir string
}

// Dir should have openapi files following directory layout:
// /<apis>/<group>/<version>.json
// /api/<version>.json
func NewLocalSchemaFiles(fs fs.FS, dirPath string) openapi.Client {
return &localSchemasClient{
fs: fs,
dir: dirPath,
}
}

func (k *localSchemasClient) Paths() (map[string]openapi.GroupVersion, error) {
if len(k.dir) == 0 {
return nil, nil
}
res := map[string]openapi.GroupVersion{}
apiGroups, _ := utils.ReadDir(k.fs, filepath.Join(k.dir, "apis"))
for _, f := range apiGroups {
groupPath := filepath.Join(k.dir, "apis", f.Name())
versions, err := utils.ReadDir(k.fs, groupPath)
if err != nil {
return nil, fmt.Errorf("failed reading local files dir %s: %w", groupPath, err)
}
for _, v := range versions {
if !utils.IsJson(v.Name()) {
continue
}
name := strings.TrimSuffix(v.Name(), filepath.Ext(v.Name()))
path := filepath.Join("apis", f.Name(), name)
res[path] = groupversion.NewForFile(k.fs, filepath.Join(groupPath, v.Name()))
}
}
coregroup, _ := utils.ReadDir(k.fs, filepath.Join(k.dir, "api"))
for _, v := range coregroup {
if !utils.IsJson(v.Name()) {
continue
}
name := strings.TrimSuffix(v.Name(), filepath.Ext(v.Name()))
path := filepath.Join("api", name)
res[path] = groupversion.NewForFile(k.fs, filepath.Join(k.dir, "api", v.Name()))
}
return res, nil
}

0 comments on commit d5bd545

Please sign in to comment.