Skip to content
This repository has been archived by the owner on May 6, 2022. It is now read-only.

Add flags to svcat register broker command #2208

Merged
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
88 changes: 77 additions & 11 deletions cmd/svcat/broker/register_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,43 @@ package broker

import (
"fmt"
"os"
"strings"
"time"

"github.com/kubernetes-incubator/service-catalog/cmd/svcat/command"
"github.com/kubernetes-incubator/service-catalog/cmd/svcat/output"
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1"
servicecatalog "github.com/kubernetes-incubator/service-catalog/pkg/svcat/service-catalog"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// RegisterCmd contains the information needed to register a broker
type RegisterCmd struct {
BrokerName string
Context *command.Context
URL string
*command.Namespaced
*command.Waitable

Context *command.Context

BasicSecret string
BearerSecret string
BrokerName string
CAFile string
ClassRestrictions []string
PlanRestrictions []string
SkipTLS bool
RelistBehavior string
RelistDuration time.Duration
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm always confused by what a create should look like with a manual relistduration. Should we pass a null duration or does the api server just ignore whatever is sent for the duration?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This sends a time.Duration of 0, which the apiserver drops on the floor. It never gets set in the spec at all.

URL string
}

// NewRegisterCmd builds a "svcat register" command
func NewRegisterCmd(cxt *command.Context) *cobra.Command {
registerCmd := &RegisterCmd{
Context: cxt,
Context: cxt,
Namespaced: command.NewNamespaced(cxt),
Waitable: command.NewWaitable(),
}
cmd := &cobra.Command{
Use: "register NAME --url URL",
Expand All @@ -48,27 +68,73 @@ func NewRegisterCmd(cxt *command.Context) *cobra.Command {
cmd.Flags().StringVar(&registerCmd.URL, "url", "",
"The broker URL (Required)")
cmd.MarkFlagRequired("url")
cmd.Flags().StringVar(&registerCmd.BasicSecret, "basic-secret", "",
"A secret containing basic auth (username/password) information to connect to the broker")
cmd.Flags().StringVar(&registerCmd.BearerSecret, "bearer-secret", "",
Copy link
Contributor

Choose a reason for hiding this comment

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

Should basic-secret and bearer-secret not be basic-secret-name and bearer-secret-name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't follow that convention for flags that indicate secrets for other commands - both provision and bind just use -secret, not -secret-name

"A secret containing a bearer token to connect to the broker")
cmd.Flags().StringVar(&registerCmd.CAFile, "ca", "",
"A file containing the CA certificate to connect to the broker")
cmd.Flags().StringSliceVar(&registerCmd.ClassRestrictions, "class-restrictions", []string{},
"A list of restrictions to apply to the classes allowed from the broker")
cmd.Flags().StringSliceVar(&registerCmd.PlanRestrictions, "plan-restrictions", []string{},
"A list of restrictions to apply to the plans allowed from the broker")
cmd.Flags().StringVar(&registerCmd.RelistBehavior, "relist-behavior", "",
"Behavior for relisting the broker's catalog. Valid options are manual or duration. Defaults to duration with an interval of 15m.")
cmd.Flags().DurationVar(&registerCmd.RelistDuration, "relist-duration", 0*time.Second,
"Interval to refetch broker catalog when relist-behavior is set to duration, specified in human readable format: 30s, 1m, 1h")
cmd.Flags().BoolVar(&registerCmd.SkipTLS, "skip-tls", false,
"Disables TLS certificate verification when communicating with this broker. This is strongly discouraged. You should use --ca instead.")
registerCmd.AddNamespaceFlags(cmd.Flags(), false)
registerCmd.AddWaitFlags(cmd)

return cmd
}

// Validate checks that the required arguements have been provided
func (c *RegisterCmd) Validate(args []string) error {
if len(args) == 0 {
if len(args) < 1 {
return fmt.Errorf("a broker name is required")
}
c.BrokerName = args[0]

if c.BasicSecret != "" && c.BearerSecret != "" {
return fmt.Errorf("cannot use both basic auth and bearer auth")
}

if c.CAFile != "" {
_, err := os.Stat(c.CAFile)
if err != nil {
return fmt.Errorf("error finding CA file: %v", err.Error())
}
}
if c.RelistBehavior != "" {
c.RelistBehavior = strings.ToLower(c.RelistBehavior)
if c.RelistBehavior != "duration" && c.RelistBehavior != "manual" {
return fmt.Errorf("invalid --relist-duration value, allowed values are: duration, manual")
}
}
return nil
}

// Run runs the command
// Run creates the broker and then displays the broker details
func (c *RegisterCmd) Run() error {
return c.Register()
}
opts := &servicecatalog.RegisterOptions{
BasicSecret: c.BasicSecret,
BearerSecret: c.BearerSecret,
CAFile: c.CAFile,
ClassRestrictions: c.ClassRestrictions,
Namespace: c.Namespace,
PlanRestrictions: c.PlanRestrictions,
SkipTLS: c.SkipTLS,
}
if c.RelistBehavior == "duration" {
opts.RelistBehavior = v1beta1.ServiceBrokerRelistBehaviorDuration
opts.RelistDuration = &metav1.Duration{Duration: c.RelistDuration}
} else if c.RelistBehavior == "manual" {
opts.RelistBehavior = v1beta1.ServiceBrokerRelistBehaviorManual
}

// Register calls out to the pkg lib to create the broker and displays the output
func (c *RegisterCmd) Register() error {
broker, err := c.Context.App.Register(c.BrokerName, c.URL)
broker, err := c.Context.App.Register(c.BrokerName, c.URL, opts)
if err != nil {
return err
}
Expand Down
Loading