Skip to content

Commit 50e847f

Browse files
authored
Move CRD convert functionality to v1alpha1 package (open-telemetry#2704)
* Move CRD convert functionality to v1alpha1 package Signed-off-by: Pavol Loffay <[email protected]> * Fix --------- Signed-off-by: Pavol Loffay <[email protected]>
1 parent 7b64ec8 commit 50e847f

11 files changed

+158
-45
lines changed

internal/api/convert/v1alpha.go apis/v1alpha1/convert.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,18 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package convert
15+
package v1alpha1
1616

1717
import (
1818
"errors"
1919

2020
"gopkg.in/yaml.v3"
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2222

23-
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
2423
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
2524
)
2625

27-
func V1Alpha1to2(in v1alpha1.OpenTelemetryCollector) (v1beta1.OpenTelemetryCollector, error) {
26+
func Tov1beta1(in OpenTelemetryCollector) (v1beta1.OpenTelemetryCollector, error) {
2827
copy := in.DeepCopy()
2928
out := v1beta1.OpenTelemetryCollector{
3029
TypeMeta: copy.TypeMeta,
@@ -125,15 +124,20 @@ func V1Alpha1to2(in v1alpha1.OpenTelemetryCollector) (v1beta1.OpenTelemetryColle
125124

126125
out.Spec.Observability.Metrics.EnableMetrics = copy.Spec.Observability.Metrics.EnableMetrics
127126

128-
out.Spec.ConfigMaps = copy.Spec.ConfigMaps
127+
for _, cm := range copy.Spec.ConfigMaps {
128+
out.Spec.ConfigMaps = append(out.Spec.ConfigMaps, v1beta1.ConfigMapsSpec{
129+
Name: cm.Name,
130+
MountPath: cm.MountPath,
131+
})
132+
}
129133
out.Spec.DaemonSetUpdateStrategy = copy.Spec.UpdateStrategy
130134
out.Spec.DeploymentUpdateStrategy.Type = copy.Spec.DeploymentUpdateStrategy.Type
131135
out.Spec.DeploymentUpdateStrategy.RollingUpdate = copy.Spec.DeploymentUpdateStrategy.RollingUpdate
132136

133137
return out, nil
134138
}
135139

136-
func TargetAllocatorEmbedded(in v1alpha1.OpenTelemetryTargetAllocator) v1beta1.TargetAllocatorEmbedded {
140+
func TargetAllocatorEmbedded(in OpenTelemetryTargetAllocator) v1beta1.TargetAllocatorEmbedded {
137141
out := v1beta1.TargetAllocatorEmbedded{}
138142
out.Replicas = in.Replicas
139143
out.NodeSelector = in.NodeSelector
@@ -151,8 +155,8 @@ func TargetAllocatorEmbedded(in v1alpha1.OpenTelemetryTargetAllocator) v1beta1.T
151155
out.TopologySpreadConstraints = in.TopologySpreadConstraints
152156
out.Tolerations = in.Tolerations
153157
out.Env = in.Env
154-
out.Observability = v1alpha1.ObservabilitySpec{
155-
Metrics: v1alpha1.MetricsConfigSpec{
158+
out.Observability = v1beta1.ObservabilitySpec{
159+
Metrics: v1beta1.MetricsConfigSpec{
156160
EnableMetrics: in.Observability.Metrics.EnableMetrics,
157161
},
158162
}

internal/api/convert/v1alpha_test.go apis/v1alpha1/convert_test.go

+19-15
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package convert
15+
package v1alpha1
1616

1717
import (
1818
"testing"
@@ -25,7 +25,6 @@ import (
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
"k8s.io/apimachinery/pkg/util/intstr"
2727

28-
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
2928
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
3029
)
3130

@@ -49,16 +48,16 @@ service:
4948
processors: [resourcedetection]
5049
exporters: [otlp]
5150
`
52-
cfgV1 := v1alpha1.OpenTelemetryCollector{
53-
Spec: v1alpha1.OpenTelemetryCollectorSpec{
51+
cfgV1 := OpenTelemetryCollector{
52+
Spec: OpenTelemetryCollectorSpec{
5453
Config: config,
5554
Args: map[string]string{
5655
"test": "something",
5756
},
5857
},
5958
}
6059

61-
cfgV2, err := V1Alpha1to2(cfgV1)
60+
cfgV2, err := Tov1beta1(cfgV1)
6261
assert.Nil(t, err)
6362
assert.NotNil(t, cfgV2)
6463
assert.Equal(t, cfgV1.Spec.Args, cfgV2.Spec.Args)
@@ -69,13 +68,13 @@ service:
6968
})
7069
t.Run("invalid config", func(t *testing.T) {
7170
config := `!!!`
72-
cfgV1 := v1alpha1.OpenTelemetryCollector{
73-
Spec: v1alpha1.OpenTelemetryCollectorSpec{
71+
cfgV1 := OpenTelemetryCollector{
72+
Spec: OpenTelemetryCollectorSpec{
7473
Config: config,
7574
},
7675
}
7776

78-
_, err := V1Alpha1to2(cfgV1)
77+
_, err := Tov1beta1(cfgV1)
7978
assert.ErrorContains(t, err, "could not convert config json to v1beta1.Config")
8079
})
8180
}
@@ -86,7 +85,7 @@ func Test_TargetAllocator(t *testing.T) {
8685
privileged := true
8786
runAsUser := int64(1337)
8887
runasGroup := int64(1338)
89-
input := v1alpha1.OpenTelemetryTargetAllocator{
88+
input := OpenTelemetryTargetAllocator{
9089
Replicas: &replicas,
9190
NodeSelector: map[string]string{"key": "value"},
9291
Resources: v1.ResourceRequirements{
@@ -99,7 +98,7 @@ func Test_TargetAllocator(t *testing.T) {
9998
v1.ResourceMemory: resource.MustParse("128Mi"),
10099
},
101100
},
102-
AllocationStrategy: v1alpha1.OpenTelemetryTargetAllocatorAllocationStrategyConsistentHashing,
101+
AllocationStrategy: OpenTelemetryTargetAllocatorAllocationStrategyConsistentHashing,
103102
FilterStrategy: "relabel-config",
104103
ServiceAccount: "serviceAccountName",
105104
Image: "custom_image",
@@ -121,7 +120,7 @@ func Test_TargetAllocator(t *testing.T) {
121120
},
122121
},
123122
},
124-
PrometheusCR: v1alpha1.OpenTelemetryTargetAllocatorPrometheusCR{
123+
PrometheusCR: OpenTelemetryTargetAllocatorPrometheusCR{
125124
Enabled: true,
126125
ScrapeInterval: &metav1.Duration{Duration: time.Second},
127126
PodMonitorSelector: map[string]string{"podmonitorkey": "podmonitorvalue"},
@@ -165,12 +164,12 @@ func Test_TargetAllocator(t *testing.T) {
165164
},
166165
},
167166
},
168-
Observability: v1alpha1.ObservabilitySpec{
169-
Metrics: v1alpha1.MetricsConfigSpec{
167+
Observability: ObservabilitySpec{
168+
Metrics: MetricsConfigSpec{
170169
EnableMetrics: true,
171170
},
172171
},
173-
PodDisruptionBudget: &v1alpha1.PodDisruptionBudgetSpec{
172+
PodDisruptionBudget: &PodDisruptionBudgetSpec{
174173
MaxUnavailable: &intstr.IntOrString{
175174
Type: intstr.Int,
176175
IntVal: 1,
@@ -203,7 +202,12 @@ func Test_TargetAllocator(t *testing.T) {
203202
TopologySpreadConstraints: input.TopologySpreadConstraints,
204203
Tolerations: input.Tolerations,
205204
Env: input.Env,
206-
Observability: input.Observability,
205+
Observability: v1beta1.ObservabilitySpec{
206+
Metrics: v1beta1.MetricsConfigSpec{
207+
EnableMetrics: input.Observability.Metrics.EnableMetrics,
208+
DisablePrometheusAnnotations: input.Observability.Metrics.DisablePrometheusAnnotations,
209+
},
210+
},
207211
PodDisruptionBudget: &v1beta1.PodDisruptionBudgetSpec{
208212
MinAvailable: input.PodDisruptionBudget.MinAvailable,
209213
MaxUnavailable: input.PodDisruptionBudget.MaxUnavailable,

apis/v1beta1/opentelemetrycollector_types.go

+57-6
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import (
2121
v1 "k8s.io/api/core/v1"
2222
networkingv1 "k8s.io/api/networking/v1"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24-
25-
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
2624
)
2725

2826
// Probe defines the OpenTelemetry's pod probe config. Only Liveness probe is supported currently.
@@ -148,12 +146,12 @@ type OpenTelemetryCollectorSpec struct {
148146
// +optional
149147
// +kubebuilder:validation:Optional
150148
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Observability"
151-
Observability v1alpha1.ObservabilitySpec `json:"observability,omitempty"`
149+
Observability ObservabilitySpec `json:"observability,omitempty"`
152150

153151
// ConfigMaps is a list of ConfigMaps in the same namespace as the OpenTelemetryCollector
154152
// object, which shall be mounted into the Collector Pods.
155153
// Each ConfigMap will be added to the Collector's Deployments as a volume named `configmap-<configmap-name>`.
156-
ConfigMaps []v1alpha1.ConfigMapsSpec `json:"configmaps,omitempty"`
154+
ConfigMaps []ConfigMapsSpec `json:"configmaps,omitempty"`
157155
// UpdateStrategy represents the strategy the operator will take replacing existing DaemonSet pods with new pods
158156
// https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/daemon-set-v1/#DaemonSetSpec
159157
// This is only applicable to Daemonset mode.
@@ -236,7 +234,7 @@ type TargetAllocatorEmbedded struct {
236234
// +optional
237235
// +kubebuilder:validation:Optional
238236
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Observability"
239-
Observability v1alpha1.ObservabilitySpec `json:"observability,omitempty"`
237+
Observability ObservabilitySpec `json:"observability,omitempty"`
240238
// PodDisruptionBudget specifies the pod disruption budget configuration to use
241239
// for the target allocator workload.
242240
//
@@ -248,7 +246,7 @@ type TargetAllocatorEmbedded struct {
248246
type OpenTelemetryCollectorStatus struct {
249247
// Scale is the OpenTelemetryCollector's scale subresource status.
250248
// +optional
251-
Scale v1alpha1.ScaleSubresourceStatus `json:"scale,omitempty"`
249+
Scale ScaleSubresourceStatus `json:"scale,omitempty"`
252250

253251
// Version of the managed OpenTelemetry Collector (operand)
254252
// +optional
@@ -270,6 +268,59 @@ type OpenTelemetryCollectorStatus struct {
270268
Replicas int32 `json:"replicas,omitempty"`
271269
}
272270

271+
// ObservabilitySpec defines how telemetry data gets handled.
272+
type ObservabilitySpec struct {
273+
// Metrics defines the metrics configuration for operands.
274+
//
275+
// +optional
276+
// +kubebuilder:validation:Optional
277+
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Metrics Config"
278+
Metrics MetricsConfigSpec `json:"metrics,omitempty"`
279+
}
280+
281+
// MetricsConfigSpec defines a metrics config.
282+
type MetricsConfigSpec struct {
283+
// EnableMetrics specifies if ServiceMonitor or PodMonitor(for sidecar mode) should be created for the service managed by the OpenTelemetry Operator.
284+
// The operator.observability.prometheus feature gate must be enabled to use this feature.
285+
//
286+
// +optional
287+
// +kubebuilder:validation:Optional
288+
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Create ServiceMonitors for OpenTelemetry Collector"
289+
EnableMetrics bool `json:"enableMetrics,omitempty"`
290+
// DisablePrometheusAnnotations controls the automatic addition of default Prometheus annotations
291+
// ('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')
292+
//
293+
// +optional
294+
// +kubebuilder:validation:Optional
295+
DisablePrometheusAnnotations bool `json:"disablePrometheusAnnotations,omitempty"`
296+
}
297+
298+
// ScaleSubresourceStatus defines the observed state of the OpenTelemetryCollector's
299+
// scale subresource.
300+
type ScaleSubresourceStatus struct {
301+
// The selector used to match the OpenTelemetryCollector's
302+
// deployment or statefulSet pods.
303+
// +optional
304+
Selector string `json:"selector,omitempty"`
305+
306+
// The total number non-terminated pods targeted by this
307+
// OpenTelemetryCollector's deployment or statefulSet.
308+
// +optional
309+
Replicas int32 `json:"replicas,omitempty"`
310+
311+
// StatusReplicas is the number of pods targeted by this OpenTelemetryCollector's with a Ready Condition /
312+
// Total number of non-terminated pods targeted by this OpenTelemetryCollector's (their labels match the selector).
313+
// Deployment, Daemonset, StatefulSet.
314+
// +optional
315+
StatusReplicas string `json:"statusReplicas,omitempty"`
316+
}
317+
318+
type ConfigMapsSpec struct {
319+
// Configmap defines name and path where the configMaps should be mounted.
320+
Name string `json:"name"`
321+
MountPath string `json:"mountpath"`
322+
}
323+
273324
//+kubebuilder:object:root=true
274325
//+kubebuilder:subresource:status
275326

apis/v1beta1/zz_generated.deepcopy.go

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

controllers/builder_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1551,8 +1551,8 @@ prometheus_cr:
15511551
Enabled: true,
15521552
},
15531553
FilterStrategy: "relabel-config",
1554-
Observability: v1alpha1.ObservabilitySpec{
1555-
Metrics: v1alpha1.MetricsConfigSpec{
1554+
Observability: v1beta1.ObservabilitySpec{
1555+
Metrics: v1beta1.MetricsConfigSpec{
15561556
EnableMetrics: true,
15571557
},
15581558
},

controllers/opentelemetrycollector_controller.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"sigs.k8s.io/controller-runtime/pkg/client"
3838

3939
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
40-
"github.com/open-telemetry/opentelemetry-operator/internal/api/convert"
4140
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
4241
"github.com/open-telemetry/opentelemetry-operator/internal/config"
4342
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
@@ -131,7 +130,7 @@ func (r *OpenTelemetryCollectorReconciler) findOtelOwnedObjects(ctx context.Cont
131130
}
132131

133132
func (r *OpenTelemetryCollectorReconciler) getParams(instance v1alpha1.OpenTelemetryCollector) (manifests.Params, error) {
134-
otelCol, err := convert.V1Alpha1to2(instance)
133+
otelCol, err := v1alpha1.Tov1beta1(instance)
135134
if err != nil {
136135
return manifests.Params{}, err
137136
}

0 commit comments

Comments
 (0)