Use apiextension v1

- upgrade from apiextension v1beta1 to v1
 - generate yaml manifest for crd intead of applying it at runtime
  - users will have to apply the manifest with kubectl
 - kg and kgctl log an error if the crd is not present
 - now validation should actually work

Signed-off-by: leonnicolas <leonloechner@gmx.de>
This commit is contained in:
leonnicolas
2021-06-14 09:08:46 +02:00
parent e272d725a5
commit 36643b77b4
584 changed files with 50911 additions and 55838 deletions

View File

@@ -0,0 +1,61 @@
/*
Copyright 2019 The Kubernetes 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 yaml
import (
"encoding/json"
"fmt"
"gopkg.in/yaml.v3"
)
// ToYAML converts some object that serializes to JSON into a YAML node tree.
// It's useful since it pays attention to JSON tags, unlike yaml.Unmarshal or
// yaml.Node.Decode.
func ToYAML(rawObj interface{}) (*yaml.Node, error) {
if rawObj == nil {
return &yaml.Node{Kind: yaml.ScalarNode, Value: "null", Tag: "!!null"}, nil
}
rawJSON, err := json.Marshal(rawObj)
if err != nil {
return nil, fmt.Errorf("failed to marshal object: %w", err)
}
var out yaml.Node
if err := yaml.Unmarshal(rawJSON, &out); err != nil {
return nil, fmt.Errorf("unable to unmarshal marshalled object: %w", err)
}
return &out, nil
}
// changeAll calls the given callback for all nodes in
// the given YAML node tree.
func changeAll(root *yaml.Node, cb func(*yaml.Node)) {
cb(root)
for _, child := range root.Content {
changeAll(child, cb)
}
}
// SetStyle sets the style for all nodes in the given
// node tree to the given style.
func SetStyle(root *yaml.Node, style yaml.Style) {
changeAll(root, func(node *yaml.Node) {
node.Style = style
})
}

View File

@@ -0,0 +1,87 @@
/*
Copyright 2019 The Kubernetes 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 yaml
import (
"fmt"
"gopkg.in/yaml.v3"
)
// ValueInMapping finds the value node with the corresponding string key
// in the given mapping node. If the given node is not a mapping, an
// error will be returned.
func ValueInMapping(root *yaml.Node, key string) (*yaml.Node, error) {
if root.Kind != yaml.MappingNode {
return nil, fmt.Errorf("unexpected non-mapping node")
}
for i := 0; i < len(root.Content)/2; i++ {
keyNode := root.Content[i*2]
if keyNode.Value == key {
return root.Content[i*2+1], nil
}
}
return nil, nil
}
// asCloseAsPossible goes as deep on the given path as possible, returning the
// last node that existed from the given path in the given tree of mapping
// nodes, as well as the rest of the path that could not be fetched, if any.
func asCloseAsPossible(root *yaml.Node, path ...string) (*yaml.Node, []string, error) {
if root == nil {
return nil, path, nil
}
if root.Kind == yaml.DocumentNode && len(root.Content) > 0 {
root = root.Content[0]
}
currNode := root
for ; len(path) > 0; path = path[1:] {
if currNode.Kind != yaml.MappingNode {
return nil, nil, fmt.Errorf("unexpected non-mapping (%v) before path %v", currNode.Kind, path)
}
nextNode, err := ValueInMapping(currNode, path[0])
if err != nil {
return nil, nil, fmt.Errorf("unable to get next node in path %v: %w", path, err)
}
if nextNode == nil {
// we're as close as possible
break
}
currNode = nextNode
}
return currNode, path, nil
}
// GetNode gets the node at the given path in the given sequence of mapping
// nodes, or, if it doesn't exist, returning false.
func GetNode(root *yaml.Node, path ...string) (*yaml.Node, bool, error) {
resNode, restPath, err := asCloseAsPossible(root, path...)
if err != nil {
return nil, false, err
}
// more path means the node didn't exist
if len(restPath) != 0 {
return nil, false, nil
}
return resNode, true, nil
}

View File

@@ -0,0 +1,80 @@
/*
Copyright 2019 The Kubernetes 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 yaml
import (
"fmt"
"gopkg.in/yaml.v3"
)
// SetNode sets the given path to the given yaml Node, creating mapping nodes along the way.
func SetNode(root *yaml.Node, val yaml.Node, path ...string) error {
currNode, path, err := asCloseAsPossible(root, path...)
if err != nil {
return err
}
if len(path) > 0 {
if currNode.Kind != yaml.MappingNode {
return fmt.Errorf("unexpected non-mapping before path %v", path)
}
for ; len(path) > 0; path = path[1:] {
keyNode := yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Style: yaml.DoubleQuotedStyle, Value: path[0]}
nextNode := &yaml.Node{Kind: yaml.MappingNode}
currNode.Content = append(currNode.Content, &keyNode, nextNode)
currNode = nextNode
}
}
*currNode = val
return nil
}
// DeleteNode deletes the node at the given path in the given tree of mapping nodes.
// It's a noop if the path doesn't exist.
func DeleteNode(root *yaml.Node, path ...string) error {
if len(path) == 0 {
return fmt.Errorf("must specify a path to delete")
}
pathToParent, keyToDelete := path[:len(path)-1], path[len(path)-1]
parentNode, path, err := asCloseAsPossible(root, pathToParent...)
if err != nil {
return err
}
if len(path) > 0 {
// no-op, parent node doesn't exist
return nil
}
if parentNode.Kind != yaml.MappingNode {
return fmt.Errorf("unexpected non-mapping node")
}
for i := 0; i < len(parentNode.Content)/2; i++ {
keyNode := parentNode.Content[i*2]
if keyNode.Value == keyToDelete {
parentNode.Content = append(parentNode.Content[:i*2], parentNode.Content[i*2+2:]...)
return nil
}
}
// no-op, key not found in parent node
return nil
}