// getServiceEnvironmentVariables populates a list of environment variables that are used // in the container environment to get access to services. func (b *BasicBoundPodFactory) getServiceEnvironmentVariables(ctx api.Context, registry service.Registry, machine string) ([]api.EnvVar, error) { var result []api.EnvVar servicesInNs, err := registry.ListServices(ctx) if err != nil { return result, err } masterServices, err := registry.ListServices(api.WithNamespace(api.NewContext(), b.MasterServiceNamespace)) if err != nil { return result, err } projection := map[string]api.Service{} services := []api.Service{} for _, service := range masterServices.Items { if masterServiceNames.Has(service.Name) { projection[service.Name] = service } } for _, service := range servicesInNs.Items { projection[service.Name] = service } for _, service := range projection { services = append(services, service) } return envvars.FromServices(&api.ServiceList{Items: services}), nil }
// getServiceEnvironmentVariables populates a list of environment variables that are use // in the container environment to get access to services. func getServiceEnvironmentVariables(ctx api.Context, registry service.Registry, machine string) ([]api.EnvVar, error) { var result []api.EnvVar services, err := registry.ListServices(ctx) if err != nil { return result, err } return envvars.FromServices(services), nil }
// getServiceEnvVarMap makes a map[string]string of env vars for services a pod in namespace ns should see func (kl *Kubelet) getServiceEnvVarMap(ns string) (map[string]string, error) { var ( serviceMap = make(map[string]api.Service) m = make(map[string]string) ) // Get all service resources from the master (via a cache), // and populate them into service enviroment variables. if kl.serviceLister == nil { // Kubelets without masters (e.g. plain GCE ContainerVM) don't set env vars. return m, nil } services, err := kl.serviceLister.List() if err != nil { return m, fmt.Errorf("Failed to list services when setting up env vars.") } // project the services in namespace ns onto the master services for _, service := range services.Items { serviceName := service.Name switch service.Namespace { // for the case whether the master service namespace is the namespace the pod // is in, pod should receive all the pods in the namespace. // // ordering of the case clauses below enforces this case ns: serviceMap[serviceName] = service case kl.masterServiceNamespace: if masterServices.Has(serviceName) { _, exists := serviceMap[serviceName] if !exists { serviceMap[serviceName] = service } } } } services.Items = []api.Service{} for _, service := range serviceMap { services.Items = append(services.Items, service) } for _, e := range envvars.FromServices(&services) { m[e.Name] = e.Value } return m, nil }
func TestFromServices(t *testing.T) { sl := api.ServiceList{ Items: []api.Service{ { ObjectMeta: api.ObjectMeta{Name: "foo-bar"}, Spec: api.ServiceSpec{ Port: 8080, Selector: map[string]string{"bar": "baz"}, Protocol: "TCP", PortalIP: "1.2.3.4", }, }, { ObjectMeta: api.ObjectMeta{Name: "abc-123"}, Spec: api.ServiceSpec{ Port: 8081, Selector: map[string]string{"bar": "baz"}, Protocol: "UDP", PortalIP: "5.6.7.8", }, }, { ObjectMeta: api.ObjectMeta{Name: "q-u-u-x"}, Spec: api.ServiceSpec{ Port: 8082, Selector: map[string]string{"bar": "baz"}, Protocol: "TCP", PortalIP: "9.8.7.6", }, }, }, } vars := envvars.FromServices(&sl) expected := []api.EnvVar{ {Name: "FOO_BAR_SERVICE_HOST", Value: "1.2.3.4"}, {Name: "FOO_BAR_SERVICE_PORT", Value: "8080"}, {Name: "FOO_BAR_PORT", Value: "tcp://1.2.3.4:8080"}, {Name: "FOO_BAR_PORT_8080_TCP", Value: "tcp://1.2.3.4:8080"}, {Name: "FOO_BAR_PORT_8080_TCP_PROTO", Value: "tcp"}, {Name: "FOO_BAR_PORT_8080_TCP_PORT", Value: "8080"}, {Name: "FOO_BAR_PORT_8080_TCP_ADDR", Value: "1.2.3.4"}, {Name: "ABC_123_SERVICE_HOST", Value: "5.6.7.8"}, {Name: "ABC_123_SERVICE_PORT", Value: "8081"}, {Name: "ABC_123_PORT", Value: "udp://5.6.7.8:8081"}, {Name: "ABC_123_PORT_8081_UDP", Value: "udp://5.6.7.8:8081"}, {Name: "ABC_123_PORT_8081_UDP_PROTO", Value: "udp"}, {Name: "ABC_123_PORT_8081_UDP_PORT", Value: "8081"}, {Name: "ABC_123_PORT_8081_UDP_ADDR", Value: "5.6.7.8"}, {Name: "Q_U_U_X_SERVICE_HOST", Value: "9.8.7.6"}, {Name: "Q_U_U_X_SERVICE_PORT", Value: "8082"}, {Name: "Q_U_U_X_PORT", Value: "tcp://9.8.7.6:8082"}, {Name: "Q_U_U_X_PORT_8082_TCP", Value: "tcp://9.8.7.6:8082"}, {Name: "Q_U_U_X_PORT_8082_TCP_PROTO", Value: "tcp"}, {Name: "Q_U_U_X_PORT_8082_TCP_PORT", Value: "8082"}, {Name: "Q_U_U_X_PORT_8082_TCP_ADDR", Value: "9.8.7.6"}, } if len(vars) != len(expected) { t.Errorf("Expected %d env vars, got: %+v", len(expected), vars) return } for i := range expected { if !reflect.DeepEqual(vars[i], expected[i]) { t.Errorf("expected %#v, got %#v", vars[i], expected[i]) } } }