@@ -22,13 +22,13 @@ import (
2222 "k8s.io/apimachinery/pkg/util/sets"
2323)
2424
25- func SetNamespace (rl * fn.ResourceList ) error {
25+ func SetNamespace (rl * fn.ResourceList ) ( bool , error ) {
2626 tc := NamespaceTransformer {}
2727 // Get "namespace" arguments from FunctionConfig
2828 err := tc .Config (rl .FunctionConfig )
2929 if err != nil {
3030 rl .Results = append (rl .Results , fn .ErrorConfigObjectResult (err , rl .FunctionConfig ))
31- return nil
31+ return true , nil
3232 }
3333 // Update "namespace" to the proper resources.
3434 tc .Transform (rl .Items )
@@ -40,7 +40,7 @@ func SetNamespace(rl *fn.ResourceList) error {
4040 result = fn .GeneralResult ("namespace updated" , fn .Info )
4141 }
4242 rl .Results = append (rl .Results , result )
43- return nil
43+ return true , nil
4444}
4545
4646// Config gets the attributes from different FunctionConfig formats.
@@ -84,43 +84,42 @@ func (p *NamespaceTransformer) Transform(objects []*fn.KubeObject) {
8484 }
8585}
8686
87- // VisitNamespaces iterates the `objects` to execute the `callback ` function on each corresponding namespace field.
88- func VisitNamespaces (objects []* fn.KubeObject , callback func (ns * Namespace )) {
87+ // VisitNamespaces iterates the `objects` to execute the `visitor ` function on each corresponding namespace field.
88+ func VisitNamespaces (objects []* fn.KubeObject , visitor func (namespace * Namespace )) {
8989 for _ , o := range objects {
9090 switch {
9191 // Skip local resource which `kpt live apply` skips.
9292 case o .IsLocalConfig ():
9393 continue
9494 case o .IsGVK ("v1" , "Namespace" ):
95- ns := NewNamespace (o , []string {"metadata" , "name" })
96- callback (ns )
95+ namespace := o .GetStringOrDie ("metadata" , "name" )
96+ visitor (NewNamespace (o , & namespace ))
97+ o .SetOrDie (& namespace , "metadata" , "name" )
9798 case o .IsGVK ("apiextensions.k8s.io/v1" , "CustomResourceDefinition" ):
98- fields := []string {"spec" , "conversion" , "webhook" , "clientConfig" , "service" , "namespace" }
99- callback (NewNamespace (o , fields ))
99+ namespace := o .GetStringOrDie ("spec" , "conversion" , "webhook" , "clientConfig" , "service" , "namespace" )
100+ visitor (NewNamespace (o , & namespace ))
101+ o .SetOrDie (& namespace , "spec" , "conversion" , "webhook" , "clientConfig" , "service" , "namespace" )
100102 case o .IsGVK ("apiregistration.k8s.io/v1" , "APIService" ):
101- fields := [] string { "spec" , "service" , "namespace" }
102- callback (NewNamespace (o , fields ))
103- /* TODO(yuwenma): waiting for the new features in kpt-functions-sdk to merge in.
103+ namespace := o . GetStringOrDie ( "spec" , "service" , "namespace" )
104+ visitor (NewNamespace (o , & namespace ))
105+ o . SetOrDie ( & namespace , "spec" , "service" , "namespace" )
104106 case o .GetKind () == "ClusterRoleBinding" || o .GetKind () == "RoleBinding" :
105- subjects := o.GetSliceOrDie("subjects")
106- for _, s := range subjects {
107- if s.GetKind() == "ServiceAccount" {
108- ns := s.GetNamespace()
109- visitor(ns, false)
110- nsCollector <- func() {
111- nsPtr := &ns
112- p.updateNamespace(nsPtr)
113- s.SetNamespace(*nsPtr)
114- o.SetOrDie(&subjects, "subjects")
107+ subjects := o .GetSlice ("subjects" )
108+ for _ , s := range subjects {
109+ var ns string
110+ found , _ := s .Get (& ns , "namespace" )
111+ if found {
112+ visitor (NewNamespace (o , & ns ))
113+ _ = s .Set (& ns , "namespace" )
115114 }
116115 }
117- }
118- */
116+ o .SetOrDie (& subjects , "subjects" )
119117 case o .HasNamespace ():
120118 // Only update the namespace scoped resource. To determine if a resource is namespace scoped,
121119 // we assume its namespace should have been set.
122- fields := []string {"metadata" , "namespace" }
123- callback (NewNamespace (o , fields ))
120+ namespace := o .GetStringOrDie ("metadata" , "namespace" )
121+ visitor (NewNamespace (o , & namespace ))
122+ o .SetOrDie (& namespace , "metadata" , "namespace" )
124123 default :
125124 // skip the cluster scoped resource. We determine if a resource is cluster scoped by
126125 // checking if the metadata.namespace is configured.
@@ -133,25 +132,25 @@ func FindAllNamespaces(objects []*fn.KubeObject) ([]string, map[string]int) {
133132 nsObjCounter := map [string ]int {}
134133 namespaces := sets .NewString ()
135134 VisitNamespaces (objects , func (ns * Namespace ) {
136- if ns .Name == "" {
135+ if * ns .Ptr == "" {
137136 return
138137 }
139138 if ns .IsNamespace {
140- nsObjCounter [ns .Name ] += 1
139+ nsObjCounter [* ns .Ptr ] += 1
141140 }
142- namespaces .Insert (ns .Name )
141+ namespaces .Insert (* ns .Ptr )
143142 })
144143 return namespaces .List (), nsObjCounter
145144}
146145
147146// ReplaceNamespace iterates the `objects` to replace the `OldNs` with `newNs` on namespace field.
148147func ReplaceNamespace (objects []* fn.KubeObject , oldNs , newNs string ) {
149148 VisitNamespaces (objects , func (ns * Namespace ) {
150- if ns .Name == "" {
149+ if * ns .Ptr == "" {
151150 return
152151 }
153- if ns .Name == oldNs {
154- ns .Set ( newNs )
152+ if * ns .Ptr == oldNs {
153+ * ns .Ptr = newNs
155154 }
156155 })
157156}
@@ -232,10 +231,7 @@ type NamespaceTransformer struct {
232231 Errors []string
233232}
234233
235- func NewNamespace (obj * fn.KubeObject , path []string ) * Namespace {
236- setter := func (newNs string ) {
237- _ = obj .Set (newNs , path ... )
238- }
234+ func NewNamespace (obj * fn.KubeObject , namespacePtr * string ) * Namespace {
239235 annotationSetter := func (newAnnotation string ) {
240236 obj .SetAnnotation (dependsOnAnnotation , newAnnotation )
241237 }
@@ -247,28 +243,22 @@ func NewNamespace(obj *fn.KubeObject, path []string) *Namespace {
247243 return dependsOnKeyPattern (group , obj .GetKind (), obj .GetName ())
248244 }
249245 return & Namespace {
250- Name : obj .GetStringOrDie (path ... ),
246+ Ptr : namespacePtr , // obj.GetStringOrDie(path...),
251247 IsNamespace : obj .IsGVK ("v1" , "Namespace" ),
252248 DependsOnAnnotation : obj .GetAnnotations ()[dependsOnAnnotation ],
253- setter : setter ,
254249 annotationGetter : annotationGetter ,
255250 annotationSetter : annotationSetter ,
256251 }
257252}
258253
259254type Namespace struct {
260- Name string
255+ Ptr * string
261256 IsNamespace bool
262257 DependsOnAnnotation string
263- setter func (newNs string )
264258 annotationGetter func () string
265259 annotationSetter func (newDependsOnAnnotation string )
266260}
267261
268- func (n * Namespace ) Set (newNs string ) {
269- n .setter (newNs )
270- }
271-
272262func (n * Namespace ) SetDependsOnAnnotation (newDependsOn string ) {
273263 n .annotationSetter (newDependsOn )
274264}
0 commit comments