Skip to content

Commit aee2d44

Browse files
authored
feat: add x-kubernetes-group-version-kind to the schema (#129)
1 parent cbf8238 commit aee2d44

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

cmd/file_handler.go

+4
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,9 @@ func (h *FileHandler) CRDs() ([]*pkg.SchemaType, error) {
3939
return nil, fmt.Errorf("failed to extract schema type: %w", err)
4040
}
4141

42+
if schemaType == nil {
43+
return nil, nil
44+
}
45+
4246
return []*pkg.SchemaType{schemaType}, nil
4347
}

cmd/folder_handler.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ func (h *FolderHandler) CRDs() ([]*pkg.SchemaType, error) {
6060
return fmt.Errorf("failed to extract schema type: %w", err)
6161
}
6262

63-
crds = append(crds, schemaType)
63+
if schemaType != nil {
64+
crds = append(crds, schemaType)
65+
}
6466

6567
return nil
6668
}); err != nil {

cmd/schema.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77

88
"github.com/spf13/cobra"
99
"k8s.io/apimachinery/pkg/util/json"
10+
11+
"github.com/Skarlso/crd-to-sample-yaml/v1beta1"
1012
)
1113

1214
// schemaCmd is a command that can generate json schemas.
@@ -22,6 +24,17 @@ type schemaCmdArgs struct {
2224

2325
var schemaArgs = &schemaCmdArgs{}
2426

27+
type KindVersionGroup struct {
28+
Kind string `json:"kind"`
29+
Version string `json:"version"`
30+
Group string `json:"group"`
31+
}
32+
33+
type Schema struct {
34+
*v1beta1.JSONSchemaProps `json:",inline"`
35+
KubernetesGroupVersionKindList []KindVersionGroup `json:"x-kubernetes-group-version-kind"`
36+
}
37+
2538
func init() {
2639
generateCmd.AddCommand(schemaCmd)
2740
f := schemaCmd.PersistentFlags()
@@ -57,7 +70,19 @@ func runGenerateSchema(_ *cobra.Command, _ []string) error {
5770
if v.Schema.Schema == "" {
5871
v.Schema.Schema = "https://json-schema.org/draft/2020-12/schema"
5972
}
60-
content, err := json.Marshal(v.Schema)
73+
74+
schema := Schema{
75+
JSONSchemaProps: v.Schema,
76+
KubernetesGroupVersionKindList: []KindVersionGroup{
77+
{
78+
Kind: crd.Kind,
79+
Group: crd.Group,
80+
Version: v.Name,
81+
},
82+
},
83+
}
84+
85+
content, err := json.Marshal(schema)
6186
if err != nil {
6287
return fmt.Errorf("failed to marshal schema: %w", err)
6388
}

cmd/url_handler.go

+4
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,9 @@ func (h *URLHandler) CRDs() ([]*pkg.SchemaType, error) {
4646
return nil, fmt.Errorf("failed to extract schema type: %w", err)
4747
}
4848

49+
if schemaType == nil {
50+
return nil, nil
51+
}
52+
4953
return []*pkg.SchemaType{schemaType}, nil
5054
}

pkg/extract_schematype.go

+8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ func ExtractSchemaType(obj *unstructured.Unstructured) (*SchemaType, error) {
3434

3535
versions, ok := specMap["versions"]
3636
if !ok {
37+
if _, ok := specMap["validation"]; !ok {
38+
// we aren't dealing with a valid resource
39+
// we might want to skip it if we are going through a
40+
// list of YAML files in a folder, and we want to skip
41+
// invalid ones.
42+
return nil, nil
43+
}
44+
3745
return extractValidation(obj, specMap)
3846
}
3947

0 commit comments

Comments
 (0)