Skip to content
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

recipe: exec hook implementation #1932

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

asn1809
Copy link
Member

@asn1809 asn1809 commented Mar 17, 2025

For hook type "exec", commands can be provided which needs to be executed on the Pods as part of recipe workflow. Based on the either labelSelector or nameSelector, pods can be selected. If both are provided, OR logic will apply. Kubernetes core client is used in order to execute the command on the pods. The required permissions were added as part of #1946

Copy link
Member

@raghavendra-talur raghavendra-talur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor changes requested, rest of the change looks good.

Move it out of draft once the changes are done.

Container string
}

// Parameters that are of interest are hook -- name, namespace,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to document the function, then start the comment with the function name.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Stderr: errBuf,
})
if err != nil {
return fmt.Errorf("error executing command: %s", errBuf.String())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ensure command is also printed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return fmt.Errorf("error executing command: %s", errBuf.String())
}

log.Info("executed exec command successfully", "pod", execPod.PodName, "namespace", execPod.Namespace, "command", execPod.Command,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might have to move to debug log but can be done in the next PR

*/
if e.Hook.LabelSelector != nil {
eps, err := e.getExecPodsUsingLabelSelector(client, log)
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible that the user provides both labelSelector and NameSelector but only one of them match with the pods. We should ignore errors in the collection stage and execute on whatever was found.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


cmd, err := covertCommandToStringArray(e.Hook.Op.Command)
if err != nil {
log.Error(err, "error occurred during exec hook execution")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Print the command and also a return is missing

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


cmd, err := covertCommandToStringArray(e.Hook.Op.Command)
if err != nil {
log.Error(err, "error occurred during exec hook execution")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above here. And maybe we should factor out the common parts of these two functions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/remotecommand"
"k8s.io/kubectl/pkg/scheme"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if this is the right scheme pkg to use. I was thinking it would be under controller-runtime

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, please check if the approach is fine.

@raghavendra-talur
Copy link
Member

Also, rename the file json_util_test.go to check_hook_test.go

@asn1809 asn1809 force-pushed the asn-exec-hooks branch 6 times, most recently from e832659 to 9b7729d Compare March 19, 2025 21:23
@asn1809 asn1809 marked this pull request as ready for review March 19, 2025 21:34
asn1809 added 2 commits March 24, 2025 14:01
Incorporating review comments
Fixing capture issue

Signed-off-by: Annaraya Narasagond <[email protected]>
Adding unit test for hooks_factory.
Adding unit test for exec hooks using fake client.

Signed-off-by: Annaraya Narasagond <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants