From 9bad974c55314229c9379e3a66e5a10638736f27 Mon Sep 17 00:00:00 2001 From: Nikita Bhatia Date: Mon, 17 Apr 2023 20:20:32 +0530 Subject: [PATCH 1/2] Revert PWX-21520 : Add custom namespace during configmap creation Signed-off-by: Nikita Bhatia --- k8s/core/configmap/configmap.go | 18 ++++++------------ k8s/core/configmap/configmap_lock_v1.go | 4 ++-- k8s/core/configmap/configmap_lock_v1_test.go | 4 ++-- k8s/core/configmap/configmap_lock_v2.go | 6 +++--- k8s/core/configmap/configmap_lock_v2_test.go | 2 +- k8s/core/configmap/configmap_test.go | 8 ++++---- k8s/core/configmap/types.go | 1 - 7 files changed, 18 insertions(+), 25 deletions(-) diff --git a/k8s/core/configmap/configmap.go b/k8s/core/configmap/configmap.go index 2e6b2363..86e0771a 100644 --- a/k8s/core/configmap/configmap.go +++ b/k8s/core/configmap/configmap.go @@ -21,7 +21,6 @@ func New( lockAttempts uint, v2LockRefreshDuration time.Duration, v2LockK8sLockTTL time.Duration, - nameSpace string, ) (ConfigMap, error) { if data == nil { data = make(map[string]string) @@ -31,15 +30,11 @@ func New( configMapUserLabelKey: TruncateLabel(name), } data[pxOwnerKey] = "" - if nameSpace == "" { - nameSpace = k8sSystemNamespace - } cm := &corev1.ConfigMap{ ObjectMeta: meta_v1.ObjectMeta{ - Name: name, - Namespace: nameSpace, - Labels: labels, + Name: name, + Labels: labels, }, Data: data, } @@ -65,14 +60,13 @@ func New( lockAttempts: lockAttempts, lockRefreshDuration: v2LockRefreshDuration, lockK8sLockTTL: v2LockK8sLockTTL, - nameSpace: nameSpace, }, nil } func (c *configMap) Get() (map[string]string, error) { cm, err := core.Instance().GetConfigMap( c.name, - c.nameSpace, + k8sSystemNamespace, ) if err != nil { return nil, err @@ -84,7 +78,7 @@ func (c *configMap) Get() (map[string]string, error) { func (c *configMap) Delete() error { return core.Instance().DeleteConfigMap( c.name, - c.nameSpace, + k8sSystemNamespace, ) } @@ -96,7 +90,7 @@ func (c *configMap) Patch(data map[string]string) error { for retries := 0; retries < maxConflictRetries; retries++ { cm, err = core.Instance().GetConfigMap( c.name, - c.nameSpace, + k8sSystemNamespace, ) if err != nil { return err @@ -127,7 +121,7 @@ func (c *configMap) Update(data map[string]string) error { for retries := 0; retries < maxConflictRetries; retries++ { cm, err = core.Instance().GetConfigMap( c.name, - c.nameSpace, + k8sSystemNamespace, ) if err != nil { return err diff --git a/k8s/core/configmap/configmap_lock_v1.go b/k8s/core/configmap/configmap_lock_v1.go index e99154e4..ea98317b 100644 --- a/k8s/core/configmap/configmap_lock_v1.go +++ b/k8s/core/configmap/configmap_lock_v1.go @@ -60,7 +60,7 @@ func (c *configMap) Unlock() error { for retries := 0; retries < maxConflictRetries; retries++ { cm, err = core.Instance().GetConfigMap( c.name, - c.nameSpace, + k8sSystemNamespace, ) if err != nil { // A ConfigMap should always be created. @@ -95,7 +95,7 @@ func (c *configMap) tryLockV1(id string, refresh bool) (string, error) { // Get the existing ConfigMap cm, err := core.Instance().GetConfigMap( c.name, - c.nameSpace, + k8sSystemNamespace, ) if err != nil { // A ConfigMap should always be created. diff --git a/k8s/core/configmap/configmap_lock_v1_test.go b/k8s/core/configmap/configmap_lock_v1_test.go index 798e1402..03e29338 100644 --- a/k8s/core/configmap/configmap_lock_v1_test.go +++ b/k8s/core/configmap/configmap_lock_v1_test.go @@ -13,7 +13,7 @@ import ( func TestLock(t *testing.T) { fakeClient := fakek8sclient.NewSimpleClientset() coreops.SetInstance(coreops.New(fakeClient)) - cm, err := New("px-configmaps-test", nil, lockTimeout, 5, 0, 0, "test-namespace") + cm, err := New("px-configmaps-test", nil, lockTimeout, 5, 0, 0) require.NoError(t, err, "Unexpected error on New") fmt.Println("testLock") @@ -112,7 +112,7 @@ func TestLockWithHoldTimeout(t *testing.T) { customHoldTimeout := defaultHoldTimeout + v1DefaultK8sLockRefreshDuration + 10*time.Second fakeClient := fakek8sclient.NewSimpleClientset() coreops.SetInstance(coreops.New(fakeClient)) - cm, err := New("px-configmaps-test", nil, defaultHoldTimeout, 5, 0, 0, "") + cm, err := New("px-configmaps-test", nil, defaultHoldTimeout, 5, 0, 0) require.NoError(t, err, "Unexpected error on New") fmt.Println("TestLockWithHoldTimeout") diff --git a/k8s/core/configmap/configmap_lock_v2.go b/k8s/core/configmap/configmap_lock_v2.go index 38f897b8..794c7c03 100644 --- a/k8s/core/configmap/configmap_lock_v2.go +++ b/k8s/core/configmap/configmap_lock_v2.go @@ -89,7 +89,7 @@ func (c *configMap) UnlockWithKey(key string) error { for retries := 0; retries < maxConflictRetries; retries++ { cm, err = core.Instance().GetConfigMap( c.name, - c.nameSpace, + k8sSystemNamespace, ) if err != nil { // A ConfigMap should always be created. @@ -140,7 +140,7 @@ func (c *configMap) IsKeyLocked(key string) (bool, string, error) { // Get the existing ConfigMap cm, err := core.Instance().GetConfigMap( c.name, - c.nameSpace, + k8sSystemNamespace, ) if err != nil { return false, "", err @@ -174,7 +174,7 @@ func (c *configMap) tryLock(owner string, key string) (string, error) { // Get the existing ConfigMap cm, err := core.Instance().GetConfigMap( c.name, - c.nameSpace, + k8sSystemNamespace, ) if err != nil { // A ConfigMap should always be created. diff --git a/k8s/core/configmap/configmap_lock_v2_test.go b/k8s/core/configmap/configmap_lock_v2_test.go index 795c0e5c..74955bc6 100644 --- a/k8s/core/configmap/configmap_lock_v2_test.go +++ b/k8s/core/configmap/configmap_lock_v2_test.go @@ -17,7 +17,7 @@ const ( func TestMultilock(t *testing.T) { fakeClient := fakek8sclient.NewSimpleClientset() coreops.SetInstance(coreops.New(fakeClient)) - cm, err := New("px-configmaps-test", nil, lockTimeout, 3, 0, 0, "") + cm, err := New("px-configmaps-test", nil, lockTimeout, 3, 0, 0) require.NoError(t, err, "Unexpected error on New") fmt.Println("testMultilock") diff --git a/k8s/core/configmap/configmap_test.go b/k8s/core/configmap/configmap_test.go index 6034c334..44bfc4ae 100644 --- a/k8s/core/configmap/configmap_test.go +++ b/k8s/core/configmap/configmap_test.go @@ -17,7 +17,7 @@ func TestGetConfigMap(t *testing.T) { configData := map[string]string{ "key1": "val1", } - cm, err := New("px-configmaps-test", configData, lockTimeout, 5, 0, 0, "test-namespace") + cm, err := New("px-configmaps-test", configData, lockTimeout, 5, 0, 0) require.NoError(t, err, "Unexpected error in creating configmap") resultMap, err := cm.Get() @@ -34,7 +34,7 @@ func TestDeleteConfigMap(t *testing.T) { "key1": "val1", } - cm, err := New("px-configmaps-test", configData, lockTimeout, 5, 0, 0, "test-namespace") + cm, err := New("px-configmaps-test", configData, lockTimeout, 5, 0, 0) require.NoError(t, err, "Unexpected error in creating configmap") err = cm.Delete() @@ -50,7 +50,7 @@ func TestPatchConfigMap(t *testing.T) { "key1": "val1", } - cm, err := New("px-configmaps-test", configData, lockTimeout, 5, 0, 0, "test-namespace") + cm, err := New("px-configmaps-test", configData, lockTimeout, 5, 0, 0) require.NoError(t, err, "Unexpected error in creating configmap") dummyData := map[string]string{ @@ -73,7 +73,7 @@ func TestUpdateConfigMap(t *testing.T) { "key1": "val1", } - cm, err := New("px-configmaps-test", configData, lockTimeout, 5, 0, 0, "test-namespace") + cm, err := New("px-configmaps-test", configData, lockTimeout, 5, 0, 0) require.NoError(t, err, "Unexpected error in creating configmap") dummyData := map[string]string{ diff --git a/k8s/core/configmap/types.go b/k8s/core/configmap/types.go index c01be7d6..97b43d1d 100644 --- a/k8s/core/configmap/types.go +++ b/k8s/core/configmap/types.go @@ -67,7 +67,6 @@ type configMap struct { lockAttempts uint lockRefreshDuration time.Duration lockK8sLockTTL time.Duration - nameSpace string } type k8sLock struct { From 75eb03c8395fe5d8a4905714ff9919f13b92586f Mon Sep 17 00:00:00 2001 From: Nikita Bhatia Date: Mon, 17 Apr 2023 20:42:32 +0530 Subject: [PATCH 2/2] Revert PWX-21520 : Add custom namespace during configmap creation Signed-off-by: Nikita Bhatia --- k8s/core/configmap/configmap.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/k8s/core/configmap/configmap.go b/k8s/core/configmap/configmap.go index 86e0771a..ed50972b 100644 --- a/k8s/core/configmap/configmap.go +++ b/k8s/core/configmap/configmap.go @@ -33,8 +33,9 @@ func New( cm := &corev1.ConfigMap{ ObjectMeta: meta_v1.ObjectMeta{ - Name: name, - Labels: labels, + Name: name, + Namespace: k8sSystemNamespace, + Labels: labels, }, Data: data, }