Skip to content

Commit 89797c2

Browse files
committed
get IP from env via Downward API
1 parent 95408aa commit 89797c2

File tree

4 files changed

+36
-34
lines changed

4 files changed

+36
-34
lines changed

README.md

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@
66

77
# Batch Job Controller
88

9-
The batch job controller allows executing pods on nodes of a cluster, where the number of concurrent running pods can be configured.
10-
Each pod can report it's results back to the controller to have them exposed as metrics.
9+
The batch job controller allows executing pods on nodes of a cluster, where the number of concurrent running pods can be
10+
configured. Each pod can report it's results back to the controller to have them exposed as metrics.
1111

1212
## Deployment
1313

1414
The controller expects the following environment variables
1515

16+
| Name | Value |
17+
| --- |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
18+
| NAMESPACE | The current namespace |
19+
| CONFIG_MAP_NAME | The name of the configmap to read the config from |
20+
| POD_IP | The IP of the controller Pod. If defined, this IP is used for the callback URL of the job pods.(should be injected via [Downward API](https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#the-downward-api)) |
1621

17-
| Name | Value |
18-
| --- | --- |
19-
| NAMESPACE | The current namespace |
20-
| CONFIG_MAP_NAME | The name of the configmap to read the config from |
22+
## Configuration
2123

22-
## Configuration
23-
24-
The configuration has to be stored in a configmap with the following values
24+
The configuration has to be stored in a configmap with the following values
2525

2626
### config.yaml
2727

@@ -30,7 +30,7 @@ Controller configuration
3030
```yaml
3131
name: "" # name of the controller; will also be used as prefix for the job pods
3232
jobServiceAccount: "" # service account to be used for the job pods. If empty the default will be used
33-
jobNodeSelector: {} # node selector labels to define in which nodes to run the jobs
33+
jobNodeSelector: { } # node selector labels to define in which nodes to run the jobs
3434
runOnUnscheduledNodes: true # if true, jobs are also started on nodes that are unschedulable
3535
cronExpression: "42 3 * * *" # the cron expression to trigger the job execution
3636
reportDirectory: "/var/www" # directory to store and serve the reports
@@ -40,23 +40,23 @@ runOnStartup: true # if 'true' the jobs are triggered on startup o
4040
startupDelay: 10s # the delay as duration that is used to start the jobs if runOnStartup is enabled. default is '10s'
4141
callbackServiceName: "" # name of the controller service
4242
callbackServicePort: 8090 # port of the controller callback api service
43-
custom: {} # additional properties that can be used in a custom implementation
43+
custom: { } # additional properties that can be used in a custom implementation
4444
latestMetricsLabel: false # if 'true' each result metric is also created with executionID='latest'
4545
leaderElectionResourceLock: "" # type of leader election resource lock to be used. ('configmapsleases' (default), 'configmaps', 'endpoints', 'leases', 'endpointsleases')
4646
metrics:
4747
prefix: "foo_...." # prefix for the metrics exposed by the controller
48-
gauges: # metric gauges that will be exposed by the jobs. The key is uses as suffix for the metrics.
49-
test: # suffix of the metric
48+
gauges: # metric gauges that will be exposed by the jobs. The key is uses as suffix for the metrics.
49+
test: # suffix of the metric
5050
help: "help ..." # help text for the metric
51-
labels: # list of labels to be used with the metric. node and executionID are automatically added
51+
labels: # list of labels to be used with the metric. node and executionID are automatically added
5252
- label_a
5353
- label_b
5454
```
5555
5656
### pod-template.yaml
5757
58-
The template of the pod to be started for each job.
59-
When a pod is created it gets enriched by the controller specific configuration. [pkg\job\job.go](pkg\job\job.go)
58+
The template of the pod to be started for each job. When a pod is created it gets enriched by the controller specific
59+
configuration. [pkg\job\job.go](pkg\job\job.go)
6060
6161
## Job Pod
6262
@@ -77,17 +77,17 @@ The job pod has the following env variables provided by the controller:
7777
7878
### Callback
7979
80-
The controller exposes by default an endpoint to receive job results. The report is stored locally and metrics of the reports will be exposed.
80+
The controller exposes by default an endpoint to receive job results. The report is stored locally and metrics of the
81+
reports will be exposed.
8182
8283
#### URL
8384
8485
The report URL is by default: **${CALLBACK_SERVICE_RESULT_URL}**
8586
8687
#### Body
8788
88-
The body of the report contains the metric suffixes that are also defined in the controller config.
89-
Each metric has a decimal value and a map where the key is the label name and value is the value to be used for the metric label.
90-
89+
The body of the report contains the metric suffixes that are also defined in the controller config. Each metric has a
90+
decimal value and a map where the key is the label name and value is the value to be used for the metric label.
9191
9292
```json
9393
{
@@ -113,29 +113,34 @@ Each metric has a decimal value and a map where the key is the label name and va
113113
Example job script: [helm\batch-job-controller\bin\run.sh](helm\batch-job-controller\bin\run.sh)
114114

115115
### Upload additional files
116-
Additional files can be uploaded.
117116

118-
Use default **'Content-Disposition'** header or the **name** query parameter to define the name of the file. If the name is not defined an uuid is generated.
119-
Each filename is prepended with the node name.
117+
Additional files can be uploaded.
118+
119+
Use default **'Content-Disposition'** header or the **name** query parameter to define the name of the file. If the name
120+
is not defined an uuid is generated. Each filename is prepended with the node name.
120121

121122
#### URL
122123

123124
The report URL is by default: **${CALLBACK_SERVICE_FILE_URL}**
124125

125126
### Create k8s Events from job pod
127+
126128
k8s Event can be created from each job pod by calling the event endpoint.
127129

128130
The 'reason' should be short and unique; it must be in UpperCamelCase format (starting with a capital letter).
129131

130132
Simple Message:
133+
131134
```json
132135
{
133136
"warning": false,
134137
"reason": "TestReason",
135138
"message": "test message"
136139
}
137140
```
141+
138142
Massage with parameters
143+
139144
```json
140145
{
141146
"warning": true,

helm/example-batch-job-controller/templates/deployment.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ spec:
3434
value: {{ .Release.Namespace }}
3535
- name: CONFIG_MAP_NAME
3636
value: {{ template "batch-job-controller.name" . }}
37+
- name: POD_IP
38+
valueFrom:
39+
fieldRef:
40+
fieldPath: status.podIP
3741
{{ include "batch-job-controller.deployment-env" . | indent 12 }}
3842
image: {{ .Values.deployment.image }}:{{- default .Chart.AppVersion .Values.deployment.imageTag }}
3943
imagePullPolicy: Always

pkg/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const (
2222
EnvHostname = "HOSTNAME"
2323
// EnvConfigMapName configmap name env variable
2424
EnvConfigMapName = "CONFIG_MAP_NAME"
25+
// EnvPodIP the controller pod's IP
26+
EnvPodIP = "POD_IP"
2527
// EnvDevMode enable dev mode
2628
EnvDevMode = "DEV_MODE"
2729
// EnvReportDirectory override for report directory

pkg/cron/cron.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cron
33
import (
44
"context"
55
"os"
6-
"strings"
76
"time"
87

98
"github.com/bakito/batch-job-controller/pkg/config"
@@ -124,16 +123,8 @@ func (j *cronJob) startPods() {
124123
}
125124

126125
var callbackAddress string
127-
if len(strings.TrimSpace(j.cfg.CallbackServiceName)) == 0 {
128-
// get my pod
129-
p := &corev1.Pod{}
130-
name := os.Getenv("HOSTNAME")
131-
err = j.client.Get(context.TODO(), client.ObjectKey{Namespace: j.cfg.Namespace, Name: name}, p)
132-
if err != nil {
133-
jobLog.Error(err, "error getting pod %q", name)
134-
return
135-
}
136-
callbackAddress = p.Status.PodIP
126+
if ip, ok := os.LookupEnv(config.EnvPodIP); ok {
127+
callbackAddress = ip
137128
} else {
138129
// get service
139130
svc := &corev1.Service{}

0 commit comments

Comments
 (0)