/
crd.go
163 lines (143 loc) · 4.32 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
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
// Copyright 2017 Istio 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 cmd
import (
"bytes"
"sort"
gotemplate "text/template"
"github.com/spf13/cobra"
"istio.io/istio/mixer/cmd/shared"
"istio.io/istio/mixer/pkg/adapter"
"istio.io/istio/mixer/pkg/runtime/config/constant"
"istio.io/istio/mixer/pkg/template"
)
// Group is the K8s API group.
const Group = "config.istio.io"
// Version is the K8s API version.
const Version = "v1alpha2"
func crdCmd(tmplInfos map[string]template.Info, adapters []adapter.InfoFn, printf, fatalf shared.FormatFn) *cobra.Command {
adapterCmd := cobra.Command{
Use: "crd",
Short: "CRDs (CustomResourceDefinition) available in Mixer",
Deprecated: "CRDs are no longer auto-generated by Mixer",
}
adapterCmd.AddCommand(&cobra.Command{
Use: "all",
Short: "List all CRDs",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
printCrd(printf, fatalf, constant.RulesKind, "istio.io.mixer", "core")
printCrd(printf, fatalf, constant.AttributeManifestKind, "istio.io.mixer", "core")
listCrdsAdapters(printf, fatalf, adapters)
listCrdsInstances(printf, fatalf, tmplInfos)
},
})
adapterCmd.AddCommand(&cobra.Command{
Use: "adapter",
Short: "List CRDs for available adapters",
Run: func(cmd *cobra.Command, args []string) {
listCrdsAdapters(printf, fatalf, adapters)
},
})
adapterCmd.AddCommand(&cobra.Command{
Use: "instance",
Short: "List CRDs for available instance kinds (mesh functions)",
Run: func(cmd *cobra.Command, args []string) {
listCrdsInstances(printf, fatalf, tmplInfos)
},
})
return &adapterCmd
}
func listCrdsAdapters(printf, fatalf shared.FormatFn, infoFns []adapter.InfoFn) {
for _, infoFn := range infoFns {
info := infoFn()
shrtName := info.Name /* TODO make this info.shortName when related PR is in. */
// TODO : Use the plural name from the adapter info
printCrd(printf, fatalf, shrtName, info.Name, "mixer-adapter")
}
}
func listCrdsInstances(printf, fatalf shared.FormatFn, infos map[string]template.Info) {
tmplNames := make([]string, 0, len(infos))
for name := range infos {
tmplNames = append(tmplNames, name)
}
sort.Strings(tmplNames)
for _, tmplName := range tmplNames {
info := infos[tmplName]
// TODO : Use the plural name from the template info
printCrd(printf, fatalf, info.Name, info.Impl, "mixer-instance")
}
}
type crdVar struct {
ShrtName string
ImplName string
PluralName string
Label string
Name string
Group string
Version string
}
// pluralize gives a plural the way k8s like it.
// https://github.com/kubernetes/gengo/blob/master/namer/plural_namer.go
func pluralize(singular string) string {
switch string(singular[len(singular)-1]) {
case "s", "x":
return singular + "es"
case "y":
return singular[:len(singular)-1] + "ies"
default:
return singular + "s"
}
}
func newCrdVar(shrtName, implName, label string) *crdVar {
plural := pluralize(shrtName)
return &crdVar{
ShrtName: shrtName,
ImplName: implName,
PluralName: plural,
Label: label,
Name: plural + "." + Group,
Group: Group,
Version: Version,
}
}
func printCrd(printf, fatalf shared.FormatFn, shrtName, implName, label string) {
crdTemplate := `kind: CustomResourceDefinition
apiVersion: apiextensions.k8s.io/v1beta1
metadata:
name: {{.Name}}
labels:
package: {{.ImplName}}
istio: {{.Label}}
spec:
group: {{.Group}}
names:
kind: {{.ShrtName}}
plural: {{.PluralName}}
singular: {{.ShrtName}}
categories:
- istio-io
- policy-istio-io
scope: Namespaced
version: {{.Version}}
---
`
t := gotemplate.New("crd")
w := &bytes.Buffer{}
t, _ = t.Parse(crdTemplate)
if err := t.Execute(w, newCrdVar(shrtName, implName, label)); err != nil {
fatalf("Could not create CRD " + err.Error())
}
printf(w.String())
}