Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion public/.evergreen.yml → .evergreen-kubectlplugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,6 @@ logs-debug/

docs/**/log/*
docs/**/test.sh.run.log

# goreleaser generated files
dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ before:
builds:
- env:
- CGO_ENABLED=0
main: ./cmd/kubectl-mongodb
goos:
- linux
- darwin
Expand All @@ -16,9 +17,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 }}
Expand All @@ -27,7 +28,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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions cmd/kubectl-mongodb/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
21 changes: 21 additions & 0 deletions cmd/kubectl-mongodb/multicluster/multicluster.go
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
package cmd
package recover

import (
"fmt"
"os"
"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"
"k8s.io/client-go/tools/clientcmd"
)

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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package setup

import (
"fmt"
Expand All @@ -7,34 +7,33 @@ 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"
"k8s.io/client-go/tools/clientcmd"
)

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.
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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) {
Expand All @@ -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
}
22 changes: 22 additions & 0 deletions cmd/kubectl-mongodb/utils/utils.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading