Skip to content

Commit

Permalink
[receiver/k8scluster] Use newer v2 HorizontalPodAutoscaler for Kubern… (
Browse files Browse the repository at this point in the history
#20483)

With these changes the k8scluster receiver will be able to monitor the deprecated v2beta2 and v2 versions of the HorizontalPodAutoscaler. We need these changes for v2 HPA to properly support Kubernetes 1.26. I left in the v2beta2 support to keep support for other clusters.

---------

Co-authored-by: Ryan Fitzpatrick <[email protected]>
  • Loading branch information
jvoravong and rmfitzpatrick committed Apr 7, 2023
1 parent bbc6faf commit 315fdf3
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 10 deletions.
16 changes: 16 additions & 0 deletions .chloggen/fix-k8scluster-use-v2-horizontalpodautoscaler.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: receiver/k8scluster

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Use newer v2 HorizontalPodAutoscaler for Kubernetes 1.26

# One or more tracking issues related to the change
issues: [20480]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: v2beta2 HorizontalPodAutoscaler is no longer available starting in Kubernetes 1.26
9 changes: 7 additions & 2 deletions receiver/k8sclusterreceiver/internal/collection/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
appsv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
Expand Down Expand Up @@ -136,8 +137,10 @@ func (dc *DataCollector) SyncMetrics(obj interface{}) {
md = ocsToMetrics(cronjob.GetMetrics(o))
case *batchv1beta1.CronJob:
md = ocsToMetrics(cronjob.GetMetricsBeta(o))
case *autoscalingv2beta2.HorizontalPodAutoscaler:
case *autoscalingv2.HorizontalPodAutoscaler:
md = ocsToMetrics(hpa.GetMetrics(o))
case *autoscalingv2beta2.HorizontalPodAutoscaler:
md = ocsToMetrics(hpa.GetMetricsBeta(o))
case *quotav1.ClusterResourceQuota:
md = ocsToMetrics(clusterresourcequota.GetMetrics(o))
default:
Expand Down Expand Up @@ -175,8 +178,10 @@ func (dc *DataCollector) SyncMetadata(obj interface{}) map[experimentalmetricmet
km = cronjob.GetMetadata(o)
case *batchv1beta1.CronJob:
km = cronjob.GetMetadataBeta(o)
case *autoscalingv2beta2.HorizontalPodAutoscaler:
case *autoscalingv2.HorizontalPodAutoscaler:
km = hpa.GetMetadata(o)
case *autoscalingv2beta2.HorizontalPodAutoscaler:
km = hpa.GetMetadataBeta(o)
}

return km
Expand Down
58 changes: 51 additions & 7 deletions receiver/k8sclusterreceiver/internal/hpa/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/constants"
Expand Down Expand Up @@ -55,7 +57,7 @@ var hpaDesiredReplicasMetric = &metricspb.MetricDescriptor{
Type: metricspb.MetricDescriptor_GAUGE_INT64,
}

func GetMetrics(hpa *autoscalingv2beta2.HorizontalPodAutoscaler) []*agentmetricspb.ExportMetricsServiceRequest {
func GetMetrics(hpa *autoscalingv2.HorizontalPodAutoscaler) []*agentmetricspb.ExportMetricsServiceRequest {
metrics := []*metricspb.Metric{
{
MetricDescriptor: hpaMaxReplicasMetric,
Expand Down Expand Up @@ -85,24 +87,66 @@ func GetMetrics(hpa *autoscalingv2beta2.HorizontalPodAutoscaler) []*agentmetrics

return []*agentmetricspb.ExportMetricsServiceRequest{
{
Resource: getResourceForHPA(hpa),
Resource: getResourceForHPA(&hpa.ObjectMeta),
Metrics: metrics,
},
}
}

func getResourceForHPA(hpa *autoscalingv2beta2.HorizontalPodAutoscaler) *resourcepb.Resource {
func GetMetricsBeta(hpa *autoscalingv2beta2.HorizontalPodAutoscaler) []*agentmetricspb.ExportMetricsServiceRequest {
metrics := []*metricspb.Metric{
{
MetricDescriptor: hpaMaxReplicasMetric,
Timeseries: []*metricspb.TimeSeries{
utils.GetInt64TimeSeries(int64(hpa.Spec.MaxReplicas)),
},
},
{
MetricDescriptor: hpaMinReplicasMetric,
Timeseries: []*metricspb.TimeSeries{
utils.GetInt64TimeSeries(int64(*hpa.Spec.MinReplicas)),
},
},
{
MetricDescriptor: hpaCurrentReplicasMetric,
Timeseries: []*metricspb.TimeSeries{
utils.GetInt64TimeSeries(int64(hpa.Status.CurrentReplicas)),
},
},
{
MetricDescriptor: hpaDesiredReplicasMetric,
Timeseries: []*metricspb.TimeSeries{
utils.GetInt64TimeSeries(int64(hpa.Status.DesiredReplicas)),
},
},
}

return []*agentmetricspb.ExportMetricsServiceRequest{
{
Resource: getResourceForHPA(&hpa.ObjectMeta),
Metrics: metrics,
},
}
}

func getResourceForHPA(om *v1.ObjectMeta) *resourcepb.Resource {
return &resourcepb.Resource{
Type: constants.K8sType,
Labels: map[string]string{
constants.K8sKeyHPAUID: string(hpa.UID),
constants.K8sKeyHPAName: hpa.Name,
conventions.AttributeK8SNamespaceName: hpa.Namespace,
constants.K8sKeyHPAUID: string(om.UID),
constants.K8sKeyHPAName: om.Name,
conventions.AttributeK8SNamespaceName: om.Namespace,
},
}
}

func GetMetadata(hpa *autoscalingv2beta2.HorizontalPodAutoscaler) map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata {
func GetMetadata(hpa *autoscalingv2.HorizontalPodAutoscaler) map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata {
return map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata{
experimentalmetricmetadata.ResourceID(hpa.UID): metadata.GetGenericMetadata(&hpa.ObjectMeta, "HPA"),
}
}

func GetMetadataBeta(hpa *autoscalingv2beta2.HorizontalPodAutoscaler) map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata {
return map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata{
experimentalmetricmetadata.ResourceID(hpa.UID): metadata.GetGenericMetadata(&hpa.ObjectMeta, "HPA"),
}
Expand Down
22 changes: 21 additions & 1 deletion receiver/k8sclusterreceiver/internal/testutils/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package testutils // import "github.com/open-telemetry/opentelemetry-collector-c

import (
appsv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -24,7 +25,26 @@ import (
"k8s.io/apimachinery/pkg/types"
)

func NewHPA(id string) *autoscalingv2beta2.HorizontalPodAutoscaler {
func NewHPA(id string) *autoscalingv2.HorizontalPodAutoscaler {
minReplicas := int32(2)
return &autoscalingv2.HorizontalPodAutoscaler{
ObjectMeta: v1.ObjectMeta{
Name: "test-hpa-" + id,
Namespace: "test-namespace",
UID: types.UID("test-hpa-" + id + "-uid"),
},
Status: autoscalingv2.HorizontalPodAutoscalerStatus{
CurrentReplicas: 5,
DesiredReplicas: 7,
},
Spec: autoscalingv2.HorizontalPodAutoscalerSpec{
MinReplicas: &minReplicas,
MaxReplicas: 10,
},
}
}

func NewHPABeta(id string) *autoscalingv2beta2.HorizontalPodAutoscaler {
minReplicas := int32(2)
return &autoscalingv2beta2.HorizontalPodAutoscaler{
ObjectMeta: v1.ObjectMeta{
Expand Down
6 changes: 6 additions & 0 deletions receiver/k8sclusterreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ func newFakeClientWithAllResources() *fake.Clientset {
gvkToAPIResource(gvk.CronJob),
},
},
{
GroupVersion: "autoscaling/v2",
APIResources: []v1.APIResource{
gvkToAPIResource(gvk.HorizontalPodAutoscaler),
},
},
{
GroupVersion: "autoscaling/v2beta2",
APIResources: []v1.APIResource{
Expand Down

0 comments on commit 315fdf3

Please sign in to comment.