mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-06 02:34:03 +00:00
Merge pull request #1415 from thockin/svcenv
Add per-service env vars for *_SERVICE_HOST
This commit is contained in:
commit
f1f54ac72e
@ -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:
|
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
|
```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:
|
Once that's up you can list the pods in the cluster, to verify that the master and slaves are running:
|
||||||
@ -270,7 +270,7 @@ if (isset($_GET['cmd']) === true) {
|
|||||||
if ($_GET['cmd'] == 'set') {
|
if ($_GET['cmd'] == 'set') {
|
||||||
$client = new Predis\Client([
|
$client = new Predis\Client([
|
||||||
'scheme' => 'tcp',
|
'scheme' => 'tcp',
|
||||||
'host' => getenv('SERVICE_HOST'),
|
'host' => getenv('REDISMASTER_SERVICE_HOST') ?: getenv('SERVICE_HOST'),
|
||||||
'port' => getenv('REDISMASTER_SERVICE_PORT'),
|
'port' => getenv('REDISMASTER_SERVICE_PORT'),
|
||||||
]);
|
]);
|
||||||
$client->set($_GET['key'], $_GET['value']);
|
$client->set($_GET['key'], $_GET['value']);
|
||||||
@ -283,7 +283,7 @@ if (isset($_GET['cmd']) === true) {
|
|||||||
}
|
}
|
||||||
$client = new Predis\Client([
|
$client = new Predis\Client([
|
||||||
'scheme' => 'tcp',
|
'scheme' => 'tcp',
|
||||||
'host' => getenv('SERVICE_HOST'),
|
'host' => getenv('REDISMASTER_SERVICE_HOST') ?: getenv('SERVICE_HOST'),
|
||||||
'port' => $read_port,
|
'port' => $read_port,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ if (isset($_GET['cmd']) === true) {
|
|||||||
if ($_GET['cmd'] == 'set') {
|
if ($_GET['cmd'] == 'set') {
|
||||||
$client = new Predis\Client([
|
$client = new Predis\Client([
|
||||||
'scheme' => 'tcp',
|
'scheme' => 'tcp',
|
||||||
'host' => getenv('SERVICE_HOST'),
|
'host' => getenv('REDISMASTER_SERVICE_HOST') ?: getenv('SERVICE_HOST'),
|
||||||
'port' => getenv('REDISMASTER_SERVICE_PORT'),
|
'port' => getenv('REDISMASTER_SERVICE_PORT'),
|
||||||
]);
|
]);
|
||||||
$client->set($_GET['key'], $_GET['value']);
|
$client->set($_GET['key'], $_GET['value']);
|
||||||
@ -25,7 +25,7 @@ if (isset($_GET['cmd']) === true) {
|
|||||||
}
|
}
|
||||||
$client = new Predis\Client([
|
$client = new Predis\Client([
|
||||||
'scheme' => 'tcp',
|
'scheme' => 'tcp',
|
||||||
'host' => getenv('SERVICE_HOST'),
|
'host' => getenv('REDISMASTER_SERVICE_HOST') ?: getenv('SERVICE_HOST'),
|
||||||
'port' => $read_port,
|
'port' => $read_port,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ if (isset($_GET['cmd']) === true) {
|
|||||||
if ($_GET['cmd'] == 'set') {
|
if ($_GET['cmd'] == 'set') {
|
||||||
$client = new Predis\Client([
|
$client = new Predis\Client([
|
||||||
'scheme' => 'tcp',
|
'scheme' => 'tcp',
|
||||||
'host' => getenv('SERVICE_HOST'),
|
'host' => getenv('REDISMASTER_SERVICE_HOST') ?: getenv('SERVICE_HOST'),
|
||||||
'port' => getenv('REDISMASTER_SERVICE_PORT'),
|
'port' => getenv('REDISMASTER_SERVICE_PORT'),
|
||||||
]);
|
]);
|
||||||
$client->set($_GET['key'], $_GET['value']);
|
$client->set($_GET['key'], $_GET['value']);
|
||||||
@ -25,7 +25,7 @@ if (isset($_GET['cmd']) === true) {
|
|||||||
}
|
}
|
||||||
$client = new Predis\Client([
|
$client = new Predis\Client([
|
||||||
'scheme' => 'tcp',
|
'scheme' => 'tcp',
|
||||||
'host' => getenv('SERVICE_HOST'),
|
'host' => getenv('REDISMASTER_SERVICE_HOST') ?: getenv('SERVICE_HOST'),
|
||||||
'port' => $read_port,
|
'port' => $read_port,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
redis-server --slaveof $SERVICE_HOST $REDISMASTER_SERVICE_PORT
|
redis-server --slaveof ${REDISMASTER_SERVICE_HOST:-$SERVICE_HOST} $REDISMASTER_SERVICE_PORT
|
||||||
|
@ -94,6 +94,10 @@ func TestMakeManifestServices(t *testing.T) {
|
|||||||
|
|
||||||
container := manifest.Containers[0]
|
container := manifest.Containers[0]
|
||||||
envs := []api.EnvVar{
|
envs := []api.EnvVar{
|
||||||
|
{
|
||||||
|
Name: "TEST_SERVICE_HOST",
|
||||||
|
Value: "machine",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Name: "TEST_SERVICE_PORT",
|
Name: "TEST_SERVICE_PORT",
|
||||||
Value: "8080",
|
Value: "8080",
|
||||||
@ -123,8 +127,8 @@ func TestMakeManifestServices(t *testing.T) {
|
|||||||
Value: "machine",
|
Value: "machine",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if len(container.Env) != 7 {
|
if len(container.Env) != len(envs) {
|
||||||
t.Errorf("Expected 7 env vars, got %d: %#v", len(container.Env), manifest)
|
t.Errorf("Expected %d env vars, got %d: %#v", len(envs), len(container.Env), manifest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for ix := range container.Env {
|
for ix := range container.Env {
|
||||||
@ -180,6 +184,10 @@ func TestMakeManifestServicesExistingEnvVar(t *testing.T) {
|
|||||||
Name: "foo",
|
Name: "foo",
|
||||||
Value: "bar",
|
Value: "bar",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "TEST_SERVICE_HOST",
|
||||||
|
Value: "machine",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Name: "TEST_SERVICE_PORT",
|
Name: "TEST_SERVICE_PORT",
|
||||||
Value: "8080",
|
Value: "8080",
|
||||||
@ -209,8 +217,8 @@ func TestMakeManifestServicesExistingEnvVar(t *testing.T) {
|
|||||||
Value: "machine",
|
Value: "machine",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if len(container.Env) != 8 {
|
if len(container.Env) != len(envs) {
|
||||||
t.Errorf("Expected 8 env vars, got: %#v", manifest)
|
t.Errorf("Expected %d env vars, got: %#v", len(envs), manifest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for ix := range container.Env {
|
for ix := range container.Env {
|
||||||
|
@ -148,11 +148,17 @@ func GetServiceEnvironmentVariables(registry Registry, machine string) ([]api.En
|
|||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
for _, service := range services.Items {
|
for _, service := range services.Items {
|
||||||
name := makeEnvVariableName(service.ID) + "_SERVICE_PORT"
|
// Host
|
||||||
value := strconv.Itoa(service.Port)
|
name := makeEnvVariableName(service.ID) + "_SERVICE_HOST"
|
||||||
result = append(result, api.EnvVar{Name: name, Value: value})
|
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)...)
|
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})
|
result = append(result, api.EnvVar{Name: "SERVICE_HOST", Value: machine})
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user