From fd492fcaa5daf0ab80b9bd25220bea6397abbef2 Mon Sep 17 00:00:00 2001 From: Matt Hagenbuch Date: Wed, 1 May 2024 12:05:56 -0400 Subject: [PATCH] append hash to configmap name --- apis/v1beta1/opentelemetrycollector_types.go | 4 ++++ apis/v1beta1/zz_generated.deepcopy.go | 5 +++++ .../manifests/opentelemetry.io_opentelemetrycollectors.yaml | 3 +++ .../crd/bases/opentelemetry.io_opentelemetrycollectors.yaml | 3 +++ config/manager/kustomization.yaml | 6 ++++++ internal/manifests/collector/configmap.go | 6 +++++- internal/manifests/collector/volume.go | 5 ++++- internal/manifests/manifestutils/annotations.go | 6 +++--- internal/naming/main.go | 5 +++-- 9 files changed, 36 insertions(+), 7 deletions(-) diff --git a/apis/v1beta1/opentelemetrycollector_types.go b/apis/v1beta1/opentelemetrycollector_types.go index 4421165944..349b802f56 100644 --- a/apis/v1beta1/opentelemetrycollector_types.go +++ b/apis/v1beta1/opentelemetrycollector_types.go @@ -94,6 +94,10 @@ type OpenTelemetryCollectorSpec struct { // +required // +kubebuilder:pruning:PreserveUnknownFields Config Config `json:"config"` + // ConfigVersions defines the number versions to keep for the collector config. Each config version is stored in a separate ConfigMap. + // Defaults to 3. The minimum value is 1. + // +optional + ConfigVersions *int32 `json:"configVersions,omitempty"` // Ingress is used to specify how OpenTelemetry Collector is exposed. This // functionality is only available if one of the valid modes is set. // Valid modes are: deployment, daemonset and statefulset. diff --git a/apis/v1beta1/zz_generated.deepcopy.go b/apis/v1beta1/zz_generated.deepcopy.go index c5e574127c..0902c1c910 100644 --- a/apis/v1beta1/zz_generated.deepcopy.go +++ b/apis/v1beta1/zz_generated.deepcopy.go @@ -300,6 +300,11 @@ func (in *OpenTelemetryCollectorSpec) DeepCopyInto(out *OpenTelemetryCollectorSp in.OpenTelemetryCommonFields.DeepCopyInto(&out.OpenTelemetryCommonFields) in.TargetAllocator.DeepCopyInto(&out.TargetAllocator) in.Config.DeepCopyInto(&out.Config) + if in.ConfigVersions != nil { + in, out := &in.ConfigVersions, &out.ConfigVersions + *out = new(int32) + **out = **in + } in.Ingress.DeepCopyInto(&out.Ingress) if in.LivenessProbe != nil { in, out := &in.LivenessProbe, &out.LivenessProbe diff --git a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml index 5688544892..938f63344a 100644 --- a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml +++ b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml @@ -5555,6 +5555,9 @@ spec: - service type: object x-kubernetes-preserve-unknown-fields: true + configVersions: + format: int32 + type: integer configmaps: items: properties: diff --git a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml index 03a1360bfa..74dd3bec52 100644 --- a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml +++ b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml @@ -5541,6 +5541,9 @@ spec: - service type: object x-kubernetes-preserve-unknown-fields: true + configVersions: + format: int32 + type: integer configmaps: items: properties: diff --git a/config/manager/kustomization.yaml b/config/manager/kustomization.yaml index 5c5f0b84cb..9396d7d702 100644 --- a/config/manager/kustomization.yaml +++ b/config/manager/kustomization.yaml @@ -1,2 +1,8 @@ resources: - manager.yaml +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +images: +- name: controller + newName: localhost:5001/opentelemetry-operator + newTag: matth-dev-1714578767 diff --git a/internal/manifests/collector/configmap.go b/internal/manifests/collector/configmap.go index 58ee4c1312..137232683d 100644 --- a/internal/manifests/collector/configmap.go +++ b/internal/manifests/collector/configmap.go @@ -24,7 +24,11 @@ import ( ) func ConfigMap(params manifests.Params) (*corev1.ConfigMap, error) { - name := naming.ConfigMap(params.OtelCol.Name) + hash, err := manifestutils.GetConfigMapSHA(params.OtelCol.Spec.Config) + if err != nil { + return nil, err + } + name := naming.ConfigMap(params.OtelCol.Name, hash) labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{}) replacedConf, err := ReplaceConfig(params.OtelCol) diff --git a/internal/manifests/collector/volume.go b/internal/manifests/collector/volume.go index 5cf82fde7a..ea033b3a4a 100644 --- a/internal/manifests/collector/volume.go +++ b/internal/manifests/collector/volume.go @@ -20,16 +20,19 @@ import ( "github.com/open-telemetry/opentelemetry-operator/apis/v1beta1" "github.com/open-telemetry/opentelemetry-operator/internal/config" + "github.com/open-telemetry/opentelemetry-operator/internal/manifests/manifestutils" "github.com/open-telemetry/opentelemetry-operator/internal/naming" ) // Volumes builds the volumes for the given instance, including the config map volume. func Volumes(cfg config.Config, otelcol v1beta1.OpenTelemetryCollector) []corev1.Volume { + hash, _ := manifestutils.GetConfigMapSHA(otelcol.Spec.Config) + configMapName := naming.ConfigMap(otelcol.Name, hash) volumes := []corev1.Volume{{ Name: naming.ConfigMapVolume(), VolumeSource: corev1.VolumeSource{ ConfigMap: &corev1.ConfigMapVolumeSource{ - LocalObjectReference: corev1.LocalObjectReference{Name: naming.ConfigMap(otelcol.Name)}, + LocalObjectReference: corev1.LocalObjectReference{Name: configMapName}, Items: []corev1.KeyToPath{{ Key: cfg.CollectorConfigMapEntry(), Path: cfg.CollectorConfigMapEntry(), diff --git a/internal/manifests/manifestutils/annotations.go b/internal/manifests/manifestutils/annotations.go index 06f1baed81..c4243e350d 100644 --- a/internal/manifests/manifestutils/annotations.go +++ b/internal/manifests/manifestutils/annotations.go @@ -44,7 +44,7 @@ func Annotations(instance v1beta1.OpenTelemetryCollector, filterAnnotations []st } } - hash, err := getConfigMapSHA(instance.Spec.Config) + hash, err := GetConfigMapSHA(instance.Spec.Config) if err != nil { return nil, err } @@ -78,7 +78,7 @@ func PodAnnotations(instance v1beta1.OpenTelemetryCollector, filterAnnotations [ } } - hash, err := getConfigMapSHA(instance.Spec.Config) + hash, err := GetConfigMapSHA(instance.Spec.Config) if err != nil { return nil, err } @@ -88,7 +88,7 @@ func PodAnnotations(instance v1beta1.OpenTelemetryCollector, filterAnnotations [ return podAnnotations, nil } -func getConfigMapSHA(config v1beta1.Config) (string, error) { +func GetConfigMapSHA(config v1beta1.Config) (string, error) { b, err := json.Marshal(&config) if err != nil { return "", err diff --git a/internal/naming/main.go b/internal/naming/main.go index 8b801ce75e..ddcc6b1458 100644 --- a/internal/naming/main.go +++ b/internal/naming/main.go @@ -16,8 +16,9 @@ package naming // ConfigMap builds the name for the config map used in the OpenTelemetryCollector containers. -func ConfigMap(otelcol string) string { - return DNSName(Truncate("%s-collector", 63, otelcol)) +// The configHash should be calculated using manifestutils.GetConfigMapSHA +func ConfigMap(otelcol, configHash string) string { + return DNSName(Truncate("%s-collector-%s", 63, otelcol, configHash[:8])) } // TAConfigMap returns the name for the config map used in the TargetAllocator.