Skip to content

Commit

Permalink
Fix for collector in k8s environment (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
jt-dd authored Sep 16, 2024
1 parent 57a2384 commit 7b23ffd
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/config/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,39 @@ package config
import (
"context"
"fmt"
"os"

"github.com/DataDog/KubeHound/pkg/telemetry/log"
"k8s.io/client-go/tools/clientcmd"
)

const (
clusterNameEnvVarPtr = "KH_K8S_CLUSTER_NAME_ENV_PTR"
clusterNameEnvVar = "KH_K8S_CLUSTER_NAME"
)

// ClusterInfo encapsulates the target cluster information for the current run.
type ClusterInfo struct {
Name string
}

func NewClusterInfo(_ context.Context) (*ClusterInfo, error) {
// Testing if running from pod
// Using an environment variable to get the cluster name as it is not provided in the pod configuration
clusterNamePtr := os.Getenv(clusterNameEnvVarPtr)
clusterName := os.Getenv(clusterNameEnvVar)
if clusterNamePtr != "" {
clusterName = os.Getenv(clusterNamePtr)
}
if clusterName != "" {
log.I.Warnf("Using cluster name from environment variable [%s]: %s", clusterNameEnvVar, clusterName)

return &ClusterInfo{
Name: clusterName,
}, nil
}

// Testing if running from outside the cluster
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configOverrides := &clientcmd.ConfigOverrides{}
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
Expand Down

0 comments on commit 7b23ffd

Please sign in to comment.