/
fs.go
158 lines (124 loc) · 4.13 KB
/
fs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
Copyright 2022 The Kubermatic Kubernetes Platform contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package crd
import (
"embed"
"errors"
"fmt"
"io/fs"
"strings"
"github.com/gobuffalo/flect"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/yaml"
)
// embeddedFS is an embedded fs that contains kubermatic CRD manifest
//
//go:embed *
var embeddedFS embed.FS
const (
k8cDir = "k8c.io"
k8sDir = "k8s.io"
)
// Groups returns a list of all known API groups for which CRDs are available.
func Groups() ([]string, error) {
dirs := []string{k8cDir, k8sDir}
entries := []fs.DirEntry{}
for _, dir := range dirs {
dirEntries, err := embeddedFS.ReadDir(dir)
if err != nil {
return nil, err
}
entries = append(entries, dirEntries...)
}
groups := sets.New[string]()
for _, entry := range entries {
name := strings.Split(entry.Name(), "_")
groups.Insert(name[0])
}
return sets.List(groups), nil
}
// CRDForType returns the CRD for a given object or returns an error if the
// object is not using one of the known types (*.k8c.io).
func CRDForObject(obj runtime.Object) (*apiextensionsv1.CustomResourceDefinition, error) {
return CRDForGVK(obj.GetObjectKind().GroupVersionKind())
}
// CRDForGVK returns the CRD for the given GKV or an error if there is no
// CRD available (i.e. any GVK outside of *.k8c.io was provided).
func CRDForGVK(gvk schema.GroupVersionKind) (*apiextensionsv1.CustomResourceDefinition, error) {
// as filenames are being generated by controller-gen, use the same pluralization mechanism
// https://github.com/kubernetes-sigs/controller-tools/blob/8cb5ce83c3cca425a4de0af3d2578e31a3cd6a48/pkg/crd/spec.go#L58
kindPlural := strings.ToLower(flect.Pluralize(gvk.Kind))
filename := gvk.Group + "_" + kindPlural + ".yaml"
dir, err := getDirForGroup(gvk.Group)
if err != nil {
return nil, fmt.Errorf("no CRD available \"%s_%s\": %w", gvk.Group, kindPlural, err)
}
crd, err := loadCRD(dir, filename)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil, fmt.Errorf("no CRD available for \"%s_%s\"", gvk.Group, kindPlural)
}
return nil, err
}
return crd, nil
}
// CRDsForGroup returns all CRDs for the given API group (e.g. "kubermatic.k8c.io").
func CRDsForGroup(apiGroup string) ([]apiextensionsv1.CustomResourceDefinition, error) {
dir, err := getDirForGroup(apiGroup)
if err != nil {
return nil, fmt.Errorf("no CRDs available: %w", err)
}
files, err := embeddedFS.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("x: %w", err)
}
prefix := fmt.Sprintf("%s_", apiGroup)
result := []apiextensionsv1.CustomResourceDefinition{}
for _, info := range files {
filename := info.Name()
if !strings.HasPrefix(filename, prefix) {
continue
}
crd, err := loadCRD(dir, filename)
if err != nil {
return nil, fmt.Errorf("failed to open CRD: %w", err)
}
result = append(result, *crd)
}
return result, nil
}
func getDirForGroup(group string) (string, error) {
switch {
case strings.HasSuffix(group, k8cDir):
return k8cDir, nil
case strings.HasSuffix(group, k8sDir):
return k8sDir, nil
}
return "", fmt.Errorf("no directory exists for \"%s\"", group)
}
func loadCRD(directory string, filename string) (*apiextensionsv1.CustomResourceDefinition, error) {
f, err := embeddedFS.Open(directory + "/" + filename)
if err != nil {
return nil, err
}
defer f.Close()
crd := &apiextensionsv1.CustomResourceDefinition{}
dec := yaml.NewYAMLOrJSONDecoder(f, 1024)
if err := dec.Decode(crd); err != nil {
return nil, err
}
return crd, nil
}