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

V2 nonkube connector CLI #1713

Merged
merged 4 commits into from
Nov 8, 2024
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
7 changes: 7 additions & 0 deletions internal/cmd/skupper/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ var (
ConnectorTypes = []string{"tcp"}
WorkloadTypes = []string{"deployment", "service", "daemonset", "statefulset"}
)

const (
Connectors string = "connectors"
Listeners string = "listeners"
Sites string = "sites"
RouterAccesses string = "routerAccesses"
)
1 change: 0 additions & 1 deletion internal/cmd/skupper/connector/kube/connector_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ func (cmd *CmdConnectorUpdate) ValidateInput(args []string) []error {
cmd.newSettings.connectorType = connector.Spec.Type
cmd.newSettings.includeNotReady = connector.Spec.IncludeNotReady
cmd.newSettings.routingKey = connector.Spec.RoutingKey

cmd.existingHost = connector.Spec.Host
cmd.existingSelector = connector.Spec.Selector
}
Expand Down
158 changes: 150 additions & 8 deletions internal/cmd/skupper/connector/nonkube/connector_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,170 @@ 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 CmdConnectorCreate struct {
CobraCmd *cobra.Command
Flags *common.CommandConnectorCreateFlags
Namespace string
siteName string
connectorHandler *fs.ConnectorHandler
CobraCmd *cobra.Command
Flags *common.CommandConnectorCreateFlags
namespace string
connectorName string
port int
output string
host string
routingKey string
connectorType string
tlsSecret string
}

func NewCmdConnectorCreate() *CmdConnectorCreate {
return &CmdConnectorCreate{}
}

func (cmd *CmdConnectorCreate) 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.connectorHandler = fs.NewConnectorHandler(cmd.namespace)
}

func (cmd *CmdConnectorCreate) 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()
connectorTypeValidator := validator.NewOptionValidator(common.ConnectorTypes)
outputTypeValidator := validator.NewOptionValidator(common.OutputTypes)
hostStringValidator := validator.NewHostStringValidator()

// Validate arguments name and port
if len(args) < 2 {
validationErrors = append(validationErrors, fmt.Errorf("connector 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("connector name must not be empty"))
} else if args[1] == "" {
validationErrors = append(validationErrors, fmt.Errorf("connector port must not be empty"))
} else {
ok, err := resourceStringValidator.Evaluate(args[0])
if !ok {
validationErrors = append(validationErrors, fmt.Errorf("connector name is not valid: %s", err))
} else {
cmd.connectorName = args[0]
}
cmd.port, err = strconv.Atoi(args[1])
if err != nil {
validationErrors = append(validationErrors, fmt.Errorf("connector port is not valid: %s", err))
}
ok, err = numberValidator.Evaluate(cmd.port)
if !ok {
validationErrors = append(validationErrors, fmt.Errorf("connector 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.ConnectorType != "" {
ok, err := connectorTypeValidator.Evaluate(cmd.Flags.ConnectorType)
if !ok {
validationErrors = append(validationErrors, fmt.Errorf("connector type is not valid: %s", err))
}
}
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))
}
}
if cmd.Flags.Host != "" {
lynnemorrison marked this conversation as resolved.
Show resolved Hide resolved
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
}

return validationErrors
}

func (cmd *CmdConnectorCreate) InputToOptions() {
// default routingkey to name of connector
if cmd.Flags.RoutingKey == "" {
cmd.routingKey = cmd.connectorName
} else {
cmd.routingKey = cmd.Flags.RoutingKey
}

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

cmd.host = cmd.Flags.Host
cmd.connectorType = cmd.Flags.ConnectorType
cmd.output = cmd.Flags.Output
cmd.tlsSecret = cmd.Flags.TlsSecret
}

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

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

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