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

Cleanup operator plugin #376

Merged
merged 2 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 10 additions & 10 deletions kubectl-minio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ Options:

#### MinIO Tenant Creation

Command: `kubectl minio tenant create --name TENANT_NAME --servers SERVERS --volumes TOTAL_VOLUMES --capacity TOTAL_RAW_CAPACITY [options]`
Command: `kubectl minio tenant create TENANT_NAME --servers SERVERS --volumes TOTAL_VOLUMES --capacity TOTAL_RAW_CAPACITY [options]`

Creates a MinIO Tenant based on the passed values.

example: `kubectl minio tenant create --name tenant1 --servers 4 --volumes 16 --capacity 16Ti`
example: `kubectl minio tenant create tenant1 --servers 4 --volumes 16 --capacity 16Ti`

Options:

Expand All @@ -55,11 +55,11 @@ Options:

#### Add Tenant pools

Command: `kubectl minio tenant expand --name TENANT_NAME --servers SERVERS --volumes TOTAL_VOLUMES --capacity TOTAL_RAW_CAPACITY [options]`
Command: `kubectl minio tenant expand TENANT_NAME --servers SERVERS --volumes TOTAL_VOLUMES --capacity TOTAL_RAW_CAPACITY [options]`

Add new volumes (and nodes) to existing MinIO Tenant.

example: `kubectl minio tenant expand --name tenant1 --servers 4 --volumes 16 --capacity 16Ti`
example: `kubectl minio tenant expand tenant1 --servers 4 --volumes 16 --capacity 16Ti`

Options:

Expand All @@ -68,23 +68,23 @@ Options:

#### List Tenant pools

Command: `kubectl minio tenant info --name TENANT_NAME [options]`
Command: `kubectl minio tenant info TENANT_NAME [options]`

List all existing MinIO pools in the given MinIO Tenant.

example: `kubectl minio tenant info --name tenant1`
example: `kubectl minio tenant info tenant1`

Options:

- `--namespace=minio`

#### Upgrade Images

Command: `kubectl minio tenant upgrade --name TENANT_NAME --image IMAGE_TAG [options]`
Command: `kubectl minio tenant upgrade TENANT_NAME --image IMAGE_TAG [options]`

Upgrade MinIO Docker image for the given MinIO Tenant.

example: `kubectl minio tenant upgrade --name tenant1 --image minio/minio:RELEASE.2020-11-19T23-48-16Z`
example: `kubectl minio tenant upgrade tenant1 --image minio/minio:RELEASE.2020-11-19T23-48-16Z`

Options:

Expand All @@ -93,11 +93,11 @@ Options:

#### Remove Tenant

Command: `kubectl minio tenant delete --name TENANT_NAME [options]`
Command: `kubectl minio tenant delete TENANT_NAME [options]`

Delete an existing MinIO Tenant.

example: `kubectl minio tenant delete --name tenant1`
example: `kubectl minio tenant delete tenant1`

Options:

Expand Down
39 changes: 7 additions & 32 deletions kubectl-minio/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@
package cmd

import (
"bufio"
"context"
"errors"
"fmt"
"io"
"os"
"strings"

"github.com/minio/kubectl-minio/cmd/helpers"
"github.com/minio/kubectl-minio/cmd/resources"
"github.com/minio/minio/pkg/color"
"github.com/spf13/cobra"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -37,7 +35,7 @@ import (

const (
deleteDesc = `
'delete' command delete MinIO Operator deployment along with all the tenants.`
'delete' command delete MinIO Operator along with all the tenants.`
deleteExample = ` kubectl minio delete`
)

Expand All @@ -54,51 +52,28 @@ func newDeleteCmd(out io.Writer, errOut io.Writer) *cobra.Command {

cmd := &cobra.Command{
Use: "delete",
Short: "Delete MinIO Operator deployment",
Short: "Delete MinIO Operator",
Long: deleteDesc,
Example: deleteExample,
PreRunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("Are you sure you want to delete ALL the MinIO Tenants and MinIO Operator? [Y/N]: ")
if !o.ask() {
return errors.New("Aborting Operator deletion")
if !helpers.Ask(fmt.Sprintf("Are you sure you want to delete ALL the MinIO Tenants and MinIO Operator?")) {
return fmt.Errorf(color.Bold("Aborting Operator deletion\n"))
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return errors.New("this command does not accept arguments")
return errors.New("delete command does not accept arguments")
}
return o.run()
},
}

cmd = helpers.DisableHelp(cmd)
f := cmd.Flags()
f.StringVarP(&o.operatorOpts.NS, "namespace", "n", helpers.DefaultNamespace, "namespace scope for this request")

return cmd
}

func (o *deleteCmd) ask() bool {
reader := bufio.NewReader(os.Stdin)
for {
s, _ := reader.ReadString('\n')
s = strings.TrimSuffix(s, "\n")
s = strings.ToLower(s)
if len(s) > 1 {
fmt.Fprintln(os.Stderr, "Please enter Y or N")
continue
}
if strings.Compare(s, "n") == 0 {
return false
} else if strings.Compare(s, "y") == 0 {
break
} else {
continue
}
}
return true
}

func (o *deleteCmd) run() error {
client, err := helpers.GetKubeClient()
if err != nil {
Expand Down
17 changes: 8 additions & 9 deletions kubectl-minio/cmd/helpers/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ import (
)

const (
// StaticYamlPath is path to the static yaml files
// StaticYamlPath = "https://raw.githubusercontent.com/nitisht/kubectl-minio/master/cmd/static/static.yaml"

// ClusterRoleBindingName is the name for CRB
ClusterRoleBindingName = "minio-operator-binding"

Expand Down Expand Up @@ -86,11 +83,13 @@ const (
DefaultConsoleImage = "minio/console:v0.4.6"
)

// DeploymentReplicas is the number of replicas for MinIO Operator
var DeploymentReplicas int32 = 1
var (
// DeploymentReplicas is the number of replicas for MinIO Operator
DeploymentReplicas int32 = 1

// KESReplicas is the number of replicas for MinIO KES
var KESReplicas int32 = 2
// KESReplicas is the number of replicas for MinIO KES
KESReplicas int32 = 2

// ConsoleReplicas is the number of replicas for MinIO Console
var ConsoleReplicas int32 = 2
// ConsoleReplicas is the number of replicas for MinIO Console
ConsoleReplicas int32 = 2
)
62 changes: 62 additions & 0 deletions kubectl-minio/cmd/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,23 @@ package helpers
import (
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"

"github.com/dustin/go-humanize"
"github.com/manifoldco/promptui"
operatorv1 "github.com/minio/operator/pkg/client/clientset/versioned"
"github.com/spf13/cobra"
apiextension "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/yaml"

miniov1 "github.com/minio/operator/pkg/apis/minio.min.io/v1"
table "github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -134,6 +142,16 @@ func CapacityPerVolume(capacity string, volumes int32) (*resource.Quantity, erro
return resource.NewQuantity(bytesPerVolume, totalQuantity.Format), nil
}

// TotalCapacity returns total capacity of a given tenant
func TotalCapacity(tenant miniov1.Tenant) string {
var totalBytes int64
for _, z := range tenant.Spec.Pools {
pvcBytes, _ := z.VolumeClaimTemplate.Spec.Resources.Requests.Storage().AsInt64()
totalBytes = totalBytes + (pvcBytes * int64(z.Servers) * int64(z.VolumesPerServer))
}
return humanize.IBytes(uint64(totalBytes))
}

// ToYaml takes a slice of values, and returns corresponding YAML
// representation as a string slice
func ToYaml(objs []runtime.Object) ([]string, error) {
Expand All @@ -148,3 +166,47 @@ func ToYaml(objs []runtime.Object) ([]string, error) {

return manifests, nil
}

// GetTable returns a formatted instance of the table
func GetTable() *table.Table {
t := table.NewWriter(os.Stdout)
t.SetAutoWrapText(false)
t.SetHeaderAlignment(table.ALIGN_LEFT)
t.SetAlignment(table.ALIGN_LEFT)
return t
}

// DisableHelp disables the help command
func DisableHelp(cmd *cobra.Command) *cobra.Command {
cmd.SetHelpCommand(&cobra.Command{
Use: "no-help",
Hidden: true,
})
return cmd
}

// Ask user for Y/N input. Return true if response is "y"
func Ask(label string) bool {
validate := func(input string) error {
s := strings.TrimSuffix(input, "\n")
s = strings.ToLower(s)
if strings.Compare(s, "n") != 0 && strings.Compare(s, "y") != 0 {
return errors.New("Please enter y/n")
}
return nil
}

prompt := promptui.Prompt{
Label: label,
Validate: validate,
}
fmt.Println()
harshavardhana marked this conversation as resolved.
Show resolved Hide resolved
result, err := prompt.Run()
if err != nil {
return false
}
if strings.Compare(result, "n") == 0 {
return false
}
return true
}
10 changes: 5 additions & 5 deletions kubectl-minio/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/minio/kubectl-minio/cmd/resources"
"github.com/spf13/cobra"

apiextensionv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextension "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"

appsv1 "k8s.io/api/apps/v1"
Expand All @@ -43,7 +43,7 @@ import (
const (
operatorInitDesc = `
'init' command creates MinIO Operator deployment along with all the dependencies.`
operatorInitExample = ` kubectl minio operator init`
operatorInitExample = ` kubectl minio init`
)

type operatorInitCmd struct {
Expand All @@ -59,7 +59,7 @@ func newInitCmd(out io.Writer, errOut io.Writer) *cobra.Command {

cmd := &cobra.Command{
Use: "init",
Short: "Initialize MinIO Operator deployment",
Short: "Initialize MinIO Operator",
Long: operatorInitDesc,
Example: operatorInitExample,
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -69,7 +69,7 @@ func newInitCmd(out io.Writer, errOut io.Writer) *cobra.Command {
return o.run()
},
}

cmd = helpers.DisableHelp(cmd)
f := cmd.Flags()
f.StringVarP(&o.operatorOpts.Image, "image", "i", helpers.DefaultOperatorImage, "operator image")
f.StringVarP(&o.operatorOpts.NS, "namespace", "n", helpers.DefaultNamespace, "namespace scope for this request")
Expand Down Expand Up @@ -124,7 +124,7 @@ func (o *operatorInitCmd) run() error {
}

func createCRD(client *apiextension.Clientset, crd *apiextensionv1.CustomResourceDefinition) error {
_, err := client.ApiextensionsV1beta1().CustomResourceDefinitions().Create(context.Background(), crd, v1.CreateOptions{})
_, err := client.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), crd, v1.CreateOptions{})
if err != nil {
if k8serrors.IsAlreadyExists(err) {
return fmt.Errorf("CustomResourceDefinition %s: already present, skipped", crd.ObjectMeta.Name)
Expand Down
10 changes: 6 additions & 4 deletions kubectl-minio/cmd/kubectl-minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ import (
"k8s.io/client-go/scale/scheme"

// Statik CRD assets for our plugin
"github.com/minio/kubectl-minio/cmd/helpers"
_ "github.com/minio/kubectl-minio/statik"
apiextensionv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
)

var (
Expand All @@ -49,7 +50,7 @@ var (

const (
minioDesc = `
kubectl plugin to manage MinIO operator CRDs.`
Deploy and manage the multi tenant, S3 API compatible object storage on Kubernetes`
)

func init() {
Expand Down Expand Up @@ -105,10 +106,11 @@ func NewCmdMinIO(streams genericclioptions.IOStreams) *cobra.Command {
Long: minioDesc,
SilenceUsage: true,
}

cmd = helpers.DisableHelp(cmd)
cobra.EnableCommandSorting = false
cmd.AddCommand(newInitCmd(cmd.OutOrStdout(), cmd.ErrOrStderr()))
cmd.AddCommand(newDeleteCmd(cmd.OutOrStdout(), cmd.ErrOrStderr()))
cmd.AddCommand(newTenantCmd(cmd.OutOrStdout(), cmd.ErrOrStderr()))
cmd.AddCommand(newDeleteCmd(cmd.OutOrStdout(), cmd.ErrOrStderr()))

return cmd
}
Loading