Skip to content

Commit

Permalink
Refactor: APPX-87: Fix input passing (#12)
Browse files Browse the repository at this point in the history
Refactor: use --kind flag for serviceDiscovery use first argument passed by args as serviceName
  • Loading branch information
martini-source authored Aug 9, 2023
1 parent d677e18 commit cec0edc
Show file tree
Hide file tree
Showing 41 changed files with 127 additions and 110 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/sirupsen/logrus v1.9.0
github.com/vshn/component-appcat/apis v0.0.0-20230411090651-b0418f5ceb5c
golang.org/x/exp v0.0.0-20230809094429-853ea248256d
k8s.io/apimachinery v0.26.3
sigs.k8s.io/yaml v1.3.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20230809094429-853ea248256d h1:wu5bD43Ana/nF1ZmaLr3lW/FQeJU8CcI+Ln7yWHViXE=
golang.org/x/exp v0.0.0-20230809094429-853ea248256d/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand Down
5 changes: 0 additions & 5 deletions internal/defaults/exoscalev1.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ func (d *Defaults) GetExoscalePostgreSQLDefault() *exoscalev1.ExoscalePostgreSQL
postgreSQLdefault.Spec.Parameters.Backup.TimeOfDay = "12:00:00"
postgreSQLdefault.Spec.Parameters.Maintenance.DayOfWeek = "Sunday"
postgreSQLdefault.Spec.Parameters.Maintenance.TimeOfDay = "24:00:00"
postgreSQLdefault.Name = "ExoscalePostgreSQL"

return &postgreSQLdefault
}
Expand All @@ -25,7 +24,6 @@ func (d *Defaults) GetExoscaleRedisDefault() *exoscalev1.ExoscaleRedis {
redisDefault.Spec.Parameters.Maintenance.TimeOfDay = "24:00:00"
redisDefault.Spec.Parameters.Service.Zone = "ch-dk-2"

redisDefault.Name = "ExoscaleRedis"
return &redisDefault
}

Expand All @@ -38,7 +36,6 @@ func (d *Defaults) GetExoscaleKafkaDefault() *exoscalev1.ExoscaleKafka {
kafkaDefault.Spec.Parameters.Maintenance.DayOfWeek = "Sunday"
kafkaDefault.Spec.Parameters.Maintenance.TimeOfDay = "24:00:00"

kafkaDefault.Name = "ExoscaleKafka"
return &kafkaDefault
}

Expand All @@ -52,7 +49,6 @@ func (d *Defaults) GetExoscaleMySQLDefault() *exoscalev1.ExoscaleMySQL {
mySQLdefault.Spec.Parameters.Maintenance.DayOfWeek = "Sunday"
mySQLdefault.Spec.Parameters.Maintenance.TimeOfDay = "24:00:00"

mySQLdefault.Name = "ExoscaleMySQL"
return &mySQLdefault
}

Expand All @@ -66,6 +62,5 @@ func (d *Defaults) GetExoscaleOpenSearchDefault() *exoscalev1.ExoscaleOpenSearch
openSearchDefault.Spec.Parameters.Maintenance.DayOfWeek = "Sunday"
openSearchDefault.Spec.Parameters.Maintenance.TimeOfDay = "24:00:00"

openSearchDefault.Name = "ExoscaleOpenSearch"
return &openSearchDefault
}
2 changes: 0 additions & 2 deletions internal/defaults/vshnv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func (d *Defaults) GetVSHNPostgreSQLDefault() *vshnv1.VSHNPostgreSQL {
postgreSQLdefault.Spec.Parameters.Backup.Retention = 12
postgreSQLdefault.Spec.Parameters.Scheduling.NodeSelector = map[string]string{"appuio.io/node-class": "plus"}

postgreSQLdefault.Name = "VSHNPostgreSQL"
return &postgreSQLdefault
}

Expand All @@ -34,6 +33,5 @@ func (d *Defaults) GetVSHNRedisDefault() *vshnv1.VSHNRedis {
redisDefault.Spec.Parameters.Size.MemoryRequests = "500Mi"
redisDefault.Spec.Parameters.Size.MemoryLimits = "1Gi"

redisDefault.Name = "VSHNRedis"
return &redisDefault
}
61 changes: 37 additions & 24 deletions internal/util/argParser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package util
import (
"fmt"
"strings"

"golang.org/x/exp/slices"
)

type Input struct {
Expand All @@ -16,6 +18,7 @@ const (
PARAMETER_PREFIX = "--"
HIERARCHY_DELIMITER = "."
PARAM_VALUE_INFIX = "="
K8S_SERVICE_KIND = "kind"
)

// Takes the input arguments and outputs them as separate Input structs
Expand All @@ -31,30 +34,6 @@ func ParseArgs(args []string) ([]Input, error) {
return inputList, nil
}

// Takes the input arguments and outputs them as separate Input structs
func mapArgsToInput(args []string) []Input {
var inputList []Input
input := Input{}
for _, arg := range args {
if isParameter(arg) {
input.ParameterHierarchy = strings.Split(strings.TrimPrefix(arg, PARAMETER_PREFIX), HIERARCHY_DELIMITER)
} else if isParamToUnset(arg) {
param := strings.TrimPrefix(arg, PARAMETER_PREFIX)
param = strings.TrimSuffix(param, UNSET_PARAMETER_SUFFIX)
input.ParameterHierarchy = strings.Split(param, HIERARCHY_DELIMITER)
input.Unset = true
inputList = append(inputList, input)
input = Input{}
} else {
input.Value = arg
inputList = append(inputList, input)
input = Input{}
}

}
return inputList
}

// Parses raw cli input parameters and returns a list of arguments
// if a "=" is used in any form of "key=value" pair, the "=" needs to be the suffix of the key or the separator between key and value
// if the "=" is the prefix of the value it is interpreted as part of the value
Expand Down Expand Up @@ -122,6 +101,40 @@ func CheckForMissingValues(arguments []string) error {

return nil
}
func FilterServiceKind(input []Input) (string, error) {
serviceKindIndex := slices.IndexFunc(input, func(input Input) bool {
return input.ParameterHierarchy[0] == K8S_SERVICE_KIND
})
if serviceKindIndex == -1 {
return "", fmt.Errorf("ServiceKind is missing")
} else {
return input[serviceKindIndex].Value, nil
}
}

// Takes the input arguments and outputs them as separate Input structs
func mapArgsToInput(args []string) []Input {
var inputList []Input
input := Input{}
for _, arg := range args {
if isParameter(arg) {
input.ParameterHierarchy = strings.Split(strings.TrimPrefix(arg, PARAMETER_PREFIX), HIERARCHY_DELIMITER)
} else if isParamToUnset(arg) {
param := strings.TrimPrefix(arg, PARAMETER_PREFIX)
param = strings.TrimSuffix(param, UNSET_PARAMETER_SUFFIX)
input.ParameterHierarchy = strings.Split(param, HIERARCHY_DELIMITER)
input.Unset = true
inputList = append(inputList, input)
input = Input{}
} else {
input.Value = arg
inputList = append(inputList, input)
input = Input{}
}

}
return inputList
}

func isValue(arg string) bool {
if !isParamToUnset(arg) && !isParameter(arg) && !isParameterValuePair(arg) {
Expand Down
54 changes: 31 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ func init() {
}

func printUsage(cmd string, apps applications.AppMap) {
out := fmt.Sprintf(`usage: %s <type> [options]
out := fmt.Sprintf(`usage: %s <serviceName> --kind <serviceKind> [options]
Generate AppCat YAML manifests
Known types:
Known service kinds:
`, cmd)

names := apps.Names()
Expand Down Expand Up @@ -56,36 +56,44 @@ func main() {
// If during execution, an unrecoverable error occurs (usually due to a bug),
// an error message is logged and the program will panic.
func Main(apps applications.AppMap, args []string, out io.Writer) int {
if len(args) < 2 {
if len(args) < 3 {
printUsage(args[0], apps)
return 1
}
plainArgs := args[1:]
parsedType := util.NormalizeName(plainArgs[0])
app, ok := apps[parsedType]

var err error
var parameters []util.Input

// args[0] is the binary
resourceName := args[1]
parameters, err = util.ParseArgs(args[2:])

if err != nil {
logrus.Errorf("Failed parsing arguments: %s", err)
return 1
}

serviceKind, err := util.FilterServiceKind(parameters)
if err != nil {
logrus.Errorf("%s", err)
printUsage(args[0], apps)
return 1
}
serviceKind = util.NormalizeName(serviceKind)
app, ok := apps[serviceKind]
if !ok {
logrus.Errorf("service type '%s' is not supported", parsedType)
logrus.Errorf("service type '%s' is not supported", serviceKind)
printUsage(args[0], apps)
return 1
}

var err error
////appcat-cli serviceKind
service := app.GetDefault()

if len(plainArgs) >= 2 {
parameters, err := util.ParseArgs(plainArgs[1:])
if err != nil {
logrus.Errorf("Failed parsing arguments: %s", err)
return 1
}

_, err = util.DecorateType(service, parameters)
if err != nil {
logrus.Errorf("failed setting parameters: %s", err)
return 1
}
parameters = append(parameters, util.Input{ParameterHierarchy: []string{"ObjectMeta", "Name"}, Value: resourceName, Unset: false})
_, err = util.DecorateType(service, parameters)
if err != nil {
logrus.Errorf("failed setting parameters: %s", err)
return 1
}

err = writeYAML(service, out)
if err != nil {
logrus.Panicf("failed writing YAML: %s", err)
Expand Down
8 changes: 4 additions & 4 deletions tests/golden/exoKafka.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"default": "ExoscaleKafka",
"parameters": "ExoscaleKafka --spec.parameters.maintenance.dayOfWeek=Monday --spec.parameters.service.zone ch-fr-1 --spec.parameters.size.plan enterprise-large",
"unset": "ExoscaleKafka --spec.parameters.maintenance.dayOfWeek- --spec.parameters.service- --spec.parameters.size.plan-",
"jsonInput": "ExoscaleKafka --spec.parameters.maintenance={\"DayOfWeek\":\"Monday\",\"TimeOfDay\":\"00:00:00\"} --spec.parameters.service {\"Zone\":\"ch-fr-99\"} --spec.parameters.size.plan {\"Foo\":\"Bar\"}"
"default": "kafka-default --kind ExoscaleKafka",
"parameters": "kafka-parameters --kind ExoscaleKafka --spec.parameters.maintenance.dayOfWeek=Monday --spec.parameters.service.zone ch-fr-1 --spec.parameters.size.plan enterprise-large",
"unset": "kafka-unset --kind ExoscaleKafka --spec.parameters.maintenance.dayOfWeek- --spec.parameters.service- --spec.parameters.size.plan-",
"jsonInput": "kafka-jsonInput --kind ExoscaleKafka --spec.parameters.maintenance={\"DayOfWeek\":\"Monday\",\"TimeOfDay\":\"00:00:00\"} --spec.parameters.service {\"Zone\":\"ch-fr-99\"} --spec.parameters.size.plan {\"Foo\":\"Bar\"}"
}
2 changes: 1 addition & 1 deletion tests/golden/exoKafka/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: exoscale.appcat.vshn.io/v1
kind: ExoscaleKafka
metadata:
creationTimestamp: null
name: ExoscaleKafka
name: kafka-default
spec:
parameters:
maintenance:
Expand Down
2 changes: 1 addition & 1 deletion tests/golden/exoKafka/jsonInput.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: exoscale.appcat.vshn.io/v1
kind: ExoscaleKafka
metadata:
creationTimestamp: null
name: ExoscaleKafka
name: kafka-jsonInput
spec:
parameters:
maintenance:
Expand Down
2 changes: 1 addition & 1 deletion tests/golden/exoKafka/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: exoscale.appcat.vshn.io/v1
kind: ExoscaleKafka
metadata:
creationTimestamp: null
name: ExoscaleKafka
name: kafka-parameters
spec:
parameters:
maintenance:
Expand Down
2 changes: 1 addition & 1 deletion tests/golden/exoKafka/unset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: exoscale.appcat.vshn.io/v1
kind: ExoscaleKafka
metadata:
creationTimestamp: null
name: ExoscaleKafka
name: kafka-unset
spec:
parameters:
maintenance:
Expand Down
8 changes: 4 additions & 4 deletions tests/golden/exoMySQL.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"default": "ExoscaleMySQL",
"parameters": "ExoscaleMySQL --spec.parameters.backup.timeOfDay=24:00:00 --spec.parameters.maintenance.dayOfWeek Monday --spec.parameters.service.majorVersion 9999",
"unset": "ExoscaleMySQL --spec.parameters.maintenance.dayOfWeek- --spec.parameters.service- --spec.parameters.size.plan-",
"jsonInput": "ExoscaleMySQL --spec.parameters.maintenance={\"DayOfWeek\":\"Monday\",\"TimeOfDay\":\"00:00:00\"} --spec.parameters.service {\"Zone\":\"ch-fr-99\"} --spec.parameters.size.plan {\"Foo\":\"Bar\"}"
"default": "mysql-default --kind ExoscaleMySQL",
"parameters": "mysql-parameters --kind ExoscaleMySQL --spec.parameters.backup.timeOfDay=24:00:00 --spec.parameters.maintenance.dayOfWeek Monday --spec.parameters.service.majorVersion 9999",
"unset": "mysql-unset --kind ExoscaleMySQL --spec.parameters.maintenance.dayOfWeek- --spec.parameters.service- --spec.parameters.size.plan-",
"jsonInput": "mysql-jsonInput --kind ExoscaleMySQL --spec.parameters.maintenance={\"DayOfWeek\":\"Monday\",\"TimeOfDay\":\"00:00:00\"} --spec.parameters.service {\"Zone\":\"ch-fr-99\"} --spec.parameters.size.plan {\"Foo\":\"Bar\"}"
}
2 changes: 1 addition & 1 deletion tests/golden/exoMySQL/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: exoscale.appcat.vshn.io/v1
kind: ExoscaleMySQL
metadata:
creationTimestamp: null
name: ExoscaleMySQL
name: mysql-default
spec:
parameters:
backup:
Expand Down
2 changes: 1 addition & 1 deletion tests/golden/exoMySQL/jsonInput.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: exoscale.appcat.vshn.io/v1
kind: ExoscaleMySQL
metadata:
creationTimestamp: null
name: ExoscaleMySQL
name: mysql-jsonInput
spec:
parameters:
backup:
Expand Down
2 changes: 1 addition & 1 deletion tests/golden/exoMySQL/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: exoscale.appcat.vshn.io/v1
kind: ExoscaleMySQL
metadata:
creationTimestamp: null
name: ExoscaleMySQL
name: mysql-parameters
spec:
parameters:
backup:
Expand Down
2 changes: 1 addition & 1 deletion tests/golden/exoMySQL/unset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: exoscale.appcat.vshn.io/v1
kind: ExoscaleMySQL
metadata:
creationTimestamp: null
name: ExoscaleMySQL
name: mysql-unset
spec:
parameters:
backup:
Expand Down
8 changes: 4 additions & 4 deletions tests/golden/exoOpenSearch.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"default": "ExoscaleOpenSearch",
"parameters": "ExoscaleOpenSearch --spec.parameters.backup.timeOfDay=24:00:00 --spec.parameters.size.plan enterprise-large --spec.parameters.service.majorVersion 9999",
"unset": "ExoscaleOpenSearch --spec.parameters.maintenance.dayOfWeek- --spec.parameters.service- --spec.parameters.size.plan-",
"jsonInput": "ExoscaleOpenSearch --spec.parameters.maintenance={\"DayOfWeek\":\"Monday\",\"TimeOfDay\":\"00:00:00\"} --spec.parameters.service {\"Zone\":\"ch-fr-99\"} --spec.parameters.size.plan {\"Foo\":\"Bar\"}"
"default": "opensearch-default --kind ExoscaleOpenSearch",
"parameters": "opensearch-parameters --kind ExoscaleOpenSearch --spec.parameters.backup.timeOfDay=24:00:00 --spec.parameters.size.plan enterprise-large --spec.parameters.service.majorVersion 9999",
"unset": "opensearch-unset --kind ExoscaleOpenSearch --spec.parameters.maintenance.dayOfWeek- --spec.parameters.service- --spec.parameters.size.plan-",
"jsonInput": "opensearch-jsonInput --kind ExoscaleOpenSearch --spec.parameters.maintenance={\"DayOfWeek\":\"Monday\",\"TimeOfDay\":\"00:00:00\"} --spec.parameters.service {\"Zone\":\"ch-fr-99\"} --spec.parameters.size.plan {\"Foo\":\"Bar\"}"
}
2 changes: 1 addition & 1 deletion tests/golden/exoOpenSearch/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: exoscale.appcat.vshn.io/v1
kind: ExoscaleOpenSearch
metadata:
creationTimestamp: null
name: ExoscaleOpenSearch
name: opensearch-default
spec:
parameters:
backup:
Expand Down
2 changes: 1 addition & 1 deletion tests/golden/exoOpenSearch/jsonInput.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: exoscale.appcat.vshn.io/v1
kind: ExoscaleOpenSearch
metadata:
creationTimestamp: null
name: ExoscaleOpenSearch
name: opensearch-jsonInput
spec:
parameters:
backup:
Expand Down
2 changes: 1 addition & 1 deletion tests/golden/exoOpenSearch/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: exoscale.appcat.vshn.io/v1
kind: ExoscaleOpenSearch
metadata:
creationTimestamp: null
name: ExoscaleOpenSearch
name: opensearch-parameters
spec:
parameters:
backup:
Expand Down
2 changes: 1 addition & 1 deletion tests/golden/exoOpenSearch/unset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: exoscale.appcat.vshn.io/v1
kind: ExoscaleOpenSearch
metadata:
creationTimestamp: null
name: ExoscaleOpenSearch
name: opensearch-unset
spec:
parameters:
backup:
Expand Down
8 changes: 4 additions & 4 deletions tests/golden/exoPostgreSQL.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"default": "ExoscalePostgreSQL",
"parameters": "ExoscalePostgreSQL --spec.parameters.backup.timeOfDay=24:00:00 --spec.parameters.service.majorVersion 13 --spec.parameters.size.plan enterprise-large",
"unset": "ExoscalePostgreSQL --spec.parameters.maintenance.dayOfWeek- --spec.parameters.service- --spec.parameters.size.plan-",
"jsonInput": "ExoscalePostgreSQL --spec.parameters.maintenance={\"DayOfWeek\":\"Monday\",\"TimeOfDay\":\"00:00:00\"} --spec.parameters.service {\"Zone\":\"ch-fr-99\"} --spec.parameters.size.plan {\"Foo\":\"Bar\"}"
"default": "postgres-default --kind ExoscalePostgreSQL",
"parameters": "postgres-parameters --kind ExoscalePostgreSQL --spec.parameters.backup.timeOfDay=24:00:00 --spec.parameters.service.majorVersion 13 --spec.parameters.size.plan enterprise-large",
"unset": "postgres-unset --kind ExoscalePostgreSQL --spec.parameters.maintenance.dayOfWeek- --spec.parameters.service- --spec.parameters.size.plan-",
"jsonInput": "postgres-jsonInput --kind ExoscalePostgreSQL --spec.parameters.maintenance={\"DayOfWeek\":\"Monday\",\"TimeOfDay\":\"00:00:00\"} --spec.parameters.service {\"Zone\":\"ch-fr-99\"} --spec.parameters.size.plan {\"Foo\":\"Bar\"}"
}

Loading

0 comments on commit cec0edc

Please sign in to comment.