Skip to content

Commit

Permalink
Add support for nonkube listener commands
Browse files Browse the repository at this point in the history
  • Loading branch information
lynnemorrison committed Nov 8, 2024
1 parent c6748fc commit a6eca55
Show file tree
Hide file tree
Showing 9 changed files with 1,538 additions and 32 deletions.
161 changes: 153 additions & 8 deletions internal/cmd/skupper/listener/nonkube/listener_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,173 @@ package nonkube

import (
"fmt"
"net"
"strconv"

"github.com/skupperproject/skupper/internal/cmd/skupper/common"
"github.com/skupperproject/skupper/internal/cmd/skupper/common/utils"
"github.com/skupperproject/skupper/internal/nonkube/client/fs"
"github.com/skupperproject/skupper/pkg/apis/skupper/v2alpha1"
"github.com/skupperproject/skupper/pkg/utils/validator"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type CmdListenerCreate struct {
CobraCmd *cobra.Command
Flags *common.CommandListenerCreateFlags
Namespace string
siteName string
listenerHandler *fs.ListenerHandler
CobraCmd *cobra.Command
Flags *common.CommandListenerCreateFlags
namespace string
listenerName string
port int
host string
tlsSecret string
listenerType string
routingKey string
output string
}

func NewCmdListenerCreate() *CmdListenerCreate {
return &CmdListenerCreate{}
}

func (cmd *CmdListenerCreate) NewClient(cobraCommand *cobra.Command, args []string) {
//TODO
if cmd.CobraCmd != nil && cmd.CobraCmd.Flag(common.FlagNameNamespace) != nil && cmd.CobraCmd.Flag(common.FlagNameNamespace).Value.String() != "" {
cmd.namespace = cmd.CobraCmd.Flag(common.FlagNameNamespace).Value.String()
}

cmd.listenerHandler = fs.NewListenerHandler(cmd.namespace)
}

func (cmd *CmdListenerCreate) ValidateInput(args []string) []error {
var validationErrors []error

if cmd.CobraCmd != nil && cmd.CobraCmd.Flag(common.FlagNameContext) != nil && cmd.CobraCmd.Flag(common.FlagNameContext).Value.String() != "" {
fmt.Println("Warning: --context flag is not supported on this platform")
}

if cmd.CobraCmd != nil && cmd.CobraCmd.Flag(common.FlagNameKubeconfig) != nil && cmd.CobraCmd.Flag(common.FlagNameKubeconfig).Value.String() != "" {
fmt.Println("Warning: --kubeconfig flag is not supported on this platform")
}

resourceStringValidator := validator.NewResourceStringValidator()
numberValidator := validator.NewNumberValidator()
listenerTypeValidator := validator.NewOptionValidator(common.ListenerTypes)
outputTypeValidator := validator.NewOptionValidator(common.OutputTypes)
hostStringValidator := validator.NewHostStringValidator()

// Validate arguments name and port
if len(args) < 2 {
validationErrors = append(validationErrors, fmt.Errorf("listener name and port must be configured"))
} else if len(args) > 2 {
validationErrors = append(validationErrors, fmt.Errorf("only two arguments are allowed for this command"))
} else if args[0] == "" {
validationErrors = append(validationErrors, fmt.Errorf("listener name must not be empty"))
} else if args[1] == "" {
validationErrors = append(validationErrors, fmt.Errorf("listener port must not be empty"))
} else {
ok, err := resourceStringValidator.Evaluate(args[0])
if !ok {
validationErrors = append(validationErrors, fmt.Errorf("listener name is not valid: %s", err))
} else {
cmd.listenerName = args[0]
}

cmd.port, err = strconv.Atoi(args[1])
if err != nil {
validationErrors = append(validationErrors, fmt.Errorf("listener port is not valid: %s", err))
}
ok, err = numberValidator.Evaluate(cmd.port)
if !ok {
validationErrors = append(validationErrors, fmt.Errorf("listener port is not valid: %s", err))
}
}

// Validate flags
if cmd.Flags.RoutingKey != "" {
ok, err := resourceStringValidator.Evaluate(cmd.Flags.RoutingKey)
if !ok {
validationErrors = append(validationErrors, fmt.Errorf("routing key is not valid: %s", err))
}
}

if cmd.Flags.ListenerType != "" {
ok, err := listenerTypeValidator.Evaluate(cmd.Flags.ListenerType)
if !ok {
validationErrors = append(validationErrors, fmt.Errorf("listener type is not valid: %s", err))
}
}

if cmd.Flags.Host != "" {
ip := net.ParseIP(cmd.Flags.Host)
ok, _ := hostStringValidator.Evaluate(cmd.Flags.Host)
if !ok || ip == nil {
validationErrors = append(validationErrors, fmt.Errorf("host is not valid: a valid IP address or hostname is expected"))
}
} else {
validationErrors = append(validationErrors, fmt.Errorf("host name must be configured: an IP address or hostname is expected"))
}
if cmd.Flags.TlsSecret != "" {
// TBD what is valid TlsSecret
}

if cmd.Flags.Output != "" {
ok, err := outputTypeValidator.Evaluate(cmd.Flags.Output)
if !ok {
validationErrors = append(validationErrors, fmt.Errorf("output type is not valid: %s", err))
}
}
return validationErrors
}

func (cmd *CmdListenerCreate) InputToOptions() {
// default routingkey to name of listener
if cmd.Flags.RoutingKey == "" {
cmd.routingKey = cmd.listenerName
} else {
cmd.routingKey = cmd.Flags.RoutingKey
}

if cmd.namespace == "" {
cmd.namespace = "default"
}

cmd.host = cmd.Flags.Host
cmd.tlsSecret = cmd.Flags.TlsSecret
cmd.listenerType = cmd.Flags.ListenerType
cmd.output = cmd.Flags.Output
}

func (cmd *CmdListenerCreate) ValidateInput(args []string) []error { return nil }
func (cmd *CmdListenerCreate) InputToOptions() {}
func (cmd *CmdListenerCreate) Run() error {
return fmt.Errorf("command not supported by the selected platform")
listenerResource := v2alpha1.Listener{
TypeMeta: metav1.TypeMeta{
APIVersion: "skupper.io/v2alpha1",
Kind: "Listener",
},
ObjectMeta: metav1.ObjectMeta{
Name: cmd.listenerName,
Namespace: cmd.namespace,
},
Spec: v2alpha1.ListenerSpec{
Host: cmd.host,
Port: cmd.port,
RoutingKey: cmd.routingKey,
TlsCredentials: cmd.tlsSecret,
Type: cmd.listenerType,
},
}

if cmd.output != "" {
encodedOutput, err := utils.Encode(cmd.output, listenerResource)
fmt.Println(encodedOutput)
return err
} else {
err := cmd.listenerHandler.Add(listenerResource)
if err != nil {
return err
}
}
return nil
}

func (cmd *CmdListenerCreate) WaitUntil() error { return nil }
Loading

0 comments on commit a6eca55

Please sign in to comment.