|
| 1 | +// Copyright 2021 The Operator-SDK Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package config3alphato3 |
| 16 | + |
| 17 | +import ( |
| 18 | + "bufio" |
| 19 | + "bytes" |
| 20 | + "errors" |
| 21 | + "io/ioutil" |
| 22 | + "path" |
| 23 | + "regexp" |
| 24 | + "strings" |
| 25 | + "text/template" |
| 26 | + |
| 27 | + "golang.org/x/mod/modfile" |
| 28 | + "sigs.k8s.io/kubebuilder/v3/pkg/model/resource" |
| 29 | + "sigs.k8s.io/yaml" |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + v3alpha = "3-alpha" |
| 34 | + versionRe = regexp.MustCompile(`version:[ ]*(?:")?3-alpha(?:")?`) |
| 35 | +) |
| 36 | + |
| 37 | +// convertConfig3AlphaTo3 returns cfgBytes converted to 3 iff cfgBytes is version 3-alpha. |
| 38 | +func convertConfig3AlphaTo3(cfgBytes []byte) (_ []byte, err error) { |
| 39 | + cfgObj := make(map[string]interface{}, 5) |
| 40 | + if err := yaml.Unmarshal(cfgBytes, &cfgObj); err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + |
| 44 | + if version, hasVersion := cfgObj["version"]; !hasVersion || version.(string) != v3alpha { |
| 45 | + return cfgBytes, nil |
| 46 | + } |
| 47 | + |
| 48 | + cfgBytes = versionRe.ReplaceAll(cfgBytes, []byte(`version: "3"`)) |
| 49 | + |
| 50 | + var modulePath string |
| 51 | + layout := cfgObj["layout"].(string) |
| 52 | + isGo := strings.HasPrefix(layout, "go.kubebuilder.io/") |
| 53 | + if isGo { |
| 54 | + if modulePath, err = getModulePath(); err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + multigroupObj, hasMultigroup := cfgObj["multigroup"] |
| 60 | + isMultigroup := hasMultigroup && multigroupObj.(bool) |
| 61 | + |
| 62 | + if obj, hasRes := cfgObj["resources"]; hasRes { |
| 63 | + domain := cfgObj["domain"].(string) |
| 64 | + resObjs := obj.([]interface{}) |
| 65 | + resources := make([]resource.Resource, len(resObjs)) |
| 66 | + |
| 67 | + for i, resObj := range resObjs { |
| 68 | + resources[i].Domain = domain |
| 69 | + |
| 70 | + res := resObj.(map[string]interface{}) |
| 71 | + if groupObj, ok := res["group"]; ok { |
| 72 | + resources[i].Group = groupObj.(string) |
| 73 | + } |
| 74 | + if versionObj, ok := res["version"]; ok { |
| 75 | + resources[i].Version = versionObj.(string) |
| 76 | + } |
| 77 | + if kindObj, ok := res["kind"]; ok { |
| 78 | + resources[i].Kind = kindObj.(string) |
| 79 | + } |
| 80 | + |
| 81 | + if isGo { |
| 82 | + // Only Go projects use "resources[*].path". |
| 83 | + var apiPath string |
| 84 | + if isMultigroup { |
| 85 | + apiPath = path.Join("apis", resources[i].Group, resources[i].Version) |
| 86 | + } else { |
| 87 | + apiPath = path.Join("api", resources[i].Version) |
| 88 | + } |
| 89 | + resources[i].Path = path.Join(modulePath, apiPath) |
| 90 | + } |
| 91 | + |
| 92 | + // Only set "resources[i].api" if "crdVersion" is present. Otherwise there is |
| 93 | + // likely no API defined by the project. |
| 94 | + if crdVersionObj, ok := res["crdVersion"]; ok { |
| 95 | + resources[i].API = &resource.API{ |
| 96 | + CRDVersion: crdVersionObj.(string), |
| 97 | + } |
| 98 | + } else if k8sDomain, found := coreGroups[resources[i].Group]; found { |
| 99 | + // Core type case. |
| 100 | + resources[i].Domain = k8sDomain |
| 101 | + resources[i].Path = path.Join("k8s.io", "api", resources[i].Group, resources[i].Version) |
| 102 | + } |
| 103 | + // Only set "resources[i].webhooks" if "webhookVersion" is present. Otherwise there is |
| 104 | + // likely no webhook defined by the project. |
| 105 | + if whVersionObj, ok := res["webhookVersion"]; ok { |
| 106 | + resources[i].Webhooks = &resource.Webhooks{ |
| 107 | + WebhookVersion: whVersionObj.(string), |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + out := bytes.Buffer{} |
| 113 | + t := template.Must(template.New("").Parse(tmpl)) |
| 114 | + if err := t.Execute(&out, resources); err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + |
| 118 | + // Scan for resources, then replace only reources to preserve comments/order of the rest of PROJECT. |
| 119 | + scanner := bufio.NewScanner(bytes.NewBuffer(cfgBytes)) |
| 120 | + start, end := -1, len(cfgBytes) |
| 121 | + for scanner.Scan() { |
| 122 | + text := scanner.Text() |
| 123 | + if strings.HasPrefix(text, "resources:") { |
| 124 | + start = bytes.Index(cfgBytes, []byte("resources:")) |
| 125 | + continue |
| 126 | + } |
| 127 | + if start != -1 && strings.Contains(text, ":") && !strings.HasPrefix(text, " ") { |
| 128 | + key := strings.TrimRightFunc(text, func(c rune) bool { |
| 129 | + return c != ':' |
| 130 | + }) |
| 131 | + if _, hasKey := cfgObj[strings.TrimSuffix(key, ":")]; hasKey { |
| 132 | + end = bytes.Index(cfgBytes, []byte(text)) |
| 133 | + break |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if start == -1 { |
| 139 | + return nil, errors.New("internal error: resources key not found in scanner") |
| 140 | + } |
| 141 | + cfgBytes = append(cfgBytes[:start], append(out.Bytes(), cfgBytes[end:]...)...) |
| 142 | + } |
| 143 | + |
| 144 | + return cfgBytes, nil |
| 145 | +} |
| 146 | + |
| 147 | +// Make this a var so it can be mocked in tests. |
| 148 | +var getModulePath = func() (string, error) { |
| 149 | + b, err := ioutil.ReadFile("go.mod") |
| 150 | + return modfile.ModulePath(b), err |
| 151 | +} |
| 152 | + |
| 153 | +// Comment-heavy "resources" template. |
| 154 | +const tmpl = `resources: |
| 155 | +{{- range $i, $res := . }} |
| 156 | +-{{- if $res.API }} api: |
| 157 | + {{- if $res.API.CRDVersion }} |
| 158 | + crdVersion: {{ $res.API.CRDVersion }} |
| 159 | + {{- else }} |
| 160 | + # TODO(user): Change this API's CRD version if not v1. |
| 161 | + crdVersion: v1 |
| 162 | + {{- end }} |
| 163 | + # TODO(user): Uncomment the below line if this resource's CRD is namespace scoped, else delete it. |
| 164 | + # namespaced: true |
| 165 | + {{- end }} |
| 166 | + # TODO(user): Uncomment the below line if this resource implements a controller, else delete it. |
| 167 | + # controller: true |
| 168 | + {{- if $res.Domain }} |
| 169 | + domain: {{ $res.Domain }} |
| 170 | + {{- end }} |
| 171 | + group: {{ $res.GVK.Group }} |
| 172 | + kind: {{ $res.GVK.Kind }} |
| 173 | + {{- if $res.Path }} |
| 174 | + # TODO(user): Update the package path for your API if the below value is incorrect. |
| 175 | + path: {{ $res.Path }} |
| 176 | + {{- end }} |
| 177 | + version: {{ $res.GVK.Version }} |
| 178 | + {{- if $res.Webhooks }} |
| 179 | + webhooks: |
| 180 | + # TODO(user): Uncomment the below line if this resource's webhook implements a conversion webhook, else delete it. |
| 181 | + # conversion: true |
| 182 | + # TODO(user): Uncomment the below line if this resource's webhook implements a defaulting webhook, else delete it. |
| 183 | + # defaulting: true |
| 184 | + # TODO(user): Uncomment the below line if this resource's webhook implements a validating webhook, else delete it. |
| 185 | + # validation: true |
| 186 | + {{- if $res.Webhooks.WebhookVersion }} |
| 187 | + webhookVersion: {{ $res.Webhooks.WebhookVersion }} |
| 188 | + {{- else }} |
| 189 | + # TODO(user): Change this API's webhook configuration version to the correct version if not v1. |
| 190 | + webhookVersion: v1 |
| 191 | + {{- end }} |
| 192 | + {{- end }} |
| 193 | +{{- end }} |
| 194 | +` |
| 195 | + |
| 196 | +// coreGroups maps a native k8s group to its domain. Several do not have a domain. |
| 197 | +var coreGroups = map[string]string{ |
| 198 | + "admission": "k8s.io", |
| 199 | + "admissionregistration": "k8s.io", |
| 200 | + "apps": "", |
| 201 | + "auditregistration": "k8s.io", |
| 202 | + "apiextensions": "k8s.io", |
| 203 | + "authentication": "k8s.io", |
| 204 | + "authorization": "k8s.io", |
| 205 | + "autoscaling": "", |
| 206 | + "batch": "", |
| 207 | + "certificates": "k8s.io", |
| 208 | + "coordination": "k8s.io", |
| 209 | + "core": "", |
| 210 | + "events": "k8s.io", |
| 211 | + "extensions": "", |
| 212 | + "imagepolicy": "k8s.io", |
| 213 | + "networking": "k8s.io", |
| 214 | + "node": "k8s.io", |
| 215 | + "metrics": "k8s.io", |
| 216 | + "policy": "", |
| 217 | + "rbac.authorization": "k8s.io", |
| 218 | + "scheduling": "k8s.io", |
| 219 | + "setting": "k8s.io", |
| 220 | + "storage": "k8s.io", |
| 221 | +} |
0 commit comments