Skip to content

Commit

Permalink
Merge branch 'release/v1.5.0' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
sklein94 authored and cesmarvin committed Dec 12, 2024
2 parents cf0f53d + 786325f commit e4dbb20
Show file tree
Hide file tree
Showing 19 changed files with 726 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [v1.5.0] - 2024-12-12
### Added
- [#77] Add labels with component name and version also to pod-templates of Deployments, StatefulSets, DaemonSets, Jobs & CronJobs

## [v1.4.0] - 2024-12-04
### Fixed
- [#73] Increase wait limit to prevent problems with slow internet connection
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ RUN make compile-generic
FROM gcr.io/distroless/static:nonroot
LABEL maintainer="hello@cloudogu.com" \
NAME="k8s-component-operator" \
VERSION="1.4.0"
VERSION="1.5.0"

WORKDIR /
COPY --from=builder /workspace/target/k8s-component-operator .
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Set these to the desired values
ARTIFACT_ID=k8s-component-operator
VERSION=1.4.0
VERSION=1.5.0
## Image URL to use all building/pushing image targets
IMAGE=cloudogu/${ARTIFACT_ID}:${VERSION}
GOTAG?=1.23.0
Expand Down
8 changes: 8 additions & 0 deletions config/samples/k8s-prometheus.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: k8s.cloudogu.com/v1
kind: Component
metadata:
name: k8s-prometheus
spec:
name: k8s-prometheus
namespace: k8s
version: 57.1.1-6
2 changes: 1 addition & 1 deletion k8s/helm/component-patch-tpl.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: v1
values:
images:
componentOperator: cloudogu/k8s-component-operator:1.4.0
componentOperator: cloudogu/k8s-component-operator:1.5.0
patches:
values.yaml:
additionalImages:
Expand Down
2 changes: 1 addition & 1 deletion k8s/helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ manager:
image:
registry: docker.io
repository: cloudogu/k8s-component-operator
tag: 1.4.0
tag: 1.5.0
imagePullPolicy: IfNotPresent
env:
logLevel: info
Expand Down
71 changes: 66 additions & 5 deletions pkg/labels/postrenderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package labels
import (
"bytes"
"fmt"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
"maps"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -12,6 +14,12 @@ import (
yamlutil "github.com/cloudogu/k8s-component-operator/pkg/yaml"
)

const deploymentKind = "apps/v1/Deployment"
const statefulSetKind = "apps/v1/StatefulSet"
const daemonSetKind = "apps/v1/DaemonSet"
const jobKind = "batch/v1/Job"
const cronJobKind = "batch/v1/CronJob"

type PostRenderer struct {
documentSplitter documentSplitter
unstructuredSerializer unstructuredSerializer
Expand Down Expand Up @@ -51,12 +59,37 @@ func (c *PostRenderer) Run(renderedManifests *bytes.Buffer) (modifiedManifests *
}

k8sObject := &unstructured.Unstructured{Object: unstructuredMap}
kind := fmt.Sprintf("%s/%s", k8sObject.GetAPIVersion(), k8sObject.GetKind())

switch kind {
case deploymentKind:
k8sObject, err = addLabelsToStructured(c, unstructuredMap, &appsv1.Deployment{}, func(a *appsv1.Deployment) objectWithLabels { return &a.Spec.Template })
if err != nil {
return nil, fmt.Errorf("failed to add labels to Deployment: %w", err)
}
case statefulSetKind:
k8sObject, err = addLabelsToStructured(c, unstructuredMap, &appsv1.StatefulSet{}, func(a *appsv1.StatefulSet) objectWithLabels { return &a.Spec.Template })
if err != nil {
return nil, fmt.Errorf("failed to add labels to StatefulSet: %w", err)
}
case daemonSetKind:
k8sObject, err = addLabelsToStructured(c, unstructuredMap, &appsv1.DaemonSet{}, func(a *appsv1.DaemonSet) objectWithLabels { return &a.Spec.Template })
if err != nil {
return nil, fmt.Errorf("failed to add labels to DaemonSet: %w", err)
}
case jobKind:
k8sObject, err = addLabelsToStructured(c, unstructuredMap, &batchv1.Job{}, func(a *batchv1.Job) objectWithLabels { return &a.Spec.Template })
if err != nil {
return nil, fmt.Errorf("failed to add labels to Job: %w", err)
}
case cronJobKind:
k8sObject, err = addLabelsToStructured(c, unstructuredMap, &batchv1.CronJob{}, func(a *batchv1.CronJob) objectWithLabels { return &a.Spec.JobTemplate.Spec.Template })
if err != nil {
return nil, fmt.Errorf("failed to add labels to CronJob: %w", err)
}
}

originalLabels := k8sObject.GetLabels()
mergedLabels := make(map[string]string, len(c.labels)+len(originalLabels))
maps.Copy(mergedLabels, originalLabels)
maps.Copy(mergedLabels, c.labels)
k8sObject.SetLabels(mergedLabels)
addLabels(k8sObject, c.labels)

yamlBytes, err := c.serializer.Marshal(k8sObject)
if err != nil {
Expand All @@ -72,3 +105,31 @@ func (c *PostRenderer) Run(renderedManifests *bytes.Buffer) (modifiedManifests *

return modifiedManifests, nil
}

func addLabelsToStructured[k any](c *PostRenderer, unstructuredMap map[string]interface{}, obj k, getTemplate func(k) objectWithLabels) (*unstructured.Unstructured, error) {
if err := c.unstructuredConverter.FromUnstructured(unstructuredMap, obj); err != nil {
return nil, fmt.Errorf("failed to convert resource to structured object: %w", err)
}

addLabels(getTemplate(obj), c.labels)

newUnstructuredMap, err := c.unstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, fmt.Errorf("failed to convert resource to unstructured object: %w", err)
}

return &unstructured.Unstructured{Object: newUnstructuredMap}, nil
}

type objectWithLabels interface {
GetLabels() map[string]string
SetLabels(labels map[string]string)
}

func addLabels(obj objectWithLabels, labels map[string]string) {
originalLabels := obj.GetLabels()
mergedLabels := make(map[string]string, len(labels)+len(originalLabels))
maps.Copy(mergedLabels, originalLabels)
maps.Copy(mergedLabels, labels)
obj.SetLabels(mergedLabels)
}
Loading

0 comments on commit e4dbb20

Please sign in to comment.