Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[receiver/k8scluster] Use newer v2 HorizontalPodAutoscaler for Kubern… #20483

Merged
merged 3 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
receiver/k8scluster] Use newer v2 HorizontalPodAutoscaler for Kuberne…
…tes 1.26 (#20483)
  • Loading branch information
jvoravong committed Mar 29, 2023
commit 715d6beba9a8d6ddf998ed0ba297259cbea76727
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/k8scluste
jvoravong marked this conversation as resolved.
Show resolved Hide resolved

# 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
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
60 changes: 57 additions & 3 deletions receiver/k8sclusterreceiver/internal/hpa/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata"
Expand Down Expand Up @@ -55,7 +56,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 @@ -91,7 +92,54 @@ func GetMetrics(hpa *autoscalingv2beta2.HorizontalPodAutoscaler) []*agentmetrics
}
}

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: getResourceForHPABeta(hpa),
Metrics: metrics,
},
}
}

func getResourceForHPA(hpa *autoscalingv2.HorizontalPodAutoscaler) *resourcepb.Resource {
jvoravong marked this conversation as resolved.
Show resolved Hide resolved
return &resourcepb.Resource{
Type: constants.K8sType,
Labels: map[string]string{
constants.K8sKeyHPAUID: string(hpa.UID),
constants.K8sKeyHPAName: hpa.Name,
conventions.AttributeK8SNamespaceName: hpa.Namespace,
},
}
}

func getResourceForHPABeta(hpa *autoscalingv2beta2.HorizontalPodAutoscaler) *resourcepb.Resource {
return &resourcepb.Resource{
Type: constants.K8sType,
Labels: map[string]string{
Expand All @@ -102,7 +150,13 @@ func getResourceForHPA(hpa *autoscalingv2beta2.HorizontalPodAutoscaler) *resourc
}
}

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