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

Create tenant in interactive mode #1249

Merged
merged 1 commit into from
Aug 25, 2022
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
44 changes: 44 additions & 0 deletions kubectl-minio/cmd/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"os/exec"
"regexp"
"strconv"
"strings"

"k8s.io/client-go/dynamic"
Expand Down Expand Up @@ -247,3 +248,46 @@ func Ask(label string) bool {
_, err := prompt.Run()
return err == nil
}

// Ask user for number input."
func AskNumber(label string, validate func(int) error) int {
prompt := promptui.Prompt{
Label: label,
Validate: func(input string) error {
value, err := strconv.Atoi(input)
if err != nil {
return nil
}
if validate != nil {
return validate(value)
} else {
return nil
}
},
}
result, err := prompt.Run()
if err == promptui.ErrInterrupt {
os.Exit(-1)
}
r, _ := strconv.Atoi(result)
return r
}

// AskQuestion user for generic input."
func AskQuestion(label string, validate func(string) error) string {
prompt := promptui.Prompt{
Label: label,
Validate: func(input string) error {
if validate != nil {
return validate(input)
} else {
return nil
}
},
}
result, err := prompt.Run()
if err == promptui.ErrInterrupt {
os.Exit(-1)
}
return result
}
4 changes: 4 additions & 0 deletions kubectl-minio/cmd/resources/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type TenantOptions struct {
PrometheusImage string
PrometheusSidecarImage string
PrometheusInitImage string
Interactive bool
}

// Validate Tenant Options
Expand All @@ -64,6 +65,9 @@ func (t TenantOptions) Validate() error {
if t.Volumes <= 0 {
return errors.New("--volumes is required. Specify a positive value")
}
if t.NS == "" {
return errors.New("--namespace flag is required")
}
if t.Capacity == "" {
return errors.New("--capacity flag is required")
}
Expand Down
78 changes: 73 additions & 5 deletions kubectl-minio/cmd/tenant-create.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/minio/operator/pkg/resources/services"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -98,15 +99,14 @@ func newTenantCreateCmd(out io.Writer, errOut io.Writer) *cobra.Command {
f.StringVar(&c.tenantOpts.PrometheusSidecarImage, "prometheus-sidecar-image", "", "(Only used when enable-prometheus is on) The Docker image to use for prometheus sidecar")
f.StringVar(&c.tenantOpts.PrometheusImage, "prometheus-init-image", "", "(Only used when enable-prometheus is on) Defines the Docker image to use as the init container for running prometheus")
f.BoolVarP(&c.output, "output", "o", false, "generate tenant yaml for 'kubectl apply -f tenant.yaml'")

cmd.MarkFlagRequired("servers")
cmd.MarkFlagRequired("volumes")
cmd.MarkFlagRequired("capacity")
cmd.MarkFlagRequired("namespace")
Copy link
Contributor Author

@reivaj05 reivaj05 Aug 22, 2022

Choose a reason for hiding this comment

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

This will be now validated in TenantOptions.Validate, that way the new interactive flag can be processed and we can keep having these flags as required

f.BoolVar(&c.tenantOpts.Interactive, "interactive", false, "Create tenant in interactive mode")
return cmd
}

func (c *createCmd) validate(args []string) error {
if c.tenantOpts.Interactive {
return nil
}
if args == nil {
return errors.New("create command requires specifying the tenant name as an argument, e.g. 'kubectl minio tenant create tenant1'")
}
Expand Down Expand Up @@ -137,6 +137,11 @@ func (c *createCmd) run(args []string) error {
if err != nil {
return err
}
if c.tenantOpts.Interactive {
if err := c.populateInteractiveTenant(); err != nil {
return err
}
}
// Generate MinIO user credentials
tenantUserCredentials, err := resources.NewUserCredentialsSecret(&c.tenantOpts, fmt.Sprintf("%s-user-1", c.tenantOpts.Name))
if err != nil {
Expand Down Expand Up @@ -187,6 +192,69 @@ func (c *createCmd) run(args []string) error {
return nil
}

func (c *createCmd) populateInteractiveTenant() error {
c.tenantOpts.Name = helpers.AskQuestion("Tenant name", helpers.CheckValidTenantName)
c.tenantOpts.ConfigurationSecretName = fmt.Sprintf("%s-env-configuration", c.tenantOpts.Name)
c.tenantOpts.Servers = int32(helpers.AskNumber("Total of servers", greaterThanZero))
c.tenantOpts.Volumes = int32(helpers.AskNumber("Total of volumes", greaterThanZero))
c.tenantOpts.NS = helpers.AskQuestion("Namespace", validateEmptyInput)
c.tenantOpts.Capacity = helpers.AskQuestion("Capacity", validateCapacity)
if err := c.tenantOpts.Validate(); err != nil {
return err
}
c.tenantOpts.DisableTLS = helpers.Ask("Disable TLS")
c.tenantOpts.EnableAuditLogs = !helpers.Ask("Disable audit logs")
if c.tenantOpts.EnableAuditLogs {
c.populateAuditConfig()
}
c.tenantOpts.EnablePrometheus = !helpers.Ask("Disable prometheus")
if c.tenantOpts.EnablePrometheus {
c.populatePrometheus()
}
return nil
}

func (c *createCmd) populateAuditConfig() {
if helpers.Ask("Would you like to customize configuration for audit logs?") {
c.tenantOpts.AuditLogsDiskSpace = int32(helpers.AskNumber("Disk space", greaterThanZero))
c.tenantOpts.AuditLogsImage = helpers.AskQuestion("Logs image", validateEmptyInput)
c.tenantOpts.AuditLogsPGImage = helpers.AskQuestion("Postgres image", validateEmptyInput)
c.tenantOpts.AuditLogsPGInitImage = helpers.AskQuestion("Postgres init image", validateEmptyInput)
c.tenantOpts.AuditLogsStorageClass = helpers.AskQuestion("Storage class", validateEmptyInput)
}
}

func (c *createCmd) populatePrometheus() {
if helpers.Ask("Would you like to customize configuration for prometheus?") {
c.tenantOpts.PrometheusDiskSpace = helpers.AskNumber("Disk space", greaterThanZero)
c.tenantOpts.PrometheusImage = helpers.AskQuestion("Prometheus image", validateEmptyInput)
c.tenantOpts.PrometheusSidecarImage = helpers.AskQuestion("Prometheus sidecar image", validateEmptyInput)
c.tenantOpts.PrometheusInitImage = helpers.AskQuestion("Prometheus init image", validateEmptyInput)
}
}

func validateEmptyInput(value string) error {
if value == "" {
return errors.New("value can't be empty")
}
return nil
}

func validateCapacity(value string) error {
if err := validateEmptyInput(value); err != nil {
return err
}
_, err := resource.ParseQuantity(value)
return err
}

func greaterThanZero(value int) error {
if value <= 0 {
return errors.New("value needs to be greater than zero")
}
return nil
}

func createTenant(operatorClient *operatorv1.Clientset, kubeClient *kubernetes.Clientset, tenant *miniov2.Tenant, tenantCredsSecret, tenantConfiguration, console *corev1.Secret) error {
if _, err := kubeClient.CoreV1().Namespaces().Get(context.Background(), tenant.Namespace, metav1.GetOptions{}); err != nil {
return fmt.Errorf("namespace %s not found, please create the namespace using 'kubectl create ns %s'", tenant.Namespace, tenant.Namespace)
Expand Down