forked from GoogleCloudPlatform/k8s-config-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippetgeneration.go
286 lines (262 loc) · 11.7 KB
/
snippetgeneration.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright 2022 Google LLC
//
// 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 snippetgeneration
import (
"fmt"
"path"
"strings"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/fileutil"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/mapslice"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/repo"
"github.com/ghodss/yaml"
goyaml "gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
// preferredSampleForResource specifies the sample to be used for snippet
// generation for resources that have multiple samples. It is a map of
// 'resource samples directory name' -> 'sample subdirectory name'.
var preferredSampleForResource = map[string]string{
"alloydbcluster": "regular-cluster",
"alloydbinstance": "primary-instance",
"alloydbuser": "database-user",
"bigqueryjob": "query-bigquery-job",
"bigtableappprofile": "multicluster-bigtable-app-profile",
"bigtableinstance": "replicated-instance",
"billingbudgetsbudget": "calendar-budget",
"binaryauthorizationpolicy": "cluster-policy",
"certificatemanagercertificate": "self-managed-certificate",
"cloudbuildtrigger": "build-trigger-for-cloud-source-repo",
"cloudfunctionsfunction": "httpstrigger",
"cloudidentitymembership": "membership-with-manager-role",
"cloudschedulerjob": "scheduler-job-pubsub",
"computehealthcheck": "global-health-check",
"computeaddress": "global-compute-address",
"computebackendbucket": "basic-backend-bucket",
"computebackendservice": "external-load-balancing-backend-service",
"computedisk": "zonal-compute-disk",
"computefirewall": "allow-rule-firewall",
"computefirewallpolicyassociation": "association-with-folder-attachment-target",
"computeforwardingrule": "global-forwarding-rule-with-target-http-proxy",
"computeimage": "image-from-url-raw",
"computeinstance": "cloud-machine-instance",
"computeinstancegroupmanager": "regional-compute-instance-group-manager",
"computenodetemplate": "flexible-node-template",
"computeregionnetworkendpointgroup": "cloud-function-region-network-endpoint-group",
"computereservation": "specialized-compute-reservation",
"computeresourcepolicy": "weekly-resource-policy-schedule",
"computerouternat": "router-nat-for-all-subnets",
"computesecuritypolicy": "multirule-security-policy",
"computesslcertificate": "global-compute-ssl-certificate",
"computesslpolicy": "modern-tls-1-1-ssl-policy",
"computetargethttpsproxy": "target-https-proxy-with-ssl-certificates",
"computeurlmap": "global-compute-url-map",
"configcontrollerinstance": "autopilot-config-controller-instance",
"containerattachedcluster": "container-attached-cluster-basic",
"containercluster": "vpc-native-container-cluster",
"containernodepool": "basic-node-pool",
"dataflowjob": "streaming-dataflow-job",
"dataflowflextemplatejob": "streaming-dataflow-flex-template-job",
"dlpstoredinfotype": "big-query-field-stored-info-type",
"dlpdeidentifytemplate": "info-type-deidentify-template",
"dlpinspecttemplate": "custom-inspect-template",
"dlpjobtrigger": "big-query-job-trigger",
"dnsrecordset": "dns-a-record-set",
"edgecontainercluster": "edgecontainercluster-remote-control-plane",
"folder": "folder-in-folder",
"gkehubfeature": "multi-cluster-ingress-feature",
"gkehubfeaturemembership": "config-management-feature-membership",
"iamauditconfig": "project-level-audit-config",
"iamcustomrole": "project-role",
"iampolicy": "external-project-level-policy",
"iampartialpolicy": "project-level-policy",
"iampolicymember": "external-project-level-policy-member",
"iamworkforcepoolprovider": "oidc-workforce-pool-provider",
"iamworkloadidentitypoolprovider": "oidc-workload-identity-pool-provider",
"logginglogbucket": "project-log-bucket",
"logginglogexclusion": "project-exclusion",
"logginglogmetric": "linear-log-metric",
"logginglogsink": "project-sink",
"logginglogview": "project-log-view",
"monitoringalertpolicy": "network-connectivity-alert-policy",
"monitoringnotificationchannel": "sms-monitoring-notification-channel",
"monitoringservicelevelobjective": "window-based-gtr-distribution-cut",
"monitoringuptimecheckconfig": "http-uptime-check-config",
"osconfigospolicyassignment": "fixed-os-policy-assignment",
"privatecacertificate": "basic-certificate",
"project": "project-in-folder",
"pubsubsubscription": "basic-pubsub-subscription",
"runjob": "basic-job",
"recaptchaenterprisekey": "challenge-based-web-recaptcha-enterprise-key",
"resourcemanagerpolicy": "organization-policy-for-project",
"runservice": "run-service-secret",
"secretmanagersecret": "automatic-secret-replication",
"sqlinstance": "mysql-sql-instance",
"vpcaccessconnector": "cidr-connector",
}
type Snippet struct {
Label string `yaml:"label"`
MarkdownDescription string `yaml:"markdownDescription"`
InsertText string `yaml:"insertText"`
}
// PathToSampleFileUsedForSnippets gets the path to the sample file used to
// generate snippets for the given resource samples directory. Note: the given
// resource samples directory must be a subdirectory of the overall resources
// samples directory at config/samples/resources.
func PathToSampleFileUsedForSnippets(resourceDirName string) (string, error) {
samplesPath := repo.GetResourcesSamplesPath()
resourceDirPath := path.Join(samplesPath, resourceDirName)
dirExists, err := fileutil.DirExists(resourceDirPath)
if err != nil {
return "", fmt.Errorf("error: failed to determine if directory with name %v exists in %v: %w", resourceDirName, samplesPath, err)
}
if !dirExists {
return "", fmt.Errorf("error: no directory with name %v found in %v", resourceDirName, samplesPath)
}
hasSubdirs, err := fileutil.HasSubdirs(resourceDirPath)
if err != nil {
return "", fmt.Errorf("error determining if directory at %v has subdirectories: %w", resourceDirPath, err)
}
sampleDirPath := resourceDirPath
if hasSubdirs {
sampleDirPath, err = pathToPreferredSamplesSubdirForResource(resourceDirPath)
if err != nil {
return "", err
}
}
fileNames, err := fileutil.FileNamesWithSuffixInDir(sampleDirPath, resourceDirName+".yaml")
if err != nil {
return "", fmt.Errorf("error getting files to use for generating snippets: %w", err)
}
if len(fileNames) != 1 {
return "", fmt.Errorf("error getting exactly one file to use for generating snippets (dir=%q, suffix=%q); expected one, got %v", sampleDirPath, resourceDirName+".yaml", fileNames)
}
return path.Join(sampleDirPath, fileNames[0]), nil
}
func pathToPreferredSamplesSubdirForResource(resourceDirPath string) (string, error) {
resourceDirName := path.Base(resourceDirPath)
sampleSubdirName, ok := preferredSampleForResource[resourceDirName]
if !ok {
return "", fmt.Errorf("error: no sample subdirectory specified for resource directory '%v'", resourceDirName)
}
sampleSubdirPath := path.Join(resourceDirPath, sampleSubdirName)
dirExists, err := fileutil.DirExists(sampleSubdirPath)
if err != nil {
return "", fmt.Errorf("error: failed to determine if directory at %v exists: %w", sampleSubdirPath, err)
}
if !dirExists {
return "", fmt.Errorf("error: no directory found at %v", sampleSubdirPath)
}
return sampleSubdirPath, nil
}
func SnippifyResourceConfig(resourceConfig []byte) (Snippet, error) {
kind, err := resourceKind(resourceConfig)
if err != nil {
return Snippet{}, fmt.Errorf("error parsing resource kind from resource config: %w", err)
}
config, err := snippifyResourceConfig(kind, resourceConfig)
if err != nil {
return Snippet{}, fmt.Errorf("error snippifying resource config: %w", err)
}
return Snippet{
Label: "Config Connector " + kind,
MarkdownDescription: fmt.Sprintf("Creates yaml for a %v resource", kind),
InsertText: config,
}, nil
}
func snippifyResourceConfig(kind string, config []byte) (string, error) {
var mapSlice goyaml.MapSlice
err := goyaml.Unmarshal(config, &mapSlice)
if err != nil {
return "", fmt.Errorf("error unmarshalling bytes: %w", err)
}
newMapSlice := goyaml.MapSlice{}
varNum := 1
for _, item := range mapSlice {
switch key := item.Key.(string); key {
case "metadata":
item.Value = snippifyMetadata(item.Value, kind, &varNum)
case "spec":
item.Value = snippifyAllLeavesInTree(item.Value, &varNum)
}
newMapSlice = append(newMapSlice, item)
}
out, err := goyaml.Marshal(newMapSlice)
if err != nil {
return "", fmt.Errorf("error marshalling bytes to YAML: %w", err)
}
return string(out), nil
}
func snippifyMetadata(metadataFields interface{}, kind string, varNum *int) interface{} {
m := metadataFields.(goyaml.MapSlice)
out := goyaml.MapSlice{}
labels := mapslice.Value(m, "labels")
name := mapslice.Value(m, "name")
if labels != nil {
labels := labels.(goyaml.MapSlice)
newLabels := make([]goyaml.MapItem, 0)
for _, l := range labels {
newLabels = append(newLabels, goyaml.MapItem{
Key: snippifyVal(l.Key.(string), varNum),
Value: snippifyVal(l.Value.(string), varNum),
})
}
out = append(out, goyaml.MapItem{
Key: "labels",
Value: newLabels,
})
}
if name != nil {
out = append(out, goyaml.MapItem{
Key: "name",
Value: snippifyVal(strings.ToLower(kind)+"-name", varNum),
})
}
return out
}
func snippifyAllLeavesInTree(node interface{}, varNum *int) interface{} {
switch v := node.(type) {
case goyaml.MapSlice:
out := goyaml.MapSlice{}
for _, item := range v {
item.Value = snippifyAllLeavesInTree(item.Value, varNum)
out = append(out, item)
}
return out
case []interface{}:
out := make([]interface{}, 0)
for _, val := range v {
out = append(out, snippifyAllLeavesInTree(val, varNum))
}
return out
default:
return snippifyVal(fmt.Sprintf("%v", v), varNum)
}
}
func snippifyVal(s string, varNum *int) string {
// Remove or replace characters that have special meaning in snippet strings.
s = strings.ReplaceAll(s, "$", "")
s = strings.ReplaceAll(s, "{", "[")
s = strings.ReplaceAll(s, "}", "]")
v := fmt.Sprintf("\\${%v:%v}", *varNum, s)
*varNum++
return v
}
func resourceKind(config []byte) (string, error) {
u := &unstructured.Unstructured{}
err := yaml.Unmarshal(config, u)
if err != nil {
return "", fmt.Errorf("error unmarshalling bytes to CRD: %w", err)
}
return u.GetKind(), nil
}