Repository navigation
[CASCL-1397] orchestrator: surface dd-cluster-info ConfigMap
#51107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d16c60b
[CASCL-1397] orchestrator: surface dd-cluster-info ConfigMap
L3n41c baf8d64
[CASCL-1397] go.mod: bump agent-payload replace to latest branch SHA
L3n41c 94e46fd
[CASCL-1397] orchestrator: trust the apiserver FieldSelector
L3n41c e85b9b3
[CASCL-1397] orchestrator: drop sort on the multi-ConfigMap branch
L3n41c ab2abdd
[CASCL-1397] orchestrator: use lo.Map for the namespaces slice
L3n41c d080e8b
[CASCL-1397] orchestrator: space FetchClusterInfo into readable blocks
L3n41c 22643ba
[CASCL-1397] orchestrator: drop overlap handling in buildNodeManagerI…
L3n41c File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 211 additions & 0 deletions
211
pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterinfo.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed | ||
| // under the Apache License Version 2.0. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| // Copyright 2016-present Datadog, Inc. | ||
|
|
||
| //go:build orchestrator | ||
|
|
||
| package k8s | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/samber/lo" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| corev1Client "k8s.io/client-go/kubernetes/typed/core/v1" | ||
| "sigs.k8s.io/yaml" | ||
|
|
||
| model "github.com/DataDog/agent-payload/v5/process" | ||
| "github.com/DataDog/datadog-agent/pkg/orchestrator" | ||
| "github.com/DataDog/datadog-agent/pkg/util/log" | ||
| ) | ||
|
|
||
| const ( | ||
| clusterInfoConfigMapName = "dd-cluster-info" | ||
| clusterInfoConfigMapDataKey = "cluster-info" | ||
| clusterInfoManagedByLabel = "app.kubernetes.io/managed-by" | ||
| clusterInfoManagedByValue = "kubectl-datadog" | ||
| clusterInfoAPIVersion = "v1" | ||
| ) | ||
|
|
||
| // ClusterInfo mirrors the YAML payload of the dd-cluster-info ConfigMap | ||
| // produced by `kubectl datadog autoscaling cluster install`. The Go schema | ||
| // lives in datadog-operator; we replicate the YAML tags here so the agent | ||
| // does not depend on that repository. | ||
| type ClusterInfo struct { | ||
| APIVersion string `json:"apiVersion"` | ||
| ClusterName string `json:"clusterName"` | ||
| ClusterARN string `json:"clusterArn,omitempty"` | ||
| Region string `json:"region,omitempty"` | ||
| GeneratedAt time.Time `json:"generatedAt"` | ||
| NodeManagement map[string]map[string]nodeManagerEntry `json:"nodeManagement"` | ||
| Autoscaling autoscalingYAML `json:"autoscaling"` | ||
| } | ||
|
|
||
| type nodeManagerEntry struct { | ||
| Nodes []string `json:"nodes"` | ||
| ManagedByDatadog bool `json:"managedByDatadog,omitempty"` | ||
| } | ||
|
|
||
| type autoscalingYAML struct { | ||
| ClusterAutoscaler clusterAutoscalerYAML `json:"clusterAutoscaler"` | ||
| Karpenter karpenterYAML `json:"karpenter"` | ||
| EKSAutoMode eksAutoModeYAML `json:"eksAutoMode"` | ||
| } | ||
|
|
||
| type clusterAutoscalerYAML struct { | ||
| Present bool `json:"present"` | ||
| Namespace string `json:"namespace,omitempty"` | ||
| Name string `json:"name,omitempty"` | ||
| Version string `json:"version,omitempty"` | ||
| } | ||
|
|
||
| type karpenterYAML struct { | ||
| Present bool `json:"present"` | ||
| Namespace string `json:"namespace,omitempty"` | ||
| Name string `json:"name,omitempty"` | ||
| Version string `json:"version,omitempty"` | ||
| ManagedByDatadog bool `json:"managedByDatadog,omitempty"` | ||
| InstallerVersion string `json:"installerVersion,omitempty"` | ||
| } | ||
|
|
||
| type eksAutoModeYAML struct { | ||
| Enabled bool `json:"enabled"` | ||
| } | ||
|
|
||
| // FetchClusterInfo locates the dd-cluster-info ConfigMap anywhere in the | ||
| // cluster and returns its parsed payload. The ConfigMap is identified by | ||
| // its fixed name and the kubectl-datadog managed-by label. Returns | ||
| // (nil, nil) when no matching ConfigMap exists, when its schema version | ||
| // is not recognised, or when the payload key is missing — the absence of | ||
| // cluster-info is not an error, the snapshot is informational. | ||
| func FetchClusterInfo(ctx context.Context, coreClient corev1Client.CoreV1Interface) (*ClusterInfo, error) { | ||
| cms, err := coreClient.ConfigMaps(metav1.NamespaceAll).List(ctx, metav1.ListOptions{ | ||
| LabelSelector: clusterInfoManagedByLabel + "=" + clusterInfoManagedByValue, | ||
| FieldSelector: "metadata.name=" + clusterInfoConfigMapName, | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("listing %s ConfigMaps: %w", clusterInfoConfigMapName, err) | ||
| } | ||
| if len(cms.Items) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| if len(cms.Items) > 1 { | ||
| namespaces := lo.Map(cms.Items, func(cm corev1.ConfigMap, _ int) string { return cm.Namespace }) | ||
| log.Warnc(fmt.Sprintf("found multiple %s ConfigMaps in namespaces %v, using %q", | ||
| clusterInfoConfigMapName, namespaces, cms.Items[0].Namespace), orchestrator.ExtraLogContext...) | ||
| } | ||
| chosen := &cms.Items[0] | ||
|
L3n41c marked this conversation as resolved.
|
||
|
|
||
| payload, ok := chosen.Data[clusterInfoConfigMapDataKey] | ||
|
L3n41c marked this conversation as resolved.
L3n41c marked this conversation as resolved.
|
||
| if !ok { | ||
| log.Warnc(fmt.Sprintf("%s ConfigMap in namespace %q is missing the %q data key", | ||
| clusterInfoConfigMapName, chosen.Namespace, clusterInfoConfigMapDataKey), | ||
| orchestrator.ExtraLogContext...) | ||
| return nil, nil | ||
| } | ||
|
|
||
| var info ClusterInfo | ||
| if err := yaml.Unmarshal([]byte(payload), &info); err != nil { | ||
| return nil, fmt.Errorf("parsing %s ConfigMap payload: %w", clusterInfoConfigMapName, err) | ||
| } | ||
| if info.APIVersion != clusterInfoAPIVersion { | ||
| log.Warnc(fmt.Sprintf("%s ConfigMap apiVersion %q is not supported (expected %q), ignoring", | ||
| clusterInfoConfigMapName, info.APIVersion, clusterInfoAPIVersion), | ||
| orchestrator.ExtraLogContext...) | ||
| return nil, nil | ||
| } | ||
|
|
||
| return &info, nil | ||
| } | ||
|
|
||
| // ApplyClusterInfo enriches a cluster model and its node summaries with | ||
| // data sourced from the dd-cluster-info ConfigMap. | ||
| func ApplyClusterInfo(cluster *model.Cluster, nodesInfo []*model.ClusterNodeInfo, info *ClusterInfo, agentClusterName string) { | ||
| if info == nil { | ||
| return | ||
| } | ||
|
|
||
| if info.ClusterName != "" && agentClusterName != "" && info.ClusterName != agentClusterName { | ||
| log.Warnc(fmt.Sprintf("dd-cluster-info clusterName %q does not match the agent cluster name %q", | ||
| info.ClusterName, agentClusterName), orchestrator.ExtraLogContext...) | ||
| } | ||
| if info.Region != "" { | ||
| for _, n := range nodesInfo { | ||
| if n.Region != "" && n.Region != info.Region { | ||
| log.Warnc(fmt.Sprintf("dd-cluster-info region %q does not match node %q region %q", | ||
| info.Region, n.Name, n.Region), orchestrator.ExtraLogContext...) | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| cluster.Autoscaling = toAutoscalingInfo(info.Autoscaling) | ||
| if !info.GeneratedAt.IsZero() { | ||
| cluster.ClusterInfoGeneratedAtUnixNano = info.GeneratedAt.UnixNano() | ||
| } | ||
| if info.ClusterARN != "" { | ||
| cluster.CloudResourceId = &model.Cluster_Arn{Arn: info.ClusterARN} | ||
| } | ||
|
|
||
| nodeIndex := buildNodeManagerIndex(info.NodeManagement) | ||
| for _, n := range nodesInfo { | ||
| if entry, ok := nodeIndex[n.Name]; ok { | ||
| n.NodeManager = entry.manager | ||
| n.NodeManagerName = entry.name | ||
| n.NodeManagerManagedByDatadog = entry.managedByDatadog | ||
| } | ||
| } | ||
| } | ||
|
|
||
| type nodeManagerAssignment struct { | ||
| manager string | ||
| name string | ||
| managedByDatadog bool | ||
| } | ||
|
|
||
| // buildNodeManagerIndex inverts the dd-cluster-info nodeManagement | ||
| // structure (manager → entity name → entry) into a lookup by node name. | ||
| // A given node belongs to exactly one management entity, so the inner | ||
| // writes never collide. | ||
| func buildNodeManagerIndex(nodeManagement map[string]map[string]nodeManagerEntry) map[string]nodeManagerAssignment { | ||
| out := make(map[string]nodeManagerAssignment) | ||
| for manager, entries := range nodeManagement { | ||
| for name, entry := range entries { | ||
| for _, node := range entry.Nodes { | ||
| out[node] = nodeManagerAssignment{ | ||
| manager: manager, | ||
| name: name, | ||
| managedByDatadog: entry.ManagedByDatadog, | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| func toAutoscalingInfo(a autoscalingYAML) *model.AutoscalingInfo { | ||
| return &model.AutoscalingInfo{ | ||
| ClusterAutoscaler: &model.ClusterAutoscalerInfo{ | ||
| Present: a.ClusterAutoscaler.Present, | ||
| Namespace: a.ClusterAutoscaler.Namespace, | ||
| Name: a.ClusterAutoscaler.Name, | ||
| Version: a.ClusterAutoscaler.Version, | ||
| }, | ||
| Karpenter: &model.KarpenterInfo{ | ||
| Present: a.Karpenter.Present, | ||
| Namespace: a.Karpenter.Namespace, | ||
| Name: a.Karpenter.Name, | ||
| Version: a.Karpenter.Version, | ||
| ManagedByDatadog: a.Karpenter.ManagedByDatadog, | ||
| InstallerVersion: a.Karpenter.InstallerVersion, | ||
| }, | ||
| EksAutoMode: &model.EKSAutoModeInfo{ | ||
| Enabled: a.EKSAutoMode.Enabled, | ||
| }, | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.