/
crd.go
107 lines (96 loc) · 3.26 KB
/
crd.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
// Copyright 2020 The Operator-SDK Authors
//
// 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 (
"fmt"
"path/filepath"
"github.com/kr/text"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
)
var _ machinery.Template = &CRD{}
// CRD scaffolds a manifest for CRD sample.
type CRD struct {
machinery.TemplateMixin
machinery.ResourceMixin
}
// SetTemplateDefaults implements machinery.Template
func (f *CRD) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("config", "crd", "bases", fmt.Sprintf("%s_%%[plural].yaml", f.Resource.QualifiedGroup()))
}
f.Path = f.Resource.Replacer().Replace(f.Path)
f.IfExistsAction = machinery.Error
f.TemplateBody = fmt.Sprintf(crdTemplate,
text.Indent(openAPIV3SchemaTemplate, " "),
text.Indent(openAPIV3SchemaTemplate, " "),
)
return nil
}
const crdTemplate = `---
apiVersion: apiextensions.k8s.io/{{ .Resource.API.CRDVersion }}
kind: CustomResourceDefinition
metadata:
name: {{ .Resource.Plural }}.{{ .Resource.QualifiedGroup }}
spec:
group: {{ .Resource.QualifiedGroup }}
names:
kind: {{ .Resource.Kind }}
listKind: {{ .Resource.Kind }}List
plural: {{ .Resource.Plural }}
singular: {{ .Resource.Kind | lower }}
scope: Namespaced
{{- if eq .Resource.API.CRDVersion "v1beta1" }}
subresources:
status: {}
validation:
%s
{{- end }}
versions:
- name: {{ .Resource.Version }}
{{- if eq .Resource.API.CRDVersion "v1" }}
schema:
%s
{{- end }}
served: true
storage: true
{{- if eq .Resource.API.CRDVersion "v1" }}
subresources:
status: {}
{{- end }}
`
const openAPIV3SchemaTemplate = `openAPIV3Schema:
description: {{ .Resource.Kind }} is the Schema for the {{ .Resource.Plural }} API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: Spec defines the desired state of {{ .Resource.Kind }}
type: object
x-kubernetes-preserve-unknown-fields: true
status:
description: Status defines the observed state of {{ .Resource.Kind }}
type: object
x-kubernetes-preserve-unknown-fields: true
type: object
`