kuma
183 строки · 4.1 Кб
1package cmd
2
3import (
4"os"
5"path/filepath"
6"text/template"
7
8"github.com/spf13/cobra"
9
10"github.com/kumahq/kuma/tools/policy-gen/generator/pkg/parse"
11"github.com/kumahq/kuma/tools/policy-gen/generator/pkg/save"
12)
13
14func newCoreResource(rootArgs *args) *cobra.Command {
15cmd := &cobra.Command{
16Use: "core-resource",
17Short: "Generate a core model resource for the policy",
18Long: "Generate a core model resource for the policy.",
19RunE: func(cmd *cobra.Command, _ []string) error {
20policyName := filepath.Base(rootArgs.pluginDir)
21policyPath := filepath.Join(rootArgs.pluginDir, "api", rootArgs.version, policyName+".go")
22if _, err := os.Stat(policyPath); err != nil {
23return err
24}
25
26pconfig, err := parse.Policy(policyPath)
27if err != nil {
28return err
29}
30
31outPath := filepath.Join(filepath.Dir(policyPath), "zz_generated.resource.go")
32return save.GoTemplate(resourceTemplate, pconfig, outPath)
33},
34}
35
36return cmd
37}
38
39// resourceTemplate for creating a Kuma resource.
40var resourceTemplate = template.Must(template.New("resource").Parse(`
41// Generated by tools/policy-gen.
42// Run "make generate" to update this file.
43
44// nolint:whitespace
45package {{.Package}}
46
47import (
48_ "embed"
49"fmt"
50
51"k8s.io/kube-openapi/pkg/validation/spec"
52"sigs.k8s.io/yaml"
53
54"github.com/kumahq/kuma/pkg/core/resources/model"
55)
56
57//go:embed schema.yaml
58var rawSchema []byte
59
60func init() {
61var schema spec.Schema
62if err := yaml.Unmarshal(rawSchema, &schema); err != nil {
63panic(err)
64}
65rawSchema = nil
66{{.Name}}ResourceTypeDescriptor.Schema = &schema
67}
68
69const (
70{{.Name}}Type model.ResourceType = "{{.Name}}"
71)
72
73var _ model.Resource = &{{.Name}}Resource{}
74
75type {{.Name}}Resource struct {
76Meta model.ResourceMeta
77Spec *{{.Name}}
78}
79
80func New{{.Name}}Resource() *{{.Name}}Resource {
81return &{{.Name}}Resource{
82Spec: &{{.Name}}{},
83}
84}
85
86func (t *{{.Name}}Resource) GetMeta() model.ResourceMeta {
87return t.Meta
88}
89
90func (t *{{.Name}}Resource) SetMeta(m model.ResourceMeta) {
91t.Meta = m
92}
93
94func (t *{{.Name}}Resource) GetSpec() model.ResourceSpec {
95return t.Spec
96}
97
98func (t *{{.Name}}Resource) SetSpec(spec model.ResourceSpec) error {
99protoType, ok := spec.(*{{.Name}})
100if !ok {
101return fmt.Errorf("invalid type %T for Spec", spec)
102} else {
103if protoType == nil {
104t.Spec = &{{.Name}}{}
105} else {
106t.Spec = protoType
107}
108return nil
109}
110}
111
112func (t *{{.Name}}Resource) Descriptor() model.ResourceTypeDescriptor {
113return {{.Name}}ResourceTypeDescriptor
114}
115
116func (t *{{.Name}}Resource) Validate() error {
117if v, ok := interface{}(t).(interface{ validate() error }); !ok {
118return nil
119} else {
120return v.validate()
121}
122}
123
124var _ model.ResourceList = &{{.Name}}ResourceList{}
125
126type {{.Name}}ResourceList struct {
127Items []*{{.Name}}Resource
128Pagination model.Pagination
129}
130
131func (l *{{.Name}}ResourceList) GetItems() []model.Resource {
132res := make([]model.Resource, len(l.Items))
133for i, elem := range l.Items {
134res[i] = elem
135}
136return res
137}
138
139func (l *{{.Name}}ResourceList) GetItemType() model.ResourceType {
140return {{.Name}}Type
141}
142
143func (l *{{.Name}}ResourceList) NewItem() model.Resource {
144return New{{.Name}}Resource()
145}
146
147func (l *{{.Name}}ResourceList) AddItem(r model.Resource) error {
148if trr, ok := r.(*{{.Name}}Resource); ok {
149l.Items = append(l.Items, trr)
150return nil
151} else {
152return model.ErrorInvalidItemType((*{{.Name}}Resource)(nil), r)
153}
154}
155
156func (l *{{.Name}}ResourceList) GetPagination() *model.Pagination {
157return &l.Pagination
158}
159
160func (l *{{.Name}}ResourceList) SetPagination(p model.Pagination) {
161l.Pagination = p
162}
163
164var {{.Name}}ResourceTypeDescriptor = model.ResourceTypeDescriptor{
165Name: {{.Name}}Type,
166Resource: New{{.Name}}Resource(),
167ResourceList: &{{.Name}}ResourceList{},
168Scope: model.ScopeMesh,
169KDSFlags: model.GlobalToAllZonesFlag | model.ZoneToGlobalFlag,
170WsPath: "{{.Path}}",
171KumactlArg: "{{index .AlternativeNames 0}}",
172KumactlListArg: "{{.Path}}",
173AllowToInspect: true,
174IsPolicy: true,
175IsExperimental: false,
176SingularDisplayName: "{{.SingularDisplayName}}",
177PluralDisplayName: "{{.PluralDisplayName}}",
178IsPluginOriginated: true,
179IsTargetRefBased: true,
180HasToTargetRef: {{.HasTo}},
181HasFromTargetRef: {{.HasFrom}},
182}
183`))
184