Skip to content

Commit 04d4b21

Browse files
author
Leonid Podolinskiy
authored
Devops 831 fix env var cleanup upon change (#23)
* redo patch env var function introduce annotations to save agent args for future cleanup * fix updating same container 2 times within the same apply to kube api add tests * remove comment * add test cases #minor * bump github actions deps * replace append with proper Delete func rename test
1 parent 605151d commit 04d4b21

8 files changed

+595
-166
lines changed

.github/workflows/e2e.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ jobs:
2020
ports:
2121
- 5000:5000
2222
steps:
23-
- uses: actions/checkout@v3
23+
- uses: actions/checkout@v4
2424

2525
- name: Setup Go environment
26-
uses: actions/setup-go@v4
26+
uses: actions/setup-go@v5
2727
with:
2828
go-version: "1.22"
2929

@@ -83,7 +83,7 @@ jobs:
8383
kubectl wait --timeout=120s --for=condition=Ready node/$(echo $HOSTNAME| awk '{print tolower($0)}')
8484
8585
- name: Build and push to local repo
86-
uses: docker/build-push-action@v3
86+
uses: docker/build-push-action@v5
8787
with:
8888
context: .
8989
push: true

.github/workflows/init_container.yaml

+92-84
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,46 @@ on:
44
workflow_dispatch:
55
inputs:
66
release_tag:
7-
description: 'Release tag of the agent'
7+
description: "Release tag of the agent"
88
required: true
99
init_image_tag:
10-
description: 'Image tag'
10+
description: "Image tag"
1111
required: true
1212
default: "0"
1313
force:
14-
description: 'Force build'
14+
description: "Force build"
1515
required: false
1616
default: "false"
1717

18-
19-
20-
jobs:
18+
jobs:
2119
set_image_tag_variable:
2220
strategy:
2321
matrix:
24-
agents: [
25-
{name: "linux", file: "agent.zip", platform: "linux/amd64"},
26-
{name: "alpine", file: "agent-alpine.zip", platform: "linux/amd64"},
27-
{name: "linux-arm64", file: "agent-arm64.zip", platform: "linux/arm64"},
28-
{name: "alpine-arm64", file: "agent-alpine-arm64.zip", platform: "linux/arm64"}
29-
]
22+
agents:
23+
[
24+
{ name: "linux", file: "agent.zip", platform: "linux/amd64" },
25+
{
26+
name: "alpine",
27+
file: "agent-alpine.zip",
28+
platform: "linux/amd64",
29+
},
30+
{
31+
name: "linux-arm64",
32+
file: "agent-arm64.zip",
33+
platform: "linux/arm64",
34+
},
35+
{
36+
name: "alpine-arm64",
37+
file: "agent-alpine-arm64.zip",
38+
platform: "linux/arm64",
39+
},
40+
]
3041
runs-on: ubuntu-latest
3142
name: Build and push Docker image
3243
steps:
33-
- name: Set release tag
34-
shell: bash
35-
run: |
44+
- name: Set release tag
45+
shell: bash
46+
run: |
3647
# check that tag is matching regex x.y.x-release.<commit hash> or force flag is enabled
3748
if [[ ! ${{ inputs.release_tag }} =~ ^[0-9]+\.[0-9]+\.[0-9]+-release\.[0-9a-f]+$ ]] ; then
3849
echo "Tag ${{ inputs.release_tag }} is not matching regex x.y.x-release.<commithash>"
@@ -43,81 +54,78 @@ jobs:
4354
fi
4455
fi
4556
echo "TAG_NAME=$(echo ${{ inputs.release_tag }} | sed -E 's/^([0-9]*\.[0-9]*\.[0-9]*).*/\1/')-init.${{ inputs.init_image_tag }}" >> "$GITHUB_OUTPUT"
46-
id: set_tag
57+
id: set_tag
4758

48-
- uses: actions/checkout@v3
59+
- uses: actions/checkout@v4
4960

50-
- name: Set up QEMU
51-
uses: docker/setup-qemu-action@v3
52-
- name: Set up Docker Buildx
53-
uses: docker/setup-buildx-action@v3
61+
- name: Set up QEMU
62+
uses: docker/setup-qemu-action@v3
63+
- name: Set up Docker Buildx
64+
uses: docker/setup-buildx-action@v3
5465

55-
- name: Login to DockerHub
56-
if: ${{ success() }}
57-
uses: docker/login-action@v2
58-
with:
59-
username: ${{ secrets.DOCKERHUB_USER }}
60-
password: ${{ secrets.DOCKERHUB_PASS }}
61-
66+
- name: Login to DockerHub
67+
if: ${{ success() }}
68+
uses: docker/login-action@v2
69+
with:
70+
username: ${{ secrets.DOCKERHUB_USER }}
71+
password: ${{ secrets.DOCKERHUB_PASS }}
6272

63-
- name: Configure AWS credentials for artifacts bucket
64-
uses: aws-actions/configure-aws-credentials@v1
65-
with:
66-
aws-access-key-id: ${{ secrets.RELEASE_ARTIFACTS_MANAGER_KEY }}
67-
aws-secret-access-key: ${{ secrets.RELEASE_ARTIFACTS_MANAGER_SECRET }}
68-
aws-region: us-east-1
73+
- name: Configure AWS credentials for artifacts bucket
74+
uses: aws-actions/configure-aws-credentials@v1
75+
with:
76+
aws-access-key-id: ${{ secrets.RELEASE_ARTIFACTS_MANAGER_KEY }}
77+
aws-secret-access-key: ${{ secrets.RELEASE_ARTIFACTS_MANAGER_SECRET }}
78+
aws-region: us-east-1
6979

70-
- name: Set docker image tags
71-
id: set_docker_tags
72-
run: |
73-
python3 -m pip install semver
74-
existing_tags=()
75-
dockerhub_tags=$(curl -s "https://hub.docker.com/v2/namespaces/lightruncom/repositories/k8s-operator-init-java-agent-${{ matrix.agents.name }}/tags?page_size=50" | jq -r ".results[].name")
76-
if [[ $? -ne 0 ]] ; then
77-
echo "Failed to fetch existing tags"
78-
exit 1
79-
fi
80-
while IFS= read -r line; do
81-
existing_tags+=("$line")
82-
done < <(echo $dockerhub_tags)
83-
for tag in $existing_tags
84-
do
85-
if [[ "$tag" == "latest" ]] ; then
86-
continue
80+
- name: Set docker image tags
81+
id: set_docker_tags
82+
run: |
83+
python3 -m pip install semver
84+
existing_tags=()
85+
dockerhub_tags=$(curl -s "https://hub.docker.com/v2/namespaces/lightruncom/repositories/k8s-operator-init-java-agent-${{ matrix.agents.name }}/tags?page_size=50" | jq -r ".results[].name")
86+
if [[ $? -ne 0 ]] ; then
87+
echo "Failed to fetch existing tags"
88+
exit 1
8789
fi
88-
echo "Comparing existing tag: $tag with new: ${{steps.set_tag.outputs.TAG_NAME}}"
89-
if [[ $(pysemver compare $tag ${{steps.set_tag.outputs.TAG_NAME}}) -ge 0 ]] ; then
90-
echo "Existing tag: $tag is greater or equal than new: ${{ inputs.release_tag }}. Skip adding latest tag"
91-
echo "DOCKER_TAGS=lightruncom/k8s-operator-init-java-agent-${{ matrix.agents.name }}:${{steps.set_tag.outputs.TAG_NAME}}" >> "$GITHUB_OUTPUT"
92-
exit 0
93-
fi
94-
done
95-
echo "Adding latest tag to ${{steps.set_tag.outputs.TAG_NAME}}"
96-
echo "DOCKER_TAGS=lightruncom/k8s-operator-init-java-agent-${{ matrix.agents.name }}:${{steps.set_tag.outputs.TAG_NAME}},lightruncom/k8s-operator-init-java-agent-${{ matrix.agents.name }}:latest" >> "$GITHUB_OUTPUT"
97-
98-
- name: Download agent artifacts
99-
run: |
100-
aws s3 cp s3://${{ secrets.RELEASE_ARTIFACTS_BUCKET }}/artifacts/${{ inputs.release_tag }}/${{ matrix.agents.file }} ./lightrun-init-agent/
101-
90+
while IFS= read -r line; do
91+
existing_tags+=("$line")
92+
done < <(echo $dockerhub_tags)
93+
for tag in $existing_tags
94+
do
95+
if [[ "$tag" == "latest" ]] ; then
96+
continue
97+
fi
98+
echo "Comparing existing tag: $tag with new: ${{steps.set_tag.outputs.TAG_NAME}}"
99+
if [[ $(pysemver compare $tag ${{steps.set_tag.outputs.TAG_NAME}}) -ge 0 ]] ; then
100+
echo "Existing tag: $tag is greater or equal than new: ${{ inputs.release_tag }}. Skip adding latest tag"
101+
echo "DOCKER_TAGS=lightruncom/k8s-operator-init-java-agent-${{ matrix.agents.name }}:${{steps.set_tag.outputs.TAG_NAME}}" >> "$GITHUB_OUTPUT"
102+
exit 0
103+
fi
104+
done
105+
echo "Adding latest tag to ${{steps.set_tag.outputs.TAG_NAME}}"
106+
echo "DOCKER_TAGS=lightruncom/k8s-operator-init-java-agent-${{ matrix.agents.name }}:${{steps.set_tag.outputs.TAG_NAME}},lightruncom/k8s-operator-init-java-agent-${{ matrix.agents.name }}:latest" >> "$GITHUB_OUTPUT"
102107
103-
- name: Build and push ${{ matrix.agents.name }} container
104-
uses: docker/build-push-action@v4
105-
with:
106-
context: .
107-
file: ./lightrun-init-agent/Dockerfile
108-
push: true
109-
platforms: ${{ matrix.agents.platform }}
110-
tags: ${{steps.set_docker_tags.outputs.DOCKER_TAGS}}
111-
build-args: |
112-
FILE=${{ matrix.agents.file }}
108+
- name: Download agent artifacts
109+
run: |
110+
aws s3 cp s3://${{ secrets.RELEASE_ARTIFACTS_BUCKET }}/artifacts/${{ inputs.release_tag }}/${{ matrix.agents.file }} ./lightrun-init-agent/
113111
112+
- name: Build and push ${{ matrix.agents.name }} container
113+
uses: docker/build-push-action@v4
114+
with:
115+
context: .
116+
file: ./lightrun-init-agent/Dockerfile
117+
push: true
118+
platforms: ${{ matrix.agents.platform }}
119+
tags: ${{steps.set_docker_tags.outputs.DOCKER_TAGS}}
120+
build-args: |
121+
FILE=${{ matrix.agents.file }}
114122
115-
- name: Slack Notification
116-
if: always()
117-
uses: rtCamp/action-slack-notify@v2.2.0
118-
env:
119-
SLACK_CHANNEL: devops-alerts
120-
SLACK_COLOR: ${{ job.status }} # or a specific color like 'good' or '#ff00ff'
121-
SLACK_MESSAGE: "Tag ${{ inputs.release_tag }} | Platform ${{ matrix.agents.name }}"
122-
SLACK_TITLE: Init contianer build status - ${{ job.status }}
123-
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
123+
- name: Slack Notification
124+
if: always()
125+
uses: rtCamp/action-slack-notify@v2.2.0
126+
env:
127+
SLACK_CHANNEL: devops-alerts
128+
SLACK_COLOR: ${{ job.status }} # or a specific color like 'good' or '#ff00ff'
129+
SLACK_MESSAGE: "Tag ${{ inputs.release_tag }} | Platform ${{ matrix.agents.name }}"
130+
SLACK_TITLE: Init contianer build status - ${{ job.status }}
131+
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}

.github/workflows/release.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ jobs:
2121
runs-on: ubuntu-latest
2222
steps:
2323
- name: Checkout
24-
uses: actions/checkout@v2
24+
uses: actions/checkout@v4
2525
with:
2626
fetch-depth: 0
2727

2828
- name: Checkout
29-
uses: actions/checkout@v2
29+
uses: actions/checkout@v4
3030
with:
3131
path: "helm-repo"
3232
ref: "helm-repo"
@@ -42,7 +42,7 @@ jobs:
4242
WITH_V: false
4343

4444
- name: Setup Go environment
45-
uses: actions/setup-go@v4
45+
uses: actions/setup-go@v5
4646
with:
4747
go-version: "1.22"
4848

@@ -63,7 +63,7 @@ jobs:
6363
6464
- name: Login to DockerHub
6565
if: ${{ success() }}
66-
uses: docker/login-action@v2
66+
uses: docker/login-action@v3
6767
with:
6868
username: ${{ secrets.DOCKERHUB_USER }}
6969
password: ${{ secrets.DOCKERHUB_PASS }}
@@ -74,7 +74,7 @@ jobs:
7474
uses: docker/setup-buildx-action@v3
7575

7676
- name: Build and push
77-
uses: docker/build-push-action@v3
77+
uses: docker/build-push-action@v5
7878
with:
7979
context: .
8080
push: true
@@ -85,7 +85,7 @@ jobs:
8585
8686
- name: Create Release
8787
if: ${{ success() }}
88-
uses: ncipollo/release-action@v1.10.0
88+
uses: ncipollo/release-action@v1.14.0
8989
with:
9090
artifacts: helm-repo/lightrun-k8s-operator-${{steps.release_tag.outputs.new_tag}}.tgz
9191
tag: ${{steps.release_tag.outputs.new_tag}}

internal/controller/helpers.go

+29
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package controller
22

33
import (
44
"context"
5+
"errors"
56
"hash/fnv"
67
"sort"
8+
"strings"
79
"time"
810

911
agentv1beta "github.com/lightrun-platform/lightrun-k8s-operator/api/v1beta"
@@ -226,3 +228,30 @@ func SetStatusCondition(conditions *[]metav1.Condition, newCondition metav1.Cond
226228
existingCondition.Message = newCondition.Message
227229
existingCondition.ObservedGeneration = newCondition.ObservedGeneration
228230
}
231+
232+
func agentEnvVarArgument(mountPath string, agentCliFlags string) (string, error) {
233+
agentArg := " -agentpath:" + mountPath + "/agent/lightrun_agent.so"
234+
if agentCliFlags != "" {
235+
agentArg += "=" + agentCliFlags
236+
if len(agentArg) > 1024 {
237+
return "", errors.New("agentpath with agentCliFlags has more than 1024 chars. This is a limitation of Java")
238+
}
239+
}
240+
return agentArg, nil
241+
}
242+
243+
// Removes from env var value. Removes env var from the list if value is empty after the update
244+
func unpatchEnvVarValue(origValue string, removalValue string) string {
245+
value := strings.ReplaceAll(origValue, removalValue, "")
246+
return value
247+
}
248+
249+
// Return index if the env var in the []corev1.EnvVar, otherwise -1
250+
func findEnvVarIndex(envVarName string, envVarList []corev1.EnvVar) int {
251+
for i, envVar := range envVarList {
252+
if envVar.Name == envVarName {
253+
return i
254+
}
255+
}
256+
return -1
257+
}

0 commit comments

Comments
 (0)