From ef645025304af0d4b1160e8c0eb7cf1b1dc3a30a Mon Sep 17 00:00:00 2001 From: Bart Smykla Date: Tue, 23 Feb 2021 13:12:36 +0100 Subject: [PATCH 1/4] feat(kuma-cp) add /versions API endpoint Added `/versions/` API endpoint which will return the object with supported versions of dependencies (currently supported versions of envoy for kumaDPs) Signed-off-by: Bart Smykla --- pkg/api-server/resources/versions.json | 31 ++++++++ pkg/api-server/server.go | 6 ++ pkg/api-server/versions_ws.go | 28 +++++++ pkg/api-server/versions_ws_test.go | 105 +++++++++++++++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 pkg/api-server/resources/versions.json create mode 100644 pkg/api-server/versions_ws.go create mode 100644 pkg/api-server/versions_ws_test.go diff --git a/pkg/api-server/resources/versions.json b/pkg/api-server/resources/versions.json new file mode 100644 index 000000000000..56600947a5c5 --- /dev/null +++ b/pkg/api-server/resources/versions.json @@ -0,0 +1,31 @@ +{ + "kumaDp": { + "1.0.0": { + "envoy": "1.16.0" + }, + "1.0.1": { + "envoy": "1.16.0" + }, + "1.0.2": { + "envoy": "1.16.1" + }, + "1.0.3": { + "envoy": "1.16.1" + }, + "1.0.4": { + "envoy": "1.16.1" + }, + "1.0.5": { + "envoy": "1.16.2" + }, + "1.0.6": { + "envoy": "1.16.2" + }, + "1.0.7": { + "envoy": "1.16.2" + }, + "1.0.8": { + "envoy": "1.16.2" + } + } +} diff --git a/pkg/api-server/server.go b/pkg/api-server/server.go index 0cb81b2e5079..81d9fedb77ec 100644 --- a/pkg/api-server/server.go +++ b/pkg/api-server/server.go @@ -113,6 +113,12 @@ func NewApiServer(resManager manager.ResourceManager, wsManager customization.AP } container.Add(configWs) + versionsWs, err := versionsWs() + if err != nil { + return nil, errors.Wrap(err, "could not create versions webservice") + } + container.Add(versionsWs) + zonesWs := zonesWs(resManager) container.Add(zonesWs) diff --git a/pkg/api-server/versions_ws.go b/pkg/api-server/versions_ws.go new file mode 100644 index 000000000000..1e1622f0f37e --- /dev/null +++ b/pkg/api-server/versions_ws.go @@ -0,0 +1,28 @@ +package api_server + +import ( + "io/ioutil" + + "github.com/emicklei/go-restful" + "github.com/pkg/errors" +) + +const VersionsFilePath = "resources/versions.json" + +func versionsWs() (*restful.WebService, error) { + json, err := ioutil.ReadFile(VersionsFilePath) + if err != nil { + return nil, errors.Wrap(err, "couldn't read versions.json file") + } + + ws := new(restful.WebService).Path("/versions") + + ws.Route(ws.GET("").To(func(req *restful.Request, resp *restful.Response) { + resp.AddHeader("content-type", "application/json") + if _, err := resp.Write(json); err != nil { + log.Error(err, "Could not write the index response") + } + })) + + return ws, nil +} diff --git a/pkg/api-server/versions_ws_test.go b/pkg/api-server/versions_ws_test.go new file mode 100644 index 000000000000..e6cbb9649c3b --- /dev/null +++ b/pkg/api-server/versions_ws_test.go @@ -0,0 +1,105 @@ +package api_server_test + +import ( + "encoding/json" + "fmt" + "net/http" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + config "github.com/kumahq/kuma/pkg/config/api-server" + "github.com/kumahq/kuma/pkg/metrics" + "github.com/kumahq/kuma/pkg/plugins/resources/memory" +) + +var _ = Describe("Versions WS", func() { + + It("should return the supported versions", func() { + // setup + resourceStore := memory.NewStore() + metrics, err := metrics.NewMetrics("Standalone") + Expect(err).ToNot(HaveOccurred()) + apiServer := createTestApiServer(resourceStore, config.DefaultApiServerConfig(), true, metrics) + + stop := make(chan struct{}) + go func() { + defer GinkgoRecover() + err := apiServer.Start(stop) + Expect(err).ToNot(HaveOccurred()) + }() + + // wait for the server + Eventually(func() error { + _, err := http.Get(fmt.Sprintf("http://%s/versions", apiServer.Address())) + return err + }, "3s").ShouldNot(HaveOccurred()) + + // when + resp, err := http.Get(fmt.Sprintf("http://%s/versions", apiServer.Address())) + Expect(err).ToNot(HaveOccurred()) + + // then + var data struct{ + KumaDp map[string]struct{ + Envoy string + } + } + + Expect(json.NewDecoder(resp.Body).Decode(&data)).ToNot(HaveOccurred()) + + // 1.0.0 + Expect(data).ToNot(BeNil()) + Expect(data.KumaDp).ToNot(BeNil()) + Expect(data.KumaDp["1.0.0"]).ToNot(BeNil()) + Expect(data.KumaDp["1.0.0"].Envoy).To(Equal("1.16.0")) + + // 1.0.1 + Expect(data).ToNot(BeNil()) + Expect(data.KumaDp).ToNot(BeNil()) + Expect(data.KumaDp["1.0.1"]).ToNot(BeNil()) + Expect(data.KumaDp["1.0.1"].Envoy).To(Equal("1.16.0")) + + // 1.0.2 + Expect(data).ToNot(BeNil()) + Expect(data.KumaDp).ToNot(BeNil()) + Expect(data.KumaDp["1.0.2"]).ToNot(BeNil()) + Expect(data.KumaDp["1.0.2"].Envoy).To(Equal("1.16.1")) + + // 1.0.3 + Expect(data).ToNot(BeNil()) + Expect(data.KumaDp).ToNot(BeNil()) + Expect(data.KumaDp["1.0.3"]).ToNot(BeNil()) + Expect(data.KumaDp["1.0.3"].Envoy).To(Equal("1.16.1")) + + // 1.0.4 + Expect(data).ToNot(BeNil()) + Expect(data.KumaDp).ToNot(BeNil()) + Expect(data.KumaDp["1.0.4"]).ToNot(BeNil()) + Expect(data.KumaDp["1.0.4"].Envoy).To(Equal("1.16.1")) + + // 1.0.5 + Expect(data).ToNot(BeNil()) + Expect(data.KumaDp).ToNot(BeNil()) + Expect(data.KumaDp["1.0.5"]).ToNot(BeNil()) + Expect(data.KumaDp["1.0.5"].Envoy).To(Equal("1.16.2")) + + // 1.0.6 + Expect(data).ToNot(BeNil()) + Expect(data.KumaDp).ToNot(BeNil()) + Expect(data.KumaDp["1.0.6"]).ToNot(BeNil()) + Expect(data.KumaDp["1.0.6"].Envoy).To(Equal("1.16.2")) + + // 1.0.7 + Expect(data).ToNot(BeNil()) + Expect(data.KumaDp).ToNot(BeNil()) + Expect(data.KumaDp["1.0.7"]).ToNot(BeNil()) + Expect(data.KumaDp["1.0.7"].Envoy).To(Equal("1.16.2")) + + // 1.0.8 + Expect(data).ToNot(BeNil()) + Expect(data.KumaDp).ToNot(BeNil()) + Expect(data.KumaDp["1.0.8"]).ToNot(BeNil()) + Expect(data.KumaDp["1.0.8"].Envoy).To(Equal("1.16.2")) + }) +}) From 8f86ab6266bd3ec3b530900d3d113096de0727bf Mon Sep 17 00:00:00 2001 From: Bart Smykla Date: Tue, 23 Feb 2021 13:15:14 +0100 Subject: [PATCH 2/4] chore(*) make check results Signed-off-by: Bart Smykla --- pkg/api-server/versions_ws_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/api-server/versions_ws_test.go b/pkg/api-server/versions_ws_test.go index e6cbb9649c3b..96fa61d60b17 100644 --- a/pkg/api-server/versions_ws_test.go +++ b/pkg/api-server/versions_ws_test.go @@ -40,8 +40,8 @@ var _ = Describe("Versions WS", func() { Expect(err).ToNot(HaveOccurred()) // then - var data struct{ - KumaDp map[string]struct{ + var data struct { + KumaDp map[string]struct { Envoy string } } From 40efb72dd6255f4e32d00b4d1da080eb8a6808ba Mon Sep 17 00:00:00 2001 From: Bart Smykla Date: Tue, 23 Feb 2021 13:31:42 +0100 Subject: [PATCH 3/4] fix(kuma-cp) include versions.json in code As suggested by Jakub in the past form it wouldn't work as it has to be compiled into kuma-cp binary Signed-off-by: Bart Smykla --- pkg/api-server/resources/versions.json | 31 ------------------- pkg/api-server/versions_ws.go | 42 ++++++++++++++++++++------ pkg/api-server/versions_ws_test.go | 1 - 3 files changed, 32 insertions(+), 42 deletions(-) delete mode 100644 pkg/api-server/resources/versions.json diff --git a/pkg/api-server/resources/versions.json b/pkg/api-server/resources/versions.json deleted file mode 100644 index 56600947a5c5..000000000000 --- a/pkg/api-server/resources/versions.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "kumaDp": { - "1.0.0": { - "envoy": "1.16.0" - }, - "1.0.1": { - "envoy": "1.16.0" - }, - "1.0.2": { - "envoy": "1.16.1" - }, - "1.0.3": { - "envoy": "1.16.1" - }, - "1.0.4": { - "envoy": "1.16.1" - }, - "1.0.5": { - "envoy": "1.16.2" - }, - "1.0.6": { - "envoy": "1.16.2" - }, - "1.0.7": { - "envoy": "1.16.2" - }, - "1.0.8": { - "envoy": "1.16.2" - } - } -} diff --git a/pkg/api-server/versions_ws.go b/pkg/api-server/versions_ws.go index 1e1622f0f37e..1e09d67bf286 100644 --- a/pkg/api-server/versions_ws.go +++ b/pkg/api-server/versions_ws.go @@ -1,25 +1,47 @@ package api_server import ( - "io/ioutil" - "github.com/emicklei/go-restful" - "github.com/pkg/errors" ) -const VersionsFilePath = "resources/versions.json" +var versions = []byte(`{ + "kumaDp": { + "1.0.0": { + "envoy": "1.16.0" + }, + "1.0.1": { + "envoy": "1.16.0" + }, + "1.0.2": { + "envoy": "1.16.1" + }, + "1.0.3": { + "envoy": "1.16.1" + }, + "1.0.4": { + "envoy": "1.16.1" + }, + "1.0.5": { + "envoy": "1.16.2" + }, + "1.0.6": { + "envoy": "1.16.2" + }, + "1.0.7": { + "envoy": "1.16.2" + }, + "1.0.8": { + "envoy": "1.16.2" + } + } +}`) func versionsWs() (*restful.WebService, error) { - json, err := ioutil.ReadFile(VersionsFilePath) - if err != nil { - return nil, errors.Wrap(err, "couldn't read versions.json file") - } - ws := new(restful.WebService).Path("/versions") ws.Route(ws.GET("").To(func(req *restful.Request, resp *restful.Response) { resp.AddHeader("content-type", "application/json") - if _, err := resp.Write(json); err != nil { + if _, err := resp.Write(versions); err != nil { log.Error(err, "Could not write the index response") } })) diff --git a/pkg/api-server/versions_ws_test.go b/pkg/api-server/versions_ws_test.go index 96fa61d60b17..9450c2bc90b1 100644 --- a/pkg/api-server/versions_ws_test.go +++ b/pkg/api-server/versions_ws_test.go @@ -14,7 +14,6 @@ import ( ) var _ = Describe("Versions WS", func() { - It("should return the supported versions", func() { // setup resourceStore := memory.NewStore() From e7acd39b065cc38ee4c1d9cc0164c71317575887 Mon Sep 17 00:00:00 2001 From: Bart Smykla Date: Tue, 23 Feb 2021 14:37:20 +0100 Subject: [PATCH 4/4] fix(kuma-cp) remove unnecessary error from return Removed unnecessary error from function returning versions endpoint handler Signed-off-by: Bart Smykla --- pkg/api-server/server.go | 6 +----- pkg/api-server/versions_ws.go | 4 ++-- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/pkg/api-server/server.go b/pkg/api-server/server.go index 81d9fedb77ec..c70aace62fae 100644 --- a/pkg/api-server/server.go +++ b/pkg/api-server/server.go @@ -113,11 +113,7 @@ func NewApiServer(resManager manager.ResourceManager, wsManager customization.AP } container.Add(configWs) - versionsWs, err := versionsWs() - if err != nil { - return nil, errors.Wrap(err, "could not create versions webservice") - } - container.Add(versionsWs) + container.Add(versionsWs()) zonesWs := zonesWs(resManager) container.Add(zonesWs) diff --git a/pkg/api-server/versions_ws.go b/pkg/api-server/versions_ws.go index 1e09d67bf286..603550655371 100644 --- a/pkg/api-server/versions_ws.go +++ b/pkg/api-server/versions_ws.go @@ -36,7 +36,7 @@ var versions = []byte(`{ } }`) -func versionsWs() (*restful.WebService, error) { +func versionsWs() *restful.WebService { ws := new(restful.WebService).Path("/versions") ws.Route(ws.GET("").To(func(req *restful.Request, resp *restful.Response) { @@ -46,5 +46,5 @@ func versionsWs() (*restful.WebService, error) { } })) - return ws, nil + return ws }