-
Notifications
You must be signed in to change notification settings - Fork 28
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
Conversation
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>
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) | ||
} |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
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.