Skip to content

Commit

Permalink
fix: fetching container env
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Jan 8, 2024
1 parent b325964 commit 1150fcd
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions scrapers/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,30 +118,12 @@ func (kubernetes KubernetesScraper) Scrape(ctx api.ScrapeContext) v1.ScrapeResul
for _, ownerRef := range obj.GetOwnerReferences() {
if ownerRef.Kind == "DaemonSet" && ownerRef.Name == "aws-node" {
var (
awsRoleARN string
vpcID string
clusterName string
awsRegion string
awsRoleARN = getContainerEnv(spec, "AWS_ROLE_ARN")
vpcID = getContainerEnv(spec, "VPC_ID")
clusterName = getContainerEnv(spec, "CLUSTER_NAME")
awsRegion = getContainerEnv(spec, "AWS_REGION")
)

for _, env := range spec["env"].([]any) {
if arn, ok := env.(map[string]any)["AWS_ROLE_ARN"]; ok {
awsRoleARN = arn.(string)
}

if vpc, ok := env.(map[string]any)["VPC_ID"]; ok {
vpcID = vpc.(string)
}

if cluster, ok := env.(map[string]any)["CLUSTER_NAME"]; ok {
clusterName = cluster.(string)
}

if region, ok := env.(map[string]any)["AWS_REGION"]; ok {
awsRegion = region.(string)
}
}

if awsRoleARN != "" {
relationships = append(relationships, v1.RelationshipResult{
ConfigExternalID: v1.ExternalID{ExternalID: []string{string(obj.GetUID())}, ConfigType: ConfigTypePrefix + "Pod"},
Expand All @@ -159,12 +141,6 @@ func (kubernetes KubernetesScraper) Scrape(ctx api.ScrapeContext) v1.ScrapeResul
clusterScrapeResult["vpc-id"] = vpcID
}

relationships = append(relationships, v1.RelationshipResult{
ConfigExternalID: v1.ExternalID{ExternalID: []string{string(obj.GetUID())}, ConfigType: ConfigTypePrefix + "Pod"},
RelatedExternalID: v1.ExternalID{ExternalID: []string{clusterName}, ConfigType: ConfigTypePrefix + "Cluster"},
Relationship: "ClusterPod",
})

relationships = append(relationships, v1.RelationshipResult{
ConfigExternalID: v1.ExternalID{ExternalID: []string{string(obj.GetUID())}, ConfigType: ConfigTypePrefix + "Pod"},
RelatedExternalID: v1.ExternalID{ExternalID: []string{fmt.Sprintf("Kubernetes/Node//%s", nodeName)}, ConfigType: ConfigTypePrefix + "Node"},
Expand Down Expand Up @@ -514,3 +490,25 @@ func extractAccountIDFromARN(input string) string {

return ""
}

// getContainerEnv returns the value of the given environment var
// on the first component it finds.
func getContainerEnv(podSpec map[string]any, envName string) string {
if containers, ok := podSpec["containers"].([]any); ok {
for _, container := range containers {
if envsRaw, ok := container.(map[string]any)["env"]; ok {
if envs, ok := envsRaw.([]any); ok {
for _, envRaw := range envs {
if env, ok := envRaw.(map[string]any); ok {
if env["name"] == envName {
return env["value"].(string)
}
}
}
}
}
}
}

return ""
}

0 comments on commit 1150fcd

Please sign in to comment.