Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Tag propagate in CI and Pre-Post CD #87

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
2 changes: 1 addition & 1 deletion ci-runner/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21

toolchain go1.21.8

replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20241216105957-d5a37695c2b2
replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20241220102750-9025eb41c6f9

require (
github.com/Knetic/govaluate v3.0.0+incompatible
Expand Down
4 changes: 2 additions & 2 deletions ci-runner/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20241216105957-d5a37695c2b2 h1:WuQfIhux7jEQpJZ6I6JbRbsn7j9R3FANedWNhmetirM=
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20241216105957-d5a37695c2b2/go.mod h1:NJSMdv+zTUK3p7rML12RZSeAUKHeLaoY3sR/oK0xhwo=
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20241220102750-9025eb41c6f9 h1:17fKsPL2gpSu1EoHkfFM5sluuQa2BpmtrzwX/0vq504=
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20241220102750-9025eb41c6f9/go.mod h1:NJSMdv+zTUK3p7rML12RZSeAUKHeLaoY3sR/oK0xhwo=
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
github.com/docker/cli v24.0.6+incompatible h1:fF+XCQCgJjjQNIMjzaSmiKJSCcfcXb3TWTcc7GAneOY=
Expand Down
67 changes: 67 additions & 0 deletions ci-runner/helper/DockerHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,9 @@ func getBuildxK8sDriverCmd(dockerConnection, buildxDriverImage string, driverOpt
buildxCreate += " --platform=%s "
buildxCreate = fmt.Sprintf(buildxCreate, platforms)
}
// add driver options for app labels and annotations
driverOpts["driverOptions"] = getBuildXDriverOptionsWithLabelsAndAnnotations(driverOpts["driverOptions"])

driverOpts["driverOptions"] = getBuildXDriverOptionsWithImage(buildxDriverImage, driverOpts["driverOptions"])
if len(driverOpts["driverOptions"]) > 0 {
buildxCreate += " '--driver-opt=%s' "
Expand All @@ -1054,6 +1057,70 @@ func getBuildXDriverOptionsWithImage(buildxDriverImage, driverOptions string) st
return driverOptions
}

func getBuildXDriverOptionsWithLabelsAndAnnotations(driverOptions string) string {
labels := make(map[string]string)
annotations := make(map[string]string)

// Read labels and annotations from files
labelsPath := utils.DEVTRON_SELF_DOWNWARD_API_VOLUME_PATH + "/labels"
prkhrkat marked this conversation as resolved.
Show resolved Hide resolved
labelsOut, err := os.ReadFile(labelsPath)
if err != nil {
log.Println(util.DEVTRON, "error in reading labels from podinfo, err:", err)
prkhrkat marked this conversation as resolved.
Show resolved Hide resolved
}
annotationsPath := utils.DEVTRON_SELF_DOWNWARD_API_VOLUME_PATH + "/annotations"
prkhrkat marked this conversation as resolved.
Show resolved Hide resolved
annotationsOut, err := os.ReadFile(annotationsPath)
if err != nil {
log.Println(util.DEVTRON, "error in reading annotations from podinfo, err:", err)
}

// Parse labels and annotations
if len(labelsOut) > 0 {
labels = parseKeyValuePairs(string(labelsOut))
}
if len(annotationsOut) > 0 {
annotations = parseKeyValuePairs(string(annotationsOut))
}

// Combine driver options
driverOptions = getBuildXDriverOptions("labels", labels, driverOptions)
prkhrkat marked this conversation as resolved.
Show resolved Hide resolved
driverOptions = getBuildXDriverOptions("annotations", annotations, driverOptions)
return driverOptions
}

func parseKeyValuePairs(input string) map[string]string {
prkhrkat marked this conversation as resolved.
Show resolved Hide resolved
keyValuePairs := make(map[string]string)
lines := strings.Split(input, "\n")
for _, line := range lines {
if len(line) > 0 {
kv := strings.SplitN(line, "=", 2)
if len(kv) == 2 {
key := kv[0]
value := strings.Trim(kv[1], `"`)
keyValuePairs[key] = value
}
}
}
return keyValuePairs
}

func getBuildXDriverOptions(optionType string, options map[string]string, driverOptions string) string {
prkhrkat marked this conversation as resolved.
Show resolved Hide resolved
if len(options) > 0 {
optionStr := fmt.Sprintf("\"%s=", optionType)
for k, v := range options {
optionStr += fmt.Sprintf("%s=%s,", k, v)
}
optionStr = strings.TrimSuffix(optionStr, ",")
optionStr += "\""

if len(driverOptions) > 0 {
driverOptions += fmt.Sprintf(",%s", optionStr)
} else {
driverOptions = optionStr
}
}
return driverOptions
}

func (impl *DockerHelperImpl) StopDocker(ciContext cicxt.CiContext) error {
cmd := exec.Command("docker", "ps", "-a", "-q")
out, err := cmd.Output()
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ci-runner/vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ github.com/cespare/xxhash/v2
# github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
## explicit
github.com/davecgh/go-spew/spew
# github.com/devtron-labs/common-lib v0.19.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20241216105957-d5a37695c2b2
# github.com/devtron-labs/common-lib v0.19.0 => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20241220102750-9025eb41c6f9
## explicit; go 1.21
github.com/devtron-labs/common-lib/blob-storage
github.com/devtron-labs/common-lib/constants
Expand Down Expand Up @@ -842,4 +842,4 @@ sigs.k8s.io/structured-merge-diff/v4/value
# sigs.k8s.io/yaml v1.3.0
## explicit; go 1.12
sigs.k8s.io/yaml
# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20241216105957-d5a37695c2b2
# github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20241220102750-9025eb41c6f9
8 changes: 5 additions & 3 deletions common-lib/utils/CommonUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ import (
var chars = []rune("abcdefghijklmnopqrstuvwxyz0123456789")

const (
DOCKER_REGISTRY_TYPE_DOCKERHUB = "docker-hub"
DEVTRON_SELF_POD_UID = "DEVTRON_SELF_POD_UID"
DEVTRON_SELF_POD_NAME = "DEVTRON_SELF_POD_NAME"
DOCKER_REGISTRY_TYPE_DOCKERHUB = "docker-hub"
DEVTRON_SELF_POD_UID = "DEVTRON_SELF_POD_UID"
DEVTRON_SELF_POD_NAME = "DEVTRON_SELF_POD_NAME"
DEVTRON_SELF_DOWNWARD_API_VOLUME = "devtron-pod-info"
DEVTRON_SELF_DOWNWARD_API_VOLUME_PATH = "/etc/devtron-pod-info"
)

// Generates random string
Expand Down
Loading