Skip to content

Commit

Permalink
feat: add crd resource visitor util (#804)
Browse files Browse the repository at this point in the history
  • Loading branch information
adohe committed Feb 21, 2024
1 parent e795868 commit 782f545
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
51 changes: 51 additions & 0 deletions pkg/cmd/util/resource/crd/multi_crd.yaml
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: []
49 changes: 49 additions & 0 deletions pkg/cmd/util/resource/crd/webapp_crd.yaml
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: []
64 changes: 64 additions & 0 deletions pkg/cmd/util/resource/visitor.go
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
}
18 changes: 18 additions & 0 deletions pkg/cmd/util/resource/visitor_test.go
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))
}
}

0 comments on commit 782f545

Please sign in to comment.