Skip to content

Commit 3ac5778

Browse files
committed
Update the ManifestReader interface and implementations to use Unstructured
1 parent 43b5215 commit 3ac5778

File tree

19 files changed

+448
-207
lines changed

19 files changed

+448
-207
lines changed

cmd/apply/cmdapply.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"sigs.k8s.io/cli-utils/cmd/printers"
1818
"sigs.k8s.io/cli-utils/pkg/apply"
1919
"sigs.k8s.io/cli-utils/pkg/common"
20-
"sigs.k8s.io/cli-utils/pkg/object"
2120
"sigs.k8s.io/cli-utils/pkg/provider"
2221
"sigs.k8s.io/kustomize/kyaml/setters2"
2322
)
@@ -107,7 +106,7 @@ func (r *ApplyRunner) RunE(cmd *cobra.Command, args []string) error {
107106
if err != nil {
108107
return err
109108
}
110-
infos, err := reader.Read()
109+
objs, err := reader.Read()
111110
if err != nil {
112111
return err
113112
}
@@ -117,7 +116,7 @@ func (r *ApplyRunner) RunE(cmd *cobra.Command, args []string) error {
117116
if err := r.Applier.Initialize(); err != nil {
118117
return err
119118
}
120-
ch := r.Applier.Run(context.Background(), object.InfosToUnstructureds(infos), apply.Options{
119+
ch := r.Applier.Run(context.Background(), objs, apply.Options{
121120
ServerSideOptions: r.serverSideOptions,
122121
PollInterval: r.period,
123122
ReconcileTimeout: r.reconcileTimeout,

cmd/destroy/cmddestroy.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"sigs.k8s.io/cli-utils/cmd/printers"
1212
"sigs.k8s.io/cli-utils/pkg/apply"
1313
"sigs.k8s.io/cli-utils/pkg/inventory"
14-
"sigs.k8s.io/cli-utils/pkg/object"
1514
"sigs.k8s.io/cli-utils/pkg/provider"
1615
)
1716

@@ -58,11 +57,11 @@ func (r *DestroyRunner) RunE(cmd *cobra.Command, args []string) error {
5857
if err != nil {
5958
return err
6059
}
61-
infos, err := reader.Read()
60+
objs, err := reader.Read()
6261
if err != nil {
6362
return err
6463
}
65-
inv, _, err := inventory.SplitUnstructureds(object.InfosToUnstructureds(infos))
64+
inv, _, err := inventory.SplitUnstructureds(objs)
6665
if err != nil {
6766
return err
6867
}

cmd/preview/cmdpreview.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"sigs.k8s.io/cli-utils/pkg/apply/event"
1616
"sigs.k8s.io/cli-utils/pkg/common"
1717
"sigs.k8s.io/cli-utils/pkg/inventory"
18-
"sigs.k8s.io/cli-utils/pkg/object"
1918
"sigs.k8s.io/cli-utils/pkg/provider"
2019
"sigs.k8s.io/kustomize/kyaml/setters2"
2120
)
@@ -90,7 +89,7 @@ func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {
9089
if err != nil {
9190
return err
9291
}
93-
infos, err := reader.Read()
92+
objs, err := reader.Read()
9493
if err != nil {
9594
return err
9695
}
@@ -118,14 +117,14 @@ func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {
118117
ForceConflicts: false,
119118
FieldManager: common.DefaultFieldManager,
120119
}
121-
ch = r.Applier.Run(ctx, object.InfosToUnstructureds(infos), apply.Options{
120+
ch = r.Applier.Run(ctx, objs, apply.Options{
122121
EmitStatusEvents: false,
123122
NoPrune: noPrune,
124123
DryRunStrategy: drs,
125124
ServerSideOptions: serverSideOptions,
126125
})
127126
} else {
128-
inv, _, err := inventory.SplitUnstructureds(object.InfosToUnstructureds(infos))
127+
inv, _, err := inventory.SplitUnstructureds(objs)
129128
if err != nil {
130129
return err
131130
}

cmd/status/cmdstatus.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/collector"
2222
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/event"
2323
"sigs.k8s.io/cli-utils/pkg/kstatus/status"
24-
"sigs.k8s.io/cli-utils/pkg/object"
2524
"sigs.k8s.io/cli-utils/pkg/provider"
2625
"sigs.k8s.io/cli-utils/pkg/util/factory"
2726
)
@@ -79,13 +78,13 @@ func (r *StatusRunner) runE(cmd *cobra.Command, args []string) error {
7978
if err != nil {
8079
return err
8180
}
82-
infos, err := reader.Read()
81+
objs, err := reader.Read()
8382
if err != nil {
8483
return err
8584
}
8685

8786
// Find the inventory template among the manifests.
88-
inv, _, err := inventory.SplitUnstructureds(object.InfosToUnstructureds(infos))
87+
inv, _, err := inventory.SplitUnstructureds(objs)
8988
if err != nil {
9089
return err
9190
}

pkg/apply/applier.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func (a *Applier) prepareObjects(objs []*unstructured.Unstructured) (*ResourceOb
105105
// algorithm requires stopping if the merge is not successful. Otherwise,
106106
// the stored objects in inventory could become inconsistent.
107107
pruneIds, err := a.invClient.Merge(localInv, currentObjs)
108+
108109
if err != nil {
109110
return nil, err
110111
}

pkg/apply/info/info_helper.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ func (ih *infoHelper) UpdateInfos(infos []*resource.Info) error {
5656
}
5757

5858
func (ih *infoHelper) BuildInfos(objs []*unstructured.Unstructured) ([]*resource.Info, error) {
59-
infos := object.UnstructuredsToInfos(objs)
60-
err := ih.UpdateInfos(infos)
59+
infos, err := object.UnstructuredsToInfos(objs)
60+
if err != nil {
61+
return nil, err
62+
}
63+
err = ih.UpdateInfos(infos)
6164
if err != nil {
6265
return nil, err
6366
}

pkg/apply/task/apply_task_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,5 +333,5 @@ func (f *fakeInfoHelper) UpdateInfos([]*resource.Info) error {
333333
}
334334

335335
func (f *fakeInfoHelper) BuildInfos(objs []*unstructured.Unstructured) ([]*resource.Info, error) {
336-
return object.UnstructuredsToInfos(objs), nil
336+
return object.UnstructuredsToInfos(objs)
337337
}

pkg/manifestreader/common.go

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
package manifestreader
55

66
import (
7+
"encoding/json"
78
"fmt"
89

910
"k8s.io/apimachinery/pkg/api/meta"
1011
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
11-
"k8s.io/cli-runtime/pkg/resource"
12-
"k8s.io/kubectl/pkg/cmd/util"
1312
"sigs.k8s.io/cli-utils/pkg/apply/solver"
1413
"sigs.k8s.io/cli-utils/pkg/inventory"
15-
"sigs.k8s.io/cli-utils/pkg/object"
1614
"sigs.k8s.io/kustomize/kyaml/kio/filters"
15+
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
16+
"sigs.k8s.io/kustomize/kyaml/yaml"
1717
)
1818

1919
// SetNamespaces verifies that every namespaced resource has the namespace
@@ -22,29 +22,24 @@ import (
2222
// This implementation will check each resource (that doesn't already have
2323
// the namespace set) on whether it is namespace or cluster scoped. It does
2424
// 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,
2727
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
3429

3530
// 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)
3934
}
4035
}
4136

42-
for _, inf := range infos {
43-
accessor, _ := meta.Accessor(inf.Object)
37+
for _, obj := range objs {
38+
accessor, _ := meta.Accessor(obj)
4439

4540
// Exclude any inventory objects here since we don't want to change
4641
// their namespace.
47-
if inventory.IsInventoryObject(object.InfoToUnstructured(inf)) {
42+
if inventory.IsInventoryObject(obj) {
4843
continue
4944
}
5045

@@ -59,7 +54,7 @@ func SetNamespaces(factory util.Factory, infos []*resource.Info,
5954
continue
6055
}
6156

62-
gk := inf.Object.GetObjectKind().GroupVersionKind().GroupKind()
57+
gk := obj.GetObjectKind().GroupVersionKind().GroupKind()
6358
mapping, err := mapper.RESTMapping(gk)
6459

6560
if err != nil && !meta.IsNoMatchError(err) {
@@ -73,7 +68,6 @@ func SetNamespaces(factory util.Factory, infos []*resource.Info,
7368
// This means the resource does not have the namespace set,
7469
// but it is a namespaced resource. So we set the namespace
7570
// to the provided default value.
76-
inf.Namespace = defaultNamespace
7771
accessor.SetNamespace(defaultNamespace)
7872
}
7973
continue
@@ -87,12 +81,11 @@ func SetNamespaces(factory util.Factory, infos []*resource.Info,
8781
// namespace-scoped. If it is the latter, we set the namespace
8882
// to the provided default.
8983
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")
9487
if gk.Kind == kind && gk.Group == group {
95-
scope, _, _ = unstructured.NestedString(u.Object, "spec", "scope")
88+
scope, _, _ = unstructured.NestedString(crdObj.Object, "spec", "scope")
9689
}
9790
}
9891

@@ -102,26 +95,55 @@ func SetNamespaces(factory util.Factory, infos []*resource.Info,
10295
case "Cluster":
10396
continue
10497
case "Namespaced":
105-
inf.Namespace = defaultNamespace
10698
accessor.SetNamespace(defaultNamespace)
10799
}
108100
}
109101

110102
return nil
111103
}
112104

113-
// FilterLocalConfig returns a new slice of infos where all resources
105+
// FilterLocalConfig returns a new slice of Unstructured where all resources
114106
// 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)
119111
// Ignoring the value of the LocalConfigAnnotation here. This is to be
120112
// consistent with the behavior in the kyaml library:
121113
// https://github.com/kubernetes-sigs/kustomize/blob/30b58e90a39485bc5724b2278651c5d26b815cb2/kyaml/kio/filters/local.go#L29
122114
if _, found := acc.GetAnnotations()[filters.LocalConfigAnnotation]; !found {
123-
filterInfos = append(filterInfos, inf)
115+
filteredObjs = append(filteredObjs, obj)
124116
}
125117
}
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
127149
}

0 commit comments

Comments
 (0)