-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add crd resource visitor util (#804)
- Loading branch information
Showing
4 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
--- | ||
apiVersion: apiextensions.k8s.io/v1beta1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
annotations: | ||
controller-gen.kubebuilder.io/version: v0.2.4 | ||
creationTimestamp: null | ||
name: guests.webapp.my.domain | ||
spec: | ||
group: webapp.my.domain | ||
names: | ||
kind: Guest | ||
listKind: GuestList | ||
plural: guests | ||
singular: guest | ||
scope: Namespaced | ||
validation: | ||
openAPIV3Schema: | ||
description: Guest is the Schema for the guests API | ||
properties: | ||
apiVersion: | ||
description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' | ||
type: string | ||
kind: | ||
description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' | ||
type: string | ||
metadata: | ||
type: object | ||
spec: | ||
description: GuestSpec defines the desired state of Guest | ||
properties: | ||
beep: | ||
description: Beep is an example field of Guest. Edit Guest_types.go to remove/update | ||
type: string | ||
type: object | ||
status: | ||
description: GuestStatus defines the observed state of Guest | ||
type: object | ||
type: object | ||
version: v1 | ||
versions: | ||
- name: v1 | ||
served: true | ||
storage: true | ||
status: | ||
acceptedNames: | ||
kind: "" | ||
plural: "" | ||
conditions: [] | ||
storedVersions: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
apiVersion: apiextensions.k8s.io/v1beta1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
annotations: | ||
controller-gen.kubebuilder.io/version: v0.2.4 | ||
creationTimestamp: null | ||
name: guestbooks.webapp.my.domain | ||
spec: | ||
group: webapp.my.domain | ||
names: | ||
kind: Guestbook | ||
listKind: GuestbookList | ||
plural: guestbooks | ||
singular: guestbook | ||
scope: Namespaced | ||
validation: | ||
openAPIV3Schema: | ||
description: Guestbook is the Schema for the guestbooks API | ||
properties: | ||
apiVersion: | ||
description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' | ||
type: string | ||
kind: | ||
description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' | ||
type: string | ||
metadata: | ||
type: object | ||
spec: | ||
description: GuestbookSpec defines the desired state of Guestbook | ||
properties: | ||
foo: | ||
description: Foo is an example field of Guestbook. Edit Guestbook_types.go to remove/update | ||
type: string | ||
type: object | ||
status: | ||
description: GuestbookStatus defines the observed state of Guestbook | ||
type: object | ||
type: object | ||
version: v1 | ||
versions: | ||
- name: v1 | ||
served: true | ||
storage: true | ||
status: | ||
acceptedNames: | ||
kind: "" | ||
plural: "" | ||
conditions: [] | ||
storedVersions: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package resource | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"k8s.io/apimachinery/pkg/util/yaml" | ||
) | ||
|
||
var FileExtensions = []string{".yaml", ".yml"} | ||
|
||
type CrdVisitor struct { | ||
Path string | ||
} | ||
|
||
// Visit walks a list of resources under target path. | ||
func (v *CrdVisitor) Visit() (objs []map[string]interface{}, err error) { | ||
err = filepath.WalkDir(v.Path, func(filePath string, d os.DirEntry, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// check file extension | ||
if ignoreFile(filePath, FileExtensions) { | ||
return nil | ||
} | ||
|
||
f, err := os.Open(filePath) | ||
if err != nil { | ||
return err | ||
} | ||
decoder := yaml.NewYAMLOrJSONDecoder(f, 4096) | ||
for { | ||
data := make(map[string]interface{}) | ||
if err := decoder.Decode(&data); err != nil { | ||
if err == io.EOF { | ||
return nil | ||
} | ||
return fmt.Errorf("error parsing %s: %v", filePath, err) | ||
} | ||
if len(data) == 0 { | ||
continue | ||
} | ||
|
||
objs = append(objs, data) | ||
} | ||
}) | ||
return objs, err | ||
} | ||
|
||
func ignoreFile(path string, extensions []string) bool { | ||
if len(extensions) == 0 { | ||
return false | ||
} | ||
ext := filepath.Ext(path) | ||
for _, s := range extensions { | ||
if s == ext { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package resource | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestCrdVisitor(t *testing.T) { | ||
visitor := CrdVisitor{ | ||
Path: "./crd", | ||
} | ||
objs, err := visitor.Visit() | ||
if err != nil { | ||
t.Errorf("unexpected err: %v", err) | ||
} | ||
if len(objs) != 2 { | ||
t.Errorf("unexpected doc size: %d", len(objs)) | ||
} | ||
} |