|
| 1 | +package templating |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "text/template" |
| 6 | +) |
| 7 | + |
| 8 | +// KindTypesTemplateConfig is used to overwrite the generated kind_types.go file |
| 9 | +type KindTypesTemplateConfig struct { |
| 10 | + Kind string |
| 11 | + Version string |
| 12 | + LowerPlural string |
| 13 | + KindSpec string |
| 14 | + SpecConfigurationName string |
| 15 | + SpecConfigurationTypeName string |
| 16 | + LowerCamelSpecConfigurationName string |
| 17 | +} |
| 18 | + |
| 19 | +// NewKindTypesTemplateConfig returns a new KindTypesConfig |
| 20 | +func NewKindTypesTemplateConfig(kind, apiVersion, kindSpec string) *KindTypesTemplateConfig { |
| 21 | + |
| 22 | + lowerPlural := getLowerPlural(kind) |
| 23 | + components, _ := getAPIVersionComponents(apiVersion) |
| 24 | + version := components.Version |
| 25 | + kindSpecName := getKindSpecName(kind) |
| 26 | + kindSpecTypeName := getKindSpecTypeName(kind) |
| 27 | + kindSpecLowerCamel := kindToLowerCamel(kind) |
| 28 | + |
| 29 | + // create config and return address |
| 30 | + return &KindTypesTemplateConfig{ |
| 31 | + Kind: kind, |
| 32 | + Version: version, |
| 33 | + LowerPlural: lowerPlural, |
| 34 | + KindSpec: kindSpec, |
| 35 | + SpecConfigurationName: kindSpecName, |
| 36 | + SpecConfigurationTypeName: kindSpecTypeName, |
| 37 | + LowerCamelSpecConfigurationName: kindSpecLowerCamel, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// Execute renders the template and returns the templated string |
| 42 | +func (k *KindTypesTemplateConfig) Execute() (string, error) { |
| 43 | + temp, err := template.New("CRTemplate").Parse(k.GetTemplate()) |
| 44 | + if err != nil { |
| 45 | + return "", err |
| 46 | + } |
| 47 | + |
| 48 | + var wr bytes.Buffer |
| 49 | + err = temp.Execute(&wr, k) |
| 50 | + if err != nil { |
| 51 | + return "", err |
| 52 | + } |
| 53 | + return wr.String(), nil |
| 54 | +} |
| 55 | + |
| 56 | +// GetTemplate returns the necessary template for the kind_types.go |
| 57 | +func (k *KindTypesTemplateConfig) GetTemplate() string { |
| 58 | + return `package {{ .Version }} |
| 59 | + import ( |
| 60 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 61 | + ) |
| 62 | + // EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! |
| 63 | + // NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. |
| 64 | +
|
| 65 | + {{ .KindSpec }} |
| 66 | + |
| 67 | +
|
| 68 | + // {{.Kind}}Spec defines the desired state of {{.Kind}} |
| 69 | + // +k8s:openapi-gen=true |
| 70 | + type {{.Kind}}Spec struct { |
| 71 | + // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster |
| 72 | + // Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file |
| 73 | + // Add custom validation using kubebuilder tags: https://book-v1.book.kubebuilder.io/beyond_basics/generating_crd.html |
| 74 | +
|
| 75 | + {{ .SpecConfigurationName }} {{ .SpecConfigurationTypeName }}` + "`" + `json:"{{ .LowerCamelSpecConfigurationName }},omitempty"` + "`" + ` |
| 76 | + |
| 77 | + } |
| 78 | + // {{.Kind}}Status defines the observed state of {{.Kind}} |
| 79 | + // +k8s:openapi-gen=true |
| 80 | + type {{.Kind}}Status struct { |
| 81 | + // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster |
| 82 | + // Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file |
| 83 | + // Add custom validation using kubebuilder tags: https://book-v1.book.kubebuilder.io/beyond_basics/generating_crd.html |
| 84 | + } |
| 85 | + // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object |
| 86 | + // {{.Kind}} is the Schema for the {{ .LowerPlural }} API |
| 87 | + // +k8s:openapi-gen=true |
| 88 | + // +kubebuilder:subresource:status |
| 89 | + type {{.Kind}} struct { |
| 90 | + metav1.TypeMeta ` + "`" + `json:",inline"` + "`" + ` |
| 91 | + metav1.ObjectMeta ` + "`" + `json:"metadata,omitempty"` + "`" + ` |
| 92 | + Spec {{.Kind}}Spec ` + "`" + `json:"spec,omitempty"` + "`" + ` |
| 93 | + Status {{.Kind}}Status ` + "`" + `json:"status,omitempty"` + "`" + ` |
| 94 | + } |
| 95 | + // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object |
| 96 | + // {{.Kind}}List contains a list of {{.Kind}} |
| 97 | + type {{.Kind}}List struct { |
| 98 | + metav1.TypeMeta ` + "`" + `json:",inline"` + "`" + ` |
| 99 | + metav1.ListMeta ` + "`" + `json:"metadata,omitempty"` + "`" + ` |
| 100 | + Items []{{ .Kind }} ` + "`" + `json:"items"` + "`" + ` |
| 101 | + } |
| 102 | + func init() { |
| 103 | + SchemeBuilder.Register(&{{.Kind}}{}, &{{.Kind}}List{}) |
| 104 | + } |
| 105 | + ` |
| 106 | +} |
0 commit comments