Skip to content

Commit 4bf5efa

Browse files
nikita-bhatiapureneelesh
authored andcommitted
PWX-21520 : Add custom namespace during configmap creation (#305)
* Add namespace while creating configmap Signed-off-by: Nikita Bhatia <nbhatia@purestorage.com> * Fix failing test Signed-off-by: Nikita Bhatia <nbhatia@purestorage.com> * test commit to trigger build Signed-off-by: Nikita Bhatia <nbhatia@purestorage.com> * Remove test logs Signed-off-by: Nikita Bhatia <nbhatia@purestorage.com> * use custom namespace for all configmap operations Signed-off-by: Nikita Bhatia <nbhatia@purestorage.com> * Fix failing test Signed-off-by: Nikita Bhatia <nbhatia@purestorage.com> --------- Signed-off-by: Nikita Bhatia <nbhatia@purestorage.com>
1 parent fd19bc3 commit 4bf5efa

File tree

7 files changed

+108
-13
lines changed

7 files changed

+108
-13
lines changed

k8s/core/configmap/configmap.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func New(
2121
lockAttempts uint,
2222
v2LockRefreshDuration time.Duration,
2323
v2LockK8sLockTTL time.Duration,
24+
nameSpace string,
2425
) (ConfigMap, error) {
2526
if data == nil {
2627
data = make(map[string]string)
@@ -30,11 +31,14 @@ func New(
3031
configMapUserLabelKey: TruncateLabel(name),
3132
}
3233
data[pxOwnerKey] = ""
34+
if nameSpace == "" {
35+
nameSpace = k8sSystemNamespace
36+
}
3337

3438
cm := &corev1.ConfigMap{
3539
ObjectMeta: meta_v1.ObjectMeta{
3640
Name: name,
37-
Namespace: k8sSystemNamespace,
41+
Namespace: nameSpace,
3842
Labels: labels,
3943
},
4044
Data: data,
@@ -61,13 +65,14 @@ func New(
6165
lockAttempts: lockAttempts,
6266
lockRefreshDuration: v2LockRefreshDuration,
6367
lockK8sLockTTL: v2LockK8sLockTTL,
68+
nameSpace: nameSpace,
6469
}, nil
6570
}
6671

6772
func (c *configMap) Get() (map[string]string, error) {
6873
cm, err := core.Instance().GetConfigMap(
6974
c.name,
70-
k8sSystemNamespace,
75+
c.nameSpace,
7176
)
7277
if err != nil {
7378
return nil, err
@@ -79,7 +84,7 @@ func (c *configMap) Get() (map[string]string, error) {
7984
func (c *configMap) Delete() error {
8085
return core.Instance().DeleteConfigMap(
8186
c.name,
82-
k8sSystemNamespace,
87+
c.nameSpace,
8388
)
8489
}
8590

@@ -91,7 +96,7 @@ func (c *configMap) Patch(data map[string]string) error {
9196
for retries := 0; retries < maxConflictRetries; retries++ {
9297
cm, err = core.Instance().GetConfigMap(
9398
c.name,
94-
k8sSystemNamespace,
99+
c.nameSpace,
95100
)
96101
if err != nil {
97102
return err
@@ -122,7 +127,7 @@ func (c *configMap) Update(data map[string]string) error {
122127
for retries := 0; retries < maxConflictRetries; retries++ {
123128
cm, err = core.Instance().GetConfigMap(
124129
c.name,
125-
k8sSystemNamespace,
130+
c.nameSpace,
126131
)
127132
if err != nil {
128133
return err

k8s/core/configmap/configmap_lock_v1.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (c *configMap) Unlock() error {
6060
for retries := 0; retries < maxConflictRetries; retries++ {
6161
cm, err = core.Instance().GetConfigMap(
6262
c.name,
63-
k8sSystemNamespace,
63+
c.nameSpace,
6464
)
6565
if err != nil {
6666
// A ConfigMap should always be created.
@@ -95,7 +95,7 @@ func (c *configMap) tryLockV1(id string, refresh bool) (string, error) {
9595
// Get the existing ConfigMap
9696
cm, err := core.Instance().GetConfigMap(
9797
c.name,
98-
k8sSystemNamespace,
98+
c.nameSpace,
9999
)
100100
if err != nil {
101101
// A ConfigMap should always be created.

k8s/core/configmap/configmap_lock_v1_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
func TestLock(t *testing.T) {
1414
fakeClient := fakek8sclient.NewSimpleClientset()
1515
coreops.SetInstance(coreops.New(fakeClient))
16-
cm, err := New("px-configmaps-test", nil, lockTimeout, 5, 0, 0)
16+
cm, err := New("px-configmaps-test", nil, lockTimeout, 5, 0, 0, "test-namespace")
1717
require.NoError(t, err, "Unexpected error on New")
1818
fmt.Println("testLock")
1919

@@ -112,7 +112,7 @@ func TestLockWithHoldTimeout(t *testing.T) {
112112
customHoldTimeout := defaultHoldTimeout + v1DefaultK8sLockRefreshDuration + 10*time.Second
113113
fakeClient := fakek8sclient.NewSimpleClientset()
114114
coreops.SetInstance(coreops.New(fakeClient))
115-
cm, err := New("px-configmaps-test", nil, defaultHoldTimeout, 5, 0, 0)
115+
cm, err := New("px-configmaps-test", nil, defaultHoldTimeout, 5, 0, 0, "")
116116
require.NoError(t, err, "Unexpected error on New")
117117
fmt.Println("TestLockWithHoldTimeout")
118118

k8s/core/configmap/configmap_lock_v2.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (c *configMap) UnlockWithKey(key string) error {
8989
for retries := 0; retries < maxConflictRetries; retries++ {
9090
cm, err = core.Instance().GetConfigMap(
9191
c.name,
92-
k8sSystemNamespace,
92+
c.nameSpace,
9393
)
9494
if err != nil {
9595
// A ConfigMap should always be created.
@@ -140,7 +140,7 @@ func (c *configMap) IsKeyLocked(key string) (bool, string, error) {
140140
// Get the existing ConfigMap
141141
cm, err := core.Instance().GetConfigMap(
142142
c.name,
143-
k8sSystemNamespace,
143+
c.nameSpace,
144144
)
145145
if err != nil {
146146
return false, "", err
@@ -174,7 +174,7 @@ func (c *configMap) tryLock(owner string, key string) (string, error) {
174174
// Get the existing ConfigMap
175175
cm, err := core.Instance().GetConfigMap(
176176
c.name,
177-
k8sSystemNamespace,
177+
c.nameSpace,
178178
)
179179
if err != nil {
180180
// A ConfigMap should always be created.

k8s/core/configmap/configmap_lock_v2_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const (
1717
func TestMultilock(t *testing.T) {
1818
fakeClient := fakek8sclient.NewSimpleClientset()
1919
coreops.SetInstance(coreops.New(fakeClient))
20-
cm, err := New("px-configmaps-test", nil, lockTimeout, 3, 0, 0)
20+
cm, err := New("px-configmaps-test", nil, lockTimeout, 3, 0, 0, "")
2121
require.NoError(t, err, "Unexpected error on New")
2222

2323
fmt.Println("testMultilock")
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package configmap
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
coreops "github.com/portworx/sched-ops/k8s/core"
10+
fakek8sclient "k8s.io/client-go/kubernetes/fake"
11+
)
12+
13+
func TestGetConfigMap(t *testing.T) {
14+
fakeClient := fakek8sclient.NewSimpleClientset()
15+
coreops.SetInstance(coreops.New(fakeClient))
16+
17+
configData := map[string]string{
18+
"key1": "val1",
19+
}
20+
cm, err := New("px-configmaps-test", configData, lockTimeout, 5, 0, 0, "test-namespace")
21+
require.NoError(t, err, "Unexpected error in creating configmap")
22+
23+
resultMap, err := cm.Get()
24+
require.NoError(t, err, "Unexpected error in getting configmap")
25+
require.Contains(t, resultMap, "key1")
26+
fmt.Println(resultMap)
27+
}
28+
29+
func TestDeleteConfigMap(t *testing.T) {
30+
fakeClient := fakek8sclient.NewSimpleClientset()
31+
coreops.SetInstance(coreops.New(fakeClient))
32+
33+
configData := map[string]string{
34+
"key1": "val1",
35+
}
36+
37+
cm, err := New("px-configmaps-test", configData, lockTimeout, 5, 0, 0, "test-namespace")
38+
require.NoError(t, err, "Unexpected error in creating configmap")
39+
40+
err = cm.Delete()
41+
require.NoError(t, err, "Unexpected error in delete")
42+
43+
}
44+
45+
func TestPatchConfigMap(t *testing.T) {
46+
fakeClient := fakek8sclient.NewSimpleClientset()
47+
coreops.SetInstance(coreops.New(fakeClient))
48+
49+
configData := map[string]string{
50+
"key1": "val1",
51+
}
52+
53+
cm, err := New("px-configmaps-test", configData, lockTimeout, 5, 0, 0, "test-namespace")
54+
require.NoError(t, err, "Unexpected error in creating configmap")
55+
56+
dummyData := map[string]string{
57+
"key2": "val2",
58+
}
59+
60+
err = cm.Patch(dummyData)
61+
require.NoError(t, err, "Unexpected error in Patch")
62+
resultMap, err := cm.Get()
63+
require.Contains(t, resultMap, "key1")
64+
require.Contains(t, resultMap, "key2")
65+
fmt.Println(resultMap)
66+
}
67+
68+
func TestUpdateConfigMap(t *testing.T) {
69+
fakeClient := fakek8sclient.NewSimpleClientset()
70+
coreops.SetInstance(coreops.New(fakeClient))
71+
72+
configData := map[string]string{
73+
"key1": "val1",
74+
}
75+
76+
cm, err := New("px-configmaps-test", configData, lockTimeout, 5, 0, 0, "test-namespace")
77+
require.NoError(t, err, "Unexpected error in creating configmap")
78+
79+
dummyData := map[string]string{
80+
"key2": "val2",
81+
}
82+
83+
err = cm.Update(dummyData)
84+
require.NoError(t, err, "Unexpected error in Update")
85+
resultMap, err := cm.Get()
86+
require.NotContains(t, resultMap, "key1")
87+
require.Contains(t, resultMap, "key2")
88+
fmt.Println(resultMap)
89+
}

k8s/core/configmap/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type configMap struct {
6767
lockAttempts uint
6868
lockRefreshDuration time.Duration
6969
lockK8sLockTTL time.Duration
70+
nameSpace string
7071
}
7172

7273
type k8sLock struct {

0 commit comments

Comments
 (0)