cert-manager/hack/filter-crd/main.go
Maartje Eyskens aaae73a45d Replace openshift/no-webhook manifests with legacy
This releases a new manifest type "legacy" to support Kubernetes <1.15 and OpenShift 3.
This version uses the webhook but disables the conversions as they are not supported.
For this reason only the v1alpha2 API is added in these manifests.
All newer APIs are filtered by the filter-crd tool.

Signed-off-by: Maartje Eyskens <maartje@eyskens.me>
2020-03-04 14:26:25 +01:00

185 lines
4.4 KiB
Go

/*
Copyright 2020 The Jetstack cert-manager contributors.
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 main
import (
"flag"
"fmt"
"log"
"os"
"reflect"
"strings"
"gopkg.in/yaml.v2"
)
var removeKeys = []string{}
var removeSliceElemForValue = map[string]string{}
var checkValidationLocation = false
func main() {
loadVariant()
if len(flag.Args()) < 1 {
log.Fatal("Usage: filter-crd <CRD YAML file>")
}
f, err := os.Open(flag.Args()[0])
if err != nil {
log.Fatal("Error opening file: ", err)
}
decoder := yaml.NewDecoder(f)
var d map[interface{}]interface{}
output := []string{}
for decoder.Decode(&d) == nil {
if len(d) == 0 {
continue
}
checkChain(d, []string{})
if checkValidationLocation {
spec, ok := d["spec"].(map[interface{}]interface{})
if !ok {
log.Fatal("Cannot read spec of CRD")
}
versions, ok := spec["versions"].([]interface{})
if !ok {
log.Fatal("Cannot read versions of CRD")
}
if len(versions) == 0 {
log.Fatal("CRD versions length is 0")
}
versionInfo, ok := versions[0].(map[interface{}]interface{})
if !ok {
log.Fatal("Cannot read version of CRD")
}
// move the schema to the root of the CRD as we only have 1 version specified
if validations, exists := versionInfo["schema"]; exists {
spec["validation"] = validations
delete(versionInfo, "schema")
}
}
fileOut, err := yaml.Marshal(d)
if err != nil {
log.Fatal("Error marshaling output: ", err)
}
output = append(output, string(fileOut))
}
fmt.Println(strings.Join(output, "---\n"))
}
func checkChain(d map[interface{}]interface{}, chain []string) {
for k, v := range d {
if key, ok := k.(string); ok {
chain = append(chain, key)
// check if keys need to be removed
for _, removeKey := range removeKeys {
if strings.Join(chain, "/") == removeKey {
delete(d, key)
}
}
// checks if keys need to be removed when a value is set
if value, ok := removeSliceElemForValue[strings.Join(chain, "/")]; ok && value == v.(string) {
// TODO
}
if value, ok := v.(map[interface{}]interface{}); ok {
checkChain(value, chain)
}
if value, ok := v.([]interface{}); ok {
d[k] = checkSliceChain(value, append(chain, "[]"))
}
chain = chain[:len(chain)-1] // we're done with this key, remove it from the chain
}
}
}
func checkSliceChain(s []interface{}, chain []string) []interface{} {
for _, sliceVal := range s {
if d, ok := sliceVal.(map[interface{}]interface{}); ok {
for k, v := range d {
if key, ok := k.(string); ok {
chain = append(chain, key)
// check if keys need to be removed
for _, removeKey := range removeKeys {
if strings.Join(chain, "/") == removeKey {
delete(d, key)
}
}
if value, ok := removeSliceElemForValue[strings.Join(chain, "/")]; ok && value == v.(string) {
newSlice := []interface{}{}
for _, sliceVal := range s {
if !reflect.DeepEqual(sliceVal, d) {
newSlice = append(newSlice, sliceVal)
}
}
s = newSlice
}
if value, ok := v.(map[interface{}]interface{}); ok {
checkChain(value, chain)
}
if value, ok := v.([]interface{}); ok {
d[k] = checkSliceChain(value, append(chain, "[]"))
}
chain = chain[:len(chain)-1] // we're done with this key, remove it from the chain
}
}
}
}
return s
}
func loadVariant() {
variant := ""
flag.StringVar(&variant, "variant", "", "variant of remove rules")
flag.Parse()
if variant == "cert-manager-legacy" {
// These are the keys that the script will remove for OpenShift compatibility
removeKeys = []string{
"spec/preserveUnknownFields",
"spec/validation/openAPIV3Schema/type",
"spec/versions/[]/schema/openAPIV3Schema/type",
"spec/conversion",
}
removeSliceElemForValue = map[string]string{
"spec/versions/[]/name": "v1alpha3",
}
checkValidationLocation = true
}
}