Skip to content

Commit

Permalink
Include PipelineRun and Taskrun name in App labels (#187)
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>
  • Loading branch information
AverageMarcus authored May 20, 2024
1 parent b34c05a commit c762e09
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- If relevant env vars are found populate the Cluster Apps and Org CRs with labels containing the Tekton run names

## [0.21.0] - 2024-05-14

### Added
Expand Down
27 changes: 19 additions & 8 deletions pkg/application/cluster_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package application

import (
"fmt"
"os"

applicationv1alpha1 "github.com/giantswarm/apiextensions-application/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -112,28 +113,38 @@ func (c *Cluster) WithExtraConfigs(extraConfigs []applicationv1alpha1.AppExtraCo
// Build defaults and populates some required values on the apps then generated the App and Configmap pairs for both the
// cluster and default-apps apps.
func (c *Cluster) Build() (*applicationv1alpha1.App, *corev1.ConfigMap, *applicationv1alpha1.App, *corev1.ConfigMap, error) {
baseLabels := map[string]string{}

// If found, populate details about Tekton run as labels
if os.Getenv("TEKTON_PIPELINE_RUN") != "" {
baseLabels["cicd.giantswarm.io/pipelinerun"] = os.Getenv("TEKTON_PIPELINE_RUN")
}
if os.Getenv("TEKTON_TASK_RUN") != "" {
baseLabels["cicd.giantswarm.io/taskrun"] = os.Getenv("TEKTON_TASK_RUN")
}

c.ClusterApp.
WithAppLabels(map[string]string{
WithAppLabels(mergeMaps(baseLabels, map[string]string{
"app-operator.giantswarm.io/version": "0.0.0",
}).
WithConfigMapLabels(map[string]string{
})).
WithConfigMapLabels(mergeMaps(baseLabels, map[string]string{
"giantswarm.io/cluster": c.Name,
})
}))

clusterApplication, clusterCM, err := c.ClusterApp.Build()
if err != nil {
return nil, nil, nil, nil, err
}

c.DefaultAppsApp.
WithAppLabels(map[string]string{
WithAppLabels(mergeMaps(baseLabels, map[string]string{
"app-operator.giantswarm.io/version": "0.0.0",
"giantswarm.io/cluster": c.Name,
"giantswarm.io/managed-by": "cluster",
}).
WithConfigMapLabels(map[string]string{
})).
WithConfigMapLabels(mergeMaps(baseLabels, map[string]string{
"giantswarm.io/cluster": c.Name,
})
}))
defaultAppsApplication, defaultAppsCM, err := c.DefaultAppsApp.Build()
if err != nil {
return nil, nil, nil, nil, err
Expand Down
11 changes: 11 additions & 0 deletions pkg/application/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,14 @@ func getOverrideVersion(app string) (string, bool) {
ver := getOverrideVersions()[strings.ToLower(app)]
return ver, ver != ""
}

func mergeMaps(m1 map[string]string, m2 map[string]string) map[string]string {
merged := make(map[string]string)
for k, v := range m1 {
merged[k] = v
}
for key, value := range m2 {
merged[key] = value
}
return merged
}
10 changes: 10 additions & 0 deletions pkg/organization/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package organization

import (
"fmt"
"os"
"strings"

templateorg "github.com/giantswarm/kubectl-gs/v2/pkg/template/organization"
Expand Down Expand Up @@ -69,6 +70,15 @@ func (o *Org) Build() (*orgv1alpha1.Organization, error) {
DeleteAnnotation: "true",
}

// If found, populate details about Tekton run as labels
orgCR.ObjectMeta.Labels = map[string]string{}
if os.Getenv("TEKTON_PIPELINE_RUN") != "" {
orgCR.ObjectMeta.Labels["cicd.giantswarm.io/pipelinerun"] = os.Getenv("TEKTON_PIPELINE_RUN")
}
if os.Getenv("TEKTON_TASK_RUN") != "" {
orgCR.ObjectMeta.Labels["cicd.giantswarm.io/taskrun"] = os.Getenv("TEKTON_TASK_RUN")
}

orgCR.Status.Namespace = o.GetNamespace()

return orgCR, err
Expand Down

0 comments on commit c762e09

Please sign in to comment.