Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions k8s/core/configmap/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func New(
lockAttempts uint,
v2LockRefreshDuration time.Duration,
v2LockK8sLockTTL time.Duration,
nameSpace string,
) (ConfigMap, error) {
if data == nil {
data = make(map[string]string)
Expand All @@ -30,11 +31,14 @@ func New(
configMapUserLabelKey: TruncateLabel(name),
}
data[pxOwnerKey] = ""
if nameSpace == "" {
nameSpace = k8sSystemNamespace
}

cm := &corev1.ConfigMap{
ObjectMeta: meta_v1.ObjectMeta{
Name: name,
Namespace: k8sSystemNamespace,
Namespace: nameSpace,
Labels: labels,
},
Data: data,
Expand All @@ -61,13 +65,14 @@ 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,
k8sSystemNamespace,
c.nameSpace,
)
if err != nil {
return nil, err
Expand All @@ -79,7 +84,7 @@ func (c *configMap) Get() (map[string]string, error) {
func (c *configMap) Delete() error {
return core.Instance().DeleteConfigMap(
c.name,
k8sSystemNamespace,
c.nameSpace,
)
}

Expand All @@ -91,7 +96,7 @@ func (c *configMap) Patch(data map[string]string) error {
for retries := 0; retries < maxConflictRetries; retries++ {
cm, err = core.Instance().GetConfigMap(
c.name,
k8sSystemNamespace,
c.nameSpace,
)
if err != nil {
return err
Expand Down Expand Up @@ -122,7 +127,7 @@ func (c *configMap) Update(data map[string]string) error {
for retries := 0; retries < maxConflictRetries; retries++ {
cm, err = core.Instance().GetConfigMap(
c.name,
k8sSystemNamespace,
c.nameSpace,
)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions k8s/core/configmap/configmap_lock_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (c *configMap) Unlock() error {
for retries := 0; retries < maxConflictRetries; retries++ {
cm, err = core.Instance().GetConfigMap(
c.name,
k8sSystemNamespace,
c.nameSpace,
)
if err != nil {
// A ConfigMap should always be created.
Expand Down Expand Up @@ -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,
k8sSystemNamespace,
c.nameSpace,
)
if err != nil {
// A ConfigMap should always be created.
Expand Down
4 changes: 2 additions & 2 deletions k8s/core/configmap/configmap_lock_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
cm, err := New("px-configmaps-test", nil, lockTimeout, 5, 0, 0, "test-namespace")
require.NoError(t, err, "Unexpected error on New")
fmt.Println("testLock")

Expand Down Expand Up @@ -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")

Expand Down
6 changes: 3 additions & 3 deletions k8s/core/configmap/configmap_lock_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (c *configMap) UnlockWithKey(key string) error {
for retries := 0; retries < maxConflictRetries; retries++ {
cm, err = core.Instance().GetConfigMap(
c.name,
k8sSystemNamespace,
c.nameSpace,
)
if err != nil {
// A ConfigMap should always be created.
Expand Down Expand Up @@ -140,7 +140,7 @@ func (c *configMap) IsKeyLocked(key string) (bool, string, error) {
// Get the existing ConfigMap
cm, err := core.Instance().GetConfigMap(
c.name,
k8sSystemNamespace,
c.nameSpace,
)
if err != nil {
return false, "", err
Expand Down Expand Up @@ -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,
k8sSystemNamespace,
c.nameSpace,
)
if err != nil {
// A ConfigMap should always be created.
Expand Down
2 changes: 1 addition & 1 deletion k8s/core/configmap/configmap_lock_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
89 changes: 89 additions & 0 deletions k8s/core/configmap/configmap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package configmap

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

coreops "github.com/portworx/sched-ops/k8s/core"
fakek8sclient "k8s.io/client-go/kubernetes/fake"
)

func TestGetConfigMap(t *testing.T) {
fakeClient := fakek8sclient.NewSimpleClientset()
coreops.SetInstance(coreops.New(fakeClient))

configData := map[string]string{
"key1": "val1",
}
cm, err := New("px-configmaps-test", configData, lockTimeout, 5, 0, 0, "test-namespace")
require.NoError(t, err, "Unexpected error in creating configmap")

resultMap, err := cm.Get()
require.NoError(t, err, "Unexpected error in getting configmap")
require.Contains(t, resultMap, "key1")
fmt.Println(resultMap)
}

func TestDeleteConfigMap(t *testing.T) {
fakeClient := fakek8sclient.NewSimpleClientset()
coreops.SetInstance(coreops.New(fakeClient))

configData := map[string]string{
"key1": "val1",
}

cm, err := New("px-configmaps-test", configData, lockTimeout, 5, 0, 0, "test-namespace")
require.NoError(t, err, "Unexpected error in creating configmap")

err = cm.Delete()
require.NoError(t, err, "Unexpected error in delete")

}

func TestPatchConfigMap(t *testing.T) {
fakeClient := fakek8sclient.NewSimpleClientset()
coreops.SetInstance(coreops.New(fakeClient))

configData := map[string]string{
"key1": "val1",
}

cm, err := New("px-configmaps-test", configData, lockTimeout, 5, 0, 0, "test-namespace")
require.NoError(t, err, "Unexpected error in creating configmap")

dummyData := map[string]string{
"key2": "val2",
}

err = cm.Patch(dummyData)
require.NoError(t, err, "Unexpected error in Patch")
resultMap, err := cm.Get()
require.Contains(t, resultMap, "key1")
require.Contains(t, resultMap, "key2")
fmt.Println(resultMap)
}

func TestUpdateConfigMap(t *testing.T) {
fakeClient := fakek8sclient.NewSimpleClientset()
coreops.SetInstance(coreops.New(fakeClient))

configData := map[string]string{
"key1": "val1",
}

cm, err := New("px-configmaps-test", configData, lockTimeout, 5, 0, 0, "test-namespace")
require.NoError(t, err, "Unexpected error in creating configmap")

dummyData := map[string]string{
"key2": "val2",
}

err = cm.Update(dummyData)
require.NoError(t, err, "Unexpected error in Update")
resultMap, err := cm.Get()
require.NotContains(t, resultMap, "key1")
require.Contains(t, resultMap, "key2")
fmt.Println(resultMap)
}
1 change: 1 addition & 0 deletions k8s/core/configmap/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type configMap struct {
lockAttempts uint
lockRefreshDuration time.Duration
lockK8sLockTTL time.Duration
nameSpace string
}

type k8sLock struct {
Expand Down