4
4
package manifestreader
5
5
6
6
import (
7
+ "encoding/json"
7
8
"fmt"
8
9
9
10
"k8s.io/apimachinery/pkg/api/meta"
10
11
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
11
- "k8s.io/cli-runtime/pkg/resource"
12
- "k8s.io/kubectl/pkg/cmd/util"
13
12
"sigs.k8s.io/cli-utils/pkg/apply/solver"
14
13
"sigs.k8s.io/cli-utils/pkg/inventory"
15
- "sigs.k8s.io/cli-utils/pkg/object"
16
14
"sigs.k8s.io/kustomize/kyaml/kio/filters"
15
+ "sigs.k8s.io/kustomize/kyaml/kio/kioutil"
16
+ "sigs.k8s.io/kustomize/kyaml/yaml"
17
17
)
18
18
19
19
// SetNamespaces verifies that every namespaced resource has the namespace
@@ -22,29 +22,24 @@ import (
22
22
// This implementation will check each resource (that doesn't already have
23
23
// the namespace set) on whether it is namespace or cluster scoped. It does
24
24
// this by first checking the RESTMapper, and it there is not match there,
25
- // it will look for CRDs in the provided Infos .
26
- func SetNamespaces (factory util. Factory , infos []* resource. Info ,
25
+ // it will look for CRDs in the provided Unstructureds .
26
+ func SetNamespaces (mapper meta. RESTMapper , objs []* unstructured. Unstructured ,
27
27
defaultNamespace string , enforceNamespace bool ) error {
28
- mapper , err := factory .ToRESTMapper ()
29
- if err != nil {
30
- return err
31
- }
32
-
33
- var crdInfos []* resource.Info
28
+ var crdObjs []* unstructured.Unstructured
34
29
35
30
// find any crds in the set of resources.
36
- for _ , inf := range infos {
37
- if solver .IsCRD (object . InfoToUnstructured ( inf ) ) {
38
- crdInfos = append (crdInfos , inf )
31
+ for _ , obj := range objs {
32
+ if solver .IsCRD (obj ) {
33
+ crdObjs = append (crdObjs , obj )
39
34
}
40
35
}
41
36
42
- for _ , inf := range infos {
43
- accessor , _ := meta .Accessor (inf . Object )
37
+ for _ , obj := range objs {
38
+ accessor , _ := meta .Accessor (obj )
44
39
45
40
// Exclude any inventory objects here since we don't want to change
46
41
// their namespace.
47
- if inventory .IsInventoryObject (object . InfoToUnstructured ( inf ) ) {
42
+ if inventory .IsInventoryObject (obj ) {
48
43
continue
49
44
}
50
45
@@ -59,7 +54,7 @@ func SetNamespaces(factory util.Factory, infos []*resource.Info,
59
54
continue
60
55
}
61
56
62
- gk := inf . Object .GetObjectKind ().GroupVersionKind ().GroupKind ()
57
+ gk := obj .GetObjectKind ().GroupVersionKind ().GroupKind ()
63
58
mapping , err := mapper .RESTMapping (gk )
64
59
65
60
if err != nil && ! meta .IsNoMatchError (err ) {
@@ -73,7 +68,6 @@ func SetNamespaces(factory util.Factory, infos []*resource.Info,
73
68
// This means the resource does not have the namespace set,
74
69
// but it is a namespaced resource. So we set the namespace
75
70
// to the provided default value.
76
- inf .Namespace = defaultNamespace
77
71
accessor .SetNamespace (defaultNamespace )
78
72
}
79
73
continue
@@ -87,12 +81,11 @@ func SetNamespaces(factory util.Factory, infos []*resource.Info,
87
81
// namespace-scoped. If it is the latter, we set the namespace
88
82
// to the provided default.
89
83
var scope string
90
- for _ , crdInf := range crdInfos {
91
- u , _ := crdInf .Object .(* unstructured.Unstructured )
92
- group , _ , _ := unstructured .NestedString (u .Object , "spec" , "group" )
93
- kind , _ , _ := unstructured .NestedString (u .Object , "spec" , "names" , "kind" )
84
+ for _ , crdObj := range crdObjs {
85
+ group , _ , _ := unstructured .NestedString (crdObj .Object , "spec" , "group" )
86
+ kind , _ , _ := unstructured .NestedString (crdObj .Object , "spec" , "names" , "kind" )
94
87
if gk .Kind == kind && gk .Group == group {
95
- scope , _ , _ = unstructured .NestedString (u .Object , "spec" , "scope" )
88
+ scope , _ , _ = unstructured .NestedString (crdObj .Object , "spec" , "scope" )
96
89
}
97
90
}
98
91
@@ -102,26 +95,55 @@ func SetNamespaces(factory util.Factory, infos []*resource.Info,
102
95
case "Cluster" :
103
96
continue
104
97
case "Namespaced" :
105
- inf .Namespace = defaultNamespace
106
98
accessor .SetNamespace (defaultNamespace )
107
99
}
108
100
}
109
101
110
102
return nil
111
103
}
112
104
113
- // FilterLocalConfig returns a new slice of infos where all resources
105
+ // FilterLocalConfig returns a new slice of Unstructured where all resources
114
106
// with the LocalConfig annotation is filtered out.
115
- func FilterLocalConfig (infos []* resource. Info ) []* resource. Info {
116
- var filterInfos []* resource. Info
117
- for _ , inf := range infos {
118
- acc , _ := meta .Accessor (inf . Object )
107
+ func FilterLocalConfig (objs []* unstructured. Unstructured ) []* unstructured. Unstructured {
108
+ var filteredObjs []* unstructured. Unstructured
109
+ for _ , obj := range objs {
110
+ acc , _ := meta .Accessor (obj )
119
111
// Ignoring the value of the LocalConfigAnnotation here. This is to be
120
112
// consistent with the behavior in the kyaml library:
121
113
// https://github.com/kubernetes-sigs/kustomize/blob/30b58e90a39485bc5724b2278651c5d26b815cb2/kyaml/kio/filters/local.go#L29
122
114
if _ , found := acc .GetAnnotations ()[filters .LocalConfigAnnotation ]; ! found {
123
- filterInfos = append (filterInfos , inf )
115
+ filteredObjs = append (filteredObjs , obj )
124
116
}
125
117
}
126
- return filterInfos
118
+ return filteredObjs
119
+ }
120
+
121
+ // removeAnnotations removes the specified kioutil annotations from the resource.
122
+ func removeAnnotations (n * yaml.RNode , annotations ... kioutil.AnnotationKey ) error {
123
+ for _ , a := range annotations {
124
+ err := n .PipeE (yaml .ClearAnnotation (a ))
125
+ if err != nil {
126
+ return err
127
+ }
128
+ }
129
+ return nil
130
+ }
131
+
132
+ // kyamlNodeToUnstructured take a resource represented as a kyaml RNode and
133
+ // turns it into an Unstructured object.
134
+ func kyamlNodeToUnstructured (n * yaml.RNode ) (* unstructured.Unstructured , error ) {
135
+ b , err := n .MarshalJSON ()
136
+ if err != nil {
137
+ return nil , err
138
+ }
139
+
140
+ var m map [string ]interface {}
141
+ err = json .Unmarshal (b , & m )
142
+ if err != nil {
143
+ return nil , err
144
+ }
145
+
146
+ return & unstructured.Unstructured {
147
+ Object : m ,
148
+ }, nil
127
149
}
0 commit comments