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

Add nonkube site commands and unit tests #1786

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions internal/cmd/skupper/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ const (
Sites string = "sites"
RouterAccesses string = "routerAccesses"
)

const (
SiteConfigNameKey string = "name"
)
5 changes: 5 additions & 0 deletions internal/cmd/skupper/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ type CommandSiteDeleteFlags struct {
Wait bool
}

type CommandSiteStatusFlags struct {
Output string
}

type CommandLinkGenerateFlags struct {
TlsCredentials string
Cost string
Expand All @@ -132,6 +136,7 @@ type CommandLinkDeleteFlags struct {
Timeout time.Duration
Wait bool
}

type CommandLinkStatusFlags struct {
Output string
}
Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/skupper/site/kube/site_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package kube
import (
"context"
"fmt"
"github.com/skupperproject/skupper/internal/cmd/skupper/common/utils"
"os"
"text/tabwriter"

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

"github.com/skupperproject/skupper/internal/kube/client"
skupperv2alpha1 "github.com/skupperproject/skupper/pkg/generated/client/clientset/versioned/typed/skupper/v2alpha1"
"github.com/spf13/cobra"
Expand All @@ -16,6 +18,7 @@ import (
type CmdSiteStatus struct {
Client skupperv2alpha1.SkupperV2alpha1Interface
CobraCmd *cobra.Command
Flags *common.CommandSiteStatusFlags
Namespace string
}

Expand Down
98 changes: 44 additions & 54 deletions internal/cmd/skupper/site/nonkube/site_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package nonkube

import (
"fmt"
"net"

"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"
Expand All @@ -14,18 +16,14 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
SiteConfigNameKey string = "name"
)

type CmdSiteCreate struct {
siteHandler *fs.SiteHandler
routerAccessHandler *fs.RouterAccessHandler
CobraCmd *cobra.Command
Flags *common.CommandSiteCreateFlags
options map[string]string
siteName string
linkAccessType string
linkAccessEnabled bool
output string
namespace string
bindHost string
Expand All @@ -48,12 +46,10 @@ func (cmd *CmdSiteCreate) NewClient(cobraCommand *cobra.Command, args []string)
}

func (cmd *CmdSiteCreate) ValidateInput(args []string) []error {

var validationErrors []error

if cmd.Flags.ServiceAccount != "" {
Copy link
Member

Choose a reason for hiding this comment

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

Why is this warning deleted?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

looks like a mistake. I'll add it back

fmt.Println("Warning: --service-account flag is not supported on this platform")
}
hostStringValidator := validator.NewHostStringValidator()
resourceStringValidator := validator.NewResourceStringValidator()
outputTypeValidator := validator.NewOptionValidator(common.OutputTypes)

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")
Expand All @@ -63,10 +59,6 @@ func (cmd *CmdSiteCreate) ValidateInput(args []string) []error {
fmt.Println("Warning: --kubeconfig flag is not supported on this platform")
}

resourceStringValidator := validator.NewResourceStringValidator()
linkAccessTypeValidator := validator.NewOptionValidator(common.LinkAccessTypes)
outputTypeValidator := validator.NewOptionValidator(common.OutputTypes)

if len(args) == 0 || args[0] == "" {
validationErrors = append(validationErrors, fmt.Errorf("site name must not be empty"))
} else if len(args) > 1 {
Expand All @@ -77,29 +69,30 @@ func (cmd *CmdSiteCreate) ValidateInput(args []string) []error {
validationErrors = append(validationErrors, fmt.Errorf("site name is not valid: %s", err))
}
cmd.siteName = args[0]

}

if cmd.Flags.LinkAccessType != "" {
ok, err := linkAccessTypeValidator.Evaluate(cmd.Flags.LinkAccessType)
if !ok {
validationErrors = append(validationErrors, fmt.Errorf("link access type is not valid: %s", err))
}
}

if !cmd.Flags.EnableLinkAccess && len(cmd.Flags.LinkAccessType) > 0 {
validationErrors = append(validationErrors, fmt.Errorf("for the site to work with this type of linkAccess, the --enable-link-access option must be set to true"))
}

if cmd.Flags.Output != "" {
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.BindHost == "" {
validationErrors = append(validationErrors, fmt.Errorf("bind host should not be empty"))
if cmd.Flags != nil && cmd.Flags.BindHost != "" {
ip := net.ParseIP(cmd.Flags.BindHost)
ok, _ := hostStringValidator.Evaluate(cmd.Flags.BindHost)
if !ok && ip == nil {
validationErrors = append(validationErrors, fmt.Errorf("bindhost is not valid: a valid IP address or hostname is expected"))
}
}
if cmd.Flags != nil && len(cmd.Flags.SubjectAlternativeNames) != 0 {
for _, name := range cmd.Flags.SubjectAlternativeNames {
ip := net.ParseIP(name)
ok, _ := hostStringValidator.Evaluate(name)
if !ok && ip == nil {
validationErrors = append(validationErrors, fmt.Errorf("SubjectAlternativeNames is not valid: a valid IP address or hostname is expected"))
}
}
}

return validationErrors
Expand All @@ -108,29 +101,20 @@ func (cmd *CmdSiteCreate) ValidateInput(args []string) []error {
func (cmd *CmdSiteCreate) InputToOptions() {

if cmd.Flags.EnableLinkAccess {
if cmd.Flags.LinkAccessType == "" {
cmd.linkAccessType = "default"
} else {
cmd.linkAccessType = cmd.Flags.LinkAccessType
}
} else {
cmd.linkAccessType = "none"
cmd.linkAccessEnabled = true
cmd.bindHost = cmd.Flags.BindHost
cmd.routerAccessName = "router-access-" + cmd.siteName
cmd.subjectAlternativeNames = cmd.Flags.SubjectAlternativeNames
}

options := make(map[string]string)
options[SiteConfigNameKey] = cmd.siteName
options[common.SiteConfigNameKey] = cmd.siteName

cmd.options = options
cmd.output = cmd.Flags.Output

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

cmd.bindHost = cmd.Flags.BindHost
cmd.routerAccessName = "router-access-" + cmd.siteName
cmd.subjectAlternativeNames = cmd.Flags.SubjectAlternativeNames

}

func (cmd *CmdSiteCreate) Run() error {
Expand All @@ -145,8 +129,7 @@ func (cmd *CmdSiteCreate) Run() error {
Namespace: cmd.namespace,
},
Spec: v2alpha1.SiteSpec{
Settings: cmd.options,
LinkAccess: cmd.linkAccessType,
Settings: cmd.options,
},
}

Expand Down Expand Up @@ -177,22 +160,29 @@ func (cmd *CmdSiteCreate) Run() error {

if cmd.output != "" {
encodedSiteOutput, err := utils.Encode(cmd.output, siteResource)
if err != nil {
return err
}
fmt.Println(encodedSiteOutput)
fmt.Println("---")
encodedRouterAccessOutput, err := utils.Encode(cmd.output, routerAccessResource)
fmt.Println(encodedRouterAccessOutput)

return err

if cmd.linkAccessEnabled == true {
fmt.Println("---")
encodedRouterAccessOutput, err := utils.Encode(cmd.output, routerAccessResource)
if err != nil {
return err
}
fmt.Println(encodedRouterAccessOutput)
}
} else {
err := cmd.siteHandler.Add(siteResource)
if err != nil {
return err
}

err = cmd.routerAccessHandler.Add(routerAccessResource)
if err != nil {
return err
if cmd.linkAccessEnabled == true {
err = cmd.routerAccessHandler.Add(routerAccessResource)
if err != nil {
return err
}
}

}
Expand Down
Loading