Skip to content

Commit

Permalink
Move CRD convert functionality to v1alpha1 package (#2704)
Browse files Browse the repository at this point in the history
* Move CRD convert functionality to v1alpha1 package

Signed-off-by: Pavol Loffay <[email protected]>

* Fix

---------

Signed-off-by: Pavol Loffay <[email protected]>
  • Loading branch information
pavolloffay authored Mar 4, 2024
1 parent 7b64ec8 commit 50e847f
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 45 deletions.
18 changes: 11 additions & 7 deletions internal/api/convert/v1alpha.go → apis/v1alpha1/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package convert
package v1alpha1

import (
"errors"

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

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
)

func V1Alpha1to2(in v1alpha1.OpenTelemetryCollector) (v1beta1.OpenTelemetryCollector, error) {
func Tov1beta1(in OpenTelemetryCollector) (v1beta1.OpenTelemetryCollector, error) {
copy := in.DeepCopy()
out := v1beta1.OpenTelemetryCollector{
TypeMeta: copy.TypeMeta,
Expand Down Expand Up @@ -125,15 +124,20 @@ func V1Alpha1to2(in v1alpha1.OpenTelemetryCollector) (v1beta1.OpenTelemetryColle

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

out.Spec.ConfigMaps = copy.Spec.ConfigMaps
for _, cm := range copy.Spec.ConfigMaps {
out.Spec.ConfigMaps = append(out.Spec.ConfigMaps, v1beta1.ConfigMapsSpec{
Name: cm.Name,
MountPath: cm.MountPath,
})
}
out.Spec.DaemonSetUpdateStrategy = copy.Spec.UpdateStrategy
out.Spec.DeploymentUpdateStrategy.Type = copy.Spec.DeploymentUpdateStrategy.Type
out.Spec.DeploymentUpdateStrategy.RollingUpdate = copy.Spec.DeploymentUpdateStrategy.RollingUpdate

return out, nil
}

func TargetAllocatorEmbedded(in v1alpha1.OpenTelemetryTargetAllocator) v1beta1.TargetAllocatorEmbedded {
func TargetAllocatorEmbedded(in OpenTelemetryTargetAllocator) v1beta1.TargetAllocatorEmbedded {
out := v1beta1.TargetAllocatorEmbedded{}
out.Replicas = in.Replicas
out.NodeSelector = in.NodeSelector
Expand All @@ -151,8 +155,8 @@ func TargetAllocatorEmbedded(in v1alpha1.OpenTelemetryTargetAllocator) v1beta1.T
out.TopologySpreadConstraints = in.TopologySpreadConstraints
out.Tolerations = in.Tolerations
out.Env = in.Env
out.Observability = v1alpha1.ObservabilitySpec{
Metrics: v1alpha1.MetricsConfigSpec{
out.Observability = v1beta1.ObservabilitySpec{
Metrics: v1beta1.MetricsConfigSpec{
EnableMetrics: in.Observability.Metrics.EnableMetrics,
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package convert
package v1alpha1

import (
"testing"
Expand All @@ -25,7 +25,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
)

Expand All @@ -49,16 +48,16 @@ service:
processors: [resourcedetection]
exporters: [otlp]
`
cfgV1 := v1alpha1.OpenTelemetryCollector{
Spec: v1alpha1.OpenTelemetryCollectorSpec{
cfgV1 := OpenTelemetryCollector{
Spec: OpenTelemetryCollectorSpec{
Config: config,
Args: map[string]string{
"test": "something",
},
},
}

cfgV2, err := V1Alpha1to2(cfgV1)
cfgV2, err := Tov1beta1(cfgV1)
assert.Nil(t, err)
assert.NotNil(t, cfgV2)
assert.Equal(t, cfgV1.Spec.Args, cfgV2.Spec.Args)
Expand All @@ -69,13 +68,13 @@ service:
})
t.Run("invalid config", func(t *testing.T) {
config := `!!!`
cfgV1 := v1alpha1.OpenTelemetryCollector{
Spec: v1alpha1.OpenTelemetryCollectorSpec{
cfgV1 := OpenTelemetryCollector{
Spec: OpenTelemetryCollectorSpec{
Config: config,
},
}

_, err := V1Alpha1to2(cfgV1)
_, err := Tov1beta1(cfgV1)
assert.ErrorContains(t, err, "could not convert config json to v1beta1.Config")
})
}
Expand All @@ -86,7 +85,7 @@ func Test_TargetAllocator(t *testing.T) {
privileged := true
runAsUser := int64(1337)
runasGroup := int64(1338)
input := v1alpha1.OpenTelemetryTargetAllocator{
input := OpenTelemetryTargetAllocator{
Replicas: &replicas,
NodeSelector: map[string]string{"key": "value"},
Resources: v1.ResourceRequirements{
Expand All @@ -99,7 +98,7 @@ func Test_TargetAllocator(t *testing.T) {
v1.ResourceMemory: resource.MustParse("128Mi"),
},
},
AllocationStrategy: v1alpha1.OpenTelemetryTargetAllocatorAllocationStrategyConsistentHashing,
AllocationStrategy: OpenTelemetryTargetAllocatorAllocationStrategyConsistentHashing,
FilterStrategy: "relabel-config",
ServiceAccount: "serviceAccountName",
Image: "custom_image",
Expand All @@ -121,7 +120,7 @@ func Test_TargetAllocator(t *testing.T) {
},
},
},
PrometheusCR: v1alpha1.OpenTelemetryTargetAllocatorPrometheusCR{
PrometheusCR: OpenTelemetryTargetAllocatorPrometheusCR{
Enabled: true,
ScrapeInterval: &metav1.Duration{Duration: time.Second},
PodMonitorSelector: map[string]string{"podmonitorkey": "podmonitorvalue"},
Expand Down Expand Up @@ -165,12 +164,12 @@ func Test_TargetAllocator(t *testing.T) {
},
},
},
Observability: v1alpha1.ObservabilitySpec{
Metrics: v1alpha1.MetricsConfigSpec{
Observability: ObservabilitySpec{
Metrics: MetricsConfigSpec{
EnableMetrics: true,
},
},
PodDisruptionBudget: &v1alpha1.PodDisruptionBudgetSpec{
PodDisruptionBudget: &PodDisruptionBudgetSpec{
MaxUnavailable: &intstr.IntOrString{
Type: intstr.Int,
IntVal: 1,
Expand Down Expand Up @@ -203,7 +202,12 @@ func Test_TargetAllocator(t *testing.T) {
TopologySpreadConstraints: input.TopologySpreadConstraints,
Tolerations: input.Tolerations,
Env: input.Env,
Observability: input.Observability,
Observability: v1beta1.ObservabilitySpec{
Metrics: v1beta1.MetricsConfigSpec{
EnableMetrics: input.Observability.Metrics.EnableMetrics,
DisablePrometheusAnnotations: input.Observability.Metrics.DisablePrometheusAnnotations,
},
},
PodDisruptionBudget: &v1beta1.PodDisruptionBudgetSpec{
MinAvailable: input.PodDisruptionBudget.MinAvailable,
MaxUnavailable: input.PodDisruptionBudget.MaxUnavailable,
Expand Down
63 changes: 57 additions & 6 deletions apis/v1beta1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
v1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
)

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

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

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

// ObservabilitySpec defines how telemetry data gets handled.
type ObservabilitySpec struct {
// Metrics defines the metrics configuration for operands.
//
// +optional
// +kubebuilder:validation:Optional
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Metrics Config"
Metrics MetricsConfigSpec `json:"metrics,omitempty"`
}

// MetricsConfigSpec defines a metrics config.
type MetricsConfigSpec struct {
// EnableMetrics specifies if ServiceMonitor or PodMonitor(for sidecar mode) should be created for the service managed by the OpenTelemetry Operator.
// The operator.observability.prometheus feature gate must be enabled to use this feature.
//
// +optional
// +kubebuilder:validation:Optional
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Create ServiceMonitors for OpenTelemetry Collector"
EnableMetrics bool `json:"enableMetrics,omitempty"`
// DisablePrometheusAnnotations controls the automatic addition of default Prometheus annotations
// ('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')
//
// +optional
// +kubebuilder:validation:Optional
DisablePrometheusAnnotations bool `json:"disablePrometheusAnnotations,omitempty"`
}

// ScaleSubresourceStatus defines the observed state of the OpenTelemetryCollector's
// scale subresource.
type ScaleSubresourceStatus struct {
// The selector used to match the OpenTelemetryCollector's
// deployment or statefulSet pods.
// +optional
Selector string `json:"selector,omitempty"`

// The total number non-terminated pods targeted by this
// OpenTelemetryCollector's deployment or statefulSet.
// +optional
Replicas int32 `json:"replicas,omitempty"`

// StatusReplicas is the number of pods targeted by this OpenTelemetryCollector's with a Ready Condition /
// Total number of non-terminated pods targeted by this OpenTelemetryCollector's (their labels match the selector).
// Deployment, Daemonset, StatefulSet.
// +optional
StatusReplicas string `json:"statusReplicas,omitempty"`
}

type ConfigMapsSpec struct {
// Configmap defines name and path where the configMaps should be mounted.
Name string `json:"name"`
MountPath string `json:"mountpath"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

Expand Down
64 changes: 62 additions & 2 deletions apis/v1beta1/zz_generated.deepcopy.go

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

4 changes: 2 additions & 2 deletions controllers/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1551,8 +1551,8 @@ prometheus_cr:
Enabled: true,
},
FilterStrategy: "relabel-config",
Observability: v1alpha1.ObservabilitySpec{
Metrics: v1alpha1.MetricsConfigSpec{
Observability: v1beta1.ObservabilitySpec{
Metrics: v1beta1.MetricsConfigSpec{
EnableMetrics: true,
},
},
Expand Down
3 changes: 1 addition & 2 deletions controllers/opentelemetrycollector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/internal/api/convert"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
Expand Down Expand Up @@ -131,7 +130,7 @@ func (r *OpenTelemetryCollectorReconciler) findOtelOwnedObjects(ctx context.Cont
}

func (r *OpenTelemetryCollectorReconciler) getParams(instance v1alpha1.OpenTelemetryCollector) (manifests.Params, error) {
otelCol, err := convert.V1Alpha1to2(instance)
otelCol, err := v1alpha1.Tov1beta1(instance)
if err != nil {
return manifests.Params{}, err
}
Expand Down
Loading

0 comments on commit 50e847f

Please sign in to comment.