-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
173 lines (157 loc) · 4.83 KB
/
main.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"context"
"fmt"
"io/ioutil"
"os"
"strings"
pluralize "github.com/gertd/go-pluralize"
"gopkg.in/yaml.v2"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
yamlsig "sigs.k8s.io/yaml"
)
func main() {
if len(os.Args) != 3 {
fmt.Printf("expected 2 args: values file path and Kubernetes GVK.\nExample:: ./helm-values-to-crd test.yaml redis.platform.kratix.io/v1alpha1\n")
os.Exit(1)
}
valuesFile := os.Args[1]
gvk := os.Args[2]
kind, version, group := splitGVK(gvk)
// Read the values file
var template map[string]interface{}
templateBytes, err := ioutil.ReadFile(valuesFile)
if err != nil {
panic("failed to read file " + valuesFile + ", " + err.Error())
}
// Unmarshal the values file
err = yaml.Unmarshal(templateBytes, &template)
if err != nil {
panic("failed tounmarshal " + valuesFile + ", " + err.Error())
}
// Convert the values file to a CRD
crd, err := convertValuesToCRD(context.TODO(), template, group, version, kind)
if err != nil {
panic("failed to generate crd: " + err.Error())
}
// Marshal CRD to bytes
bytes, err := yamlsig.Marshal(crd)
if err != nil {
panic(err)
}
fmt.Println(string(bytes))
}
func splitGVK(gvk string) (string, string, string) {
kind := strings.Split(gvk, ".")[0]
version := strings.Split(gvk, "/")[len(strings.Split(gvk, "/"))-1]
group := strings.TrimSuffix(strings.TrimPrefix(gvk, fmt.Sprintf("%s.", kind)), fmt.Sprintf("/%s", version))
return kind, version, group
}
func convertValuesToCRD(ctx context.Context, template map[string]interface{}, group, version, kind string) (*apiextensionsv1.CustomResourceDefinition, error) {
var validationSchema *apiextensionsv1.JSONSchemaProps = &apiextensionsv1.JSONSchemaProps{
Type: "object",
Properties: map[string]apiextensionsv1.JSONSchemaProps{},
}
//Auto inject the `spec: ` top level key
var openAPIV3Schema *apiextensionsv1.JSONSchemaProps = &apiextensionsv1.JSONSchemaProps{
Type: "object",
Properties: map[string]apiextensionsv1.JSONSchemaProps{
"spec": *validationSchema,
},
}
//Add to the `spec` the keys from the values file
for key, value := range map[string]interface{}(template) {
validationSchema.Properties[key] = generateJSONSchemaFromValue(value)
}
//Generate CRD with the spec and GVK
pluralKind := strings.ToLower(pluralize.NewClient().Plural(kind))
xaasCRD := &apiextensionsv1.CustomResourceDefinition{
TypeMeta: metav1.TypeMeta{
Kind: "CustomResourceDefinition",
APIVersion: apiextensionsv1.SchemeGroupVersion.Identifier(),
},
ObjectMeta: metav1.ObjectMeta{
Name: pluralKind + "." + group,
},
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
Group: group,
Names: apiextensionsv1.CustomResourceDefinitionNames{
Plural: pluralKind,
Singular: strings.ToLower(kind),
Kind: kind,
},
Scope: apiextensionsv1.NamespaceScoped,
Versions: []apiextensionsv1.CustomResourceDefinitionVersion{
{
Name: version,
Served: true,
Storage: true,
Schema: &apiextensionsv1.CustomResourceValidation{
OpenAPIV3Schema: openAPIV3Schema,
},
},
},
},
}
return xaasCRD, nil
}
func generateJSONSchemaFromValue(value interface{}) apiextensionsv1.JSONSchemaProps {
boolTrue := true
switch valueType := value.(type) {
case string:
return apiextensionsv1.JSONSchemaProps{
Type: "string",
}
case int, int16, int32, int64, int8, float32, float64:
return apiextensionsv1.JSONSchemaProps{
Type: "integer",
}
case bool:
return apiextensionsv1.JSONSchemaProps{
Type: "boolean",
}
case []interface{}:
v := value.([]interface{})
var schemaV apiextensionsv1.JSONSchemaProps
if len(v) > 0 {
schemaV = generateJSONSchemaFromValue(v[0])
} else {
schemaV = apiextensionsv1.JSONSchemaProps{
XIntOrString: true,
}
}
return apiextensionsv1.JSONSchemaProps{
Type: "array",
Items: &apiextensionsv1.JSONSchemaPropsOrArray{
Schema: &schemaV,
},
}
case map[string]interface{}:
jsonSchema := map[string]apiextensionsv1.JSONSchemaProps{}
for key, value := range valueType {
jsonSchema[key] = generateJSONSchemaFromValue(value)
}
return apiextensionsv1.JSONSchemaProps{
Type: "object",
Properties: jsonSchema,
XPreserveUnknownFields: &boolTrue,
}
case map[interface{}]interface{}:
jsonSchema := map[string]apiextensionsv1.JSONSchemaProps{}
for key, value := range valueType {
keyString, ok := key.(string)
if !ok {
panic(fmt.Sprintf("key is not string: %v", key))
}
jsonSchema[keyString] = generateJSONSchemaFromValue(value)
}
return apiextensionsv1.JSONSchemaProps{
Type: "object",
Properties: jsonSchema,
XPreserveUnknownFields: &boolTrue,
}
default:
panic("unknown type, code needs to be updated to handle this")
}
}