Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix docker-links-style env vars for services #1431

Merged
merged 1 commit into from
Sep 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions pkg/registry/pod/manifest_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,19 @@ func TestMakeManifestServices(t *testing.T) {
Value: "tcp://machine:8080",
},
{
Name: "TEST_PORT_900_TCP",
Name: "TEST_PORT_8080_TCP",
Value: "tcp://machine:8080",
},
{
Name: "TEST_PORT_900_TCP_PROTO",
Name: "TEST_PORT_8080_TCP_PROTO",
Value: "tcp",
},
{
Name: "TEST_PORT_900_TCP_PORT",
Name: "TEST_PORT_8080_TCP_PORT",
Value: "8080",
},
{
Name: "TEST_PORT_900_TCP_ADDR",
Name: "TEST_PORT_8080_TCP_ADDR",
Value: "machine",
},
{
Expand Down Expand Up @@ -197,19 +197,19 @@ func TestMakeManifestServicesExistingEnvVar(t *testing.T) {
Value: "tcp://machine:8080",
},
{
Name: "TEST_PORT_900_TCP",
Name: "TEST_PORT_8080_TCP",
Value: "tcp://machine:8080",
},
{
Name: "TEST_PORT_900_TCP_PROTO",
Name: "TEST_PORT_8080_TCP_PROTO",
Value: "tcp",
},
{
Name: "TEST_PORT_900_TCP_PORT",
Name: "TEST_PORT_8080_TCP_PORT",
Value: "8080",
},
{
Name: "TEST_PORT_900_TCP_ADDR",
Name: "TEST_PORT_8080_TCP_ADDR",
Value: "machine",
},
{
Expand Down
16 changes: 7 additions & 9 deletions pkg/registry/service/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,25 +222,23 @@ func makeEnvVariableName(str string) string {

func makeLinkVariables(service api.Service, machine string) []api.EnvVar {
prefix := makeEnvVariableName(service.ID)
var port string
if service.ContainerPort.Kind == util.IntstrString {
port = service.ContainerPort.StrVal
} else {
port = strconv.Itoa(service.ContainerPort.IntVal)
protocol := "TCP"
if service.Protocol != "" {
protocol = service.Protocol
}
portPrefix := prefix + "_PORT_" + makeEnvVariableName(port) + "_TCP"
portPrefix := fmt.Sprintf("%s_PORT_%d_%s", prefix, service.Port, strings.ToUpper(protocol))
return []api.EnvVar{
{
Name: prefix + "_PORT",
Value: fmt.Sprintf("tcp://%s:%d", machine, service.Port),
Value: fmt.Sprintf("%s://%s:%d", strings.ToLower(protocol), machine, service.Port),
},
{
Name: portPrefix,
Value: fmt.Sprintf("tcp://%s:%d", machine, service.Port),
Value: fmt.Sprintf("%s://%s:%d", strings.ToLower(protocol), machine, service.Port),
},
{
Name: portPrefix + "_PROTO",
Value: "tcp",
Value: strings.ToLower(protocol),
},
{
Name: portPrefix + "_PORT",
Expand Down
62 changes: 52 additions & 10 deletions pkg/registry/service/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package service

import (
"fmt"
"reflect"
"testing"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
Expand All @@ -27,7 +28,6 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/minion"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)

func TestServiceRegistryCreate(t *testing.T) {
Expand Down Expand Up @@ -237,23 +237,65 @@ func TestServiceRegistryDeleteExternal(t *testing.T) {
}

func TestServiceRegistryMakeLinkVariables(t *testing.T) {
service := api.Service{
JSONBase: api.JSONBase{ID: "foo-bar"},
Selector: map[string]string{"bar": "baz"},
ContainerPort: util.IntOrString{Kind: util.IntstrString, StrVal: "a-b-c"},
}
registry := registrytest.NewServiceRegistry()
registry.List = api.ServiceList{
Items: []api.Service{service},
Items: []api.Service{
{
JSONBase: api.JSONBase{ID: "foo-bar"},
Selector: map[string]string{"bar": "baz"},
Port: 8080,
Protocol: "TCP",
},
{
JSONBase: api.JSONBase{ID: "abc-123"},
Selector: map[string]string{"bar": "baz"},
Port: 8081,
Protocol: "UDP",
},
{
JSONBase: api.JSONBase{ID: "q-u-u-x"},
Selector: map[string]string{"bar": "baz"},
Port: 8082,
Protocol: "",
},
},
}
machine := "machine"
vars, err := GetServiceEnvironmentVariables(registry, machine)
if err != nil {
t.Errorf("Unexpected err: %v", err)
}
for _, v := range vars {
if !util.IsCIdentifier(v.Name) {
t.Errorf("Environment variable name is not valid: %v", v.Name)
expected := []api.EnvVar{
{Name: "FOO_BAR_SERVICE_HOST", Value: "machine"},
{Name: "FOO_BAR_SERVICE_PORT", Value: "8080"},
{Name: "FOO_BAR_PORT", Value: "tcp://machine:8080"},
{Name: "FOO_BAR_PORT_8080_TCP", Value: "tcp://machine: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: "machine"},
{Name: "ABC_123_SERVICE_HOST", Value: "machine"},
{Name: "ABC_123_SERVICE_PORT", Value: "8081"},
{Name: "ABC_123_PORT", Value: "udp://machine:8081"},
{Name: "ABC_123_PORT_8081_UDP", Value: "udp://machine: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: "machine"},
{Name: "Q_U_U_X_SERVICE_HOST", Value: "machine"},
{Name: "Q_U_U_X_SERVICE_PORT", Value: "8082"},
{Name: "Q_U_U_X_PORT", Value: "tcp://machine:8082"},
{Name: "Q_U_U_X_PORT_8082_TCP", Value: "tcp://machine: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: "machine"},
{Name: "SERVICE_HOST", Value: "machine"},
}
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])
}
}
}
Expand Down