From 66b4acfd8f986a1d37426135eb37d563a81321ff Mon Sep 17 00:00:00 2001 From: "Ling Samuel (WSL)" Date: Thu, 24 Aug 2023 11:15:34 +0800 Subject: [PATCH 1/4] fix: add placeholder for validate Signed-off-by: Ling Samuel (WSL) --- internal/pkg/validator/validator.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/pkg/validator/validator.go b/internal/pkg/validator/validator.go index bb57a1f..07e9590 100644 --- a/internal/pkg/validator/validator.go +++ b/internal/pkg/validator/validator.go @@ -43,6 +43,9 @@ func (v *Validator) Validate() []error { for _, service := range v.localConfig.Services { service := service + if service.ID == "" { + service.ID = "placeholder" + } err := v.cluster.Service().Validate(context.Background(), service) if err != nil { allErr = append(allErr, err) @@ -51,6 +54,9 @@ func (v *Validator) Validate() []error { for _, route := range v.localConfig.Routes { route := route + if route.ID == "" { + route.ID = "placeholder" + } err := v.cluster.Route().Validate(context.Background(), route) if err != nil { allErr = append(allErr, err) From 5dcd7bfafe37eaea14c3acd81c412818c5394356 Mon Sep 17 00:00:00 2001 From: "Ling Samuel (WSL)" Date: Thu, 24 Aug 2023 11:26:00 +0800 Subject: [PATCH 2/4] use .Name Signed-off-by: Ling Samuel (WSL) --- internal/pkg/validator/validator.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/pkg/validator/validator.go b/internal/pkg/validator/validator.go index 07e9590..22e87bd 100644 --- a/internal/pkg/validator/validator.go +++ b/internal/pkg/validator/validator.go @@ -44,7 +44,7 @@ func (v *Validator) Validate() []error { for _, service := range v.localConfig.Services { service := service if service.ID == "" { - service.ID = "placeholder" + service.ID = service.Name } err := v.cluster.Service().Validate(context.Background(), service) if err != nil { @@ -55,7 +55,7 @@ func (v *Validator) Validate() []error { for _, route := range v.localConfig.Routes { route := route if route.ID == "" { - route.ID = "placeholder" + route.ID = route.Name } err := v.cluster.Route().Validate(context.Background(), route) if err != nil { From b4358384250543205f50e413022adfe02d6c0cb3 Mon Sep 17 00:00:00 2001 From: "Ling Samuel (WSL)" Date: Thu, 24 Aug 2023 11:37:03 +0800 Subject: [PATCH 3/4] fix Signed-off-by: Ling Samuel (WSL) --- adc.yaml | 6 +++--- internal/pkg/validator/validator.go | 8 ++++++++ pkg/api/apisix/types/types.go | 22 +++++++++++----------- test/cli/suites/validate.go | 24 ++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 test/cli/suites/validate.go diff --git a/adc.yaml b/adc.yaml index 2cbb100..9a84b91 100644 --- a/adc.yaml +++ b/adc.yaml @@ -5,9 +5,9 @@ services: hosts: - foo.com - bar.com - upstreams: - - name: upstream1 - targets: + upstream: + name: upstream1 + nodes: - host: 10.10.16.12 port: 8080 weight: 50 diff --git a/internal/pkg/validator/validator.go b/internal/pkg/validator/validator.go index 22e87bd..da456be 100644 --- a/internal/pkg/validator/validator.go +++ b/internal/pkg/validator/validator.go @@ -4,6 +4,9 @@ import ( "context" "errors" + "github.com/fatih/color" + "sigs.k8s.io/yaml" + "github.com/api7/adc/pkg/api/apisix" "github.com/api7/adc/pkg/api/apisix/types" "github.com/api7/adc/pkg/data" @@ -46,8 +49,13 @@ func (v *Validator) Validate() []error { if service.ID == "" { service.ID = service.Name } + if service.Upstream.ID == "" { + service.Upstream.ID = service.Upstream.Name + } err := v.cluster.Service().Validate(context.Background(), service) if err != nil { + b, _ := yaml.Marshal(service) + color.Red(string(b)) allErr = append(allErr, err) } } diff --git a/pkg/api/apisix/types/types.go b/pkg/api/apisix/types/types.go index 60a38f9..2212c9f 100644 --- a/pkg/api/apisix/types/types.go +++ b/pkg/api/apisix/types/types.go @@ -50,31 +50,31 @@ type Route struct { // Service is the abstraction of a backend service on API gateway. type Service struct { - ID string `json:"id"` + ID string `json:"id" yaml:"id"` - Name string `json:"name"` - Description string `json:"desc,omitempty"` + Name string `json:"name" yaml:"name"` + Description string `json:"desc,omitempty" yaml:"desc,omitempty"` // Labels are used for resource classification and indexing - Labels StringArray `json:"labels,omitempty"` + Labels StringArray `json:"labels,omitempty" yaml:"labels,omitempty"` // HTTP hosts for this service. - Hosts []string `json:"hosts,omitempty"` + Hosts []string `json:"hosts,omitempty" yaml:"hosts,omitempty"` // Plugin settings on Service level - Plugins Plugins `json:"plugins,omitempty"` + Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"` // Upstream settings for the Service. - Upstream Upstream `json:"upstream,omitempty"` + Upstream Upstream `json:"upstream,omitempty" yaml:"upstream,omitempty"` // UpstreamId settings for the Service. - UpstreamId string `json:"upstream_id,omitempty"` + UpstreamId string `json:"upstream_id,omitempty" yaml:"upstream_id,omitempty"` // Enables a websocket. Set to false by default. - EnableWebsocket bool `json:"enable_websocket,omitempty"` + EnableWebsocket bool `json:"enable_websocket,omitempty" yaml:"enable_websocket,omitempty"` } // Upstream is the definition of the upstream on Service. type Upstream struct { // ID is the upstream name. It should be unique among all upstreams // in the same service. - ID string `json:"id"` + ID string `json:"id" yaml:"id"` - Name string `json:"name"` + Name string `json:"name" yaml:"name"` Type string `json:"type,omitempty" yaml:"type,omitempty"` HashOn string `json:"hash_on,omitempty" yaml:"hash_on,omitempty"` Key string `json:"key,omitempty" yaml:"key,omitempty"` diff --git a/test/cli/suites/validate.go b/test/cli/suites/validate.go new file mode 100644 index 0000000..e730b71 --- /dev/null +++ b/test/cli/suites/validate.go @@ -0,0 +1,24 @@ +package suites + +import ( + "bytes" + "os/exec" + + "github.com/onsi/ginkgo/v2" + "github.com/onsi/gomega" +) + +var _ = ginkgo.Describe("`adc validate` tests", func() { + ginkgo.Context("Basic functions", func() { + _ = NewScaffold() + ginkgo.It("should validate schema", func() { + var pingOutput bytes.Buffer + cmd := exec.Command("adc", "validate", "-f", "testdata/test.yaml") + cmd.Stdout = &pingOutput + err := cmd.Run() + gomega.Expect(err).To(gomega.BeNil()) + + gomega.Expect(pingOutput.String()).To(gomega.Equal("Get file content success: config name: test, version: test, routes: 1, services: 1.\nValidate file content success\n")) + }) + }) +}) From 6aa9eef09d6ee0f11bbd077055ef0c0b73544a3d Mon Sep 17 00:00:00 2001 From: "Ling Samuel (WSL)" Date: Thu, 24 Aug 2023 11:42:43 +0800 Subject: [PATCH 4/4] remove debug lines Signed-off-by: Ling Samuel (WSL) --- internal/pkg/validator/validator.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/internal/pkg/validator/validator.go b/internal/pkg/validator/validator.go index da456be..f2586bb 100644 --- a/internal/pkg/validator/validator.go +++ b/internal/pkg/validator/validator.go @@ -4,9 +4,6 @@ import ( "context" "errors" - "github.com/fatih/color" - "sigs.k8s.io/yaml" - "github.com/api7/adc/pkg/api/apisix" "github.com/api7/adc/pkg/api/apisix/types" "github.com/api7/adc/pkg/data" @@ -54,8 +51,6 @@ func (v *Validator) Validate() []error { } err := v.cluster.Service().Validate(context.Background(), service) if err != nil { - b, _ := yaml.Marshal(service) - color.Red(string(b)) allErr = append(allErr, err) } }