Skip to content

Commit

Permalink
[processor/resourcedetectionprocessor] adapt openshift resource detec…
Browse files Browse the repository at this point in the history
…tion

Signed-off-by: Benedikt Bongartz <[email protected]>
  • Loading branch information
frzifus committed Jan 12, 2023
1 parent 94c8eec commit 2cbd70b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package openshift // import "github.com/open-telemetry/opentelemetry-collector-c

import (
"context"
"strings"

"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/processor"
Expand Down Expand Up @@ -61,17 +62,44 @@ func (d *detector) Detect(ctx context.Context) (resource pcommon.Resource, schem
return res, "", nil
}

if infra.Provider != "" {
attrs.PutStr(conventions.AttributeCloudProvider, infra.Provider)
var (
region string
platform string
provider string
)

switch strings.ToLower(infra.Status.PlatformStatus.Type) {
case "aws":
provider = conventions.AttributeCloudProviderAWS
platform = conventions.AttributeCloudPlatformAWSOpenshift
region = strings.ToLower(infra.Status.PlatformStatus.Aws.Region)
case "azure":
provider = conventions.AttributeCloudProviderAzure
platform = conventions.AttributeCloudPlatformAzureOpenshift
region = strings.ToLower(infra.Status.PlatformStatus.Azure.CloudName)
case "gcp":
provider = conventions.AttributeCloudProviderGCP
platform = conventions.AttributeCloudPlatformGoogleCloudOpenshift
region = strings.ToLower(infra.Status.PlatformStatus.GCP.Region)
case "ibmcloud":
provider = conventions.AttributeCloudProviderIbmCloud
platform = conventions.AttributeCloudPlatformIbmCloudOpenshift
region = strings.ToLower(infra.Status.PlatformStatus.IBMCloud.Location)
case "openstack":
region = strings.ToLower(infra.Status.PlatformStatus.OpenStack.CloudName)
}

if infra.Status.InfrastructureName != "" {
attrs.PutStr(conventions.AttributeK8SClusterName, infra.Status.InfrastructureName)
}
if infra.Platform != "" {
attrs.PutStr(conventions.AttributeCloudPlatform, infra.Platform)
if provider != "" {
attrs.PutStr(conventions.AttributeCloudProvider, provider)
}
if infra.Name != "" {
attrs.PutStr(conventions.AttributeK8SClusterName, infra.Name)
if platform != "" {
attrs.PutStr(conventions.AttributeCloudPlatform, platform)
}
if infra.Region != "" {
attrs.PutStr(conventions.AttributeCloudRegion, infra.Region)
if region != "" {
attrs.PutStr(conventions.AttributeCloudRegion, region)
}

// TODO(frzifus): support conventions openshift and kubernetes cluster version.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

type providerResponse struct {
ocp.InfrastructureMetadata
ocp.InfrastructureAPIResponse

OpenShiftClusterVersion string
K8SClusterVersion string
Expand All @@ -56,11 +56,11 @@ func (m *mockProvider) K8SClusterVersion(context.Context) (string, error) {
return m.res.K8SClusterVersion, nil
}

func (m *mockProvider) Infrastructure(context.Context) (*ocp.InfrastructureMetadata, error) {
func (m *mockProvider) Infrastructure(context.Context) (*ocp.InfrastructureAPIResponse, error) {
if m.infraErr != nil {
return nil, m.infraErr
}
return &m.res.InfrastructureMetadata, nil
return &m.res.InfrastructureAPIResponse, nil
}

func newTestDetector(t *testing.T, res *providerResponse, ocpCVErr, k8sCVErr, infraErr error) internal.Detector {
Expand Down Expand Up @@ -107,11 +107,18 @@ func TestDetect(t *testing.T) {
{
name: "detect all details",
detector: newTestDetector(t, &providerResponse{
InfrastructureMetadata: ocp.InfrastructureMetadata{
Name: "name",
Region: "region",
Platform: "aws_openshift",
Provider: "aws",
InfrastructureAPIResponse: ocp.InfrastructureAPIResponse{
Status: ocp.InfrastructureStatus{
InfrastructureName: "test-d-bm4rt",
ControlPlaneTopology: "HighlyAvailable",
InfrastructureTopology: "HighlyAvailable",
PlatformStatus: ocp.InfrastructurePlatformStatus{
Type: "AWS",
Aws: ocp.InfrastructureStatusAWS{
Region: "us-east-1",
},
},
},
},
OpenShiftClusterVersion: "4.1.2",
K8SClusterVersion: "1.23.4",
Expand All @@ -120,10 +127,10 @@ func TestDetect(t *testing.T) {
expectedResource: func() pcommon.Resource {
res := pcommon.NewResource()
attrs := res.Attributes()
attrs.PutStr(conventions.AttributeK8SClusterName, "test-d-bm4rt")
attrs.PutStr(conventions.AttributeCloudProvider, "aws")
attrs.PutStr(conventions.AttributeCloudPlatform, "aws_openshift")
attrs.PutStr(conventions.AttributeK8SClusterName, "name")
attrs.PutStr(conventions.AttributeCloudRegion, "region")
attrs.PutStr(conventions.AttributeCloudRegion, "us-east-1")
return res
}(),
expectedSchemaURL: conventions.SchemaURL,
Expand Down

0 comments on commit 2cbd70b

Please sign in to comment.