kubelet: Make service environment variables optional

This commit is contained in:
Brad Hoekstra
2018-09-17 16:27:36 -04:00
parent 690179b122
commit ac8799a80d
43 changed files with 1413 additions and 867 deletions

View File

@@ -439,20 +439,22 @@ func TestMakeEnvironmentVariables(t *testing.T) {
}
testCases := []struct {
name string // the name of the test case
ns string // the namespace to generate environment for
container *v1.Container // the container to use
masterServiceNs string // the namespace to read master service info from
nilLister bool // whether the lister should be nil
configMap *v1.ConfigMap // an optional ConfigMap to pull from
secret *v1.Secret // an optional Secret to pull from
expectedEnvs []kubecontainer.EnvVar // a set of expected environment vars
expectedError bool // does the test fail
expectedEvent string // does the test emit an event
name string // the name of the test case
ns string // the namespace to generate environment for
enableServiceLinks bool // enabling service links
container *v1.Container // the container to use
masterServiceNs string // the namespace to read master service info from
nilLister bool // whether the lister should be nil
configMap *v1.ConfigMap // an optional ConfigMap to pull from
secret *v1.Secret // an optional Secret to pull from
expectedEnvs []kubecontainer.EnvVar // a set of expected environment vars
expectedError bool // does the test fail
expectedEvent string // does the test emit an event
}{
{
name: "api server = Y, kubelet = Y",
ns: "test1",
name: "api server = Y, kubelet = Y",
ns: "test1",
enableServiceLinks: false,
container: &v1.Container{
Env: []v1.EnvVar{
{Name: "FOO", Value: "BAR"},
@@ -486,8 +488,9 @@ func TestMakeEnvironmentVariables(t *testing.T) {
},
},
{
name: "api server = Y, kubelet = N",
ns: "test1",
name: "api server = Y, kubelet = N",
ns: "test1",
enableServiceLinks: false,
container: &v1.Container{
Env: []v1.EnvVar{
{Name: "FOO", Value: "BAR"},
@@ -514,8 +517,31 @@ func TestMakeEnvironmentVariables(t *testing.T) {
},
},
{
name: "api server = N; kubelet = Y",
ns: "test1",
name: "api server = N; kubelet = Y",
ns: "test1",
enableServiceLinks: false,
container: &v1.Container{
Env: []v1.EnvVar{
{Name: "FOO", Value: "BAZ"},
},
},
masterServiceNs: metav1.NamespaceDefault,
nilLister: false,
expectedEnvs: []kubecontainer.EnvVar{
{Name: "FOO", Value: "BAZ"},
{Name: "KUBERNETES_SERVICE_HOST", Value: "1.2.3.1"},
{Name: "KUBERNETES_SERVICE_PORT", Value: "8081"},
{Name: "KUBERNETES_PORT", Value: "tcp://1.2.3.1:8081"},
{Name: "KUBERNETES_PORT_8081_TCP", Value: "tcp://1.2.3.1:8081"},
{Name: "KUBERNETES_PORT_8081_TCP_PROTO", Value: "tcp"},
{Name: "KUBERNETES_PORT_8081_TCP_PORT", Value: "8081"},
{Name: "KUBERNETES_PORT_8081_TCP_ADDR", Value: "1.2.3.1"},
},
},
{
name: "api server = N; kubelet = Y; service env vars",
ns: "test1",
enableServiceLinks: true,
container: &v1.Container{
Env: []v1.EnvVar{
{Name: "FOO", Value: "BAZ"},
@@ -542,8 +568,31 @@ func TestMakeEnvironmentVariables(t *testing.T) {
},
},
{
name: "master service in pod ns",
ns: "test2",
name: "master service in pod ns",
ns: "test2",
enableServiceLinks: false,
container: &v1.Container{
Env: []v1.EnvVar{
{Name: "FOO", Value: "ZAP"},
},
},
masterServiceNs: "kubernetes",
nilLister: false,
expectedEnvs: []kubecontainer.EnvVar{
{Name: "FOO", Value: "ZAP"},
{Name: "KUBERNETES_SERVICE_HOST", Value: "1.2.3.6"},
{Name: "KUBERNETES_SERVICE_PORT", Value: "8086"},
{Name: "KUBERNETES_PORT", Value: "tcp://1.2.3.6:8086"},
{Name: "KUBERNETES_PORT_8086_TCP", Value: "tcp://1.2.3.6:8086"},
{Name: "KUBERNETES_PORT_8086_TCP_PROTO", Value: "tcp"},
{Name: "KUBERNETES_PORT_8086_TCP_PORT", Value: "8086"},
{Name: "KUBERNETES_PORT_8086_TCP_ADDR", Value: "1.2.3.6"},
},
},
{
name: "master service in pod ns, service env vars",
ns: "test2",
enableServiceLinks: true,
container: &v1.Container{
Env: []v1.EnvVar{
{Name: "FOO", Value: "ZAP"},
@@ -570,11 +619,21 @@ func TestMakeEnvironmentVariables(t *testing.T) {
},
},
{
name: "pod in master service ns",
ns: "kubernetes",
container: &v1.Container{},
masterServiceNs: "kubernetes",
nilLister: false,
name: "pod in master service ns",
ns: "kubernetes",
enableServiceLinks: false,
container: &v1.Container{},
masterServiceNs: "kubernetes",
nilLister: false,
expectedEnvs: nil,
},
{
name: "pod in master service ns, service env vars",
ns: "kubernetes",
enableServiceLinks: true,
container: &v1.Container{},
masterServiceNs: "kubernetes",
nilLister: false,
expectedEnvs: []kubecontainer.EnvVar{
{Name: "NOT_SPECIAL_SERVICE_HOST", Value: "1.2.3.8"},
{Name: "NOT_SPECIAL_SERVICE_PORT", Value: "8088"},
@@ -593,8 +652,9 @@ func TestMakeEnvironmentVariables(t *testing.T) {
},
},
{
name: "downward api pod",
ns: "downward-api",
name: "downward api pod",
ns: "downward-api",
enableServiceLinks: false,
container: &v1.Container{
Env: []v1.EnvVar{
{
@@ -665,8 +725,105 @@ func TestMakeEnvironmentVariables(t *testing.T) {
},
},
{
name: "env expansion",
ns: "test1",
name: "env expansion",
ns: "test1",
enableServiceLinks: false,
container: &v1.Container{
Env: []v1.EnvVar{
{
Name: "TEST_LITERAL",
Value: "test-test-test",
},
{
Name: "POD_NAME",
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
APIVersion: "v1", //legacyscheme.Registry.GroupOrDie(v1.GroupName).GroupVersion.String(),
FieldPath: "metadata.name",
},
},
},
{
Name: "OUT_OF_ORDER_TEST",
Value: "$(OUT_OF_ORDER_TARGET)",
},
{
Name: "OUT_OF_ORDER_TARGET",
Value: "FOO",
},
{
Name: "EMPTY_VAR",
},
{
Name: "EMPTY_TEST",
Value: "foo-$(EMPTY_VAR)",
},
{
Name: "POD_NAME_TEST2",
Value: "test2-$(POD_NAME)",
},
{
Name: "POD_NAME_TEST3",
Value: "$(POD_NAME_TEST2)-3",
},
{
Name: "LITERAL_TEST",
Value: "literal-$(TEST_LITERAL)",
},
{
Name: "TEST_UNDEFINED",
Value: "$(UNDEFINED_VAR)",
},
},
},
masterServiceNs: "nothing",
nilLister: false,
expectedEnvs: []kubecontainer.EnvVar{
{
Name: "TEST_LITERAL",
Value: "test-test-test",
},
{
Name: "POD_NAME",
Value: "dapi-test-pod-name",
},
{
Name: "POD_NAME_TEST2",
Value: "test2-dapi-test-pod-name",
},
{
Name: "POD_NAME_TEST3",
Value: "test2-dapi-test-pod-name-3",
},
{
Name: "LITERAL_TEST",
Value: "literal-test-test-test",
},
{
Name: "OUT_OF_ORDER_TEST",
Value: "$(OUT_OF_ORDER_TARGET)",
},
{
Name: "OUT_OF_ORDER_TARGET",
Value: "FOO",
},
{
Name: "TEST_UNDEFINED",
Value: "$(UNDEFINED_VAR)",
},
{
Name: "EMPTY_VAR",
},
{
Name: "EMPTY_TEST",
Value: "foo-",
},
},
},
{
name: "env expansion, service env vars",
ns: "test1",
enableServiceLinks: true,
container: &v1.Container{
Env: []v1.EnvVar{
{
@@ -796,8 +953,9 @@ func TestMakeEnvironmentVariables(t *testing.T) {
},
},
{
name: "configmapkeyref_missing_optional",
ns: "test",
name: "configmapkeyref_missing_optional",
ns: "test",
enableServiceLinks: false,
container: &v1.Container{
Env: []v1.EnvVar{
{
@@ -816,8 +974,9 @@ func TestMakeEnvironmentVariables(t *testing.T) {
expectedEnvs: nil,
},
{
name: "configmapkeyref_missing_key_optional",
ns: "test",
name: "configmapkeyref_missing_key_optional",
ns: "test",
enableServiceLinks: false,
container: &v1.Container{
Env: []v1.EnvVar{
{
@@ -846,8 +1005,9 @@ func TestMakeEnvironmentVariables(t *testing.T) {
expectedEnvs: nil,
},
{
name: "secretkeyref_missing_optional",
ns: "test",
name: "secretkeyref_missing_optional",
ns: "test",
enableServiceLinks: false,
container: &v1.Container{
Env: []v1.EnvVar{
{
@@ -866,8 +1026,9 @@ func TestMakeEnvironmentVariables(t *testing.T) {
expectedEnvs: nil,
},
{
name: "secretkeyref_missing_key_optional",
ns: "test",
name: "secretkeyref_missing_key_optional",
ns: "test",
enableServiceLinks: false,
container: &v1.Container{
Env: []v1.EnvVar{
{
@@ -896,8 +1057,77 @@ func TestMakeEnvironmentVariables(t *testing.T) {
expectedEnvs: nil,
},
{
name: "configmap",
ns: "test1",
name: "configmap",
ns: "test1",
enableServiceLinks: false,
container: &v1.Container{
EnvFrom: []v1.EnvFromSource{
{
ConfigMapRef: &v1.ConfigMapEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-config-map"}},
},
{
Prefix: "p_",
ConfigMapRef: &v1.ConfigMapEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-config-map"}},
},
},
Env: []v1.EnvVar{
{
Name: "TEST_LITERAL",
Value: "test-test-test",
},
{
Name: "EXPANSION_TEST",
Value: "$(REPLACE_ME)",
},
{
Name: "DUPE_TEST",
Value: "ENV_VAR",
},
},
},
masterServiceNs: "nothing",
nilLister: false,
configMap: &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test1",
Name: "test-configmap",
},
Data: map[string]string{
"REPLACE_ME": "FROM_CONFIG_MAP",
"DUPE_TEST": "CONFIG_MAP",
},
},
expectedEnvs: []kubecontainer.EnvVar{
{
Name: "TEST_LITERAL",
Value: "test-test-test",
},
{
Name: "REPLACE_ME",
Value: "FROM_CONFIG_MAP",
},
{
Name: "EXPANSION_TEST",
Value: "FROM_CONFIG_MAP",
},
{
Name: "DUPE_TEST",
Value: "ENV_VAR",
},
{
Name: "p_REPLACE_ME",
Value: "FROM_CONFIG_MAP",
},
{
Name: "p_DUPE_TEST",
Value: "CONFIG_MAP",
},
},
},
{
name: "configmap, service env vars",
ns: "test1",
enableServiceLinks: true,
container: &v1.Container{
EnvFrom: []v1.EnvFromSource{
{
@@ -991,8 +1221,9 @@ func TestMakeEnvironmentVariables(t *testing.T) {
},
},
{
name: "configmap_missing",
ns: "test1",
name: "configmap_missing",
ns: "test1",
enableServiceLinks: false,
container: &v1.Container{
EnvFrom: []v1.EnvFromSource{
{ConfigMapRef: &v1.ConfigMapEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-config-map"}}},
@@ -1002,8 +1233,9 @@ func TestMakeEnvironmentVariables(t *testing.T) {
expectedError: true,
},
{
name: "configmap_missing_optional",
ns: "test",
name: "configmap_missing_optional",
ns: "test",
enableServiceLinks: false,
container: &v1.Container{
EnvFrom: []v1.EnvFromSource{
{ConfigMapRef: &v1.ConfigMapEnvSource{
@@ -1015,8 +1247,9 @@ func TestMakeEnvironmentVariables(t *testing.T) {
expectedEnvs: nil,
},
{
name: "configmap_invalid_keys",
ns: "test",
name: "configmap_invalid_keys",
ns: "test",
enableServiceLinks: false,
container: &v1.Container{
EnvFrom: []v1.EnvFromSource{
{ConfigMapRef: &v1.ConfigMapEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-config-map"}}},
@@ -1043,8 +1276,9 @@ func TestMakeEnvironmentVariables(t *testing.T) {
expectedEvent: "Warning InvalidEnvironmentVariableNames Keys [1234, 1z] from the EnvFrom configMap test/test-config-map were skipped since they are considered invalid environment variable names.",
},
{
name: "configmap_invalid_keys_valid",
ns: "test",
name: "configmap_invalid_keys_valid",
ns: "test",
enableServiceLinks: false,
container: &v1.Container{
EnvFrom: []v1.EnvFromSource{
{
@@ -1071,8 +1305,77 @@ func TestMakeEnvironmentVariables(t *testing.T) {
},
},
{
name: "secret",
ns: "test1",
name: "secret",
ns: "test1",
enableServiceLinks: false,
container: &v1.Container{
EnvFrom: []v1.EnvFromSource{
{
SecretRef: &v1.SecretEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-secret"}},
},
{
Prefix: "p_",
SecretRef: &v1.SecretEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-secret"}},
},
},
Env: []v1.EnvVar{
{
Name: "TEST_LITERAL",
Value: "test-test-test",
},
{
Name: "EXPANSION_TEST",
Value: "$(REPLACE_ME)",
},
{
Name: "DUPE_TEST",
Value: "ENV_VAR",
},
},
},
masterServiceNs: "nothing",
nilLister: false,
secret: &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test1",
Name: "test-secret",
},
Data: map[string][]byte{
"REPLACE_ME": []byte("FROM_SECRET"),
"DUPE_TEST": []byte("SECRET"),
},
},
expectedEnvs: []kubecontainer.EnvVar{
{
Name: "TEST_LITERAL",
Value: "test-test-test",
},
{
Name: "REPLACE_ME",
Value: "FROM_SECRET",
},
{
Name: "EXPANSION_TEST",
Value: "FROM_SECRET",
},
{
Name: "DUPE_TEST",
Value: "ENV_VAR",
},
{
Name: "p_REPLACE_ME",
Value: "FROM_SECRET",
},
{
Name: "p_DUPE_TEST",
Value: "SECRET",
},
},
},
{
name: "secret, service env vars",
ns: "test1",
enableServiceLinks: true,
container: &v1.Container{
EnvFrom: []v1.EnvFromSource{
{
@@ -1166,8 +1469,9 @@ func TestMakeEnvironmentVariables(t *testing.T) {
},
},
{
name: "secret_missing",
ns: "test1",
name: "secret_missing",
ns: "test1",
enableServiceLinks: false,
container: &v1.Container{
EnvFrom: []v1.EnvFromSource{
{SecretRef: &v1.SecretEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-secret"}}},
@@ -1177,8 +1481,9 @@ func TestMakeEnvironmentVariables(t *testing.T) {
expectedError: true,
},
{
name: "secret_missing_optional",
ns: "test",
name: "secret_missing_optional",
ns: "test",
enableServiceLinks: false,
container: &v1.Container{
EnvFrom: []v1.EnvFromSource{
{SecretRef: &v1.SecretEnvSource{
@@ -1190,8 +1495,9 @@ func TestMakeEnvironmentVariables(t *testing.T) {
expectedEnvs: nil,
},
{
name: "secret_invalid_keys",
ns: "test",
name: "secret_invalid_keys",
ns: "test",
enableServiceLinks: false,
container: &v1.Container{
EnvFrom: []v1.EnvFromSource{
{SecretRef: &v1.SecretEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-secret"}}},
@@ -1218,8 +1524,9 @@ func TestMakeEnvironmentVariables(t *testing.T) {
expectedEvent: "Warning InvalidEnvironmentVariableNames Keys [1234, 1z] from the EnvFrom secret test/test-secret were skipped since they are considered invalid environment variable names.",
},
{
name: "secret_invalid_keys_valid",
ns: "test",
name: "secret_invalid_keys_valid",
ns: "test",
enableServiceLinks: false,
container: &v1.Container{
EnvFrom: []v1.EnvFromSource{
{
@@ -1291,6 +1598,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
Spec: v1.PodSpec{
ServiceAccountName: "special",
NodeName: "node-name",
EnableServiceLinks: &tc.enableServiceLinks,
},
}
podIP := "1.2.3.4"