Repository: open-telemetry/opentelemetry-go-contrib (go)

Issue:
EKS detector errors outside of EKS/K8S
### Description

I'm adding the EKS detector to my code which in production, runs in/on EKS. However when I run my code locally on my machine, the detector errors, yet when I use the EC2 detector for example, (and I'm _not_ in/on an EC2 instance), it just silently returns no error.

### Environment

- OS: Linux
- Architecture: x86
- Go Version: 1.23.4
- `eks` version: 1.32

### Steps To Reproduce

Using this code:
```golang
r, err := resource.New(ctx, resource.WithFromEnv(), resource.WithDetectors(eks.NewResourceDetector()))
```

I get error `failed to create config: unable to load in-cluster configuration, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined`.

The `Detect()` method is written to cater for not running inside K8S/EKS but the constructor calls `newK8sDetectorUtils()` which taints the detector from ever working.

### Expected behavior

I would expect the EKS detector to simply return the default `resource.Empty()`. I assume the `resource.WithDetectors()` option is intended to be used by adding as many different detectors for environments where your code _might_ run and they either return useful attributes or nothing.

Pull request:
fix: Stop EKS detector erroring outside of K8S
This is a simple fix to stop the EKS detector erroring when run outside of Kubernetes.

Fixes #7477

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 79fd2735a..d8481d6dd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -73,6 +73,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
 
 ### Fixed
 
+- Fix EKS detector erroring outside of Kubernetes. (#7483)
 - Fix data race when writing log entries with `context.Context` fields in `go.opentelemetry.io/contrib/bridges/otelzap`. (#7368)
 - Fix nil pointer dereference when `ClientTracer` did not have a span in `go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace`. (#7464)
 - Record all non-failure metrics on transport round trip errors in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`. (#7146)
diff --git a/detectors/aws/eks/detector.go b/detectors/aws/eks/detector.go
index 0cf2f64ff..f34d38d2a 100644
--- a/detectors/aws/eks/detector.go
+++ b/detectors/aws/eks/detector.go
@@ -65,6 +65,10 @@ func NewResourceDetector() resource.Detector {
 // Detect returns a Resource describing the Amazon EKS environment being run in.
 func (detector *resourceDetector) Detect(ctx context.Context) (*resource.Resource, error) {
 	if detector.err != nil {
+		if errors.Is(detector.err, rest.ErrNotInCluster) {
+			return resource.Empty(), nil
+		}
+
 		return nil, detector.err
 	}
 


Test patch:
diff --git a/detectors/aws/eks/detector_test.go b/detectors/aws/eks/detector_test.go
index 6dec9debb..f52df6931 100644
--- a/detectors/aws/eks/detector_test.go
+++ b/detectors/aws/eks/detector_test.go
@@ -10,6 +10,7 @@ import (
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/mock"
 	"github.com/stretchr/testify/require"
+	"k8s.io/client-go/rest"
 
 	"go.opentelemetry.io/otel/attribute"
 	"go.opentelemetry.io/otel/sdk/resource"
@@ -82,3 +83,13 @@ func TestNotEKS(t *testing.T) {
 	assert.Equal(t, resource.Empty(), r, "Resource object should be empty")
 	detectorUtils.AssertExpectations(t)
 }
+
+// Tests EKS resource detector not running K8S at all.
+func TestNotK8S(t *testing.T) {
+	detectorUtils := new(MockDetectorUtils)
+	detector := resourceDetector{utils: detectorUtils, err: rest.ErrNotInCluster}
+	r, err := detector.Detect(context.Background())
+	require.NoError(t, err)
+	assert.Equal(t, resource.Empty(), r, "Resource object should be empty")
+	detectorUtils.AssertExpectations(t)
+}