Skip to content

Commit

Permalink
fix: remove duplicated code
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Sukhin <[email protected]>
  • Loading branch information
vsukhin committed Jan 14, 2025
1 parent 9e29be1 commit 9a76197
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 184 deletions.
94 changes: 94 additions & 0 deletions cmd/kubectl-testkube/commands/common/webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package common

import (
"encoding/csv"
"errors"
"strconv"
"strings"

"github.com/kubeshop/testkube/pkg/api/v1/testkube"
)

func GetWebhookConfig(configs map[string]string) (map[string]testkube.WebhookConfigValue, error) {
config := map[string]testkube.WebhookConfigValue{}
for key, value := range configs {
switch {
case strings.HasPrefix(value, "value="):
config[key] = testkube.WebhookConfigValue{
Value: &testkube.BoxedString{Value: strings.TrimPrefix(value, "value=")},
}
case strings.HasPrefix(value, "secret="):
data := strings.TrimPrefix(value, "secret=")
r := csv.NewReader(strings.NewReader(data))
r.Comma = ';'
r.LazyQuotes = true
r.TrimLeadingSpace = true

records, err := r.ReadAll()
if err != nil {
return nil, err
}

if len(records) != 1 {
return nil, errors.New("single string expected")
}

if len(records[0]) != 3 {
return nil, errors.New("3 fields expected")
}

config[key] = testkube.WebhookConfigValue{
Secret: &testkube.SecretRef{
Namespace: records[0][0],
Name: records[0][1],
Key: records[0][2],
},
}
default:
continue
}
}

return config, nil
}

func GetWebhookParameters(parameters map[string]string) (map[string]testkube.WebhookParameterSchema, error) {
parameter := map[string]testkube.WebhookParameterSchema{}
for key, value := range parameters {
r := csv.NewReader(strings.NewReader(value))
r.Comma = ';'
r.LazyQuotes = true
r.TrimLeadingSpace = true

records, err := r.ReadAll()
if err != nil {
return nil, err
}

if len(records) != 1 {
return nil, errors.New("single string expected")
}

if len(records[0]) != 5 {
return nil, errors.New("5 fields expected")
}

var required bool
required, err = strconv.ParseBool(records[0][1])
if err != nil {
return nil, err
}

parameter[key] = testkube.WebhookParameterSchema{
Description: records[0][0],
Required: required,
Example: records[0][2],
Default_: &testkube.BoxedString{
Value: records[0][3],
},
Pattern: records[0][4],
}
}

return parameter, nil
}
97 changes: 5 additions & 92 deletions cmd/kubectl-testkube/commands/webhooks/common.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package webhooks

import (
"encoding/csv"
"errors"
"fmt"
"os"
"strconv"
"strings"

"github.com/spf13/cobra"

cmdcommon "github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
"github.com/kubeshop/testkube/internal/common"
apiv1 "github.com/kubeshop/testkube/pkg/api/v1/client"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
Expand Down Expand Up @@ -60,7 +57,7 @@ func NewCreateWebhookOptionsFromFlags(cmd *cobra.Command) (options apiv1.CreateW
}

if len(configs) != 0 {
config, err = getWebhookConfig(configs)
config, err = cmdcommon.GetWebhookConfig(configs)
if err != nil {
return options, err
}
Expand All @@ -73,7 +70,7 @@ func NewCreateWebhookOptionsFromFlags(cmd *cobra.Command) (options apiv1.CreateW
}

if len(parameters) != 0 {
parameter, err = getWebhookParameters(parameters)
parameter, err = cmdcommon.GetWebhookParameters(parameters)
if err != nil {
return options, err
}
Expand Down Expand Up @@ -198,7 +195,7 @@ func NewUpdateWebhookOptionsFromFlags(cmd *cobra.Command) (options apiv1.UpdateW
return options, err
}

values, err := getWebhookConfig(configs)
values, err := cmdcommon.GetWebhookConfig(configs)
if err != nil {
return options, err
}
Expand All @@ -211,7 +208,7 @@ func NewUpdateWebhookOptionsFromFlags(cmd *cobra.Command) (options apiv1.UpdateW
return options, err
}

values, err := getWebhookParameters(parameters)
values, err := cmdcommon.GetWebhookParameters(parameters)
if err != nil {
return options, err
}
Expand All @@ -226,87 +223,3 @@ func NewUpdateWebhookOptionsFromFlags(cmd *cobra.Command) (options apiv1.UpdateW

return options, nil
}

func getWebhookConfig(configs map[string]string) (map[string]testkube.WebhookConfigValue, error) {
config := map[string]testkube.WebhookConfigValue{}
for key, value := range configs {
switch {
case strings.HasPrefix(value, "value="):
config[key] = testkube.WebhookConfigValue{
Value: &testkube.BoxedString{Value: strings.TrimPrefix(value, "value=")},
}
case strings.HasPrefix(value, "secret="):
data := strings.TrimPrefix(value, "secret=")
r := csv.NewReader(strings.NewReader(data))
r.Comma = ';'
r.LazyQuotes = true
r.TrimLeadingSpace = true

records, err := r.ReadAll()
if err != nil {
return nil, err
}

if len(records) != 1 {
return nil, errors.New("single string expected")
}

if len(records[0]) != 3 {
return nil, errors.New("3 fields expected")
}

config[key] = testkube.WebhookConfigValue{
Secret: &testkube.SecretRef{
Namespace: records[0][0],
Name: records[0][1],
Key: records[0][2],
},
}
default:
continue
}
}

return config, nil
}

func getWebhookParameters(parameters map[string]string) (map[string]testkube.WebhookParameterSchema, error) {
parameter := map[string]testkube.WebhookParameterSchema{}
for key, value := range parameters {
r := csv.NewReader(strings.NewReader(value))
r.Comma = ';'
r.LazyQuotes = true
r.TrimLeadingSpace = true

records, err := r.ReadAll()
if err != nil {
return nil, err
}

if len(records) != 1 {
return nil, errors.New("single string expected")
}

if len(records[0]) != 5 {
return nil, errors.New("5 fields expected")
}

var required bool
required, err = strconv.ParseBool(records[0][1])
if err != nil {
return nil, err
}

parameter[key] = testkube.WebhookParameterSchema{
Description: records[0][0],
Required: required,
Example: records[0][2],
Default_: &testkube.BoxedString{
Value: records[0][3],
},
Pattern: records[0][4],
}
}

return parameter, nil
}
97 changes: 5 additions & 92 deletions cmd/kubectl-testkube/commands/webhooktemplates/common.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package webhooktemplates

import (
"encoding/csv"
"errors"
"fmt"
"os"
"strconv"
"strings"

"github.com/spf13/cobra"

"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
apiv1 "github.com/kubeshop/testkube/pkg/api/v1/client"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
webhooktemplatesmapper "github.com/kubeshop/testkube/pkg/mapper/webhooktemplates"
Expand Down Expand Up @@ -59,7 +56,7 @@ func NewCreateWebhookTemplateOptionsFromFlags(cmd *cobra.Command) (options apiv1
}

if len(configs) != 0 {
config, err = getWebhookTemplateConfig(configs)
config, err = common.GetWebhookConfig(configs)
if err != nil {
return options, err
}
Expand All @@ -72,7 +69,7 @@ func NewCreateWebhookTemplateOptionsFromFlags(cmd *cobra.Command) (options apiv1
}

if len(parameters) != 0 {
parameter, err = getWebhookTemplateParameters(parameters)
parameter, err = common.GetWebhookParameters(parameters)
if err != nil {
return options, err
}
Expand Down Expand Up @@ -189,7 +186,7 @@ func NewUpdateWebhookTemplateOptionsFromFlags(cmd *cobra.Command) (options apiv1
return options, err
}

values, err := getWebhookTemplateConfig(configs)
values, err := common.GetWebhookConfig(configs)
if err != nil {
return options, err
}
Expand All @@ -202,7 +199,7 @@ func NewUpdateWebhookTemplateOptionsFromFlags(cmd *cobra.Command) (options apiv1
return options, err
}

values, err := getWebhookTemplateParameters(parameters)
values, err := common.GetWebhookParameters(parameters)
if err != nil {
return options, err
}
Expand All @@ -211,87 +208,3 @@ func NewUpdateWebhookTemplateOptionsFromFlags(cmd *cobra.Command) (options apiv1

return options, nil
}

func getWebhookTemplateConfig(configs map[string]string) (map[string]testkube.WebhookConfigValue, error) {
config := map[string]testkube.WebhookConfigValue{}
for key, value := range configs {
switch {
case strings.HasPrefix(value, "value="):
config[key] = testkube.WebhookConfigValue{
Value: &testkube.BoxedString{Value: strings.TrimPrefix(value, "value=")},
}
case strings.HasPrefix(value, "secret="):
data := strings.TrimPrefix(value, "secret=")
r := csv.NewReader(strings.NewReader(data))
r.Comma = ';'
r.LazyQuotes = true
r.TrimLeadingSpace = true

records, err := r.ReadAll()
if err != nil {
return nil, err
}

if len(records) != 1 {
return nil, errors.New("single string expected")
}

if len(records[0]) != 3 {
return nil, errors.New("3 fields expected")
}

config[key] = testkube.WebhookConfigValue{
Secret: &testkube.SecretRef{
Namespace: records[0][0],
Name: records[0][1],
Key: records[0][2],
},
}
default:
continue
}
}

return config, nil
}

func getWebhookTemplateParameters(parameters map[string]string) (map[string]testkube.WebhookParameterSchema, error) {
parameter := map[string]testkube.WebhookParameterSchema{}
for key, value := range parameters {
r := csv.NewReader(strings.NewReader(value))
r.Comma = ';'
r.LazyQuotes = true
r.TrimLeadingSpace = true

records, err := r.ReadAll()
if err != nil {
return nil, err
}

if len(records) != 1 {
return nil, errors.New("single string expected")
}

if len(records[0]) != 5 {
return nil, errors.New("5 fields expected")
}

var required bool
required, err = strconv.ParseBool(records[0][1])
if err != nil {
return nil, err
}

parameter[key] = testkube.WebhookParameterSchema{
Description: records[0][0],
Required: required,
Example: records[0][2],
Default_: &testkube.BoxedString{
Value: records[0][3],
},
Pattern: records[0][4],
}
}

return parameter, nil
}

0 comments on commit 9a76197

Please sign in to comment.