-
Notifications
You must be signed in to change notification settings - Fork 88
/
gvkn.go
70 lines (54 loc) · 1.9 KB
/
gvkn.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
package client
import (
"fmt"
"strings"
"gopkg.in/yaml.v2"
)
type OverlySimpleGVKWithName struct {
APIVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Metadata OverlySimpleMetadata `yaml:"metadata"`
}
type OverlySimpleMetadata struct {
Name string `yaml:"name"`
Namespace string `yaml:"namespace"`
Labels map[string]string `yaml:"labels,omitempty"`
}
func GetGVKWithNameAndNs(content []byte, baseNS string) (string, OverlySimpleGVKWithName) {
o := OverlySimpleGVKWithName{}
if err := yaml.Unmarshal(content, &o); err != nil {
return "", o
}
namespace := baseNS
if o.Metadata.Namespace != "" {
namespace = o.Metadata.Namespace
}
// TODO: this is a hack, find a better way to do this.
// kubernetes does not consider the api version when identifying manifests,
// and it automatically converts the schema when the api version changes for native k8s objects.
// kubernetes doesn't/can't handle automatically converting the schema when the api version changes for CRDs though,
// and vendors would have to implement that themselves using webhook conversion (which is currently pretty complicated).
// for now, include the api version when identifying CRDs so that they will be re-created to apply the new schema.
key := fmt.Sprintf("%s-%s-%s", o.Kind, o.Metadata.Name, namespace)
if IsCRD(content) {
key = fmt.Sprintf("%s-%s", o.APIVersion, key)
}
return key, o
}
func IsCRD(content []byte) bool {
o := OverlySimpleGVKWithName{}
if err := yaml.Unmarshal(content, &o); err != nil {
return false
}
if o.Kind == "CustomResourceDefinition" {
return strings.HasPrefix(o.APIVersion, "apiextensions.k8s.io/")
}
return false
}
func IsNamespace(content []byte) bool {
o := OverlySimpleGVKWithName{}
if err := yaml.Unmarshal(content, &o); err != nil {
return false
}
return o.APIVersion == "v1" && o.Kind == "Namespace"
}