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

Use recommended context.Context passing #112

Merged
merged 1 commit into from
May 25, 2023

Conversation

BlaineEXE
Copy link
Member

Documentation in context.Context states never to embed a context.Context in a struct. It should always be passed to functions and methods, ideally as the first parameter. Kubernetes' Go API follows this syntax, and since it is much easier to fix the issue in kubectl-rook-ceph compared to in rook/rook, we should do so.

The old k8sutil.Context has been renamed to k8sutil.Clientsets to reflect its new purpose.

Documentation in context.Context states never to embed a context.Context
in a struct. It should always be passed to functions and methods,
ideally as the first parameter. Kubernetes' Go API follows this syntax,
and since it is much easier to fix the issue in kubectl-rook-ceph
compared to in rook/rook, we should do so.

The old k8sutil.Context has been renamed to k8sutil.Clientsets to
reflect its new purpose.

Signed-off-by: Blaine Gardner <blaine.gardner@redhat.com>
@BlaineEXE BlaineEXE requested review from travisn and subhamkrai May 24, 2023 17:43
Comment on lines -72 to -85
context.RookClientset, err = rookclient.NewForConfig(context.KubeConfig)
clientsets.Rook, err = rookclient.NewForConfig(clientsets.KubeConfig)
if err != nil {
logging.Fatal(err)
}

context.RookClientset, err = rookclient.NewForConfig(context.KubeConfig)
clientsets.Kube, err = k8s.NewForConfig(clientsets.KubeConfig)
if err != nil {
logging.Fatal(err)
}

context.Clientset, err = k8s.NewForConfig(context.KubeConfig)
if err != nil {
logging.Fatal(err)
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This also fixes a duplicate context.RookClientset, err = ...

context := GetContext()
fmt.Println(exec.RunCommandInOperatorPod(context, cmd.Use, args, OperatorNamespace, CephClusterNamespace, true))
clientsets := GetClientsets()
fmt.Println(exec.RunCommandInOperatorPod(cmd.Context(), clientsets, cmd.Use, args, OperatorNamespace, CephClusterNamespace, true))
Copy link
Member Author

Choose a reason for hiding this comment

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

One of the core reasons to make this change, versus letting it stand, is to make sure all commands explicitly use cmd.Context() for their context.

Copy link
Member

Choose a reason for hiding this comment

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

Documentation in context.Context states never to embed a context.Context in a struct. It should always be passed to functions and methods, ideally as the first parameter.

We don't need to pass the context as a pointer? Seems like it would be similar behavior to pass it by value as to pass it around with a struct, but I didn't go dig into the whole issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

context.Context is an interface, and I think it fails to compile if a * is in front of the param.

Copy link
Collaborator

@subhamkrai subhamkrai left a comment

Choose a reason for hiding this comment

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

Overall looks good to me.

@BlaineEXE BlaineEXE merged commit 21a07b9 into rook:master May 25, 2023
@BlaineEXE BlaineEXE deleted the use-passed-context branch May 25, 2023 15:36
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.

3 participants