Skip to content

Commit

Permalink
feat(lifecycle-operator): rename CONTEXT to KEPTN_CONTEXT in task run…
Browse files Browse the repository at this point in the history
…times (keptn#2521)

BREAKING CHANGE: The environment variable giving deno and python runtime access to context information has been renamed from `CONTEXT` to `KEPTN_CONTEXT`

Signed-off-by: Florian Bacher <[email protected]>
  • Loading branch information
bacherfl authored Nov 21, 2023
1 parent 7e3f5e6 commit a7322bd
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 28 deletions.
4 changes: 2 additions & 2 deletions docs/content/en/docs/implementing/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ For an example of how to do this, see the
[keptn-tasks.yaml](https://github.com/keptn-sandbox/klt-on-k3s-with-argocd/blob/main/simplenode-dev/keptn-tasks.yaml)
file.

A context environment variable is available via `Deno.env.get("CONTEXT")`.
A context environment variable is available via `Deno.env.get("KEPTN_CONTEXT")`.
It can be used like this:

```javascript
let context = Deno.env.get("CONTEXT");
let context = Deno.env.get("KEPTN_CONTEXT");

if (context.objectType == "Application") {
let application_name = contextdata.appName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ func (c *ContainerBuilder) CreateContainer(ctx context.Context) (*corev1.Contain

foundKeptnContextVar := false
for i, envVar := range result.Env {
if envVar.Name == KeptnContextEnvVarName {
if envVar.Name == KeptnContextEnvVar {
foundKeptnContextVar = true
result.Env[i].Value = string(jsonContext)
}
}

if !foundKeptnContextVar {
result.Env = append(result.Env, corev1.EnvVar{
Name: KeptnContextEnvVarName,
Name: KeptnContextEnvVar,
Value: string(jsonContext),
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestContainerBuilder_CreateContainerWithVolumes(t *testing.T) {
Image: "image",
Env: []v1.EnvVar{
{
Name: KeptnContextEnvVarName,
Name: KeptnContextEnvVar,
Value: `{"workloadName":"","appName":"","appVersion":"","workloadVersion":"","taskType":"","objectType":""}`,
},
},
Expand All @@ -54,7 +54,7 @@ func TestContainerBuilder_CreateContainerWithVolumes(t *testing.T) {
Image: "image",
Env: []v1.EnvVar{
{
Name: KeptnContextEnvVarName,
Name: KeptnContextEnvVar,
Value: `{"workloadName":"my-workload","appName":"","appVersion":"","workloadVersion":"","taskType":"","objectType":""}`,
},
},
Expand All @@ -68,7 +68,7 @@ func TestContainerBuilder_CreateContainerWithVolumes(t *testing.T) {
Image: "image",
Env: []v1.EnvVar{
{
Name: KeptnContextEnvVarName,
Name: KeptnContextEnvVar,
Value: `foo`,
},
},
Expand All @@ -84,7 +84,7 @@ func TestContainerBuilder_CreateContainerWithVolumes(t *testing.T) {
Image: "image",
Env: []v1.EnvVar{
{
Name: KeptnContextEnvVarName,
Name: KeptnContextEnvVar,
Value: `{"workloadName":"my-workload","appName":"","appVersion":"","workloadVersion":"","taskType":"","objectType":""}`,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ type RuntimeExecutionParams struct {
}

const (
Context = "CONTEXT"
KeptnContextEnvVarName = "KEPTN_CONTEXT"
SecureData = "SECURE_DATA"
Data = "DATA"
CmdArgs = "CMD_ARGS"
Script = "SCRIPT"
FunctionMountName = "function-mount"
KeptnContextEnvVar = "KEPTN_CONTEXT"
SecureData = "SECURE_DATA"
Data = "DATA"
CmdArgs = "CMD_ARGS"
Script = "SCRIPT"
FunctionMountName = "function-mount"
)

func (fb *RuntimeBuilder) CreateContainer(ctx context.Context) (*corev1.Container, error) {
Expand All @@ -68,7 +67,7 @@ func (fb *RuntimeBuilder) CreateContainer(ctx context.Context) (*corev1.Containe
if err != nil {
return nil, err
}
envVars = append(envVars, corev1.EnvVar{Name: Context, Value: string(jsonParams)})
envVars = append(envVars, corev1.EnvVar{Name: KeptnContextEnvVar, Value: string(jsonParams)})
envVars = append(envVars, corev1.EnvVar{Name: CmdArgs, Value: params.CmdParameters})
if params.SecureParameters != "" {
envVars = append(envVars, corev1.EnvVar{
Expand Down
2 changes: 1 addition & 1 deletion runtimes/deno-runtime/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

set -eu

deno run --allow-net --allow-env=DATA,SECURE_DATA,CONTEXT "$SCRIPT"
deno run --allow-net --allow-env=DATA,SECURE_DATA,KEPTN_CONTEXT "$SCRIPT"
2 changes: 1 addition & 1 deletion runtimes/deno-runtime/samples/ts/slack.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let text = Deno.env.get("SECURE_DATA");
let context = Deno.env.get("CONTEXT");
let context = Deno.env.get("KEPTN_CONTEXT");
let data;
let body;
let contextdata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ spec:
- env:
- name: DATA
value: '{"data":"myotherdata","other":"data","user":"myotheruser"}'
- name: CONTEXT
- name: KEPTN_CONTEXT
value: >-
{"workloadName":"waiter-waiter","appName":"waiter","appVersion":"","workloadVersion":"0.4","taskType":"post","objectType":"Workload"}
- name: CMD_ARGS
Expand Down Expand Up @@ -48,7 +48,7 @@ spec:
- env:
- name: DATA
value: '{"data":"mydata","other":"data","user":"myuser"}'
- name: CONTEXT
- name: KEPTN_CONTEXT
value: >-
{"workloadName":"waiter-waiter","appName":"waiter","appVersion":"","workloadVersion":"0.4","taskType":"pre","objectType":"Workload"}
- name: CMD_ARGS
Expand Down
18 changes: 18 additions & 0 deletions test/integration/simple-task/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: batch/v1
kind: Job
metadata:
annotations:
keptn.sh/task-name: pre-deployment-hello-1234
spec:
template:
spec:
containers:
- env:
- name: KEPTN_CONTEXT
value: '{"workloadName":"workload","appName":"podtato-head","appVersion":"1.0.0","workloadVersion":"0.1.0","taskType":"","objectType":""}'
- name: CMD_ARGS
- name: SCRIPT
value: /var/data/function.ts
imagePullPolicy: IfNotPresent
name: keptn-function-runner
ttlSecondsAfterFinished: 300
12 changes: 4 additions & 8 deletions test/integration/simple-task/00-install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@ kind: KeptnTask
metadata:
name: pre-deployment-hello-1234
spec:
app: podtato-head
appVersion: 0.1.2
checkType: pre
taskDefinition: pre-deployment-hello
timeout: 5m0s
workload: ""
workloadVersion: ""
context:
appName: ""
appVersion: ""
appName: "podtato-head"
appVersion: "1.0.0"
objectType: ""
taskType: ""
workloadName: ""
workloadVersion: ""
workloadName: "workload"
workloadVersion: "0.1.0"

0 comments on commit a7322bd

Please sign in to comment.