-
Notifications
You must be signed in to change notification settings - Fork 1.4k
CONTINT-5286 - add new action in remote config k8s actions #49331
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
Open
lavigne958
wants to merge
2
commits into
main
Choose a base branch
from
crayon/CONTINT-5286_kube_action_get_resource
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // 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 kubeapiserver | ||
|
|
||
| package executors | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "k8s.io/client-go/kubernetes" | ||
|
|
||
| "sigs.k8s.io/yaml" | ||
|
|
||
| "github.com/DataDog/datadog-agent/pkg/util/log" | ||
|
|
||
| kubeactions "github.com/DataDog/agent-payload/v5/kubeactions" | ||
| ) | ||
|
|
||
| type GetResourceExecutor struct { | ||
| clientset kubernetes.Interface | ||
| } | ||
|
|
||
| // NewGetResourceExecutor creates a new GetResourceExecutor | ||
| func NewGetResourceExecutor(clientset kubernetes.Interface) *GetResourceExecutor { | ||
| return &GetResourceExecutor{ | ||
| clientset: clientset, | ||
| } | ||
| } | ||
|
|
||
| // Execute retrieves the specified Kubernetes resource and returns it as JSON string in the message field of ExecutionResult | ||
| func (e *GetResourceExecutor) Execute(ctx context.Context, action *kubeactions.KubeAction) ExecutionResult { | ||
| resource := action.Resource | ||
| namespace := strings.ToLower(resource.GetNamespace()) | ||
| name := strings.ToLower(resource.GetName()) | ||
| apiVersion := strings.ToLower(resource.GetApiVersion()) | ||
| kind := strings.ToLower(resource.GetKind()) | ||
|
|
||
| if apiVersion == "" { | ||
| return ExecutionResult{ | ||
| Status: StatusFailed, | ||
| Message: "apiVersion is required to get resource", | ||
| } | ||
| } | ||
|
|
||
| log.Infof("Getting resource %s/%s of type %s", namespace, name, resource.Kind) | ||
|
|
||
| // build the raw REST request to get the resource as unstructured JSON | ||
| // resource.GetApiVersion() returns group/version, it will automagically handle adding the group prefix if needed | ||
| // or not adding it for core resources | ||
| var path string | ||
| if namespace == "" { | ||
| path = fmt.Sprintf("/apis/%s/%s/%s", apiVersion, kind, name) | ||
| } else { | ||
| path = fmt.Sprintf("/apis/%s/namespaces/%s/%s/%s", apiVersion, namespace, kind, name) | ||
| } | ||
|
|
||
| ctx, cancel := context.WithTimeout(ctx, defaultExecutorTimeout) | ||
| defer cancel() | ||
|
|
||
| log.Debugf("get_resource using path '%s'", path) | ||
| data, err := e.clientset.CoreV1().RESTClient().Get().AbsPath(path).Do(ctx).Raw() | ||
| if err != nil { | ||
| return ExecutionResult{ | ||
| Status: StatusFailed, | ||
| Message: fmt.Sprintf("failed to get resource: %v -- raw response body: %s", err, string(data)), | ||
| } | ||
| } | ||
|
|
||
| outputFormat := "json" | ||
| // if output := action.GetGetResource_().GetOutputFormat(); output != "" { | ||
| // outputFormat = strings.ToLower(output) | ||
| // } | ||
|
|
||
| output, err := formatOutput(data, outputFormat) | ||
| if err != nil { | ||
| return ExecutionResult{ | ||
| Status: StatusFailed, | ||
| Message: fmt.Sprintf("failed to format output to %s: %v", outputFormat, err), | ||
| } | ||
| } | ||
|
|
||
| return ExecutionResult{ | ||
| Status: StatusSuccess, | ||
| Message: output, | ||
| } | ||
| } | ||
|
|
||
| func formatOutput(data []byte, format string) (string, error) { | ||
| switch format { | ||
| case "json": | ||
| return string(data), nil | ||
| case "yaml": | ||
| jsonData := data | ||
| yamlData, err := yaml.JSONToYAML(jsonData) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to convert resource JSON to YAML: %v", err) | ||
| } | ||
| return string(yamlData), nil | ||
| default: | ||
| return "", fmt.Errorf("unsupported output format: %s", format) | ||
| } | ||
| } | ||
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
12 changes: 12 additions & 0 deletions
12
releasenotes/notes/add-new-remote-config-action-k8s-get-resource-b6cbed5ae1f48410.yaml
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,12 @@ | ||
| # Each section from every release note are combined when the | ||
| # CHANGELOG.rst is rendered. So the text needs to be worded so that | ||
| # it does not depend on any information only available in another | ||
| # section. This may mean repeating some details, but each section | ||
| # must be readable independently of the other. | ||
| # | ||
| # Each section note must be formatted as reStructuredText. | ||
| --- | ||
| features: | ||
| - | | ||
| Add a new action get-resource in kubeactions | ||
|
|
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.