Fix to inject KUBERNETES_ env vars when enableServiceLinks is

false and the pod is in the master namespace.
This commit is contained in:
Brad Hoekstra 2018-08-30 14:42:11 -04:00
parent ac8799a80d
commit e8366c8e99
2 changed files with 14 additions and 15 deletions

View File

@ -514,24 +514,14 @@ func (kl *Kubelet) getServiceEnvVarMap(ns string, enableServiceLinks *bool) (map
}
serviceName := service.Name
switch service.Namespace {
// for the case whether the master service namespace is the namespace the pod
// is in, the pod should receive all the services in the namespace.
//
// ordering of the case clauses below enforces this
case ns:
// If service links are not enabled, we shouldn't add services from
// the same namespace.
if !*enableServiceLinks {
continue
if service.Namespace == kl.masterServiceNamespace && masterServices.Has(serviceName) {
if _, exists := serviceMap[serviceName]; !exists {
serviceMap[serviceName] = service
}
} else if service.Namespace == ns && *enableServiceLinks {
serviceMap[serviceName] = service
case kl.masterServiceNamespace:
if masterServices.Has(serviceName) {
if _, exists := serviceMap[serviceName]; !exists {
serviceMap[serviceName] = service
}
}
}
}

View File

@ -36,6 +36,7 @@ import (
utilfeature "k8s.io/apiserver/pkg/util/feature"
core "k8s.io/client-go/testing"
"k8s.io/client-go/tools/record"
// TODO: remove this import if
// api.Registry.GroupOrDie(v1.GroupName).GroupVersions[0].String() is changed
// to "v1"?
@ -625,7 +626,15 @@ func TestMakeEnvironmentVariables(t *testing.T) {
container: &v1.Container{},
masterServiceNs: "kubernetes",
nilLister: false,
expectedEnvs: nil,
expectedEnvs: []kubecontainer.EnvVar{
{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: "pod in master service ns, service env vars",