Skip to content

Commit a6f98bc

Browse files
author
Jijie Wei
committed
namespaced kpt live init
1 parent 7d23762 commit a6f98bc

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

cmd/initcmd/cmdinit.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ func NewCmdInit(ioStreams genericclioptions.IOStreams) *cobra.Command {
2525
},
2626
}
2727
cmd.Flags().StringVarP(&io.InventoryID, "inventory-id", "i", "", "Identifier for group of applied resources. Must be composed of valid label characters.")
28+
cmd.Flags().StringVarP(&io.Namespace, "inventory-namespace", "", "", "namespace for the resources to be initialized")
2829
return cmd
2930
}

pkg/config/initoptions.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import (
1111
"strings"
1212
"time"
1313

14+
"github.com/go-errors/errors"
15+
"github.com/google/uuid"
1416
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1517
"k8s.io/cli-runtime/pkg/genericclioptions"
1618
"sigs.k8s.io/kustomize/kyaml/kio"
17-
18-
"github.com/google/uuid"
1919
)
2020

2121
const manifestFilename = "inventory-template.yaml"
@@ -142,19 +142,25 @@ func calcPackageNamespace(packageDir string) (string, error) {
142142
if err != nil {
143143
return "", err
144144
}
145-
// Return the first non-empty namespace found. Cluster-scoped
145+
// Return the non-empty unique namespace if found. Cluster-scoped
146146
// resources do not have namespace set.
147+
currentNamespace := metav1.NamespaceDefault
147148
for _, node := range nodes {
148149
rm, err := node.GetMeta()
149-
if err != nil {
150+
if err != nil || len(rm.ObjectMeta.Namespace) == 0 {
150151
continue
151152
}
152-
if len(rm.ObjectMeta.Namespace) > 0 {
153-
return rm.ObjectMeta.Namespace, nil
153+
if currentNamespace == metav1.NamespaceDefault {
154+
currentNamespace = rm.ObjectMeta.Namespace
155+
}
156+
if currentNamespace != rm.ObjectMeta.Namespace {
157+
return "", errors.Errorf(
158+
"resources belong to different namespaces, a namespace is required to create the resource " +
159+
"used for keeping track of past apply operations. Please specify ---inv-namespace.")
154160
}
155161
}
156162
// Return the default namespace if none found.
157-
return metav1.NamespaceDefault, nil
163+
return currentNamespace, nil
158164
}
159165

160166
// defaultInventoryID returns a UUID string as a default unique

pkg/config/initoptions_test.go

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,66 @@ package config
55

66
import (
77
"fmt"
8+
"io/ioutil"
9+
"os"
10+
"path/filepath"
811
"regexp"
912
"strings"
1013
"testing"
1114

15+
"github.com/stretchr/testify/assert"
1216
"k8s.io/cli-runtime/pkg/genericclioptions"
1317
)
1418

1519
var ioStreams = genericclioptions.IOStreams{}
1620

21+
// writeFile writes a file under the test directory
22+
func writeFile(t *testing.T, path string, value []byte) {
23+
err := ioutil.WriteFile(path, value, 0600)
24+
if !assert.NoError(t, err) {
25+
assert.FailNow(t, err.Error())
26+
}
27+
}
28+
29+
var readFileA = []byte(`
30+
apiVersion: v1
31+
kind: Pod
32+
metadata:
33+
name: objA
34+
namespace: namespaceA
35+
`)
36+
37+
var readFileB = []byte(`
38+
apiVersion: v1
39+
kind: Pod
40+
metadata:
41+
name: objB
42+
namespace: namespaceB
43+
`)
44+
45+
var readFileC = []byte(`
46+
apiVersion: v1
47+
kind: Pod
48+
metadata:
49+
name: objC
50+
`)
51+
1752
func TestComplete(t *testing.T) {
53+
d1, err := ioutil.TempDir("", "test-dir")
54+
if !assert.NoError(t, err) {
55+
assert.FailNow(t, err.Error())
56+
}
57+
defer os.RemoveAll(d1)
58+
d2, err := ioutil.TempDir("", "test-dir")
59+
if !assert.NoError(t, err) {
60+
assert.FailNow(t, err.Error())
61+
}
62+
defer os.RemoveAll(d2)
63+
64+
writeFile(t, filepath.Join(d1, "a_test.yaml"), readFileA)
65+
writeFile(t, filepath.Join(d1, "b_test.yaml"), readFileB)
66+
writeFile(t, filepath.Join(d2, "b_test.yaml"), readFileC)
67+
1868
tests := map[string]struct {
1969
args []string
2070
isError bool
@@ -31,8 +81,15 @@ func TestComplete(t *testing.T) {
3181
args: []string{"foo"},
3282
isError: true,
3383
},
84+
"More than one namespace should fail": {
85+
args: []string{d1},
86+
isError: true,
87+
},
88+
"No namespace set is fine": {
89+
args: []string{d2},
90+
isError: false,
91+
},
3492
}
35-
3693
for name, tc := range tests {
3794
t.Run(name, func(t *testing.T) {
3895
io := NewInitOptions(ioStreams)

0 commit comments

Comments
 (0)