Skip to content

Commit

Permalink
Merge pull request kubernetes#1415 from thockin/svcenv
Browse files Browse the repository at this point in the history
Add per-service env vars for *_SERVICE_HOST
  • Loading branch information
thockin committed Sep 24, 2014
2 parents 9564dac + 089c560 commit f1f54ac
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 15 deletions.
6 changes: 3 additions & 3 deletions examples/guestbook/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ redisSlaveController brendanburns/redis-slave name=redisslave 2
The redis slave configures itself by looking for the Kubernetes service environment variables in the container environment. In particular, the redis slave is started with the following command:

```shell
redis-server --slaveof $SERVICE_HOST $REDISMASTER_SERVICE_PORT
redis-server --slaveof ${REDISMASTER_SERVICE_HOST:-$SERVICE_HOST} $REDISMASTER_SERVICE_PORT
```

Once that's up you can list the pods in the cluster, to verify that the master and slaves are running:
Expand Down Expand Up @@ -270,7 +270,7 @@ if (isset($_GET['cmd']) === true) {
if ($_GET['cmd'] == 'set') {
$client = new Predis\Client([
'scheme' => 'tcp',
'host' => getenv('SERVICE_HOST'),
'host' => getenv('REDISMASTER_SERVICE_HOST') ?: getenv('SERVICE_HOST'),
'port' => getenv('REDISMASTER_SERVICE_PORT'),
]);
$client->set($_GET['key'], $_GET['value']);
Expand All @@ -283,7 +283,7 @@ if (isset($_GET['cmd']) === true) {
}
$client = new Predis\Client([
'scheme' => 'tcp',
'host' => getenv('SERVICE_HOST'),
'host' => getenv('REDISMASTER_SERVICE_HOST') ?: getenv('SERVICE_HOST'),
'port' => $read_port,
]);

Expand Down
4 changes: 2 additions & 2 deletions examples/guestbook/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
if ($_GET['cmd'] == 'set') {
$client = new Predis\Client([
'scheme' => 'tcp',
'host' => getenv('SERVICE_HOST'),
'host' => getenv('REDISMASTER_SERVICE_HOST') ?: getenv('SERVICE_HOST'),
'port' => getenv('REDISMASTER_SERVICE_PORT'),
]);
$client->set($_GET['key'], $_GET['value']);
Expand All @@ -25,7 +25,7 @@
}
$client = new Predis\Client([
'scheme' => 'tcp',
'host' => getenv('SERVICE_HOST'),
'host' => getenv('REDISMASTER_SERVICE_HOST') ?: getenv('SERVICE_HOST'),
'port' => $read_port,
]);

Expand Down
4 changes: 2 additions & 2 deletions examples/guestbook/php-redis/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
if ($_GET['cmd'] == 'set') {
$client = new Predis\Client([
'scheme' => 'tcp',
'host' => getenv('SERVICE_HOST'),
'host' => getenv('REDISMASTER_SERVICE_HOST') ?: getenv('SERVICE_HOST'),
'port' => getenv('REDISMASTER_SERVICE_PORT'),
]);
$client->set($_GET['key'], $_GET['value']);
Expand All @@ -25,7 +25,7 @@
}
$client = new Predis\Client([
'scheme' => 'tcp',
'host' => getenv('SERVICE_HOST'),
'host' => getenv('REDISMASTER_SERVICE_HOST') ?: getenv('SERVICE_HOST'),
'port' => $read_port,
]);

Expand Down
2 changes: 1 addition & 1 deletion examples/guestbook/redis-slave/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

redis-server --slaveof $SERVICE_HOST $REDISMASTER_SERVICE_PORT
redis-server --slaveof ${REDISMASTER_SERVICE_HOST:-$SERVICE_HOST} $REDISMASTER_SERVICE_PORT
16 changes: 12 additions & 4 deletions pkg/registry/pod/manifest_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ func TestMakeManifestServices(t *testing.T) {

container := manifest.Containers[0]
envs := []api.EnvVar{
{
Name: "TEST_SERVICE_HOST",
Value: "machine",
},
{
Name: "TEST_SERVICE_PORT",
Value: "8080",
Expand Down Expand Up @@ -123,8 +127,8 @@ func TestMakeManifestServices(t *testing.T) {
Value: "machine",
},
}
if len(container.Env) != 7 {
t.Errorf("Expected 7 env vars, got %d: %#v", len(container.Env), manifest)
if len(container.Env) != len(envs) {
t.Errorf("Expected %d env vars, got %d: %#v", len(envs), len(container.Env), manifest)
return
}
for ix := range container.Env {
Expand Down Expand Up @@ -180,6 +184,10 @@ func TestMakeManifestServicesExistingEnvVar(t *testing.T) {
Name: "foo",
Value: "bar",
},
{
Name: "TEST_SERVICE_HOST",
Value: "machine",
},
{
Name: "TEST_SERVICE_PORT",
Value: "8080",
Expand Down Expand Up @@ -209,8 +217,8 @@ func TestMakeManifestServicesExistingEnvVar(t *testing.T) {
Value: "machine",
},
}
if len(container.Env) != 8 {
t.Errorf("Expected 8 env vars, got: %#v", manifest)
if len(container.Env) != len(envs) {
t.Errorf("Expected %d env vars, got: %#v", len(envs), manifest)
return
}
for ix := range container.Env {
Expand Down
12 changes: 9 additions & 3 deletions pkg/registry/service/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,17 @@ func GetServiceEnvironmentVariables(registry Registry, machine string) ([]api.En
return result, err
}
for _, service := range services.Items {
name := makeEnvVariableName(service.ID) + "_SERVICE_PORT"
value := strconv.Itoa(service.Port)
result = append(result, api.EnvVar{Name: name, Value: value})
// Host
name := makeEnvVariableName(service.ID) + "_SERVICE_HOST"
result = append(result, api.EnvVar{Name: name, Value: machine})
// Port
name = makeEnvVariableName(service.ID) + "_SERVICE_PORT"
result = append(result, api.EnvVar{Name: name, Value: strconv.Itoa(service.Port)})
// Docker-compatible vars.
result = append(result, makeLinkVariables(service, machine)...)
}
// The 'SERVICE_HOST' variable is deprecated.
// TODO(thockin): get rid of it once ip-per-service is in and "deployed".
result = append(result, api.EnvVar{Name: "SERVICE_HOST", Value: machine})
return result, nil
}
Expand Down

0 comments on commit f1f54ac

Please sign in to comment.