From 02b94d0a918dc4b2710f6c63d15ebd71b7850ca0 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Wed, 16 Jul 2025 18:10:04 +0200 Subject: [PATCH 1/4] Move the kubectl-mongodb plugin to cmd dir from public/tools/multicluster This commit moves the source code of mongodb kubectl plugin from `public/tools/multicluster` to `cmd` directory. This is to align with how go code that ships a CLI is structured. The command/subcommand building logic is moved to the `cmd` directory and and rest of the logic The command/subcommand building logic is moved to the `cmd` directory and and rest of the logic is moved to the new package `pkg/kubectl-mongodb`. This commit also moves the `.goreleaser.yaml` to the root of the project because kubectl plugin is now at the root of the project. --- .gitignore | 3 ++ .../.goreleaser.yaml => .goreleaser.yaml | 10 +++-- .../kubectl-mongodb/debug}/debug.go | 26 ++++++------ cmd/kubectl-mongodb/main.go | 12 ++++++ .../multicluster/multicluster.go | 21 ++++++++++ .../multicluster/recover}/recover.go | 34 ++++++++------- .../multicluster/setup}/setup.go | 41 +++++++++---------- .../cmd => cmd/kubectl-mongodb/root}/root.go | 30 +++++--------- cmd/kubectl-mongodb/utils/utils.go | 22 ++++++++++ go.mod | 2 + go.sum | 6 +++ .../tools}/install_istio_separate_network.sh | 0 .../tools}/kubectl_mac_notarize.sh | 0 .../tools}/setup_tls.sh | 0 .../tools}/sign.sh | 0 .../tools}/verify.sh | 0 .../kubectl-mongodb}/common/common.go | 0 .../kubectl-mongodb}/common/common_test.go | 0 .../common/kubeclientcontainer.go | 0 .../kubectl-mongodb}/common/kubeconfig.go | 0 .../kubectl-mongodb}/debug/anonymize.go | 0 .../kubectl-mongodb}/debug/anonymize_test.go | 0 .../kubectl-mongodb}/debug/collectors.go | 9 ++-- .../kubectl-mongodb}/debug/collectors_test.go | 3 +- .../kubectl-mongodb}/debug/writer.go | 0 .../kubectl-mongodb}/debug/writer_test.go | 0 public/tools/multicluster/cmd/multicluster.go | 17 -------- public/tools/multicluster/main.go | 12 ------ 28 files changed, 137 insertions(+), 111 deletions(-) rename public/tools/multicluster/.goreleaser.yaml => .goreleaser.yaml (83%) rename {public/tools/multicluster/cmd => cmd/kubectl-mongodb/debug}/debug.go (88%) create mode 100644 cmd/kubectl-mongodb/main.go create mode 100644 cmd/kubectl-mongodb/multicluster/multicluster.go rename {public/tools/multicluster/cmd => cmd/kubectl-mongodb/multicluster/recover}/recover.go (82%) rename {public/tools/multicluster/cmd => cmd/kubectl-mongodb/multicluster/setup}/setup.go (79%) rename {public/tools/multicluster/cmd => cmd/kubectl-mongodb/root}/root.go (67%) create mode 100644 cmd/kubectl-mongodb/utils/utils.go rename {public/tools/multicluster => multi_cluster/tools}/install_istio_separate_network.sh (100%) rename {public/tools/multicluster => multi_cluster/tools}/kubectl_mac_notarize.sh (100%) rename {public/tools/multicluster => multi_cluster/tools}/setup_tls.sh (100%) rename {public/tools/multicluster => multi_cluster/tools}/sign.sh (100%) rename {public/tools/multicluster => multi_cluster/tools}/verify.sh (100%) rename {public/tools/multicluster/pkg => pkg/kubectl-mongodb}/common/common.go (100%) rename {public/tools/multicluster/pkg => pkg/kubectl-mongodb}/common/common_test.go (100%) rename {public/tools/multicluster/pkg => pkg/kubectl-mongodb}/common/kubeclientcontainer.go (100%) rename {public/tools/multicluster/pkg => pkg/kubectl-mongodb}/common/kubeconfig.go (100%) rename {public/tools/multicluster/pkg => pkg/kubectl-mongodb}/debug/anonymize.go (100%) rename {public/tools/multicluster/pkg => pkg/kubectl-mongodb}/debug/anonymize_test.go (100%) rename {public/tools/multicluster/pkg => pkg/kubectl-mongodb}/debug/collectors.go (99%) rename {public/tools/multicluster/pkg => pkg/kubectl-mongodb}/debug/collectors_test.go (98%) rename {public/tools/multicluster/pkg => pkg/kubectl-mongodb}/debug/writer.go (100%) rename {public/tools/multicluster/pkg => pkg/kubectl-mongodb}/debug/writer_test.go (100%) delete mode 100644 public/tools/multicluster/cmd/multicluster.go delete mode 100644 public/tools/multicluster/main.go diff --git a/.gitignore b/.gitignore index c5ca572c5..6bb21f27a 100644 --- a/.gitignore +++ b/.gitignore @@ -94,3 +94,6 @@ logs-debug/ docs/**/log/* docs/**/test.sh.run.log + +# goreleaser generated files +dist diff --git a/public/tools/multicluster/.goreleaser.yaml b/.goreleaser.yaml similarity index 83% rename from public/tools/multicluster/.goreleaser.yaml rename to .goreleaser.yaml index 49d4e32b0..138c09609 100644 --- a/public/tools/multicluster/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -1,4 +1,5 @@ project_name: kubectl-mongodb +version: 2 before: hooks: @@ -7,6 +8,7 @@ before: builds: - env: - CGO_ENABLED=0 + main: ./cmd/kubectl-mongodb goos: - linux - darwin @@ -16,9 +18,9 @@ builds: hooks: # This will notarize Apple binaries and replace goreleaser bins with the notarized ones post: - - cmd: ./kubectl_mac_notarize.sh + - cmd: ./multi_cluster/tools/kubectl_mac_notarize.sh output: true - - cmd: ./sign.sh {{ .Path }} + - cmd: ./multi_cluster/tools/sign.sh {{ .Path }} env: - GRS_USERNAME={{ .Env.GRS_USERNAME }} - GRS_PASSWORD={{ .Env.GRS_PASSWORD }} @@ -27,7 +29,7 @@ builds: - SIGNING_IMAGE_URI={{ .Env.SIGNING_IMAGE_URI }} - ARTIFACTORY_USERNAME=mongodb-enterprise-kubernetes-operator - ARTIFACTORY_PASSWORD={{ .Env.ARTIFACTORY_PASSWORD }} - - cmd: ./verify.sh {{ .Path }} && echo "VERIFIED OK" + - cmd: ./multi_cluster/tools/verify.sh {{ .Path }} && echo "VERIFIED OK" archives: - format: tar.gz @@ -41,7 +43,7 @@ checksum: snapshot: name_template: "{{ incpatch .Version }}-next" changelog: - skip: true + disable: true release: prerelease: auto diff --git a/public/tools/multicluster/cmd/debug.go b/cmd/kubectl-mongodb/debug/debug.go similarity index 88% rename from public/tools/multicluster/cmd/debug.go rename to cmd/kubectl-mongodb/debug/debug.go index 1d49b7079..d5c0f4828 100644 --- a/public/tools/multicluster/cmd/debug.go +++ b/cmd/kubectl-mongodb/debug/debug.go @@ -1,15 +1,15 @@ -package cmd +package debug import ( "fmt" "os" "strings" - "k8s.io/client-go/tools/clientcmd" + "github.com/mongodb/mongodb-kubernetes/pkg/kubectl-mongodb/common" + "github.com/mongodb/mongodb-kubernetes/pkg/kubectl-mongodb/debug" - "github.com/mongodb/mongodb-kubernetes/multi/pkg/common" - "github.com/mongodb/mongodb-kubernetes/multi/pkg/debug" "github.com/spf13/cobra" + "k8s.io/client-go/tools/clientcmd" ) type Flags struct { @@ -39,18 +39,16 @@ func (f *Flags) ParseDebugFlags() error { var debugFlags = &Flags{} func init() { - rootCmd.AddCommand(debugCmd) - - debugCmd.Flags().StringVar(&common.MemberClusters, "member-clusters", "", "Comma separated list of member clusters. [optional]") - debugCmd.Flags().StringVar(&debugFlags.CentralCluster, "central-cluster", "", "The central cluster the operator will be deployed in. [optional]") - debugCmd.Flags().StringVar(&debugFlags.MemberClusterNamespace, "member-cluster-namespace", "", "The namespace the member cluster resources will be deployed to. [optional]") - debugCmd.Flags().StringVar(&debugFlags.CentralClusterNamespace, "central-cluster-namespace", "", "The namespace the Operator will be deployed to. [optional]") - debugCmd.Flags().StringVar(&common.MemberClustersApiServers, "member-clusters-api-servers", "", "Comma separated list of api servers addresses. [optional, default will take addresses from KUBECONFIG env var]") - debugCmd.Flags().BoolVar(&debugFlags.Anonymize, "anonymize", true, "True if anonymization should be turned on") - debugCmd.Flags().BoolVar(&debugFlags.UseOwnerRef, "ownerRef", false, "True if the collection should be made with owner references (consider turning it on after CLOUDP-176772 is fixed)") + DebugCmd.Flags().StringVar(&common.MemberClusters, "member-clusters", "", "Comma separated list of member clusters. [optional]") + DebugCmd.Flags().StringVar(&debugFlags.CentralCluster, "central-cluster", "", "The central cluster the operator will be deployed in. [optional]") + DebugCmd.Flags().StringVar(&debugFlags.MemberClusterNamespace, "member-cluster-namespace", "", "The namespace the member cluster resources will be deployed to. [optional]") + DebugCmd.Flags().StringVar(&debugFlags.CentralClusterNamespace, "central-cluster-namespace", "", "The namespace the Operator will be deployed to. [optional]") + DebugCmd.Flags().StringVar(&common.MemberClustersApiServers, "member-clusters-api-servers", "", "Comma separated list of api servers addresses. [optional, default will take addresses from KUBECONFIG env var]") + DebugCmd.Flags().BoolVar(&debugFlags.Anonymize, "anonymize", true, "True if anonymization should be turned on") + DebugCmd.Flags().BoolVar(&debugFlags.UseOwnerRef, "ownerRef", false, "True if the collection should be made with owner references (consider turning it on after CLOUDP-176772 is fixed)") } -var debugCmd = &cobra.Command{ +var DebugCmd = &cobra.Command{ Use: "debug", Short: "Downloads all resources required for debugging and stores them into the disk", Long: `'debug' downloads all resources required for debugging and stores them into the disk. diff --git a/cmd/kubectl-mongodb/main.go b/cmd/kubectl-mongodb/main.go new file mode 100644 index 000000000..19fc4525f --- /dev/null +++ b/cmd/kubectl-mongodb/main.go @@ -0,0 +1,12 @@ +package main + +import ( + "context" + + "github.com/mongodb/mongodb-kubernetes/cmd/kubectl-mongodb/root" +) + +func main() { + ctx := context.Background() + root.Execute(ctx) +} diff --git a/cmd/kubectl-mongodb/multicluster/multicluster.go b/cmd/kubectl-mongodb/multicluster/multicluster.go new file mode 100644 index 000000000..d9051126e --- /dev/null +++ b/cmd/kubectl-mongodb/multicluster/multicluster.go @@ -0,0 +1,21 @@ +package multicluster + +import ( + "github.com/mongodb/mongodb-kubernetes/cmd/kubectl-mongodb/multicluster/recover" + "github.com/mongodb/mongodb-kubernetes/cmd/kubectl-mongodb/multicluster/setup" + + "github.com/spf13/cobra" +) + +// MulticlusterCmd represents the multicluster command +var MulticlusterCmd = &cobra.Command{ + Use: "multicluster", + Short: "Manage MongoDB multicluster environments on k8s", + Long: `'multicluster' is the toplevel command for managing +multicluster environments that hold MongoDB resources.`, +} + +func init() { + MulticlusterCmd.AddCommand(setup.SetupCmd) + MulticlusterCmd.AddCommand(recover.RecoverCmd) +} diff --git a/public/tools/multicluster/cmd/recover.go b/cmd/kubectl-mongodb/multicluster/recover/recover.go similarity index 82% rename from public/tools/multicluster/cmd/recover.go rename to cmd/kubectl-mongodb/multicluster/recover/recover.go index 322aa413b..9d85afa52 100644 --- a/public/tools/multicluster/cmd/recover.go +++ b/cmd/kubectl-mongodb/multicluster/recover/recover.go @@ -1,4 +1,4 @@ -package cmd +package recover import ( "fmt" @@ -6,7 +6,7 @@ import ( "slices" "strings" - "github.com/mongodb/mongodb-kubernetes/multi/pkg/common" + "github.com/mongodb/mongodb-kubernetes/pkg/kubectl-mongodb/common" "github.com/spf13/cobra" "golang.org/x/xerrors" @@ -14,24 +14,22 @@ import ( ) func init() { - multiclusterCmd.AddCommand(recoverCmd) - - recoverCmd.Flags().StringVar(&common.MemberClusters, "member-clusters", "", "Comma separated list of member clusters. [required]") - recoverCmd.Flags().StringVar(&RecoverFlags.ServiceAccount, "service-account", "mongodb-kubernetes-operator-multi-cluster", "Name of the service account which should be used for the Operator to communicate with the member clusters. [optional, default: mongodb-kubernetes-operator-multi-cluster]") - recoverCmd.Flags().StringVar(&RecoverFlags.CentralCluster, "central-cluster", "", "The central cluster the operator will be deployed in. [required]") - recoverCmd.Flags().StringVar(&RecoverFlags.MemberClusterNamespace, "member-cluster-namespace", "", "The namespace the member cluster resources will be deployed to. [required]") - recoverCmd.Flags().StringVar(&RecoverFlags.CentralClusterNamespace, "central-cluster-namespace", "", "The namespace the Operator will be deployed to. [required]") - recoverCmd.Flags().BoolVar(&RecoverFlags.Cleanup, "cleanup", false, "Delete all previously created resources except for namespaces. [optional default: false]") - recoverCmd.Flags().BoolVar(&RecoverFlags.ClusterScoped, "cluster-scoped", false, "Create ClusterRole and ClusterRoleBindings for member clusters. [optional default: false]") - recoverCmd.Flags().StringVar(&RecoverFlags.OperatorName, "operator-name", common.DefaultOperatorName, "Name used to identify the deployment of the operator. [optional, default: mongodb-kubernetes-operator]") - recoverCmd.Flags().BoolVar(&RecoverFlags.InstallDatabaseRoles, "install-database-roles", false, "Install the ServiceAccounts and Roles required for running database workloads in the member clusters. [optional default: false]") - recoverCmd.Flags().StringVar(&RecoverFlags.SourceCluster, "source-cluster", "", "The source cluster for recovery. This has to be one of the healthy member cluster that is the source of truth for new cluster configuration. [required]") - recoverCmd.Flags().BoolVar(&RecoverFlags.CreateServiceAccountSecrets, "create-service-account-secrets", true, "Create service account token secrets. [optional default: true]") - recoverCmd.Flags().StringVar(&common.MemberClustersApiServers, "member-clusters-api-servers", "", "Comma separated list of api servers addresses. [optional, default will take addresses from KUBECONFIG env var]") + RecoverCmd.Flags().StringVar(&common.MemberClusters, "member-clusters", "", "Comma separated list of member clusters. [required]") + RecoverCmd.Flags().StringVar(&RecoverFlags.ServiceAccount, "service-account", "mongodb-kubernetes-operator-multi-cluster", "Name of the service account which should be used for the Operator to communicate with the member clusters. [optional, default: mongodb-kubernetes-operator-multi-cluster]") + RecoverCmd.Flags().StringVar(&RecoverFlags.CentralCluster, "central-cluster", "", "The central cluster the operator will be deployed in. [required]") + RecoverCmd.Flags().StringVar(&RecoverFlags.MemberClusterNamespace, "member-cluster-namespace", "", "The namespace the member cluster resources will be deployed to. [required]") + RecoverCmd.Flags().StringVar(&RecoverFlags.CentralClusterNamespace, "central-cluster-namespace", "", "The namespace the Operator will be deployed to. [required]") + RecoverCmd.Flags().BoolVar(&RecoverFlags.Cleanup, "cleanup", false, "Delete all previously created resources except for namespaces. [optional default: false]") + RecoverCmd.Flags().BoolVar(&RecoverFlags.ClusterScoped, "cluster-scoped", false, "Create ClusterRole and ClusterRoleBindings for member clusters. [optional default: false]") + RecoverCmd.Flags().StringVar(&RecoverFlags.OperatorName, "operator-name", common.DefaultOperatorName, "Name used to identify the deployment of the operator. [optional, default: mongodb-kubernetes-operator]") + RecoverCmd.Flags().BoolVar(&RecoverFlags.InstallDatabaseRoles, "install-database-roles", false, "Install the ServiceAccounts and Roles required for running database workloads in the member clusters. [optional default: false]") + RecoverCmd.Flags().StringVar(&RecoverFlags.SourceCluster, "source-cluster", "", "The source cluster for recovery. This has to be one of the healthy member cluster that is the source of truth for new cluster configuration. [required]") + RecoverCmd.Flags().BoolVar(&RecoverFlags.CreateServiceAccountSecrets, "create-service-account-secrets", true, "Create service account token secrets. [optional default: true]") + RecoverCmd.Flags().StringVar(&common.MemberClustersApiServers, "member-clusters-api-servers", "", "Comma separated list of api servers addresses. [optional, default will take addresses from KUBECONFIG env var]") } -// recoverCmd represents the recover command -var recoverCmd = &cobra.Command{ +// RecoverCmd represents the recover command +var RecoverCmd = &cobra.Command{ Use: "recover", Short: "Recover the multicluster environment for MongoDB resources after a dataplane failure", Long: `'recover' re-configures a failed multicluster environment to a enable the shuffling of dataplane diff --git a/public/tools/multicluster/cmd/setup.go b/cmd/kubectl-mongodb/multicluster/setup/setup.go similarity index 79% rename from public/tools/multicluster/cmd/setup.go rename to cmd/kubectl-mongodb/multicluster/setup/setup.go index 89a24197f..f177f27a3 100644 --- a/public/tools/multicluster/cmd/setup.go +++ b/cmd/kubectl-mongodb/multicluster/setup/setup.go @@ -1,4 +1,4 @@ -package cmd +package setup import ( "fmt" @@ -7,7 +7,8 @@ import ( "slices" "strings" - "github.com/mongodb/mongodb-kubernetes/multi/pkg/common" + "github.com/mongodb/mongodb-kubernetes/cmd/kubectl-mongodb/utils" + "github.com/mongodb/mongodb-kubernetes/pkg/kubectl-mongodb/common" "github.com/spf13/cobra" "golang.org/x/xerrors" @@ -15,26 +16,24 @@ import ( ) func init() { - multiclusterCmd.AddCommand(setupCmd) - - setupCmd.Flags().StringVar(&common.MemberClusters, "member-clusters", "", "Comma separated list of member clusters. [required]") - setupCmd.Flags().StringVar(&setupFlags.ServiceAccount, "service-account", "mongodb-kubernetes-operator-multi-cluster", "Name of the service account which should be used for the Operator to communicate with the member clusters. [optional, default: mongodb-kubernetes-operator-multi-cluster]") - setupCmd.Flags().StringVar(&setupFlags.CentralCluster, "central-cluster", "", "The central cluster the operator will be deployed in. [required]") - setupCmd.Flags().StringVar(&setupFlags.MemberClusterNamespace, "member-cluster-namespace", "", "The namespace the member cluster resources will be deployed to. [required]") - setupCmd.Flags().StringVar(&setupFlags.CentralClusterNamespace, "central-cluster-namespace", "", "The namespace the Operator will be deployed to. [required]") - setupCmd.Flags().StringVar(&setupFlags.OperatorName, "operator-name", common.DefaultOperatorName, "Name used to identify the deployment of the operator. [optional, default: mongodb-kubernetes-operator]") - setupCmd.Flags().BoolVar(&setupFlags.Cleanup, "cleanup", false, "Delete all previously created resources except for namespaces. [optional default: false]") - setupCmd.Flags().BoolVar(&setupFlags.ClusterScoped, "cluster-scoped", false, "Create ClusterRole and ClusterRoleBindings for member clusters. [optional default: false]") - setupCmd.Flags().BoolVar(&setupFlags.CreateTelemetryClusterRoles, "create-telemetry-roles", true, "Create ClusterRole and ClusterRoleBindings for member clusters for telemetry. [optional default: true]") - setupCmd.Flags().BoolVar(&setupFlags.CreateMongoDBRolesClusterRole, "create-mongodb-roles-cluster-role", true, "Create ClusterRole and ClusterRoleBinding for central cluster for ClusterMongoDBRole resources. [optional default: true]") - setupCmd.Flags().BoolVar(&setupFlags.InstallDatabaseRoles, "install-database-roles", false, "Install the ServiceAccounts and Roles required for running database workloads in the member clusters. [optional default: false]") - setupCmd.Flags().BoolVar(&setupFlags.CreateServiceAccountSecrets, "create-service-account-secrets", true, "Create service account token secrets. [optional default: true]") - setupCmd.Flags().StringVar(&setupFlags.ImagePullSecrets, "image-pull-secrets", "", "Name of the secret for imagePullSecrets to set in created service accounts") - setupCmd.Flags().StringVar(&common.MemberClustersApiServers, "member-clusters-api-servers", "", "Comma separated list of api servers addresses. [optional, default will take addresses from KUBECONFIG env var]") + SetupCmd.Flags().StringVar(&common.MemberClusters, "member-clusters", "", "Comma separated list of member clusters. [required]") + SetupCmd.Flags().StringVar(&setupFlags.ServiceAccount, "service-account", "mongodb-kubernetes-operator-multi-cluster", "Name of the service account which should be used for the Operator to communicate with the member clusters. [optional, default: mongodb-kubernetes-operator-multi-cluster]") + SetupCmd.Flags().StringVar(&setupFlags.CentralCluster, "central-cluster", "", "The central cluster the operator will be deployed in. [required]") + SetupCmd.Flags().StringVar(&setupFlags.MemberClusterNamespace, "member-cluster-namespace", "", "The namespace the member cluster resources will be deployed to. [required]") + SetupCmd.Flags().StringVar(&setupFlags.CentralClusterNamespace, "central-cluster-namespace", "", "The namespace the Operator will be deployed to. [required]") + SetupCmd.Flags().StringVar(&setupFlags.OperatorName, "operator-name", common.DefaultOperatorName, "Name used to identify the deployment of the operator. [optional, default: mongodb-kubernetes-operator]") + SetupCmd.Flags().BoolVar(&setupFlags.Cleanup, "cleanup", false, "Delete all previously created resources except for namespaces. [optional default: false]") + SetupCmd.Flags().BoolVar(&setupFlags.ClusterScoped, "cluster-scoped", false, "Create ClusterRole and ClusterRoleBindings for member clusters. [optional default: false]") + SetupCmd.Flags().BoolVar(&setupFlags.CreateTelemetryClusterRoles, "create-telemetry-roles", true, "Create ClusterRole and ClusterRoleBindings for member clusters for telemetry. [optional default: true]") + SetupCmd.Flags().BoolVar(&setupFlags.CreateMongoDBRolesClusterRole, "create-mongodb-roles-cluster-role", true, "Create ClusterRole and ClusterRoleBinding for central cluster for ClusterMongoDBRole resources. [optional default: true]") + SetupCmd.Flags().BoolVar(&setupFlags.InstallDatabaseRoles, "install-database-roles", false, "Install the ServiceAccounts and Roles required for running database workloads in the member clusters. [optional default: false]") + SetupCmd.Flags().BoolVar(&setupFlags.CreateServiceAccountSecrets, "create-service-account-secrets", true, "Create service account token secrets. [optional default: true]") + SetupCmd.Flags().StringVar(&setupFlags.ImagePullSecrets, "image-pull-secrets", "", "Name of the secret for imagePullSecrets to set in created service accounts") + SetupCmd.Flags().StringVar(&common.MemberClustersApiServers, "member-clusters-api-servers", "", "Comma separated list of api servers addresses. [optional, default will take addresses from KUBECONFIG env var]") } -// setupCmd represents the setup command -var setupCmd = &cobra.Command{ +// SetupCmd represents the setup command +var SetupCmd = &cobra.Command{ Use: "setup", Short: "Setup the multicluster environment for MongoDB resources", Long: `'setup' configures the central and member clusters in preparation for a MongoDBMultiCluster deployment. @@ -52,7 +51,7 @@ kubectl-mongodb multicluster setup --central-cluster="operator-cluster" --member buildInfo, ok := debug.ReadBuildInfo() if ok { - fmt.Println(getBuildInfoString(buildInfo)) + fmt.Println(utils.GetBuildInfoString(buildInfo)) } clientMap, err := common.CreateClientMap(setupFlags.MemberClusters, setupFlags.CentralCluster, common.LoadKubeConfigFilePath(), common.GetKubernetesClient) diff --git a/public/tools/multicluster/cmd/root.go b/cmd/kubectl-mongodb/root/root.go similarity index 67% rename from public/tools/multicluster/cmd/root.go rename to cmd/kubectl-mongodb/root/root.go index 17c99aac9..7c7aec1e4 100644 --- a/public/tools/multicluster/cmd/root.go +++ b/cmd/kubectl-mongodb/root/root.go @@ -1,13 +1,16 @@ -package cmd +package root import ( "context" - "fmt" "os" "os/signal" "runtime/debug" "syscall" + cmddebug "github.com/mongodb/mongodb-kubernetes/cmd/kubectl-mongodb/debug" + "github.com/mongodb/mongodb-kubernetes/cmd/kubectl-mongodb/multicluster" + "github.com/mongodb/mongodb-kubernetes/cmd/kubectl-mongodb/utils" + "github.com/spf13/cobra" ) @@ -20,6 +23,11 @@ of MongoDB resources in your kubernetes cluster. `, } +func init() { + rootCmd.AddCommand(multicluster.MulticlusterCmd) + rootCmd.AddCommand(cmddebug.DebugCmd) +} + // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute(ctx context.Context) { @@ -34,26 +42,10 @@ func Execute(ctx context.Context) { }() buildInfo, ok := debug.ReadBuildInfo() if ok { - rootCmd.Long += getBuildInfoString(buildInfo) + rootCmd.Long += utils.GetBuildInfoString(buildInfo) } err := rootCmd.ExecuteContext(ctx) if err != nil { os.Exit(1) } } - -func getBuildInfoString(buildInfo *debug.BuildInfo) string { - var vcsHash string - var vcsTime string - for _, setting := range buildInfo.Settings { - if setting.Key == "vcs.revision" { - vcsHash = setting.Value - } - if setting.Key == "vcs.time" { - vcsTime = setting.Value - } - } - - buildInfoStr := fmt.Sprintf("\nBuild: %s, %s", vcsHash, vcsTime) - return buildInfoStr -} diff --git a/cmd/kubectl-mongodb/utils/utils.go b/cmd/kubectl-mongodb/utils/utils.go new file mode 100644 index 000000000..d02490fc9 --- /dev/null +++ b/cmd/kubectl-mongodb/utils/utils.go @@ -0,0 +1,22 @@ +package utils + +import ( + "fmt" + "runtime/debug" +) + +func GetBuildInfoString(buildInfo *debug.BuildInfo) string { + var vcsHash string + var vcsTime string + for _, setting := range buildInfo.Settings { + if setting.Key == "vcs.revision" { + vcsHash = setting.Value + } + if setting.Key == "vcs.time" { + vcsTime = setting.Value + } + } + + buildInfoStr := fmt.Sprintf("\nBuild: %s, %s", vcsHash, vcsTime) + return buildInfoStr +} diff --git a/go.mod b/go.mod index f2246cc30..faa3fdca5 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/prometheus/client_golang v1.22.0 github.com/r3labs/diff/v3 v3.0.1 github.com/spf13/cast v1.8.0 + github.com/spf13/cobra v1.7.0 github.com/stretchr/objx v0.5.2 github.com/stretchr/testify v1.10.0 github.com/xdg/stringprep v1.0.3 @@ -69,6 +70,7 @@ require ( github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect github.com/hashicorp/go-sockaddr v1.0.2 // indirect github.com/hashicorp/hcl v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.18.0 // indirect diff --git a/go.sum b/go.sum index 48aa3fef3..69adbd33f 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,7 @@ github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK3 github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -112,6 +113,8 @@ github.com/hashicorp/vault/api v1.16.0/go.mod h1:KhuUhzOD8lDSk29AtzNjgAu2kxRA9jL github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM= github.com/imdario/mergo v0.3.15/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= @@ -188,6 +191,7 @@ github.com/r3labs/diff/v3 v3.0.1 h1:CBKqf3XmNRHXKmdU7mZP1w7TV0pDyVCis1AUHtA4Xtg= github.com/r3labs/diff/v3 v3.0.1/go.mod h1:f1S9bourRbiM66NskseyUdo0fTmEE0qKrikYJX63dgo= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk= github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc= @@ -195,6 +199,8 @@ github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I= github.com/spf13/cast v1.8.0 h1:gEN9K4b8Xws4EX0+a0reLmhq8moKn7ntRlQYgjPeCDk= github.com/spf13/cast v1.8.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= +github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/public/tools/multicluster/install_istio_separate_network.sh b/multi_cluster/tools/install_istio_separate_network.sh similarity index 100% rename from public/tools/multicluster/install_istio_separate_network.sh rename to multi_cluster/tools/install_istio_separate_network.sh diff --git a/public/tools/multicluster/kubectl_mac_notarize.sh b/multi_cluster/tools/kubectl_mac_notarize.sh similarity index 100% rename from public/tools/multicluster/kubectl_mac_notarize.sh rename to multi_cluster/tools/kubectl_mac_notarize.sh diff --git a/public/tools/multicluster/setup_tls.sh b/multi_cluster/tools/setup_tls.sh similarity index 100% rename from public/tools/multicluster/setup_tls.sh rename to multi_cluster/tools/setup_tls.sh diff --git a/public/tools/multicluster/sign.sh b/multi_cluster/tools/sign.sh similarity index 100% rename from public/tools/multicluster/sign.sh rename to multi_cluster/tools/sign.sh diff --git a/public/tools/multicluster/verify.sh b/multi_cluster/tools/verify.sh similarity index 100% rename from public/tools/multicluster/verify.sh rename to multi_cluster/tools/verify.sh diff --git a/public/tools/multicluster/pkg/common/common.go b/pkg/kubectl-mongodb/common/common.go similarity index 100% rename from public/tools/multicluster/pkg/common/common.go rename to pkg/kubectl-mongodb/common/common.go diff --git a/public/tools/multicluster/pkg/common/common_test.go b/pkg/kubectl-mongodb/common/common_test.go similarity index 100% rename from public/tools/multicluster/pkg/common/common_test.go rename to pkg/kubectl-mongodb/common/common_test.go diff --git a/public/tools/multicluster/pkg/common/kubeclientcontainer.go b/pkg/kubectl-mongodb/common/kubeclientcontainer.go similarity index 100% rename from public/tools/multicluster/pkg/common/kubeclientcontainer.go rename to pkg/kubectl-mongodb/common/kubeclientcontainer.go diff --git a/public/tools/multicluster/pkg/common/kubeconfig.go b/pkg/kubectl-mongodb/common/kubeconfig.go similarity index 100% rename from public/tools/multicluster/pkg/common/kubeconfig.go rename to pkg/kubectl-mongodb/common/kubeconfig.go diff --git a/public/tools/multicluster/pkg/debug/anonymize.go b/pkg/kubectl-mongodb/debug/anonymize.go similarity index 100% rename from public/tools/multicluster/pkg/debug/anonymize.go rename to pkg/kubectl-mongodb/debug/anonymize.go diff --git a/public/tools/multicluster/pkg/debug/anonymize_test.go b/pkg/kubectl-mongodb/debug/anonymize_test.go similarity index 100% rename from public/tools/multicluster/pkg/debug/anonymize_test.go rename to pkg/kubectl-mongodb/debug/anonymize_test.go diff --git a/public/tools/multicluster/pkg/debug/collectors.go b/pkg/kubectl-mongodb/debug/collectors.go similarity index 99% rename from public/tools/multicluster/pkg/debug/collectors.go rename to pkg/kubectl-mongodb/debug/collectors.go index beca7086f..7cbf69896 100644 --- a/public/tools/multicluster/pkg/debug/collectors.go +++ b/pkg/kubectl-mongodb/debug/collectors.go @@ -7,17 +7,16 @@ import ( "fmt" "strings" - "k8s.io/utils/ptr" - - "k8s.io/client-go/rest" - "k8s.io/client-go/tools/remotecommand" + "github.com/mongodb/mongodb-kubernetes/pkg/kubectl-mongodb/common" - "github.com/mongodb/mongodb-kubernetes/multi/pkg/common" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/meta" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/remotecommand" + "k8s.io/utils/ptr" ) var ( diff --git a/public/tools/multicluster/pkg/debug/collectors_test.go b/pkg/kubectl-mongodb/debug/collectors_test.go similarity index 98% rename from public/tools/multicluster/pkg/debug/collectors_test.go rename to pkg/kubectl-mongodb/debug/collectors_test.go index aac1c0cd6..93743ff57 100644 --- a/public/tools/multicluster/pkg/debug/collectors_test.go +++ b/pkg/kubectl-mongodb/debug/collectors_test.go @@ -4,7 +4,8 @@ import ( "context" "testing" - "github.com/mongodb/mongodb-kubernetes/multi/pkg/common" + "github.com/mongodb/mongodb-kubernetes/pkg/kubectl-mongodb/common" + "github.com/stretchr/testify/assert" v1 "k8s.io/api/apps/v1" v12 "k8s.io/api/core/v1" diff --git a/public/tools/multicluster/pkg/debug/writer.go b/pkg/kubectl-mongodb/debug/writer.go similarity index 100% rename from public/tools/multicluster/pkg/debug/writer.go rename to pkg/kubectl-mongodb/debug/writer.go diff --git a/public/tools/multicluster/pkg/debug/writer_test.go b/pkg/kubectl-mongodb/debug/writer_test.go similarity index 100% rename from public/tools/multicluster/pkg/debug/writer_test.go rename to pkg/kubectl-mongodb/debug/writer_test.go diff --git a/public/tools/multicluster/cmd/multicluster.go b/public/tools/multicluster/cmd/multicluster.go deleted file mode 100644 index c04f955af..000000000 --- a/public/tools/multicluster/cmd/multicluster.go +++ /dev/null @@ -1,17 +0,0 @@ -package cmd - -import ( - "github.com/spf13/cobra" -) - -// multiclusterCmd represents the multicluster command -var multiclusterCmd = &cobra.Command{ - Use: "multicluster", - Short: "Manage MongoDB multicluster environments on k8s", - Long: `'multicluster' is the toplevel command for managing -multicluster environments that hold MongoDB resources.`, -} - -func init() { - rootCmd.AddCommand(multiclusterCmd) -} diff --git a/public/tools/multicluster/main.go b/public/tools/multicluster/main.go deleted file mode 100644 index 5c8ef45c8..000000000 --- a/public/tools/multicluster/main.go +++ /dev/null @@ -1,12 +0,0 @@ -package main - -import ( - "context" - - "github.com/mongodb/mongodb-kubernetes/multi/cmd" -) - -func main() { - ctx := context.Background() - cmd.Execute(ctx) -} From 1884f1938525ec3deeed9e2fb6cf447000d125bf Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Thu, 17 Jul 2025 12:40:59 +0200 Subject: [PATCH 2/4] Move evergreen file for kubectl plugin to root of the project 1. Since most of our evergreen config files are in root of the repo and now that kubectl plugin is also moved to `cmd/` it makes sense to move the evergreen config file to the root of repo with correct name. 2. Change the goreleaser config file to version 1. We upgraded it temporarily to version 2 but CI complains that only version 1 is supported. --- public/.evergreen.yml => .evergreen-kubectlplugin.yml | 2 +- .goreleaser.yaml | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) rename public/.evergreen.yml => .evergreen-kubectlplugin.yml (97%) diff --git a/public/.evergreen.yml b/.evergreen-kubectlplugin.yml similarity index 97% rename from public/.evergreen.yml rename to .evergreen-kubectlplugin.yml index 43852dfe6..72129009a 100644 --- a/public/.evergreen.yml +++ b/.evergreen-kubectlplugin.yml @@ -45,7 +45,7 @@ functions: - command: shell.exec type: setup params: - working_dir: src/github.com/mongodb/mongodb-kubernetes/public/tools/multicluster + working_dir: src/github.com/mongodb/mongodb-kubernetes include_expansions_in_env: - GRS_USERNAME - GRS_PASSWORD diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 138c09609..7c7ce3778 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -1,5 +1,4 @@ project_name: kubectl-mongodb -version: 2 before: hooks: @@ -43,7 +42,7 @@ checksum: snapshot: name_template: "{{ incpatch .Version }}-next" changelog: - disable: true + skip: true release: prerelease: auto From 46fc4569a5a4e8d1edb11eb18d7fdc6779588ae9 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Thu, 17 Jul 2025 20:36:32 +0200 Subject: [PATCH 3/4] Change the way kubectl-mongodb plugin is built by evergreen function The evergreen function `build_multi_cluster_binary` is used to build the `kubectl-mongodb` plugin. And this function gets used in the main `.evergreen.yml` file to build the `kubectl-mongodb` binary. Since we moved the location of the source files for `kubectl-mongodb` plugin as part of [PR](https://github.com/mongodb/mongodb-kubernetes/pull/271), this PR makes respective changes in the evergreen function so that it can properly build the `kubectl-mongodb` plugin from the new location. --- scripts/evergreen/build_multi_cluster_kubeconfig_creator.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/evergreen/build_multi_cluster_kubeconfig_creator.sh b/scripts/evergreen/build_multi_cluster_kubeconfig_creator.sh index 33c502fd6..f8bbf1890 100755 --- a/scripts/evergreen/build_multi_cluster_kubeconfig_creator.sh +++ b/scripts/evergreen/build_multi_cluster_kubeconfig_creator.sh @@ -15,7 +15,7 @@ fi echo "Building multi cluster kube config creation tool." project_dir="$(pwd)" -pushd public/tools/multicluster +pushd cmd/kubectl-mongodb GOOS="${OS}" GOARCH="${ARCH}" CGO_ENABLED=0 go build -buildvcs=false -o "${project_dir}/docker/mongodb-kubernetes-tests/multi-cluster-kube-config-creator" main.go GOOS="linux" GOARCH="amd64" CGO_ENABLED=0 go build -buildvcs=false -o "${project_dir}/docker/mongodb-kubernetes-tests/multi-cluster-kube-config-creator_linux" main.go popd From 1ae3bc4fd89b7409996444a89dfa18a3823d1868 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Fri, 18 Jul 2025 11:17:44 +0200 Subject: [PATCH 4/4] Run `make precommit' to fix lint issues --- cmd/kubectl-mongodb/debug/debug.go | 6 +++--- .../multicluster/multicluster.go | 4 ++-- .../multicluster/recover/recover.go | 4 ++-- .../multicluster/setup/setup.go | 6 +++--- cmd/kubectl-mongodb/root/root.go | 4 ++-- pkg/kubectl-mongodb/common/common.go | 11 ++++++----- pkg/kubectl-mongodb/common/common_test.go | 18 +++++++++--------- .../common/kubeclientcontainer.go | 3 ++- pkg/kubectl-mongodb/common/kubeconfig.go | 3 ++- pkg/kubectl-mongodb/debug/anonymize_test.go | 1 + pkg/kubectl-mongodb/debug/collectors.go | 11 ++++++----- pkg/kubectl-mongodb/debug/collectors_test.go | 13 +++++++------ pkg/kubectl-mongodb/debug/writer.go | 2 +- pkg/kubectl-mongodb/debug/writer_test.go | 3 ++- 14 files changed, 48 insertions(+), 41 deletions(-) diff --git a/cmd/kubectl-mongodb/debug/debug.go b/cmd/kubectl-mongodb/debug/debug.go index d5c0f4828..d3bce21fb 100644 --- a/cmd/kubectl-mongodb/debug/debug.go +++ b/cmd/kubectl-mongodb/debug/debug.go @@ -5,11 +5,11 @@ import ( "os" "strings" - "github.com/mongodb/mongodb-kubernetes/pkg/kubectl-mongodb/common" - "github.com/mongodb/mongodb-kubernetes/pkg/kubectl-mongodb/debug" - "github.com/spf13/cobra" "k8s.io/client-go/tools/clientcmd" + + "github.com/mongodb/mongodb-kubernetes/pkg/kubectl-mongodb/common" + "github.com/mongodb/mongodb-kubernetes/pkg/kubectl-mongodb/debug" ) type Flags struct { diff --git a/cmd/kubectl-mongodb/multicluster/multicluster.go b/cmd/kubectl-mongodb/multicluster/multicluster.go index d9051126e..7b19ad443 100644 --- a/cmd/kubectl-mongodb/multicluster/multicluster.go +++ b/cmd/kubectl-mongodb/multicluster/multicluster.go @@ -1,10 +1,10 @@ package multicluster import ( + "github.com/spf13/cobra" + "github.com/mongodb/mongodb-kubernetes/cmd/kubectl-mongodb/multicluster/recover" "github.com/mongodb/mongodb-kubernetes/cmd/kubectl-mongodb/multicluster/setup" - - "github.com/spf13/cobra" ) // MulticlusterCmd represents the multicluster command diff --git a/cmd/kubectl-mongodb/multicluster/recover/recover.go b/cmd/kubectl-mongodb/multicluster/recover/recover.go index 9d85afa52..7afcfd144 100644 --- a/cmd/kubectl-mongodb/multicluster/recover/recover.go +++ b/cmd/kubectl-mongodb/multicluster/recover/recover.go @@ -6,11 +6,11 @@ import ( "slices" "strings" - "github.com/mongodb/mongodb-kubernetes/pkg/kubectl-mongodb/common" - "github.com/spf13/cobra" "golang.org/x/xerrors" "k8s.io/client-go/tools/clientcmd" + + "github.com/mongodb/mongodb-kubernetes/pkg/kubectl-mongodb/common" ) func init() { diff --git a/cmd/kubectl-mongodb/multicluster/setup/setup.go b/cmd/kubectl-mongodb/multicluster/setup/setup.go index f177f27a3..0c0a09406 100644 --- a/cmd/kubectl-mongodb/multicluster/setup/setup.go +++ b/cmd/kubectl-mongodb/multicluster/setup/setup.go @@ -7,12 +7,12 @@ import ( "slices" "strings" - "github.com/mongodb/mongodb-kubernetes/cmd/kubectl-mongodb/utils" - "github.com/mongodb/mongodb-kubernetes/pkg/kubectl-mongodb/common" - "github.com/spf13/cobra" "golang.org/x/xerrors" "k8s.io/client-go/tools/clientcmd" + + "github.com/mongodb/mongodb-kubernetes/cmd/kubectl-mongodb/utils" + "github.com/mongodb/mongodb-kubernetes/pkg/kubectl-mongodb/common" ) func init() { diff --git a/cmd/kubectl-mongodb/root/root.go b/cmd/kubectl-mongodb/root/root.go index 7c7aec1e4..429cccc0e 100644 --- a/cmd/kubectl-mongodb/root/root.go +++ b/cmd/kubectl-mongodb/root/root.go @@ -7,11 +7,11 @@ import ( "runtime/debug" "syscall" + "github.com/spf13/cobra" + cmddebug "github.com/mongodb/mongodb-kubernetes/cmd/kubectl-mongodb/debug" "github.com/mongodb/mongodb-kubernetes/cmd/kubectl-mongodb/multicluster" "github.com/mongodb/mongodb-kubernetes/cmd/kubectl-mongodb/utils" - - "github.com/spf13/cobra" ) // rootCmd represents the base command when called without any subcommands diff --git a/pkg/kubectl-mongodb/common/common.go b/pkg/kubectl-mongodb/common/common.go index bba97054a..7dd88b440 100644 --- a/pkg/kubectl-mongodb/common/common.go +++ b/pkg/kubectl-mongodb/common/common.go @@ -6,16 +6,17 @@ import ( "strings" "time" - "k8s.io/apimachinery/pkg/util/wait" - "github.com/ghodss/yaml" "golang.org/x/xerrors" - corev1 "k8s.io/api/core/v1" - rbacv1 "k8s.io/api/rbac/v1" "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/kubernetes" + _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" + + corev1 "k8s.io/api/core/v1" + rbacv1 "k8s.io/api/rbac/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) type clusterType string diff --git a/pkg/kubectl-mongodb/common/common_test.go b/pkg/kubectl-mongodb/common/common_test.go index ab45b6c47..7e797dd56 100644 --- a/pkg/kubectl-mongodb/common/common_test.go +++ b/pkg/kubectl-mongodb/common/common_test.go @@ -10,19 +10,19 @@ import ( "testing" "time" + "github.com/ghodss/yaml" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/informers" + "k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/tools/cache" + "k8s.io/client-go/tools/clientcmd" - "github.com/ghodss/yaml" - "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" - "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/client-go/kubernetes/fake" - "k8s.io/client-go/tools/clientcmd" ) const testKubeconfig = `apiVersion: v1 @@ -553,10 +553,10 @@ func TestPrintingOutRolesServiceAccountsAndRoleBindings(t *testing.T) { } func marshalToYaml[T interface{}](t *testing.T, sb *strings.Builder, comment string, apiVersion string, kind string, items []T) *strings.Builder { - sb.WriteString(fmt.Sprintf("# %s\n", comment)) + fmt.Fprintf(sb, "# %s\n", comment) for _, cr := range items { - sb.WriteString(fmt.Sprintf("apiVersion: %s\n", apiVersion)) - sb.WriteString(fmt.Sprintf("kind: %s\n", kind)) + fmt.Fprintf(sb, "apiVersion: %s\n", apiVersion) + fmt.Fprintf(sb, "kind: %s\n", kind) marshalledBytes, err := yaml.Marshal(cr) assert.NoError(t, err) sb.WriteString(string(marshalledBytes)) diff --git a/pkg/kubectl-mongodb/common/kubeclientcontainer.go b/pkg/kubectl-mongodb/common/kubeclientcontainer.go index 1c30e5aa9..8f7db1e9f 100644 --- a/pkg/kubectl-mongodb/common/kubeclientcontainer.go +++ b/pkg/kubectl-mongodb/common/kubeclientcontainer.go @@ -5,6 +5,8 @@ import ( "k8s.io/client-go/discovery" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" + admissionregistrationv1 "k8s.io/client-go/kubernetes/typed/admissionregistration/v1" admissionregistrationv1alpha1 "k8s.io/client-go/kubernetes/typed/admissionregistration/v1alpha1" admissionregistrationv1beta1 "k8s.io/client-go/kubernetes/typed/admissionregistration/v1beta1" @@ -57,7 +59,6 @@ import ( storagev1alpha1 "k8s.io/client-go/kubernetes/typed/storage/v1alpha1" storagev1beta1 "k8s.io/client-go/kubernetes/typed/storage/v1beta1" storagemigrationv1alpha1 "k8s.io/client-go/kubernetes/typed/storagemigration/v1alpha1" - "k8s.io/client-go/rest" ) // KubeClient is wrapper (decorator pattern) over the static and dynamic Kube Clients. diff --git a/pkg/kubectl-mongodb/common/kubeconfig.go b/pkg/kubectl-mongodb/common/kubeconfig.go index a353b6e0c..2b606a12b 100644 --- a/pkg/kubectl-mongodb/common/kubeconfig.go +++ b/pkg/kubectl-mongodb/common/kubeconfig.go @@ -8,8 +8,9 @@ import ( "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" - clientcmdapi "k8s.io/client-go/tools/clientcmd/api" "k8s.io/client-go/util/homedir" + + clientcmdapi "k8s.io/client-go/tools/clientcmd/api" ) const ( diff --git a/pkg/kubectl-mongodb/debug/anonymize_test.go b/pkg/kubectl-mongodb/debug/anonymize_test.go index c4b8e78ed..2746a164d 100644 --- a/pkg/kubectl-mongodb/debug/anonymize_test.go +++ b/pkg/kubectl-mongodb/debug/anonymize_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + v1 "k8s.io/api/core/v1" ) diff --git a/pkg/kubectl-mongodb/debug/collectors.go b/pkg/kubectl-mongodb/debug/collectors.go index 7cbf69896..45ac8c926 100644 --- a/pkg/kubectl-mongodb/debug/collectors.go +++ b/pkg/kubectl-mongodb/debug/collectors.go @@ -7,16 +7,17 @@ import ( "fmt" "strings" - "github.com/mongodb/mongodb-kubernetes/pkg/kubectl-mongodb/common" - - corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/meta" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/rest" "k8s.io/client-go/tools/remotecommand" "k8s.io/utils/ptr" + + corev1 "k8s.io/api/core/v1" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/mongodb/mongodb-kubernetes/pkg/kubectl-mongodb/common" ) var ( @@ -269,7 +270,7 @@ func (s *AgentHealthFileCollector) Collect(ctx context.Context, kubeClient commo found := false for _, c := range pod.Spec.Containers { for _, e := range c.Env { - if "AGENT_STATUS_FILEPATH" == e.Name { + if e.Name == "AGENT_STATUS_FILEPATH" { add.agentFileName = e.Value found = true break diff --git a/pkg/kubectl-mongodb/debug/collectors_test.go b/pkg/kubectl-mongodb/debug/collectors_test.go index 93743ff57..4cb31190b 100644 --- a/pkg/kubectl-mongodb/debug/collectors_test.go +++ b/pkg/kubectl-mongodb/debug/collectors_test.go @@ -4,18 +4,19 @@ import ( "context" "testing" - "github.com/mongodb/mongodb-kubernetes/pkg/kubectl-mongodb/common" - "github.com/stretchr/testify/assert" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/client-go/kubernetes/fake" + v1 "k8s.io/api/apps/v1" v12 "k8s.io/api/core/v1" v13 "k8s.io/api/rbac/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/runtime/schema" fake2 "k8s.io/client-go/dynamic/fake" - "k8s.io/client-go/kubernetes/fake" + + "github.com/mongodb/mongodb-kubernetes/pkg/kubectl-mongodb/common" ) func TestCollectors(t *testing.T) { diff --git a/pkg/kubectl-mongodb/debug/writer.go b/pkg/kubectl-mongodb/debug/writer.go index 7471bfcd6..bdf331483 100644 --- a/pkg/kubectl-mongodb/debug/writer.go +++ b/pkg/kubectl-mongodb/debug/writer.go @@ -129,5 +129,5 @@ func getType(obj runtime.Object) (string, error) { } func cleanContext(context string) string { - return strings.Replace(context, "/", "-", -1) + return strings.ReplaceAll(context, "/", "-") } diff --git a/pkg/kubectl-mongodb/debug/writer_test.go b/pkg/kubectl-mongodb/debug/writer_test.go index 47217c364..7b597239e 100644 --- a/pkg/kubectl-mongodb/debug/writer_test.go +++ b/pkg/kubectl-mongodb/debug/writer_test.go @@ -7,9 +7,10 @@ import ( "testing" "github.com/stretchr/testify/assert" + "k8s.io/apimachinery/pkg/runtime" + v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" ) func TestWriteToFile(t *testing.T) {