Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions api/v1alpha1/managedmetric_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package v1alpha1

import (
"fmt"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand All @@ -30,12 +28,9 @@ type ManagedMetricSpec struct {
// Sets the description that will be used to identify the metric in Dynatrace(or other providers)
// +optional
Description string `json:"description,omitempty"`
// Decide which kind the metric should keep track of (needs to be plural version)
Kind string `json:"kind,omitempty"`
// Define the group of your object that should be instrumented (without version at the end)
Group string `json:"group,omitempty"`
// Define version of the object you want to intrsument
Version string `json:"version,omitempty"`
// Defines which managed resources to observe
// +optional
Target *GroupVersionKind `json:"target,omitempty"`
// Define labels of your object to adapt filters of the query
// +optional
LabelSelector string `json:"labelSelector,omitempty"`
Expand Down Expand Up @@ -92,10 +87,10 @@ type ManagedMetricStatus struct {

// GvkToString returns group, version and kind as a string
func (r *ManagedMetric) GvkToString() string {
if r.Spec.Group == "" {
return fmt.Sprintf("/%s, Kind=%s", r.Spec.Version, r.Spec.Kind)
if r.Spec.Target != nil {
return r.Spec.Target.GVK().String()
}
return fmt.Sprintf("%s/%s, Kind=%s", r.Spec.Group, r.Spec.Version, r.Spec.Kind)
return ""
}

// SetConditions sets the conditions for the ManagedMetric
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,10 @@ spec:
description: Define fields of your object to adapt filters of the
query
type: string
group:
description: Define the group of your object that should be instrumented
(without version at the end)
type: string
interval:
default: 10m
description: Define in what interval the query should be recorded
type: string
kind:
description: Decide which kind the metric should keep track of (needs
to be plural version)
type: string
labelSelector:
description: Define labels of your object to adapt filters of the
query
Expand All @@ -98,9 +90,19 @@ spec:
namespace:
type: string
type: object
version:
description: Define version of the object you want to intrsument
type: string
target:
description: Defines which managed resources to observe
properties:
group:
description: Define the group of your object that should be instrumented
type: string
kind:
description: Define the kind of the object that should be instrumented
type: string
version:
description: Define version of the object you want to be instrumented
type: string
type: object
type: object
status:
description: ManagedMetricStatus defines the observed state of ManagedMetric
Expand Down
20 changes: 15 additions & 5 deletions internal/orchestrator/managedhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ func (h *ManagedHandler) getManagedResources(ctx context.Context) ([]Managed, er
if !crdv.Served {
continue
}
// only use the metric target version if provided
if h.metric.Spec.Version != "" && crdv.Name != h.metric.Spec.Version {
// drop versions that don't match the user provided target
target := h.metric.Spec.Target
if target != nil && target.Version != "" && target.Version != crdv.Name {
continue
}
versionsToRetrieve = append(versionsToRetrieve, crdv.Name)
Expand Down Expand Up @@ -249,17 +250,26 @@ type ClusterResourceStatus struct {
}

func (h *ManagedHandler) matchesGroupVersionKind(crd apiextensionsv1.CustomResourceDefinition) bool {
target := h.metric.Spec.Target
// if the user does not specify a GVK target, any managed CRD is considered a match
if target == nil {
return true
}
// CRDs may define multi-version APIs
// we consider a version to be a match if it exists in a CRD
crdVersions := make([]string, 0, len(crd.Spec.Versions))
for _, version := range crd.Spec.Versions {
crdVersions = append(crdVersions, version.Name)
}
if h.metric.Spec.Version != "" && !slices.Contains(crdVersions, h.metric.Spec.Version) {
// if the user specifies a target, we consider each GVK attribute and check if it matches the user value
// if the user does not specify a single GVK part, that part is considered unconditional and always a match
if target.Version != "" && !slices.Contains(crdVersions, target.Version) {
return false
}
if h.metric.Spec.Group != "" && crd.Spec.Group != h.metric.Spec.Group {
if target.Group != "" && target.Group != crd.Spec.Group {
return false
}
if h.metric.Spec.Kind != "" && crd.Spec.Names.Kind != h.metric.Spec.Kind {
if target.Kind != "" && target.Kind != crd.Spec.Names.Kind {
return false
}
return true
Expand Down
30 changes: 16 additions & 14 deletions internal/orchestrator/managedhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ func TestGetManagedResources(t *testing.T) {

tests := []struct {
name string
filter schema.GroupVersionKind
gvkTarget schema.GroupVersionKind
clusterCRDs []string
clusterResources []string
wantResources []string
}{
{
name: "fully qualified target spec",
filter: k8sObjectGVK,
name: "fully qualified target spec",
gvkTarget: k8sObjectGVK,
clusterCRDs: []string{
managedAndServedCRD(k8sObjectGVK),
managedAndServedCRD(k8sObjectCollectionGVK),
Expand All @@ -95,7 +95,7 @@ func TestGetManagedResources(t *testing.T) {
},
{
name: "group version target",
filter: schema.GroupVersionKind{
gvkTarget: schema.GroupVersionKind{
Group: k8sObjectGVK.Group,
Version: k8sObjectGVK.Version,
},
Expand All @@ -118,7 +118,7 @@ func TestGetManagedResources(t *testing.T) {
},
{
name: "version target",
filter: schema.GroupVersionKind{
gvkTarget: schema.GroupVersionKind{
Version: k8sObjectGVK.Version,
},
clusterCRDs: []string{
Expand All @@ -140,8 +140,8 @@ func TestGetManagedResources(t *testing.T) {
),
},
{
name: "unqualified target",
filter: schema.GroupVersionKind{},
name: "unqualified target",
gvkTarget: schema.GroupVersionKind{},
clusterCRDs: []string{
managedAndServedCRD(k8sObjectGVK),
managedAndServedCRD(k8sObjectCollectionGVK),
Expand All @@ -162,8 +162,8 @@ func TestGetManagedResources(t *testing.T) {
),
},
{
name: "unmanaged custom resources get filtered out",
filter: schema.GroupVersionKind{},
name: "unmanaged custom resources get filtered out",
gvkTarget: schema.GroupVersionKind{},
clusterCRDs: []string{
unmanagedCRD(k8sObjectGVK),
managedAndServedCRD(k8sObjectCollectionGVK),
Expand All @@ -182,8 +182,8 @@ func TestGetManagedResources(t *testing.T) {
),
},
{
name: "unserved custom resources are not retrievable",
filter: schema.GroupVersionKind{},
name: "unserved custom resources are not retrievable",
gvkTarget: schema.GroupVersionKind{},
clusterCRDs: []string{
unservedCRD(k8sObjectGVK),
managedAndServedCRD(k8sObjectCollectionGVK),
Expand Down Expand Up @@ -211,9 +211,11 @@ func TestGetManagedResources(t *testing.T) {
dCli: setupFakeDynamicClient(t, tt.clusterResources),
metric: v1alpha1.ManagedMetric{
Spec: v1alpha1.ManagedMetricSpec{
Kind: tt.filter.Kind,
Group: tt.filter.Group,
Version: tt.filter.Version,
Target: &v1alpha1.GroupVersionKind{
Group: tt.gvkTarget.Group,
Version: tt.gvkTarget.Version,
Kind: tt.gvkTarget.Kind,
},
},
},
}
Expand Down