mirror of https://github.com/k3s-io/k3s
Merge pull request #74529 from liggitt/kubelet-service-links-error
Kubelet service links errorpull/564/head
commit
0a4308f641
|
@ -546,11 +546,11 @@ func (kl *Kubelet) getServiceEnvVarMap(ns string, enableServiceLinks bool) (map[
|
|||
|
||||
// Make the environment variables for a pod in the given namespace.
|
||||
func (kl *Kubelet) makeEnvironmentVariables(pod *v1.Pod, container *v1.Container, podIP string) ([]kubecontainer.EnvVar, error) {
|
||||
var result []kubecontainer.EnvVar
|
||||
enableServiceLinks := v1.DefaultEnableServiceLinks
|
||||
if pod.Spec.EnableServiceLinks != nil {
|
||||
enableServiceLinks = *pod.Spec.EnableServiceLinks
|
||||
if pod.Spec.EnableServiceLinks == nil {
|
||||
return nil, fmt.Errorf("nil pod.spec.enableServiceLinks encountered, cannot construct envvars")
|
||||
}
|
||||
|
||||
var result []kubecontainer.EnvVar
|
||||
// Note: These are added to the docker Config, but are not included in the checksum computed
|
||||
// by kubecontainer.HashContainer(...). That way, we can still determine whether an
|
||||
// v1.Container is already running by its hash. (We don't want to restart a container just
|
||||
|
@ -560,7 +560,7 @@ func (kl *Kubelet) makeEnvironmentVariables(pod *v1.Pod, container *v1.Container
|
|||
// To avoid this users can: (1) wait between starting a service and starting; or (2) detect
|
||||
// missing service env var and exit and be restarted; or (3) use DNS instead of env vars
|
||||
// and keep trying to resolve the DNS name of the service (recommended).
|
||||
serviceEnv, err := kl.getServiceEnvVarMap(pod.Namespace, enableServiceLinks)
|
||||
serviceEnv, err := kl.getServiceEnvVarMap(pod.Namespace, *pod.Spec.EnableServiceLinks)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
|
|
@ -429,10 +429,12 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
buildService("not-special", "kubernetes", "", "TCP", 8088),
|
||||
}
|
||||
|
||||
trueValue := true
|
||||
falseValue := false
|
||||
testCases := []struct {
|
||||
name string // the name of the test case
|
||||
ns string // the namespace to generate environment for
|
||||
enableServiceLinks bool // enabling service links
|
||||
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
|
||||
|
@ -445,7 +447,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "api server = Y, kubelet = Y",
|
||||
ns: "test1",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
Env: []v1.EnvVar{
|
||||
{Name: "FOO", Value: "BAR"},
|
||||
|
@ -481,7 +483,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "api server = Y, kubelet = N",
|
||||
ns: "test1",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
Env: []v1.EnvVar{
|
||||
{Name: "FOO", Value: "BAR"},
|
||||
|
@ -510,7 +512,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "api server = N; kubelet = Y",
|
||||
ns: "test1",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
Env: []v1.EnvVar{
|
||||
{Name: "FOO", Value: "BAZ"},
|
||||
|
@ -532,7 +534,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "api server = N; kubelet = Y; service env vars",
|
||||
ns: "test1",
|
||||
enableServiceLinks: true,
|
||||
enableServiceLinks: &trueValue,
|
||||
container: &v1.Container{
|
||||
Env: []v1.EnvVar{
|
||||
{Name: "FOO", Value: "BAZ"},
|
||||
|
@ -561,7 +563,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "master service in pod ns",
|
||||
ns: "test2",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
Env: []v1.EnvVar{
|
||||
{Name: "FOO", Value: "ZAP"},
|
||||
|
@ -583,7 +585,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "master service in pod ns, service env vars",
|
||||
ns: "test2",
|
||||
enableServiceLinks: true,
|
||||
enableServiceLinks: &trueValue,
|
||||
container: &v1.Container{
|
||||
Env: []v1.EnvVar{
|
||||
{Name: "FOO", Value: "ZAP"},
|
||||
|
@ -612,7 +614,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "pod in master service ns",
|
||||
ns: "kubernetes",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{},
|
||||
masterServiceNs: "kubernetes",
|
||||
nilLister: false,
|
||||
|
@ -629,7 +631,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "pod in master service ns, service env vars",
|
||||
ns: "kubernetes",
|
||||
enableServiceLinks: true,
|
||||
enableServiceLinks: &trueValue,
|
||||
container: &v1.Container{},
|
||||
masterServiceNs: "kubernetes",
|
||||
nilLister: false,
|
||||
|
@ -653,7 +655,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "downward api pod",
|
||||
ns: "downward-api",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
Env: []v1.EnvVar{
|
||||
{
|
||||
|
@ -726,7 +728,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "env expansion",
|
||||
ns: "test1",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
Env: []v1.EnvVar{
|
||||
{
|
||||
|
@ -822,7 +824,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "env expansion, service env vars",
|
||||
ns: "test1",
|
||||
enableServiceLinks: true,
|
||||
enableServiceLinks: &trueValue,
|
||||
container: &v1.Container{
|
||||
Env: []v1.EnvVar{
|
||||
{
|
||||
|
@ -954,7 +956,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "configmapkeyref_missing_optional",
|
||||
ns: "test",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
Env: []v1.EnvVar{
|
||||
{
|
||||
|
@ -975,7 +977,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "configmapkeyref_missing_key_optional",
|
||||
ns: "test",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
Env: []v1.EnvVar{
|
||||
{
|
||||
|
@ -1006,7 +1008,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "secretkeyref_missing_optional",
|
||||
ns: "test",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
Env: []v1.EnvVar{
|
||||
{
|
||||
|
@ -1027,7 +1029,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "secretkeyref_missing_key_optional",
|
||||
ns: "test",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
Env: []v1.EnvVar{
|
||||
{
|
||||
|
@ -1058,7 +1060,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "configmap",
|
||||
ns: "test1",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
EnvFrom: []v1.EnvFromSource{
|
||||
{
|
||||
|
@ -1126,7 +1128,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "configmap, service env vars",
|
||||
ns: "test1",
|
||||
enableServiceLinks: true,
|
||||
enableServiceLinks: &trueValue,
|
||||
container: &v1.Container{
|
||||
EnvFrom: []v1.EnvFromSource{
|
||||
{
|
||||
|
@ -1222,7 +1224,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "configmap_missing",
|
||||
ns: "test1",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
EnvFrom: []v1.EnvFromSource{
|
||||
{ConfigMapRef: &v1.ConfigMapEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-config-map"}}},
|
||||
|
@ -1234,7 +1236,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "configmap_missing_optional",
|
||||
ns: "test",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
EnvFrom: []v1.EnvFromSource{
|
||||
{ConfigMapRef: &v1.ConfigMapEnvSource{
|
||||
|
@ -1248,7 +1250,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "configmap_invalid_keys",
|
||||
ns: "test",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
EnvFrom: []v1.EnvFromSource{
|
||||
{ConfigMapRef: &v1.ConfigMapEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-config-map"}}},
|
||||
|
@ -1277,7 +1279,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "configmap_invalid_keys_valid",
|
||||
ns: "test",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
EnvFrom: []v1.EnvFromSource{
|
||||
{
|
||||
|
@ -1306,7 +1308,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "secret",
|
||||
ns: "test1",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
EnvFrom: []v1.EnvFromSource{
|
||||
{
|
||||
|
@ -1374,7 +1376,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "secret, service env vars",
|
||||
ns: "test1",
|
||||
enableServiceLinks: true,
|
||||
enableServiceLinks: &trueValue,
|
||||
container: &v1.Container{
|
||||
EnvFrom: []v1.EnvFromSource{
|
||||
{
|
||||
|
@ -1470,7 +1472,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "secret_missing",
|
||||
ns: "test1",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
EnvFrom: []v1.EnvFromSource{
|
||||
{SecretRef: &v1.SecretEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-secret"}}},
|
||||
|
@ -1482,7 +1484,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "secret_missing_optional",
|
||||
ns: "test",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
EnvFrom: []v1.EnvFromSource{
|
||||
{SecretRef: &v1.SecretEnvSource{
|
||||
|
@ -1496,7 +1498,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "secret_invalid_keys",
|
||||
ns: "test",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
EnvFrom: []v1.EnvFromSource{
|
||||
{SecretRef: &v1.SecretEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-secret"}}},
|
||||
|
@ -1525,7 +1527,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
{
|
||||
name: "secret_invalid_keys_valid",
|
||||
ns: "test",
|
||||
enableServiceLinks: false,
|
||||
enableServiceLinks: &falseValue,
|
||||
container: &v1.Container{
|
||||
EnvFrom: []v1.EnvFromSource{
|
||||
{
|
||||
|
@ -1551,6 +1553,30 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "nil_enableServiceLinks",
|
||||
ns: "test",
|
||||
enableServiceLinks: nil,
|
||||
container: &v1.Container{
|
||||
EnvFrom: []v1.EnvFromSource{
|
||||
{
|
||||
Prefix: "p_",
|
||||
SecretRef: &v1.SecretEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-secret"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
masterServiceNs: "",
|
||||
secret: &v1.Secret{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: "test1",
|
||||
Name: "test-secret",
|
||||
},
|
||||
Data: map[string][]byte{
|
||||
"1234.name": []byte("abc"),
|
||||
},
|
||||
},
|
||||
expectedError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
|
@ -1597,7 +1623,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
|||
Spec: v1.PodSpec{
|
||||
ServiceAccountName: "special",
|
||||
NodeName: "node-name",
|
||||
EnableServiceLinks: &tc.enableServiceLinks,
|
||||
EnableServiceLinks: tc.enableServiceLinks,
|
||||
},
|
||||
}
|
||||
podIP := "1.2.3.4"
|
||||
|
|
Loading…
Reference in New Issue