Skip to content

Commit 4495756

Browse files
committed
Fixes namespace calculation for cluster-scoped resources
1 parent 8400b85 commit 4495756

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

pkg/config/initoptions.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ import (
1313

1414
"github.com/google/uuid"
1515
"k8s.io/cli-runtime/pkg/genericclioptions"
16+
"k8s.io/klog"
1617
cmdutil "k8s.io/kubectl/pkg/cmd/util"
1718
"sigs.k8s.io/cli-utils/pkg/common"
1819
"sigs.k8s.io/cli-utils/pkg/inventory/configmap"
1920
"sigs.k8s.io/kustomize/kyaml/kio"
2021
"sigs.k8s.io/kustomize/kyaml/kio/filters"
22+
"sigs.k8s.io/kustomize/kyaml/openapi"
2123
)
2224

2325
const (
@@ -60,6 +62,7 @@ func (i *InitOptions) Complete(args []string) error {
6062
return err
6163
}
6264
i.Dir = dir
65+
klog.V(4).Infof("init directory: %s", i.Dir)
6366

6467
ns, err := FindNamespace(i.factory.ToRawKubeConfigLoader(), i.Dir)
6568
if err != nil {
@@ -99,6 +102,7 @@ func FindNamespace(loader namespaceLoader, dir string) (string, error) {
99102
return "", err
100103
}
101104
if enforceNamespace {
105+
klog.V(6).Infof("enforcing namespace: %s", namespace)
102106
return namespace, nil
103107
}
104108

@@ -107,8 +111,10 @@ func FindNamespace(loader namespaceLoader, dir string) (string, error) {
107111
return "", err
108112
}
109113
if allInSameNs {
114+
klog.V(6).Infof("all in same namespace: %s", ns)
110115
return ns, nil
111116
}
117+
klog.V(6).Infof("returning namespace: %s", namespace)
112118
return namespace, nil
113119
}
114120

@@ -148,16 +154,24 @@ func allInSameNamespace(packageDir string) (string, bool, error) {
148154
if err != nil {
149155
return "", false, err
150156
}
157+
// Skip found cluster-scoped resources. If not found, just assume namespaced.
158+
namespaced, found := openapi.IsNamespaceScoped(rm.TypeMeta)
159+
if found && !namespaced {
160+
continue
161+
}
151162
if rm.Namespace == "" {
163+
klog.V(6).Infof("one resource missing namespace (%s): return empty namespace", rm.Name)
152164
return "", false, nil
153165
}
154166
if ns == "" {
155167
ns = rm.Namespace
156168
} else if rm.Namespace != ns {
169+
klog.V(6).Infof("two namespaces not same: %s versus %s", rm.Namespace, ns)
157170
return "", false, nil
158171
}
159172
}
160173
if ns != "" {
174+
klog.V(6).Infof("returning empty namespace")
161175
return ns, true, nil
162176
}
163177
return "", false, nil
@@ -208,6 +222,7 @@ func (i *InitOptions) fillInValues() string {
208222
nowStr := now.Format("2006-01-02 15:04:05 MST")
209223
randomSuffix := common.RandomStr(now.UTC().UnixNano())
210224
manifestStr := i.Template
225+
klog.V(4).Infof("namespace/inventory-id: %s/%s", i.Namespace, i.InventoryID)
211226
manifestStr = strings.ReplaceAll(manifestStr, "<DATETIME>", nowStr)
212227
manifestStr = strings.ReplaceAll(manifestStr, "<NAMESPACE>", i.Namespace)
213228
manifestStr = strings.ReplaceAll(manifestStr, "<RANDOMSUFFIX>", randomSuffix)
@@ -220,6 +235,7 @@ func (i *InitOptions) Run() error {
220235
if fileExists(manifestFilePath) {
221236
return fmt.Errorf("inventory object template file already exists: %s", manifestFilePath)
222237
}
238+
klog.V(4).Infof("creating manifest filename: %s", manifestFilePath)
223239
f, err := os.Create(manifestFilePath)
224240
if err != nil {
225241
return fmt.Errorf("unable to create inventory object template file: %s", err)

pkg/config/initoptions_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ metadata:
5858
config.kubernetes.io/local-config: "true"
5959
`)
6060

61+
var readFileE = []byte(`
62+
apiVersion: v1
63+
kind: Pod
64+
metadata:
65+
name: objE
66+
namespace: namespaceA
67+
`)
68+
69+
var readFileF = []byte(`
70+
apiVersion: v1
71+
kind: Namespace
72+
metadata:
73+
name: namespaceA
74+
`)
75+
6176
func TestComplete(t *testing.T) {
6277
tests := map[string]struct {
6378
args []string
@@ -124,6 +139,16 @@ func TestComplete(t *testing.T) {
124139
isError: false,
125140
expectedNamespace: "foo",
126141
},
142+
"Cluster-scoped resources are ignored in namespace calculation": {
143+
args: []string{},
144+
files: map[string][]byte{
145+
"a_test.yaml": readFileA,
146+
"e_test.yaml": readFileE,
147+
"f_test.yaml": readFileF,
148+
},
149+
isError: false,
150+
expectedNamespace: "namespaceA",
151+
},
127152
}
128153
for name, tc := range tests {
129154
t.Run(name, func(t *testing.T) {

0 commit comments

Comments
 (0)