/
doc.go
200 lines (178 loc) · 4.87 KB
/
doc.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Package v1beta1 contains API Schema definitions for the tf v1beta1 API group
// +k8s:deepcopy-gen=package,register
// +groupName=tf.galleybytes.com
package v1beta1
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"strings"
"text/template"
"github.com/go-openapi/jsonreference"
"k8s.io/kube-openapi/pkg/common"
"k8s.io/kube-openapi/pkg/validation/spec"
)
var pkgpath = "github.com/galleybytes/terraform-operator/pkg/apis/tf/v1beta1"
// +kubebuilder:object:generate=false
type Definition struct {
Ref spec.Ref `json:"ref"`
Name string `json:"name"`
Kind string `json:"api"`
Group string
Version string
OpenAPIDefinition common.OpenAPIDefinition `json:"openAPIDefinition"`
}
// +kubebuilder:object:generate=false
type GenValues struct {
Definitions map[string][]Definition `json:"definitions"`
}
// +kubebuilder:object:generate=false
type Api struct {
Name string
Version string
Group string
}
func (a Api) String() string {
return fmt.Sprintf("%s %s %s", a.Name, a.Version, a.Group)
}
func Generate(templatefile, outputfile string) {
genValues := GenValues{
Definitions: make(map[string][]Definition),
}
var callback common.ReferenceCallback = func(path string) spec.Ref {
ref, err := jsonreference.New(path)
if err != nil {
log.Fatal(err)
}
return spec.Ref{
Ref: ref,
}
}
openAPIDefinitions := GetOpenAPIDefinitions(callback)
for rawpath, openAPIDefinition := range openAPIDefinitions {
if !isDependedOn(rawpath, openAPIDefinitions) {
name := strings.Replace(rawpath, pkgpath+".", "", 1)
api := Api{
Name: name,
Version: SchemeGroupVersion.Version,
Group: SchemeGroupVersion.Group,
}
genValues.Definitions[api.String()] = []Definition{
{
Name: api.String(),
OpenAPIDefinition: openAPIDefinition,
Kind: api.Name,
Group: api.Group,
Version: api.Version,
},
}
}
}
// For every independent item, generate a new list
for api, v := range genValues.Definitions {
var definitions []Definition = v
dependencies := []string{api}
for {
addedNewDeps := false
for _, definition := range v {
for _, dependency := range definition.OpenAPIDefinition.Dependencies {
name := strings.Replace(dependency, pkgpath+".", "", 1)
api := Api{
Name: name,
Version: SchemeGroupVersion.Version,
Group: SchemeGroupVersion.Group,
}
if shouldAppendDependency(api.String(), dependencies) {
if _, found := openAPIDefinitions[dependency]; !found {
continue
}
addedNewDeps = true
dependencies = append(dependencies, api.String())
// Strip the pkg name from the type
definitions = append(definitions, Definition{
Name: api.String(),
OpenAPIDefinition: openAPIDefinitions[dependency],
})
}
}
}
v = definitions
if !addedNewDeps {
break
}
}
genValues.Definitions[api] = definitions
}
tplfileb, err := ioutil.ReadFile(templatefile)
if err != nil {
log.Panic(err)
}
tplfile := string(tplfileb)
tpl := template.Must(template.New("").Funcs(Custom).Parse(tplfile))
var buf bytes.Buffer
err = tpl.Execute(&buf, genValues)
if err != nil {
log.Panic(err)
}
err = ioutil.WriteFile(outputfile, buf.Bytes(), 0655)
if err != nil {
log.Panic(err)
}
log.Printf("%s written\n", outputfile)
}
func shouldAppendDependency(dep string, deps []string) bool {
for _, d := range deps {
if dep == d {
return false
}
}
return true
}
func isDependedOn(name string, openAPIDefinition map[string]common.OpenAPIDefinition) bool {
for _, item := range openAPIDefinition {
for _, dependency := range item.Dependencies {
if dependency == name {
return true
}
}
}
return false
}
var Custom = map[string]interface{}{
"replace": replace,
"reflink": reflink,
"refname": refname,
}
func replace(old, new, src string) string {
return strings.Replace(src, old, new, -1)
}
func refname(s spec.Schema) string {
apipath := s.Ref.String()
if strings.Contains(apipath, pkgpath) {
return strings.Replace(apipath, pkgpath+".", "", 1)
}
return apipath
}
func reflink(s spec.Schema) string {
apipath := s.Ref.String()
if strings.Contains(apipath, pkgpath) {
apipath = strings.Replace(apipath, pkgpath+".", "", 1)
api := Api{
Name: apipath,
Version: SchemeGroupVersion.Version,
Group: SchemeGroupVersion.Group,
}
return "#" + api.String()
}
if strings.Contains(apipath, "k8s.io") {
// Make an assumption about the pkg link if host is k8s.io
resourceName := strings.Split(apipath, ".")[len(strings.Split(apipath, "."))-1]
path := strings.Split(apipath, ".")[0 : len(strings.Split(apipath, "."))-1]
return "https://pkg.go.dev/" + strings.Join(path, ".") + "#" + resourceName
}
return "#" + apipath
}
func typeOf(src interface{}) string {
return fmt.Sprintf("%T", src)
}