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

core: move project to golang #79

Merged
merged 1 commit into from
Jan 20, 2023
Merged

Conversation

subhamkrai
Copy link
Collaborator

starting migration from bash to go, starting with
basic ceph commands and getting mons endpoint

Signed-off-by: subhamkrai srai@redhat.com

Copy link
Member

@travisn travisn left a comment

Choose a reason for hiding this comment

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

A few questions to get started

pkg/mons/kubectl.go Outdated Show resolved Hide resolved
pkg/kube-client.go Outdated Show resolved Hide resolved
pkg/ceph/ceph-commands.go Outdated Show resolved Hide resolved
kubectl-rook-ceph.go Outdated Show resolved Hide resolved
@subhamkrai subhamkrai force-pushed the move-to-go branch 2 times, most recently from 90401ac to aa7a5fd Compare January 9, 2023 05:10
@subhamkrai subhamkrai marked this pull request as ready for review January 9, 2023 05:12
@subhamkrai
Copy link
Collaborator Author

subhamkrai commented Jan 9, 2023

btw this how ci is executing commands, not sure why

kubectl rook-ceph ceph status
ceph status --connect-timeout=10 --conf=/var/lib/rook/rook-ceph/rook-ceph.config

+ kubectl rook-ceph mons
a=10.99.115.54:67

@subhamkrai subhamkrai force-pushed the move-to-go branch 2 times, most recently from e5407b8 to 52e573f Compare January 9, 2023 13:33
@subhamkrai subhamkrai requested a review from travisn January 9, 2023 14:04
.github/workflows/go-test.yaml Outdated Show resolved Hide resolved
cmd/ceph/ceph.go Outdated
@@ -0,0 +1,35 @@
/*
Copy link
Member

Choose a reason for hiding this comment

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

How about if we put all the go files with the cobra definitions directly in cmd/? Seems like we don't need the subdirectories for ceph, rbd, etc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I kept main.go under cmd folder and all other commands inside commands folder otherwise everything go file will be under main package and now current structure seems okay to me

cmd/ceph/ceph.go Outdated
var CephCmd = &cobra.Command{
Use: "ceph",
Short: "call a 'ceph' CLI command with arbitrary args",
Long: `call a 'ceph' CLI command with arbitrary args`,
Copy link
Member

Choose a reason for hiding this comment

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

If we don't define Long, will it default to the same as Short? It would be nice to avoid the redundancy, or else define a different Short and Long description.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

removed the Long

cmd/mon/mons.go Outdated Show resolved Hide resolved
cmd/root/root.go Outdated Show resolved Hide resolved
pkg/cephrbd/ceph_rbd_commands.go Outdated Show resolved Hide resolved
pkg/cephrbd/ceph_rbd_commands.go Outdated Show resolved Hide resolved
pkg/cephrbd/ceph_rbd_commands.go Outdated Show resolved Hide resolved
pkg/cephrbd/ceph_rbd_commands.go Outdated Show resolved Hide resolved
pkg/mons/mon-endpoints.go Outdated Show resolved Hide resolved
@subhamkrai subhamkrai force-pushed the move-to-go branch 2 times, most recently from 6d0346c to 1da0c4e Compare January 10, 2023 15:17
cmd/commands/ceph.go Show resolved Hide resolved
cmd/commands/root.go Outdated Show resolved Hide resolved
pkg/cephrbd/ceph_rbd_commands.go Outdated Show resolved Hide resolved
pkg/kube-client.go Outdated Show resolved Hide resolved
pkg/mons/mon-endpoints.go Outdated Show resolved Hide resolved
@subhamkrai subhamkrai force-pushed the move-to-go branch 2 times, most recently from c1f4493 to aee859e Compare January 11, 2023 14:52
cmd/commands/root.go Outdated Show resolved Hide resolved

- name: build the binary
run: |
go build -o kubectl-rook-ceph cmd/main.go
Copy link
Member

Choose a reason for hiding this comment

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

Instead of running go build in this action, we need a makefile. Then developers can easily and consistently build locally during development, and the CI can also call make. But let's try to make it much simpler than the rook makefile. :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

created makefile, I think this is one of the simplest makefile you will come across 😅

Copy link
Member

Choose a reason for hiding this comment

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

Simple makefiles are the best kind!

pkg/k8sutil/ceph_rbd_commands.go Outdated Show resolved Hide resolved
pkg/k8sutil/ceph_rbd_commands.go Outdated Show resolved Hide resolved
pkg/k8sutil/ceph_rbd_commands.go Outdated Show resolved Hide resolved
pkg/mons/mon-endpoints.go Outdated Show resolved Hide resolved
Makefile Show resolved Hide resolved
cmd/commands/root.go Outdated Show resolved Hide resolved
pkg/k8sutil/exec.go Outdated Show resolved Hide resolved

const monConfigMap = "rook-ceph-mon-endpoints"

func GetMonEndpoint(clusterNamespace string) {
Copy link
Member

Choose a reason for hiding this comment

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

Let's add a unit test for this method so we can establish the unit tests from the start. You'll just need to have the kube client as a parameter to the method.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this method is not returning anything, how we should write the unit test?

Copy link
Member

Choose a reason for hiding this comment

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

As discussed, how about refactoring lines 40-44 to a separate method that can be unit tested.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added a unit-test

cmd/commands/ceph.go Outdated Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
go.mod Outdated Show resolved Hide resolved
pkg/k8sutil/exec.go Outdated Show resolved Hide resolved
pkg/k8sutil/exec.go Outdated Show resolved Hide resolved
pkg/k8sutil/exec.go Outdated Show resolved Hide resolved
@subhamkrai subhamkrai force-pushed the move-to-go branch 2 times, most recently from 4fe837b to b6207fe Compare January 16, 2023 13:54
Copy link
Member

@Madhu-1 Madhu-1 left a comment

Choose a reason for hiding this comment

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

Some nits. LGTM

cmd/commands/mons.go Show resolved Hide resolved
pkg/k8sutil/exec.go Outdated Show resolved Hide resolved
pkg/k8sutil/exec.go Outdated Show resolved Hide resolved
pkg/k8sutil/kube-client.go Outdated Show resolved Hide resolved
pkg/mons/mon-endpoints.go Outdated Show resolved Hide resolved

RootCmd.PersistentFlags().StringVar(&KubeConfig, "kubeconfig", "", "kubernetes config path")
RootCmd.PersistentFlags().StringVar(&OperatorNamespace, "opeator-namespace", "rook-ceph", "Kubernetes namespace where rook operator is running")
RootCmd.PersistentFlags().StringVar(&CephClusterNamespace, "namespace", "rook-ceph", "Kubernetes namespace where ceph cluster is created")
Copy link
Member

Choose a reason for hiding this comment

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

How the krew cmd will run can you give a doc example for ceph health,

I can't how you might be calling the status, does it like calling the k8s client and then calling ceph cmd through it?

And one more question/doubt What if we just create and call the cmds from toolbox and wrap them into a go package?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Currently, we are using operator pod for running ceph commands and toolbox and operator pod are almost same. So we prefer rook operator pod as rook/rook also uses operator pod for running ceph command internally

clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{},
)
rest, err := kubeconfig.ClientConfig()
Copy link
Member

Choose a reason for hiding this comment

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

Seems like these lines 42-49 are duplicated from what is in GetKubeClient(). Instead of duplicating, how about a different helper method instead of GetKubeClient() that gives back the rest and client? Or some other refactoring.

Copy link
Member

Choose a reason for hiding this comment

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

In the future we may also want to unit test commands where we can mock K8s and the exec commands, like we do in the Rook core. So I'd suggest that this method should take the kubeconfig as a parameter, then the core implementation takes parameters for the context that can be mocked. The kubeconfig would likely be initialized at the top layer under the cmd package.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

now, I'm returning the rest and client from GetKubeClient only.
I'm not sure about second part could you refer some example


const monConfigMap = "rook-ceph-mon-endpoints"

func GetMonEndpoint(clusterNamespace string) {
Copy link
Member

Choose a reason for hiding this comment

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

As discussed, how about refactoring lines 40-44 to a separate method that can be unit tested.

@travisn
Copy link
Member

travisn commented Jan 18, 2023

In the future we may also want to unit test commands where we can mock K8s and the exec commands, like we do in the Rook core. So I'd suggest that this method should take the kubeconfig as a parameter, then the core implementation takes parameters for the context that can be mocked. The kubeconfig would likely be initialized at the top layer under the cmd package.

now, I'm returning the rest and client from GetKubeClient only.
I'm not sure about second part could you refer some example

Rook has a method to create the context, which initializes most of the context here. By default, it will create types that will exec real commands and expect a real k8s cluster. But then the unit tests can mock the exec and k8s methods by passing a different context, for example as created here for the K8s tests, or search for MockExecutor for mocking the commands. This can all be done in a separate PR though.

.github/workflows/go-test.yaml Outdated Show resolved Hide resolved
cmd/commands/root.go Outdated Show resolved Hide resolved
pkg/k8sutil/exec.go Outdated Show resolved Hide resolved
pkg/mons/mon-endpoints_test.go Outdated Show resolved Hide resolved
pkg/mons/mon-endpoints_test.go Outdated Show resolved Hide resolved
@subhamkrai
Copy link
Collaborator Author

In the future we may also want to unit test commands where we can mock K8s and the exec commands, like we do in the Rook core. So I'd suggest that this method should take the kubeconfig as a parameter, then the core implementation takes parameters for the context that can be mocked. The kubeconfig would likely be initialized at the top layer under the cmd package.

now, I'm returning the rest and client from GetKubeClient only.
I'm not sure about second part could you refer some example

Rook has a method to create the context, which initializes most of the context here. By default, it will create types that will exec real commands and expect a real k8s cluster. But then the unit tests can mock the exec and k8s methods by passing a different context, for example as created here for the K8s tests, or search for MockExecutor for mocking the commands. This can all be done in a separate PR though.

I think I'm not very much confident about how that's works, may be discussed in call will make it clear.

Copy link
Member

@travisn travisn left a comment

Choose a reason for hiding this comment

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

Approved with one more suggestion, thanks

Makefile Outdated
build:
gofmt -w $(shell find . -type f -name '*.go')
@echo
env GOOS=$(shell go env GOOS) GOARCH=$(shell go env GOARCH) go build -o kubectl-rook-ceph cmd/main.go
Copy link
Member

Choose a reason for hiding this comment

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

After a build, the binary shows up in the local repo as a new file, and could be accidentally committed. How about if we output the binary to a bin/ subfolder, then add bin to a .gitignore?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done PTAL

starting migration from bash to go, starting with
basic ceph commands and getting mons endpoint

Signed-off-by: subhamkrai <srai@redhat.com>
@travisn
Copy link
Member

travisn commented Jan 20, 2023

LGTM!

@subhamkrai subhamkrai merged commit 1ff1c5c into rook:master Jan 20, 2023
@subhamkrai subhamkrai deleted the move-to-go branch January 20, 2023 14:54
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.

4 participants