Skip to content

Commit

Permalink
[receiver/k8scluster] Use newer batch and autoscaling APIs (#7406)
Browse files Browse the repository at this point in the history
* Otel-collector-contrib-7386 removing batch/v1beta1 and autoscaling/v2beta1 k8s api from receiver/k8scluster

* Otel-collector-contrib-7386 replacing autoscaling/v2beta1 with  autoscaling/v2beta2 k8s api from receiver/k8scluster

* Otel-collector-contrib-7386 replacing autoscaling/v2beta1 with  autoscaling/v2beta2 k8s api from receiver/k8scluster

* Otel-collector-contrib-7386 replacing autoscaling/v2beta1 with  autoscaling/v2beta2 k8s api from receiver/k8scluster

* Otel-collector-contrib-7386 overcome redundant alias for k8s.io/api/autoscaling/v2beta2

* Otel-collector-contrib-7386 Matching k8s Object for Autoscaling Api

* Otel-collector-contrib-7386 Matching k8s Object for Batch Api
  • Loading branch information
shree007 committed Feb 9, 2022
1 parent 2754c19 commit 154d221
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 29 deletions.
11 changes: 5 additions & 6 deletions receiver/k8sclusterreceiver/internal/collection/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ import (
"go.opentelemetry.io/collector/model/pdata"
"go.uber.org/zap"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/api/autoscaling/v2beta1"
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -146,9 +145,9 @@ func (dc *DataCollector) SyncMetrics(obj interface{}) {
rm = getMetricsForStatefulSet(o)
case *batchv1.Job:
rm = getMetricsForJob(o)
case *batchv1beta1.CronJob:
case *batchv1.CronJob:
rm = getMetricsForCronJob(o)
case *v2beta1.HorizontalPodAutoscaler:
case *autoscalingv2beta2.HorizontalPodAutoscaler:
rm = getMetricsForHPA(o)
case *quotav1.ClusterResourceQuota:
rm = getMetricsForClusterResourceQuota(o)
Expand Down Expand Up @@ -183,9 +182,9 @@ func (dc *DataCollector) SyncMetadata(obj interface{}) map[metadata.ResourceID]*
km = getMetadataForStatefulSet(o)
case *batchv1.Job:
km = getMetadataForJob(o)
case *batchv1beta1.CronJob:
case *batchv1.CronJob:
km = getMetadataForCronJob(o)
case *v2beta1.HorizontalPodAutoscaler:
case *autoscalingv2beta2.HorizontalPodAutoscaler:
km = getMetadataForHPA(o)
}

Expand Down
8 changes: 4 additions & 4 deletions receiver/k8sclusterreceiver/internal/collection/cronjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,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/model/semconv/v1.5.0"
batchv1beta1 "k8s.io/api/batch/v1beta1"
batchv1 "k8s.io/api/batch/v1"

metadata "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/utils"
Expand All @@ -37,7 +37,7 @@ var activeJobs = &metricspb.MetricDescriptor{
Type: metricspb.MetricDescriptor_GAUGE_INT64,
}

func getMetricsForCronJob(cj *batchv1beta1.CronJob) []*resourceMetrics {
func getMetricsForCronJob(cj *batchv1.CronJob) []*resourceMetrics {
metrics := []*metricspb.Metric{
{
MetricDescriptor: activeJobs,
Expand All @@ -55,7 +55,7 @@ func getMetricsForCronJob(cj *batchv1beta1.CronJob) []*resourceMetrics {
}
}

func getResourceForCronJob(cj *batchv1beta1.CronJob) *resourcepb.Resource {
func getResourceForCronJob(cj *batchv1.CronJob) *resourcepb.Resource {
return &resourcepb.Resource{
Type: k8sType,
Labels: map[string]string{
Expand All @@ -67,7 +67,7 @@ func getResourceForCronJob(cj *batchv1beta1.CronJob) *resourcepb.Resource {
}
}

func getMetadataForCronJob(cj *batchv1beta1.CronJob) map[metadata.ResourceID]*KubernetesMetadata {
func getMetadataForCronJob(cj *batchv1.CronJob) map[metadata.ResourceID]*KubernetesMetadata {
rm := getGenericMetadata(&cj.ObjectMeta, k8sKindCronJob)
rm.metadata[cronJobKeySchedule] = cj.Spec.Schedule
rm.metadata[cronJobKeyConcurrencyPolicy] = string(cj.Spec.ConcurrencyPolicy)
Expand Down
10 changes: 5 additions & 5 deletions receiver/k8sclusterreceiver/internal/collection/cronjobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
"github.com/stretchr/testify/require"
batchv1beta1 "k8s.io/api/batch/v1beta1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -74,8 +74,8 @@ func TestCronJobMetadata(t *testing.T) {
)
}

func newCronJob(id string) *batchv1beta1.CronJob {
return &batchv1beta1.CronJob{
func newCronJob(id string) *batchv1.CronJob {
return &batchv1.CronJob{
ObjectMeta: v1.ObjectMeta{
Name: "test-cronjob-" + id,
Namespace: "test-namespace",
Expand All @@ -86,11 +86,11 @@ func newCronJob(id string) *batchv1beta1.CronJob {
"foo1": "",
},
},
Spec: batchv1beta1.CronJobSpec{
Spec: batchv1.CronJobSpec{
Schedule: "schedule",
ConcurrencyPolicy: "concurrency_policy",
},
Status: batchv1beta1.CronJobStatus{
Status: batchv1.CronJobStatus{
Active: []corev1.ObjectReference{{}, {}},
},
}
Expand Down
8 changes: 4 additions & 4 deletions receiver/k8sclusterreceiver/internal/collection/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,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/model/semconv/v1.5.0"
"k8s.io/api/autoscaling/v2beta1"
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"

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

func getMetricsForHPA(hpa *v2beta1.HorizontalPodAutoscaler) []*resourceMetrics {
func getMetricsForHPA(hpa *autoscalingv2beta2.HorizontalPodAutoscaler) []*resourceMetrics {
metrics := []*metricspb.Metric{
{
MetricDescriptor: hpaMaxReplicasMetric,
Expand Down Expand Up @@ -88,7 +88,7 @@ func getMetricsForHPA(hpa *v2beta1.HorizontalPodAutoscaler) []*resourceMetrics {
}
}

func getResourceForHPA(hpa *v2beta1.HorizontalPodAutoscaler) *resourcepb.Resource {
func getResourceForHPA(hpa *autoscalingv2beta2.HorizontalPodAutoscaler) *resourcepb.Resource {
return &resourcepb.Resource{
Type: k8sType,
Labels: map[string]string{
Expand All @@ -100,7 +100,7 @@ func getResourceForHPA(hpa *v2beta1.HorizontalPodAutoscaler) *resourcepb.Resourc
}
}

func getMetadataForHPA(hpa *v2beta1.HorizontalPodAutoscaler) map[metadata.ResourceID]*KubernetesMetadata {
func getMetadataForHPA(hpa *autoscalingv2beta2.HorizontalPodAutoscaler) map[metadata.ResourceID]*KubernetesMetadata {
return map[metadata.ResourceID]*KubernetesMetadata{
metadata.ResourceID(hpa.UID): getGenericMetadata(&hpa.ObjectMeta, "HPA"),
}
Expand Down
10 changes: 5 additions & 5 deletions receiver/k8sclusterreceiver/internal/collection/hpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
"github.com/stretchr/testify/require"
"k8s.io/api/autoscaling/v2beta1"
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

Expand Down Expand Up @@ -57,20 +57,20 @@ func TestHPAMetrics(t *testing.T) {
metricspb.MetricDescriptor_GAUGE_INT64, 7)
}

func newHPA(id string) *v2beta1.HorizontalPodAutoscaler {
func newHPA(id string) *autoscalingv2beta2.HorizontalPodAutoscaler {
minReplicas := int32(2)
return &v2beta1.HorizontalPodAutoscaler{
return &autoscalingv2beta2.HorizontalPodAutoscaler{
ObjectMeta: v1.ObjectMeta{
Name: "test-hpa-" + id,
Namespace: "test-namespace",
UID: types.UID("test-hpa-" + id + "-uid"),
ClusterName: "test-cluster",
},
Status: v2beta1.HorizontalPodAutoscalerStatus{
Status: autoscalingv2beta2.HorizontalPodAutoscalerStatus{
CurrentReplicas: 5,
DesiredReplicas: 7,
},
Spec: v2beta1.HorizontalPodAutoscalerSpec{
Spec: autoscalingv2beta2.HorizontalPodAutoscalerSpec{
MinReplicas: &minReplicas,
MaxReplicas: 10,
},
Expand Down
9 changes: 4 additions & 5 deletions receiver/k8sclusterreceiver/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ import (
"go.uber.org/atomic"
"go.uber.org/zap"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/api/autoscaling/v2beta1"
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/informers"
Expand Down Expand Up @@ -98,9 +97,9 @@ func (rw *resourceWatcher) prepareSharedInformerFactory() {
rw.setupInformers(&appsv1.ReplicaSet{}, factory.Apps().V1().ReplicaSets().Informer())
rw.setupInformers(&appsv1.StatefulSet{}, factory.Apps().V1().StatefulSets().Informer())
rw.setupInformers(&batchv1.Job{}, factory.Batch().V1().Jobs().Informer())
rw.setupInformers(&batchv1beta1.CronJob{}, factory.Batch().V1beta1().CronJobs().Informer())
rw.setupInformers(&v2beta1.HorizontalPodAutoscaler{},
factory.Autoscaling().V2beta1().HorizontalPodAutoscalers().Informer(),
rw.setupInformers(&batchv1.CronJob{}, factory.Batch().V1().CronJobs().Informer())
rw.setupInformers(&autoscalingv2beta2.HorizontalPodAutoscaler{},
factory.Autoscaling().V2beta2().HorizontalPodAutoscalers().Informer(),
)

if rw.osQuotaClient != nil {
Expand Down

0 comments on commit 154d221

Please sign in to comment.