Skip to content

Commit c21d111

Browse files
authored
commands/.../scorecard: move getCRDs to k8sutil (#1071)
* commands/.../scorecard/olm_tests.go: move getCRDs to k8sutil * internal/util/k8sutil/crd.go: move getCRDs here from scorecard * commands/.../generate/openapi.go: use k8sutil.GetCRDs instead of splitting file names
1 parent 330bc35 commit c21d111

File tree

5 files changed

+71
-96
lines changed

5 files changed

+71
-96
lines changed

commands/operator-sdk/cmd/generate/openapi.go

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ import (
2323
"strings"
2424

2525
genutil "github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/generate/internal"
26+
"github.com/operator-framework/operator-sdk/internal/util/k8sutil"
2627
"github.com/operator-framework/operator-sdk/internal/util/projutil"
2728
"github.com/operator-framework/operator-sdk/pkg/scaffold"
2829
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
2930

30-
"github.com/ghodss/yaml"
3131
log "github.com/sirupsen/logrus"
3232
"github.com/spf13/cobra"
33-
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
34-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3533
)
3634

3735
var headerFile string
@@ -111,26 +109,29 @@ func OpenAPIGen() error {
111109
AbsProjectPath: absProjectPath,
112110
ProjectName: filepath.Base(absProjectPath),
113111
}
114-
crdMap, err := getCRDGVKMap()
112+
crds, err := k8sutil.GetCRDs(scaffold.CRDsDir)
115113
if err != nil {
116114
return err
117115
}
118-
for g, vs := range gvMap {
119-
for _, v := range vs {
120-
gvks := crdMap[filepath.Join(g, v)]
121-
for _, gvk := range gvks {
122-
r, err := scaffold.NewResource(filepath.Join(gvk.Group, gvk.Version), gvk.Kind)
123-
if err != nil {
124-
return err
125-
}
126-
err = s.Execute(cfg,
127-
&scaffold.CRD{Resource: r, IsOperatorGo: projutil.IsOperatorGo()},
128-
)
129-
if err != nil {
130-
return err
131-
}
116+
for _, crd := range crds {
117+
g, v, k := crd.Spec.Group, crd.Spec.Version, crd.Spec.Names.Kind
118+
if v == "" {
119+
if len(crd.Spec.Versions) != 0 {
120+
v = crd.Spec.Versions[0].Name
121+
} else {
122+
return fmt.Errorf("crd of group %s kind %s has no version", g, k)
132123
}
133124
}
125+
r, err := scaffold.NewResource(g+"/"+v, k)
126+
if err != nil {
127+
return err
128+
}
129+
err = s.Execute(cfg,
130+
&scaffold.CRD{Resource: r, IsOperatorGo: projutil.IsOperatorGo()},
131+
)
132+
if err != nil {
133+
return err
134+
}
134135
}
135136

136137
log.Info("Code-generation complete.")
@@ -179,34 +180,3 @@ func openAPIGen(binDir string, fqApis []string) (err error) {
179180
}
180181
return nil
181182
}
182-
183-
func getCRDGVKMap() (map[string][]metav1.GroupVersionKind, error) {
184-
crdInfos, err := ioutil.ReadDir(scaffold.CRDsDir)
185-
if err != nil {
186-
return nil, err
187-
}
188-
crdMap := make(map[string][]metav1.GroupVersionKind)
189-
for _, info := range crdInfos {
190-
if filepath.Ext(info.Name()) == ".yaml" {
191-
path := filepath.Join(scaffold.CRDsDir, info.Name())
192-
b, err := ioutil.ReadFile(path)
193-
if err != nil {
194-
return nil, err
195-
}
196-
crd := &apiextv1beta1.CustomResourceDefinition{}
197-
if err := yaml.Unmarshal(b, crd); err != nil {
198-
return nil, err
199-
}
200-
if crd.Kind != "CustomResourceDefinition" {
201-
continue
202-
}
203-
gv := filepath.Join(strings.Split(info.Name(), "_")[:2]...)
204-
crdMap[gv] = append(crdMap[gv], metav1.GroupVersionKind{
205-
Group: crd.Spec.Group,
206-
Version: crd.Spec.Version,
207-
Kind: crd.Spec.Names.Kind,
208-
})
209-
}
210-
}
211-
return crdMap, nil
212-
}

commands/operator-sdk/cmd/olm-catalog/gen-csv.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func genCSVFunc(cmd *cobra.Command, args []string) error {
6767
AbsProjectPath: absProjectPath,
6868
ProjectName: filepath.Base(absProjectPath),
6969
}
70-
if projutil.GetOperatorType() == projutil.OperatorTypeGo {
70+
if projutil.IsOperatorGo() {
7171
cfg.Repo = projutil.CheckAndGetProjectGoPkg()
7272
}
7373

commands/operator-sdk/cmd/scorecard/olm_tests.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ package scorecard
1717
import (
1818
"context"
1919
"fmt"
20-
"io/ioutil"
21-
"path/filepath"
2220
"strings"
2321

24-
"github.com/operator-framework/operator-sdk/pkg/scaffold"
22+
"github.com/operator-framework/operator-sdk/internal/util/k8sutil"
2523

2624
olmapiv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
2725
log "github.com/sirupsen/logrus"
@@ -31,28 +29,6 @@ import (
3129
"sigs.k8s.io/controller-runtime/pkg/client"
3230
)
3331

34-
func getCRDs(crdsDir string) ([]apiextv1beta1.CustomResourceDefinition, error) {
35-
files, err := ioutil.ReadDir(crdsDir)
36-
if err != nil {
37-
return nil, fmt.Errorf("could not read deploy directory: (%v)", err)
38-
}
39-
crds := []apiextv1beta1.CustomResourceDefinition{}
40-
for _, file := range files {
41-
if strings.HasSuffix(file.Name(), "crd.yaml") {
42-
obj, err := yamlToUnstructured(filepath.Join(scaffold.CRDsDir, file.Name()))
43-
if err != nil {
44-
return nil, err
45-
}
46-
crd, err := unstructuredToCRD(obj)
47-
if err != nil {
48-
return nil, err
49-
}
50-
crds = append(crds, *crd)
51-
}
52-
}
53-
return crds, nil
54-
}
55-
5632
func matchKind(kind1, kind2 string) bool {
5733
singularKind1, err := restMapper.ResourceSingularizer(kind1)
5834
if err != nil {
@@ -68,7 +44,7 @@ func matchKind(kind1, kind2 string) bool {
6844
}
6945

7046
// matchVersion checks if a CRD contains a specified version in a case insensitive manner
71-
func matchVersion(version string, crd apiextv1beta1.CustomResourceDefinition) bool {
47+
func matchVersion(version string, crd *apiextv1beta1.CustomResourceDefinition) bool {
7248
if strings.EqualFold(version, crd.Spec.Version) {
7349
return true
7450
}
@@ -84,7 +60,7 @@ func matchVersion(version string, crd apiextv1beta1.CustomResourceDefinition) bo
8460
// crdsHaveValidation makes sure that all CRDs have a validation block
8561
func crdsHaveValidation(crdsDir string, runtimeClient client.Client, obj *unstructured.Unstructured) error {
8662
test := scorecardTest{testType: olmIntegration, name: "Provided APIs have validation"}
87-
crds, err := getCRDs(crdsDir)
63+
crds, err := k8sutil.GetCRDs(crdsDir)
8864
if err != nil {
8965
return fmt.Errorf("failed to get CRDs in %s directory: %v", crdsDir, err)
9066
}

commands/operator-sdk/cmd/scorecard/resource_handler.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
log "github.com/sirupsen/logrus"
3232
appsv1 "k8s.io/api/apps/v1"
3333
v1 "k8s.io/api/core/v1"
34-
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
3534
apierrors "k8s.io/apimachinery/pkg/api/errors"
3635
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3736
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -226,24 +225,6 @@ func addProxyContainer(dep *appsv1.Deployment) {
226225
})
227226
}
228227

229-
// unstructuredToCRD converts an unstructured object to a CRD
230-
func unstructuredToCRD(obj *unstructured.Unstructured) (*apiextv1beta1.CustomResourceDefinition, error) {
231-
jsonByte, err := obj.MarshalJSON()
232-
if err != nil {
233-
return nil, fmt.Errorf("failed to convert CRD to json: %v", err)
234-
}
235-
crdObj, _, err := dynamicDecoder.Decode(jsonByte, nil, nil)
236-
if err != nil {
237-
return nil, fmt.Errorf("failed to decode CRD object: %v", err)
238-
}
239-
switch o := crdObj.(type) {
240-
case *apiextv1beta1.CustomResourceDefinition:
241-
return o, nil
242-
default:
243-
return nil, fmt.Errorf("conversion of runtime object to CRD failed (resulting runtime object not CRD type)")
244-
}
245-
}
246-
247228
// unstructuredToDeployment converts an unstructured object to a deployment
248229
func unstructuredToDeployment(obj *unstructured.Unstructured) (*appsv1.Deployment, error) {
249230
jsonByte, err := obj.MarshalJSON()

internal/util/k8sutil/crd.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package k8sutil
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
10+
yaml "github.com/ghodss/yaml"
11+
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
12+
)
13+
14+
func GetCRDs(crdsDir string) ([]*apiextv1beta1.CustomResourceDefinition, error) {
15+
manifests, err := GetCRDManifestPaths(crdsDir)
16+
if err != nil {
17+
return nil, fmt.Errorf("failed to get CRD's from %s: (%v)", crdsDir, err)
18+
}
19+
var crds []*apiextv1beta1.CustomResourceDefinition
20+
for _, m := range manifests {
21+
b, err := ioutil.ReadFile(m)
22+
if err != nil {
23+
return nil, err
24+
}
25+
crd := &apiextv1beta1.CustomResourceDefinition{}
26+
if err = yaml.Unmarshal(b, crd); err != nil {
27+
return nil, err
28+
}
29+
crds = append(crds, crd)
30+
}
31+
return crds, nil
32+
}
33+
34+
func GetCRDManifestPaths(crdsDir string) (crdPaths []string, err error) {
35+
err = filepath.Walk(crdsDir, func(path string, info os.FileInfo, werr error) error {
36+
if werr != nil {
37+
return werr
38+
}
39+
if info == nil {
40+
return nil
41+
}
42+
if !info.IsDir() && strings.HasSuffix(path, "_crd.yaml") {
43+
crdPaths = append(crdPaths, path)
44+
}
45+
return nil
46+
})
47+
return crdPaths, err
48+
}

0 commit comments

Comments
 (0)