Skip to content

Commit 2cc2fee

Browse files
raffishiddeco
authored andcommitted
feat: manage label and annotations for a helmchart
Signed-off-by: Raffael Sahli <raffael.sahli@doodle.com>
1 parent af59329 commit 2cc2fee

File tree

5 files changed

+145
-2
lines changed

5 files changed

+145
-2
lines changed

api/v2beta1/helmrelease_types.go

+19
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,30 @@ func (in HelmReleaseSpec) GetUninstall() Uninstall {
219219
// generate a v1beta2.HelmChart object in the same namespace as the referenced
220220
// v1beta2.Source.
221221
type HelmChartTemplate struct {
222+
// ObjectMeta holds the template for metadata including Labels and Annotations
223+
// +optional
224+
HelmChartTemplateObjectMeta `json:"metadata,omitempty"`
225+
222226
// Spec holds the template for the v1beta2.HelmChartSpec for this HelmRelease.
223227
// +required
224228
Spec HelmChartTemplateSpec `json:"spec"`
225229
}
226230

231+
type HelmChartTemplateObjectMeta struct {
232+
// Map of string keys and values that can be used to organize and categorize
233+
// (scope and select) objects.
234+
// More info: http://kubernetes.io/docs/user-guide/labels
235+
// +optional
236+
Labels map[string]string `json:"labels,omitempty"`
237+
238+
// Annotations is an unstructured key value map stored with a resource that may be
239+
// set by external tools to store and retrieve arbitrary metadata. They are not
240+
// queryable and should be preserved when modifying objects.
241+
// More info: http://kubernetes.io/docs/user-guide/annotations
242+
// +optional
243+
Annotations map[string]string `json:"annotations,omitempty"`
244+
}
245+
227246
// HelmChartTemplateSpec defines the template from which the controller will
228247
// generate a v1beta2.HelmChartSpec object.
229248
type HelmChartTemplateSpec struct {

api/v2beta1/zz_generated.deepcopy.go

+30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/helm.toolkit.fluxcd.io_helmreleases.yaml

+21
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,27 @@ spec:
5151
description: Chart defines the template of the v1beta2.HelmChart that
5252
should be created for this HelmRelease.
5353
properties:
54+
metadata:
55+
description: ObjectMeta holds the template for metadata including
56+
Labels and Annotations
57+
properties:
58+
annotations:
59+
additionalProperties:
60+
type: string
61+
description: 'Annotations is an unstructured key value map
62+
stored with a resource that may be set by external tools
63+
to store and retrieve arbitrary metadata. They are not queryable
64+
and should be preserved when modifying objects. More info:
65+
http://kubernetes.io/docs/user-guide/annotations'
66+
type: object
67+
labels:
68+
additionalProperties:
69+
type: string
70+
description: 'Map of string keys and values that can be used
71+
to organize and categorize (scope and select) objects. More
72+
info: http://kubernetes.io/docs/user-guide/labels'
73+
type: object
74+
type: object
5475
spec:
5576
description: Spec holds the template for the v1beta2.HelmChartSpec
5677
for this HelmRelease.

docs/api/helmrelease.md

+63
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,20 @@ v1beta2.Source.</p>
457457
<tbody>
458458
<tr>
459459
<td>
460+
<code>metadata</code><br>
461+
<em>
462+
<a href="#helm.toolkit.fluxcd.io/v2beta1.HelmChartTemplateObjectMeta">
463+
HelmChartTemplateObjectMeta
464+
</a>
465+
</em>
466+
</td>
467+
<td>
468+
<em>(Optional)</em>
469+
<p>ObjectMeta holds the template for metadata including Labels and Annotations</p>
470+
</td>
471+
</tr>
472+
<tr>
473+
<td>
460474
<code>spec</code><br>
461475
<em>
462476
<a href="#helm.toolkit.fluxcd.io/v2beta1.HelmChartTemplateSpec">
@@ -591,6 +605,55 @@ Chart dependencies, which are not bundled in the umbrella chart artifact, are no
591605
</table>
592606
</div>
593607
</div>
608+
<h3 id="helm.toolkit.fluxcd.io/v2beta1.HelmChartTemplateObjectMeta">HelmChartTemplateObjectMeta
609+
</h3>
610+
<p>
611+
(<em>Appears on:</em>
612+
<a href="#helm.toolkit.fluxcd.io/v2beta1.HelmChartTemplate">HelmChartTemplate</a>)
613+
</p>
614+
<div class="md-typeset__scrollwrap">
615+
<div class="md-typeset__table">
616+
<table>
617+
<thead>
618+
<tr>
619+
<th>Field</th>
620+
<th>Description</th>
621+
</tr>
622+
</thead>
623+
<tbody>
624+
<tr>
625+
<td>
626+
<code>labels</code><br>
627+
<em>
628+
map[string]string
629+
</em>
630+
</td>
631+
<td>
632+
<em>(Optional)</em>
633+
<p>Map of string keys and values that can be used to organize and categorize
634+
(scope and select) objects.
635+
More info: <a href="http://kubernetes.io/docs/user-guide/labels">http://kubernetes.io/docs/user-guide/labels</a></p>
636+
</td>
637+
</tr>
638+
<tr>
639+
<td>
640+
<code>annotations</code><br>
641+
<em>
642+
map[string]string
643+
</em>
644+
</td>
645+
<td>
646+
<em>(Optional)</em>
647+
<p>Annotations is an unstructured key value map stored with a resource that may be
648+
set by external tools to store and retrieve arbitrary metadata. They are not
649+
queryable and should be preserved when modifying objects.
650+
More info: <a href="http://kubernetes.io/docs/user-guide/annotations">http://kubernetes.io/docs/user-guide/annotations</a></p>
651+
</td>
652+
</tr>
653+
</tbody>
654+
</table>
655+
</div>
656+
</div>
594657
<h3 id="helm.toolkit.fluxcd.io/v2beta1.HelmChartTemplateSpec">HelmChartTemplateSpec
595658
</h3>
596659
<p>

internal/controllers/helmrelease_controller_chart.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
_ "github.com/opencontainers/go-digest/blake3"
3535
"helm.sh/helm/v3/pkg/chart"
3636
"helm.sh/helm/v3/pkg/chart/loader"
37+
apiequality "k8s.io/apimachinery/pkg/api/equality"
3738
apierrors "k8s.io/apimachinery/pkg/api/errors"
3839
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3940
"k8s.io/apimachinery/pkg/types"
@@ -82,6 +83,9 @@ func (r *HelmReleaseReconciler) reconcileChart(ctx context.Context, hr *v2.HelmR
8283
case helmChartRequiresUpdate(hr, &helmChart):
8384
ctrl.LoggerFrom(ctx).Info("chart diverged from template", strings.ToLower(sourcev1.HelmChartKind), chartName.String())
8485
helmChart.Spec = hc.Spec
86+
helmChart.Labels = hc.Labels
87+
helmChart.Annotations = hc.Annotations
88+
8589
if err = r.Client.Update(ctx, &helmChart); err != nil {
8690
return nil, err
8791
}
@@ -196,8 +200,10 @@ func buildHelmChartFromTemplate(hr *v2.HelmRelease) *sourcev1.HelmChart {
196200
template := hr.Spec.Chart
197201
return &sourcev1.HelmChart{
198202
ObjectMeta: metav1.ObjectMeta{
199-
Name: hr.GetHelmChartName(),
200-
Namespace: hr.Spec.Chart.GetNamespace(hr.Namespace),
203+
Name: hr.GetHelmChartName(),
204+
Namespace: hr.Spec.Chart.GetNamespace(hr.Namespace),
205+
Labels: hr.Spec.Chart.Labels,
206+
Annotations: hr.Spec.Chart.Annotations,
201207
},
202208
Spec: sourcev1.HelmChartSpec{
203209
Chart: template.Spec.Chart,
@@ -239,6 +245,10 @@ func helmChartRequiresUpdate(hr *v2.HelmRelease, chart *sourcev1.HelmChart) bool
239245
return true
240246
case template.Spec.ValuesFile != chart.Spec.ValuesFile:
241247
return true
248+
case !apiequality.Semantic.DeepEqual(template.Annotations, chart.Annotations):
249+
return true
250+
case !apiequality.Semantic.DeepEqual(template.Labels, chart.Labels):
251+
return true
242252
case !reflect.DeepEqual(templateVerificationToSourceVerification(template.Spec.Verify), chart.Spec.Verify):
243253
return true
244254
default:

0 commit comments

Comments
 (0)