From 6f235ccd051facf4c795b591f16147d984fc96b7 Mon Sep 17 00:00:00 2001 From: vsoch Date: Fri, 3 Nov 2023 15:43:07 -0600 Subject: [PATCH] add pod annotations and labels Problem: to use the ORAS operator with the metrics operator we need annotations Solution: expose annotations and labels in the spec Signed-off-by: vsoch --- api/v1alpha2/metric_types.go | 18 +++++++++++++++++- api/v1alpha2/zz_generated.deepcopy.go | 14 ++++++++++++++ .../bases/flux-framework.org_metricsets.yaml | 10 ++++++++++ .../custom-resource-definition.md | 18 ++++++++++++++++++ examples/dist/metrics-operator-arm.yaml | 10 ++++++++++ examples/dist/metrics-operator.yaml | 10 ++++++++++ pkg/metrics/set.go | 7 ++++--- 7 files changed, 83 insertions(+), 4 deletions(-) diff --git a/api/v1alpha2/metric_types.go b/api/v1alpha2/metric_types.go index 64e9cb1..d7d6007 100644 --- a/api/v1alpha2/metric_types.go +++ b/api/v1alpha2/metric_types.go @@ -81,6 +81,14 @@ type Logging struct { // Pod attributes that can be given to an application or metric type Pod struct { + // Annotations to add to the pod + //+optional + Annotations map[string]string `json:"annotations"` + + // Labels to add to the pod + //+optional + Labels map[string]string `json:"labels"` + // name of service account to associate with pod //+optional ServiceAccountName string `json:"serviceAccountName"` @@ -205,7 +213,9 @@ type Metric struct { // Get pod labels for a metric set func (m *MetricSet) GetPodLabels() map[string]string { - podLabels := map[string]string{} + // Start with those provided by the user + podLabels := m.Spec.Pod.Labels + // This is for autoscaling, although haven't used yet podLabels["cluster-name"] = m.Name // This is for the headless service @@ -233,6 +243,12 @@ type MetricSet struct { // Validate a requested metricset func (m *MetricSet) Validate() bool { + if m.Spec.Pod.Labels == nil { + m.Spec.Pod.Labels = map[string]string{} + } + if m.Spec.Pod.Annotations == nil { + m.Spec.Pod.Annotations = map[string]string{} + } if len(m.Spec.Metrics) == 0 { fmt.Printf("😥️ One or more metrics are required.\n") return false diff --git a/api/v1alpha2/zz_generated.deepcopy.go b/api/v1alpha2/zz_generated.deepcopy.go index cb43d2a..21a05ff 100644 --- a/api/v1alpha2/zz_generated.deepcopy.go +++ b/api/v1alpha2/zz_generated.deepcopy.go @@ -347,6 +347,20 @@ func (in *MetricSetStatus) DeepCopy() *MetricSetStatus { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Pod) DeepCopyInto(out *Pod) { *out = *in + if in.Annotations != nil { + in, out := &in.Annotations, &out.Annotations + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + if in.Labels != nil { + in, out := &in.Labels, &out.Labels + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } if in.NodeSelector != nil { in, out := &in.NodeSelector, &out.NodeSelector *out = make(map[string]string, len(*in)) diff --git a/config/crd/bases/flux-framework.org_metricsets.yaml b/config/crd/bases/flux-framework.org_metricsets.yaml index ad58e31..4467753 100644 --- a/config/crd/bases/flux-framework.org_metricsets.yaml +++ b/config/crd/bases/flux-framework.org_metricsets.yaml @@ -178,6 +178,16 @@ spec: description: Pod spec for the application, standalone, or storage metrics properties: + annotations: + additionalProperties: + type: string + description: Annotations to add to the pod + type: object + labels: + additionalProperties: + type: string + description: Labels to add to the pod + type: object nodeSelector: additionalProperties: type: string diff --git a/docs/getting_started/custom-resource-definition.md b/docs/getting_started/custom-resource-definition.md index 1d17ad9..e65a301 100644 --- a/docs/getting_started/custom-resource-definition.md +++ b/docs/getting_started/custom-resource-definition.md @@ -135,3 +135,21 @@ metrics: Each addon has its own custom options. You can look at examples and at our [addons documentation](addons.md) for more detail on how to add existing volumes or other custom functionality. + + +### pod + +You can customize variables for the pod, which currently includes labels, annotations, selectors, and a service account name, + +```yaml +spec: + pod: + annotations: + key.subkey: value + labels: + dinner: lasagna + serviceAccountName: mySVCAccount + nodeSelector: + key: value +``` + diff --git a/examples/dist/metrics-operator-arm.yaml b/examples/dist/metrics-operator-arm.yaml index a1237d4..0afd08a 100644 --- a/examples/dist/metrics-operator-arm.yaml +++ b/examples/dist/metrics-operator-arm.yaml @@ -173,6 +173,16 @@ spec: pod: description: Pod spec for the application, standalone, or storage metrics properties: + annotations: + additionalProperties: + type: string + description: Annotations to add to the pod + type: object + labels: + additionalProperties: + type: string + description: Labels to add to the pod + type: object nodeSelector: additionalProperties: type: string diff --git a/examples/dist/metrics-operator.yaml b/examples/dist/metrics-operator.yaml index 6075192..c82cbcd 100644 --- a/examples/dist/metrics-operator.yaml +++ b/examples/dist/metrics-operator.yaml @@ -173,6 +173,16 @@ spec: pod: description: Pod spec for the application, standalone, or storage metrics properties: + annotations: + additionalProperties: + type: string + description: Annotations to add to the pod + type: object + labels: + additionalProperties: + type: string + description: Labels to add to the pod + type: object nodeSelector: additionalProperties: type: string diff --git a/pkg/metrics/set.go b/pkg/metrics/set.go index f7556a8..88c3833 100644 --- a/pkg/metrics/set.go +++ b/pkg/metrics/set.go @@ -118,9 +118,10 @@ func AssembleReplicatedJob( // Note there is parameter to limit runtime Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ - Name: set.Name, - Namespace: set.Namespace, - Labels: podLabels, + Name: set.Name, + Namespace: set.Namespace, + Labels: podLabels, + Annotations: set.Spec.Pod.Annotations, }, Spec: corev1.PodSpec{ // matches the service