Skip to content

Commit

Permalink
Add generate option to all commands and remove output flag
Browse files Browse the repository at this point in the history
from commands.
  • Loading branch information
lynnemorrison committed Dec 16, 2024
1 parent c4ef199 commit c5edb27
Show file tree
Hide file tree
Showing 47 changed files with 3,451 additions and 991 deletions.
37 changes: 28 additions & 9 deletions internal/cmd/skupper/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ type CommandSiteCreateFlags struct {
EnableLinkAccess bool
LinkAccessType string
ServiceAccount string
Output string
Host string
Timeout time.Duration
BindHost string
SubjectAlternativeNames []string
Expand All @@ -104,8 +102,6 @@ type CommandSiteUpdateFlags struct {
EnableLinkAccess bool
LinkAccessType string
ServiceAccount string
Output string
Host string
Timeout time.Duration
BindHost string
SubjectAlternativeNames []string
Expand All @@ -122,6 +118,15 @@ type CommandSiteStatusFlags struct {
Output string
}

type CommandSiteGenerateFlags struct {
EnableLinkAccess bool
LinkAccessType string
ServiceAccount string
BindHost string
SubjectAlternativeNames []string
Output string
}

type CommandLinkGenerateFlags struct {
TlsCredentials string
Cost string
Expand All @@ -132,7 +137,6 @@ type CommandLinkGenerateFlags struct {
type CommandLinkUpdateFlags struct {
TlsCredentials string
Cost string
Output string
Timeout time.Duration
Wait string
}
Expand Down Expand Up @@ -167,7 +171,6 @@ type CommandConnectorCreateFlags struct {
IncludeNotReadyPods bool
Workload string
Timeout time.Duration
Output string
Wait string
}

Expand All @@ -181,7 +184,6 @@ type CommandConnectorUpdateFlags struct {
Selector string
IncludeNotReadyPods bool
Timeout time.Duration
Output string
Wait string
}

Expand All @@ -194,13 +196,23 @@ type CommandConnectorStatusFlags struct {
Output string
}

type CommandConnectorGenerateFlags struct {
RoutingKey string
Host string
Selector string
TlsCredentials string
ConnectorType string
IncludeNotReadyPods bool
Workload string
Output string
}

type CommandListenerCreateFlags struct {
RoutingKey string
Host string
TlsCredentials string
ListenerType string
Timeout time.Duration
Output string
Wait string
}

Expand All @@ -211,7 +223,6 @@ type CommandListenerUpdateFlags struct {
ListenerType string
Timeout time.Duration
Port int
Output string
Wait string
}

Expand All @@ -230,6 +241,14 @@ type CommandSystemSetupFlags struct {
Force bool
}

type CommandListenerGenerateFlags struct {
RoutingKey string
Host string
TlsCredentials string
ListenerType string
Output string
}

type CommandVersionFlags struct {
Output string
}
Expand Down
42 changes: 40 additions & 2 deletions internal/cmd/skupper/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ skupper connector status my-connector`,
cmd.AddCommand(CmdConnectorStatusFactory(config.GetPlatform()))
cmd.AddCommand(CmdConnectorUpdateFactory(config.GetPlatform()))
cmd.AddCommand(CmdConnectorDeleteFactory(config.GetPlatform()))
cmd.AddCommand(CmdConnectorGenerateFactory(config.GetPlatform()))

return cmd
}
Expand Down Expand Up @@ -53,7 +54,6 @@ skupper connector create backend 8080 --workload deployment/backend`,
cmd.Flags().StringVarP(&cmdFlags.Selector, common.FlagNameSelector, "s", "", common.FlagDescSelector)
cmd.Flags().StringVarP(&cmdFlags.Workload, common.FlagNameWorkload, "w", "", common.FlagDescWorkload)
cmd.Flags().DurationVar(&cmdFlags.Timeout, common.FlagNameTimeout, 60*time.Second, common.FlagDescTimeout)
cmd.Flags().StringVarP(&cmdFlags.Output, common.FlagNameOutput, "o", "", common.FlagDescOutput)
cmd.Flags().StringVar(&cmdFlags.Wait, common.FlagNameWait, "configured", common.FlagDescWait)

kubeCommand.CobraCmd = cmd
Expand Down Expand Up @@ -120,7 +120,6 @@ func CmdConnectorUpdateFactory(configuredPlatform types.Platform) *cobra.Command
cmd.Flags().StringVarP(&cmdFlags.Selector, common.FlagNameSelector, "s", "", common.FlagDescSelector)
cmd.Flags().StringVarP(&cmdFlags.Workload, common.FlagNameWorkload, "w", "", common.FlagDescWorkload)
cmd.Flags().DurationVar(&cmdFlags.Timeout, common.FlagNameTimeout, 60*time.Second, common.FlagDescTimeout)
cmd.Flags().StringVarP(&cmdFlags.Output, common.FlagNameOutput, "o", "", common.FlagDescOutput)
cmd.Flags().IntVar(&cmdFlags.Port, common.FlagNameConnectorPort, 0, common.FlagDescConnectorPort)
cmd.Flags().StringVar(&cmdFlags.Wait, common.FlagNameWait, "configured", common.FlagDescWait)

Expand Down Expand Up @@ -164,3 +163,42 @@ func CmdConnectorDeleteFactory(configuredPlatform types.Platform) *cobra.Command

return cmd
}

func CmdConnectorGenerateFactory(configuredPlatform types.Platform) *cobra.Command {
kubeCommand := kube.NewCmdConnectorGenerate()
nonKubeCommand := nonkube.NewCmdConnectorGenerate()

cmdConnectorGenerateDesc := common.SkupperCmdDescription{
Use: "generate <name> <port>",
Short: "Generate a connector",
Long: "Clients at this site use the connector host and port to establish connections to the remote service.",
Example: `skupper connector Generate database 5432
skupper connector Generate backend 8080 --workload deployment/backend`,
}

cmd := common.ConfigureCobraCommand(configuredPlatform, cmdConnectorGenerateDesc, kubeCommand, nonKubeCommand)

cmdFlags := common.CommandConnectorGenerateFlags{}

cmd.Flags().StringVarP(&cmdFlags.RoutingKey, common.FlagNameRoutingKey, "r", "", common.FlagDescRoutingKey)
cmd.Flags().StringVar(&cmdFlags.Host, common.FlagNameHost, "", common.FlagDescHost)
cmd.Flags().StringVar(&cmdFlags.TlsCredentials, common.FlagNameTlsCredentials, "", common.FlagDescTlsCredentials)
cmd.Flags().StringVar(&cmdFlags.ConnectorType, common.FlagNameConnectorType, "tcp", common.FlagDescConnectorType)
cmd.Flags().BoolVarP(&cmdFlags.IncludeNotReadyPods, common.FlagNameIncludeNotReadyPods, "i", false, common.FlagDescIncludeNotRead)
cmd.Flags().StringVarP(&cmdFlags.Selector, common.FlagNameSelector, "s", "", common.FlagDescSelector)
cmd.Flags().StringVarP(&cmdFlags.Workload, common.FlagNameWorkload, "w", "", common.FlagDescWorkload)
cmd.Flags().StringVarP(&cmdFlags.Output, common.FlagNameOutput, "o", "yaml", common.FlagDescOutput)

kubeCommand.CobraCmd = cmd
kubeCommand.Flags = &cmdFlags
nonKubeCommand.CobraCmd = cmd
nonKubeCommand.Flags = &cmdFlags

if configuredPlatform != types.PlatformKubernetes {
cmd.Flags().MarkHidden(common.FlagNameIncludeNotReadyPods)
cmd.Flags().MarkHidden(common.FlagNameSelector)
cmd.Flags().MarkHidden(common.FlagNameWorkload)
}

return cmd
}
16 changes: 14 additions & 2 deletions internal/cmd/skupper/connector/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func TestCmdConnectorFactory(t *testing.T) {
common.FlagNameIncludeNotReadyPods: "false",
common.FlagNameSelector: "",
common.FlagNameWorkload: "",
common.FlagNameOutput: "",
common.FlagNameTimeout: "1m0s",
common.FlagNameWait: "configured",
},
Expand All @@ -46,7 +45,6 @@ func TestCmdConnectorFactory(t *testing.T) {
common.FlagNameIncludeNotReadyPods: "false",
common.FlagNameSelector: "",
common.FlagNameWorkload: "",
common.FlagNameOutput: "",
common.FlagNameTimeout: "1m0s",
common.FlagNameConnectorPort: "0",
common.FlagNameWait: "configured",
Expand All @@ -68,6 +66,20 @@ func TestCmdConnectorFactory(t *testing.T) {
},
command: CmdConnectorDeleteFactory(types.PlatformKubernetes),
},
{
name: "CmdConnectorGenerateFactory",
expectedFlagsWithDefaultValue: map[string]interface{}{
common.FlagNameRoutingKey: "",
common.FlagNameHost: "",
common.FlagNameTlsCredentials: "",
common.FlagNameConnectorType: "tcp",
common.FlagNameIncludeNotReadyPods: "false",
common.FlagNameSelector: "",
common.FlagNameWorkload: "",
common.FlagNameOutput: "yaml",
},
command: CmdConnectorGenerateFactory(types.PlatformKubernetes),
},
}

for _, test := range testTable {
Expand Down
27 changes: 4 additions & 23 deletions internal/cmd/skupper/connector/kube/connector_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package kube
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
"strconv"
"time"

"k8s.io/apimachinery/pkg/api/meta"

"github.com/skupperproject/skupper/internal/cmd/skupper/common"
"github.com/skupperproject/skupper/internal/cmd/skupper/common/utils"

Expand All @@ -28,7 +29,6 @@ type CmdConnectorCreate struct {
namespace string
name string
port int
output string
host string
selector string
tlsCredentials string
Expand Down Expand Up @@ -59,7 +59,6 @@ func (cmd *CmdConnectorCreate) ValidateInput(args []string) []error {
resourceStringValidator := validator.NewResourceStringValidator()
numberValidator := validator.NewNumberValidator()
connectorTypeValidator := validator.NewOptionValidator(common.ConnectorTypes)
outputTypeValidator := validator.NewOptionValidator(common.OutputTypes)
timeoutValidator := validator.NewTimeoutInSecondsValidator()
workloadStringValidator := validator.NewWorkloadStringValidator(common.WorkloadTypes)
selectorStringValidator := validator.NewSelectorStringValidator()
Expand Down Expand Up @@ -193,19 +192,12 @@ func (cmd *CmdConnectorCreate) ValidateInput(args []string) []error {
}
}
}
//TBD what is valid timeout
if cmd.Flags != nil && cmd.Flags.Timeout.String() != "" {
ok, err := timeoutValidator.Evaluate(cmd.Flags.Timeout)
if !ok {
validationErrors = append(validationErrors, fmt.Errorf("timeout is not valid: %s", err))
}
}
if cmd.Flags != nil && cmd.Flags.Output != "" {
ok, err := outputTypeValidator.Evaluate(cmd.Flags.Output)
if !ok {
validationErrors = append(validationErrors, fmt.Errorf("output type is not valid: %s", err))
}
}

if cmd.Flags != nil && cmd.Flags.Wait != "" {
ok, err := statusValidator.Evaluate(cmd.Flags.Wait)
Expand Down Expand Up @@ -240,7 +232,6 @@ func (cmd *CmdConnectorCreate) InputToOptions() {
cmd.timeout = cmd.Flags.Timeout
cmd.tlsCredentials = cmd.Flags.TlsCredentials
cmd.connectorType = cmd.Flags.ConnectorType
cmd.output = cmd.Flags.Output
cmd.includeNotReadyPods = cmd.Flags.IncludeNotReadyPods
cmd.status = cmd.Flags.Wait
}
Expand All @@ -267,21 +258,11 @@ func (cmd *CmdConnectorCreate) Run() error {
},
}

if cmd.output != "" {
encodedOutput, err := utils.Encode(cmd.output, resource)
fmt.Println(encodedOutput)
return err
} else {
_, err := cmd.client.Connectors(cmd.namespace).Create(context.TODO(), &resource, metav1.CreateOptions{})
return err
}
_, err := cmd.client.Connectors(cmd.namespace).Create(context.TODO(), &resource, metav1.CreateOptions{})
return err
}

func (cmd *CmdConnectorCreate) WaitUntil() error {
// the site resource was not created
if cmd.output != "" {
return nil
}

if cmd.status == "none" {
return nil
Expand Down
Loading

0 comments on commit c5edb27

Please sign in to comment.