Skip to content

Commit

Permalink
Merge pull request #107 from controlplaneio-fluxcd/instance-common-me…
Browse files Browse the repository at this point in the history
…tadata

Add `.spec.commonMetadata` to `FluxInstance` API
  • Loading branch information
stefanprodan authored Oct 19, 2024
2 parents ffa0523 + 1778ed2 commit 07e76b2
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 0 deletions.
15 changes: 15 additions & 0 deletions api/v1/common_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2024 Stefan Prodan.
// SPDX-License-Identifier: AGPL-3.0

package v1

// CommonMetadata defines the common labels and annotations.
type CommonMetadata struct {
// Annotations to be added to the object's metadata.
// +optional
Annotations map[string]string `json:"annotations,omitempty"`

// Labels to be added to the object's metadata.
// +optional
Labels map[string]string `json:"labels,omitempty"`
}
6 changes: 6 additions & 0 deletions api/v1/fluxinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ type FluxInstanceSpec struct {
// +optional
Components []Component `json:"components,omitempty"`

// CommonMetadata specifies the common labels and annotations that are
// applied to all resources. Any existing label or annotation will be
// overridden if its key matches a common one.
// +optional
CommonMetadata *CommonMetadata `json:"commonMetadata,omitempty"`

// Cluster holds the specification of the Kubernetes cluster.
// +optional
Cluster *Cluster `json:"cluster,omitempty"`
Expand Down
34 changes: 34 additions & 0 deletions api/v1/zz_generated.deepcopy.go

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

17 changes: 17 additions & 0 deletions config/crd/bases/fluxcd.controlplane.io_fluxinstances.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@ spec:
- domain
- networkPolicy
type: object
commonMetadata:
description: |-
CommonMetadata specifies the common labels and annotations that are
applied to all resources. Any existing label or annotation will be
overridden if its key matches a common one.
properties:
annotations:
additionalProperties:
type: string
description: Annotations to be added to the object's metadata.
type: object
labels:
additionalProperties:
type: string
description: Labels to be added to the object's metadata.
type: object
type: object
components:
description: |-
Components is the list of controllers to install.
Expand Down
28 changes: 28 additions & 0 deletions docs/api/v1/fluxinstance.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ spec:
storage:
class: "standard"
size: "10Gi"
commonMetadata:
labels:
app.kubernetes.io/name: flux
kustomize:
patches:
- target:
Expand Down Expand Up @@ -376,6 +379,31 @@ By default, the key is set to `sharding.fluxcd.io/key`.

The `.spec.sharding.shards` field is required and specifies the list of sharding values to use for the Flux controllers.

### Common metadata

The `.spec.commonMetadata` field is optional and specifies common metadata to be applied to all Kubernetes resources
part of the Flux instance.

It has two optional fields:

- `labels`: A map used for setting [labels](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/)
on an object. Any existing label will be overridden if it matches with a key in
this map.
- `annotations`: A map used for setting [annotations](https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/)
on an object. Any existing annotation will be overridden if it matches with a key
in this map.

Example common metadata:

```yaml
spec:
commonMetadata:
labels:
app.kubernetes.io/name: flux
annotations:
toolkit.fluxcd.io/tenant: sre-team
```

### Kustomize patches

The `.spec.kustomize.patches` field is optional and specifies the Kustomize patches to apply to the Flux controllers.
Expand Down
5 changes: 5 additions & 0 deletions internal/controller/fluxinstance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/fluxcd/pkg/runtime/patch"
"github.com/fluxcd/pkg/ssa"
"github.com/fluxcd/pkg/ssa/normalize"
ssautil "github.com/fluxcd/pkg/ssa/utils"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -377,6 +378,10 @@ func (r *FluxInstanceReconciler) apply(ctx context.Context,
return err
}

if cm := obj.Spec.CommonMetadata; cm != nil {
ssautil.SetCommonMetadata(objects, cm.Labels, cm.Annotations)
}

applyOpts := ssa.DefaultApplyOptions()
applyOpts.Cleanup = ssa.ApplyCleanupOptions{
// Remove the kubectl and helm annotations.
Expand Down
8 changes: 8 additions & 0 deletions internal/controller/fluxinstance_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ func TestFluxInstanceReconciler_LifeCycle(t *testing.T) {
g.Expect(sc.Spec.Template.Spec.Containers[0].Image).To(HavePrefix("ghcr.io/fluxcd/source-controller"))
g.Expect(sc.Spec.Template.Spec.Containers[0].Image).To(ContainSubstring("@sha256:"))

// Check if the deployments have the correct labels.
g.Expect(sc.Labels).To(HaveKeyWithValue("app.kubernetes.io/name", "flux"))

// Update the instance.
resultP := result.DeepCopy()
resultP.SetAnnotations(map[string]string{
Expand Down Expand Up @@ -680,6 +683,11 @@ func getDefaultFluxSpec() fluxcdv1.FluxInstanceSpec {
Path: "./",
Ref: "latest",
},
CommonMetadata: &fluxcdv1.CommonMetadata{
Labels: map[string]string{
"app.kubernetes.io/name": "flux",
},
},
Kustomize: &fluxcdv1.Kustomize{
Patches: []kustomize.Patch{
{
Expand Down

0 comments on commit 07e76b2

Please sign in to comment.