-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change Preferred Controller Config to Use Secret Instead of ConfigMap (…
…#318) * add test for kapp-controller getting secret over configmap Co-Authored-By: Joe Kimmel <[email protected]> * add ability for kapp-controller to fetch controller config from k8s secret Co-Authored-By: Joe Kimmel <[email protected]> * added unit tests for when only secret or configmap exist for kapp-controller config Co-Authored-By: Joe Kimmel <[email protected]> * add all keys from secret to configmap Co-Authored-By: Joe Kimmel <[email protected]> * initial feedback Co-Authored-By: Joe Kimmel <[email protected]> * remove base64 and convert Config to a struct with explicit fields populated from Either a secret or configmap not both * rewrote findExternalConfig to avoid nested ifs * remove stale TODO * change config-map.yml to secret-config.yml * add copyright header to config_test.go * convert helper funcs to Config struct funcs * remove checking for key existence * remove keys list from addSecretDataToConfig Co-authored-by: Joe Kimmel <[email protected]> Co-authored-by: Joe Kimmel <[email protected]>
- Loading branch information
1 parent
60d8e36
commit 9214dec
Showing
3 changed files
with
180 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2021 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package config_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
kcconfig "github.com/vmware-tanzu/carvel-kapp-controller/pkg/config" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
k8sfake "k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
func Test_GetConfig_ReturnsSecret_WhenBothConfigMapAndSecretExist(t *testing.T) { | ||
configMap := &v1.ConfigMap{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "kapp-controller-config", | ||
Namespace: "default", | ||
}, | ||
Data: map[string]string{ | ||
"httpProxy": "wrong-proxy", | ||
}, | ||
} | ||
|
||
secret := &v1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "kapp-controller-config", | ||
Namespace: "default", | ||
}, | ||
Data: map[string][]byte{ | ||
"httpProxy": []byte("proxy-svc.proxy-server.svc.cluster.local:80"), | ||
}, | ||
} | ||
|
||
defer os.Unsetenv("http_proxy") | ||
|
||
k8scs := k8sfake.NewSimpleClientset(configMap, secret) | ||
|
||
config, err := kcconfig.GetConfig(k8scs) | ||
assert.Nil(t, err, "unexpected error after running config.GetConfig()", err) | ||
|
||
assert.Nil(t, config.Apply(), "unexpected error after running config.Apply()", err) | ||
|
||
expected := "proxy-svc.proxy-server.svc.cluster.local:80" | ||
httpProxyActual := os.Getenv("http_proxy") | ||
|
||
assert.Equal(t, expected, httpProxyActual) | ||
} | ||
|
||
func Test_GetConfig_ReturnsConfigMap_WhenOnlyConfigMapExists(t *testing.T) { | ||
configMap := &v1.ConfigMap{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "kapp-controller-config", | ||
Namespace: "default", | ||
}, | ||
Data: map[string]string{ | ||
"httpProxy": "proxy-svc.proxy-server.svc.cluster.local:80", | ||
}, | ||
} | ||
|
||
defer os.Unsetenv("http_proxy") | ||
|
||
k8scs := k8sfake.NewSimpleClientset(configMap) | ||
|
||
config, err := kcconfig.GetConfig(k8scs) | ||
assert.Nil(t, err, "unexpected error after running config.GetConfig()", err) | ||
|
||
assert.Nil(t, config.Apply(), "unexpected error after running config.Apply()", err) | ||
|
||
expected := "proxy-svc.proxy-server.svc.cluster.local:80" | ||
httpProxyActual := os.Getenv("http_proxy") | ||
|
||
assert.Equal(t, expected, httpProxyActual) | ||
} | ||
|
||
func Test_GetConfig_ReturnsSecret_WhenOnlySecretExists(t *testing.T) { | ||
secret := &v1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "kapp-controller-config", | ||
Namespace: "default", | ||
}, | ||
Data: map[string][]byte{ | ||
"httpProxy": []byte("proxy-svc.proxy-server.svc.cluster.local:80"), | ||
}, | ||
} | ||
|
||
defer os.Unsetenv("http_proxy") | ||
|
||
k8scs := k8sfake.NewSimpleClientset(secret) | ||
|
||
config, err := kcconfig.GetConfig(k8scs) | ||
assert.Nil(t, err, "unexpected error after running config.GetConfig()", err) | ||
|
||
assert.Nil(t, config.Apply(), "unexpected error after running config.Apply()", err) | ||
|
||
expected := "proxy-svc.proxy-server.svc.cluster.local:80" | ||
httpProxyActual := os.Getenv("http_proxy") | ||
|
||
assert.Equal(t, expected, httpProxyActual) | ||
} |