From 04bc7db98999a5722f8b252d64c94b4db58adddb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Weisbarth?= Date: Wed, 8 Mar 2023 16:08:12 +0100 Subject: [PATCH] Rename health-endpoint and variables; --- src/autoscaler/api/cmd/api/api_test.go | 10 ++-- .../healthendpoint/health_readiness_test.go | 60 +++++++++---------- src/autoscaler/healthendpoint/server.go | 30 +++++----- 3 files changed, 50 insertions(+), 50 deletions(-) diff --git a/src/autoscaler/api/cmd/api/api_test.go b/src/autoscaler/api/cmd/api/api_test.go index 758412fed1..c12af041c7 100644 --- a/src/autoscaler/api/cmd/api/api_test.go +++ b/src/autoscaler/api/cmd/api/api_test.go @@ -208,8 +208,8 @@ var _ = Describe("Api", func() { basicAuthConfig.Health.HealthCheckUsername = "" basicAuthConfig.Health.HealthCheckPassword = "" basicAuthConfig.Health.ReadinessCheckEnabled = true - basicAuthConfig.Health.UnprotectedEndpoints = []string{"/", healthendpoint.LIVELINESS_PATH, - healthendpoint.READINESS_PATH, healthendpoint.PPROF_PATH, healthendpoint.PROMETHEUS_PATH} + basicAuthConfig.Health.UnprotectedEndpoints = []string{"/", healthendpoint.LivenessPath, + healthendpoint.ReadinessPath, healthendpoint.PprofPath, healthendpoint.PrometheusPath} runner.configPath = writeConfig(&basicAuthConfig).Name() runner.Start() }) @@ -219,7 +219,7 @@ var _ = Describe("Api", func() { }) Context("when a request to query health comes", func() { It("returns with a 200", func() { - url := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, healthendpoint.PROMETHEUS_PATH) + url := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, healthendpoint.PrometheusPath) rsp, err := healthHttpClient.Get(url) Expect(err).NotTo(HaveOccurred()) Expect(rsp.StatusCode).To(Equal(http.StatusOK)) @@ -252,7 +252,7 @@ var _ = Describe("Api", func() { }) Context("when username and password are incorrect for basic authentication during health check", func() { It("should return 401", func() { - url := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, healthendpoint.LIVELINESS_PATH) + url := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, healthendpoint.LivenessPath) req, err := http.NewRequest(http.MethodGet, url, nil) Expect(err).NotTo(HaveOccurred()) @@ -266,7 +266,7 @@ var _ = Describe("Api", func() { Context("when username and password are correct for basic authentication during health check", func() { It("should return 200", func() { - url := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, healthendpoint.LIVELINESS_PATH) + url := fmt.Sprintf("http://127.0.0.1:%d%s", healthport, healthendpoint.LivenessPath) req, err := http.NewRequest(http.MethodGet, url, nil) Expect(err).NotTo(HaveOccurred()) diff --git a/src/autoscaler/healthendpoint/health_readiness_test.go b/src/autoscaler/healthendpoint/health_readiness_test.go index 609b431dd3..7c04ee91f3 100644 --- a/src/autoscaler/healthendpoint/health_readiness_test.go +++ b/src/autoscaler/healthendpoint/health_readiness_test.go @@ -82,7 +82,7 @@ var _ = Describe("Health Readiness", func() { It("should require basic auth", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.PROMETHEUS_PATH). + Get(healthendpoint.PrometheusPath). Expect(t). Status(http.StatusUnauthorized). End() @@ -100,7 +100,7 @@ var _ = Describe("Health Readiness", func() { It("should require basic auth", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.PROMETHEUS_PATH). + Get(healthendpoint.PrometheusPath). Expect(t). Status(http.StatusUnauthorized). End() @@ -113,14 +113,14 @@ var _ = Describe("Health Readiness", func() { BeforeEach(func() { config.HealthCheckUsername = "" config.HealthCheckPassword = "" - config.UnprotectedEndpoints = []string{"", healthendpoint.LIVELINESS_PATH, - healthendpoint.READINESS_PATH, healthendpoint.PROMETHEUS_PATH, healthendpoint.PPROF_PATH} + config.UnprotectedEndpoints = []string{"", healthendpoint.LivenessPath, + healthendpoint.ReadinessPath, healthendpoint.PrometheusPath, healthendpoint.PprofPath} }) When("Prometheus Health or / endpoint is called", func() { It("should respond OK", func() { apitest.New().Debug(). Handler(healthRoute). - Get(healthendpoint.PROMETHEUS_PATH). + Get(healthendpoint.PrometheusPath). Expect(t). Status(http.StatusOK). Header("Content-Type", "text/plain; version=0.0.4; charset=utf-8"). @@ -136,22 +136,22 @@ var _ = Describe("Health Readiness", func() { }) }) When("/health/liveness endpoint is called", func() { - It("should response OK", func(){ + It("should response OK", func() { apitest.New().Debug(). - Handler(healthRoute). - Get(healthendpoint.LIVELINESS_PATH). - Expect(t). - Status(http.StatusOK). - Header("Content-Type", "application/json"). - Body(`{"overall_status" : "UP", "checks" : [] }`). - End() + Handler(healthRoute). + Get(healthendpoint.LivenessPath). + Expect(t). + Status(http.StatusOK). + Header("Content-Type", "application/json"). + Body(`{"overall_status" : "UP", "checks" : [] }`). + End() }) }) When("/health/readiness endpoint is called", func() { It("should response OK", func() { apitest.New().Debug(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(healthendpoint.ReadinessPath). Expect(t). Status(http.StatusOK). Header("Content-Type", "application/json"). @@ -164,7 +164,7 @@ var _ = Describe("Health Readiness", func() { It("should respond with 404", func() { apitest.New().Debug(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(healthendpoint.ReadinessPath). Expect(t). Status(http.StatusNotFound). End() @@ -177,7 +177,7 @@ var _ = Describe("Health Readiness", func() { It("should return 401", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(healthendpoint.ReadinessPath). Expect(t). Status(http.StatusUnauthorized). End() @@ -188,7 +188,7 @@ var _ = Describe("Health Readiness", func() { It("should have json response", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(healthendpoint.ReadinessPath). BasicAuth("test-user-name", "test-user-password"). Expect(t). Status(http.StatusOK). @@ -208,7 +208,7 @@ var _ = Describe("Health Readiness", func() { It("should have database check passing", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(healthendpoint.ReadinessPath). BasicAuth("test-user-name", "test-user-password"). Expect(t). Status(http.StatusOK). @@ -222,7 +222,7 @@ var _ = Describe("Health Readiness", func() { It("should cache health result", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(healthendpoint.ReadinessPath). BasicAuth("test-user-name", "test-user-password"). Expect(t). Status(http.StatusOK). @@ -232,7 +232,7 @@ var _ = Describe("Health Readiness", func() { timesetter = &(tmsttr) apitest.New(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(healthendpoint.ReadinessPath). BasicAuth("test-user-name", "test-user-password"). Expect(t). Status(http.StatusOK). @@ -242,7 +242,7 @@ var _ = Describe("Health Readiness", func() { It("should expire the cache entry after 30 seconds", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(healthendpoint.ReadinessPath). BasicAuth("test-user-name", "test-user-password"). Expect(t). Status(http.StatusOK). @@ -252,7 +252,7 @@ var _ = Describe("Health Readiness", func() { timesetter = &(tmsttr) apitest.New(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(healthendpoint.ReadinessPath). BasicAuth("test-user-name", "test-user-password"). Expect(t). Status(http.StatusOK). @@ -269,7 +269,7 @@ var _ = Describe("Health Readiness", func() { It("should respond with 404", func() { apitest.New().Debug(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(healthendpoint.ReadinessPath). Expect(t). Status(http.StatusNotFound). End() @@ -288,7 +288,7 @@ var _ = Describe("Health Readiness", func() { It("should have overall status down", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(healthendpoint.ReadinessPath). BasicAuth("test-user-name", "test-user-password"). Expect(t). Status(http.StatusOK). @@ -323,7 +323,7 @@ var _ = Describe("Health Readiness", func() { mu.RLock() apitest.New(). Handler(healthRoute). - Get(healthendpoint.READINESS_PATH). + Get(healthendpoint.ReadinessPath). BasicAuth("test-user-name", "test-user-password"). Expect(t). Status(http.StatusOK). @@ -341,7 +341,7 @@ var _ = Describe("Health Readiness", func() { It("should return 401", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.LIVELINESS_PATH). + Get(healthendpoint.LivenessPath). Expect(t). Status(http.StatusUnauthorized). End() @@ -352,7 +352,7 @@ var _ = Describe("Health Readiness", func() { It("should have json response", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.LIVELINESS_PATH). + Get(healthendpoint.LivenessPath). BasicAuth("test-user-name", "test-user-password"). Expect(t). Status(http.StatusOK). @@ -367,7 +367,7 @@ var _ = Describe("Health Readiness", func() { It("should require basic auth", func() { apitest.New(). Handler(healthRoute). - Get(healthendpoint.PROMETHEUS_PATH). + Get(healthendpoint.PrometheusPath). Expect(t). Status(http.StatusUnauthorized). End() @@ -402,8 +402,8 @@ var _ = Describe("Health Readiness", func() { BeforeEach(func() { config.HealthCheckUsername = "" config.HealthCheckPassword = "" - config.UnprotectedEndpoints = []string{"", healthendpoint.LIVELINESS_PATH, - healthendpoint.READINESS_PATH, healthendpoint.PROMETHEUS_PATH, healthendpoint.PPROF_PATH} + config.UnprotectedEndpoints = []string{"", healthendpoint.LivenessPath, + healthendpoint.ReadinessPath, healthendpoint.PrometheusPath, healthendpoint.PprofPath} }) It("should work", func() { apitest.New(). diff --git a/src/autoscaler/healthendpoint/server.go b/src/autoscaler/healthendpoint/server.go index f412a3262f..59c4bdb0d6 100644 --- a/src/autoscaler/healthendpoint/server.go +++ b/src/autoscaler/healthendpoint/server.go @@ -22,10 +22,10 @@ import ( ) const ( - LIVELINESS_PATH string = "/health/liveliness" - READINESS_PATH string = "/health/readiness" - PPROF_PATH string = "/debug/pprof" - PROMETHEUS_PATH string = "/health/prometheus" + LivenessPath string = "/health/liveness" + ReadinessPath string = "/health/readiness" + PprofPath string = "/debug/pprof" + PrometheusPath string = "/health/prometheus" ) // basic authentication credentials struct @@ -114,12 +114,12 @@ func addLivelinessHandlers(conf models.HealthConfig, mainRouter *mux.Router, tim authMiddleware *basicAuthenticationMiddleware) error { livenessHandler := common.VarsFunc(readiness([]Checker{}, time)) - livenessRouter := mainRouter.PathPrefix(LIVELINESS_PATH).Subrouter() + livenessRouter := mainRouter.PathPrefix(LivenessPath).Subrouter() - if endpointsNeedsProtection(LIVELINESS_PATH, conf) { + if endpointsNeedsProtection(LivenessPath, conf) { if !conf.BasicAuthPossible() { msg := "Basic authentication required for endpoint %s, but credentials not set up properly." - return fmt.Errorf(msg, LIVELINESS_PATH) + return fmt.Errorf(msg, LivenessPath) } livenessRouter.Use(authMiddleware.middleware) } @@ -137,11 +137,11 @@ func addReadinessHandler(conf models.HealthConfig, mainRouter *mux.Router, ) error { readinessRouter := mainRouter.PathPrefix("/health").Subrouter() - if endpointsNeedsProtection(READINESS_PATH, conf) { + if endpointsNeedsProtection(ReadinessPath, conf) { readinessRouter.Use(authMiddleware.middleware) if !conf.BasicAuthPossible() { msg := "Basic authentication required for endpoint %s, but credentials not set up properly." - return fmt.Errorf(msg, READINESS_PATH) + return fmt.Errorf(msg, ReadinessPath) } } // unauthenticated route @@ -157,12 +157,12 @@ func addReadinessHandler(conf models.HealthConfig, mainRouter *mux.Router, func addPprofHandlers(conf models.HealthConfig, mainRouter *mux.Router, authMiddleware *basicAuthenticationMiddleware) error { - pprofRouter := mainRouter.PathPrefix(PPROF_PATH).Subrouter() + pprofRouter := mainRouter.PathPrefix(PprofPath).Subrouter() - if endpointsNeedsProtection(PPROF_PATH, conf) { + if endpointsNeedsProtection(PprofPath, conf) { if !conf.BasicAuthPossible() { msg := "Basic authentication required for endpoint %s, but credentials not set up properly." - return fmt.Errorf(msg, PPROF_PATH) + return fmt.Errorf(msg, PprofPath) } pprofRouter.Use(authMiddleware.middleware) } @@ -185,12 +185,12 @@ func addPrometheusHandler(mainRouter *mux.Router, conf models.HealthConfig, promHandler := promhttp.HandlerFor(gatherer, promhttp.HandlerOpts{}) - prometheusRouter := mainRouter.PathPrefix(PROMETHEUS_PATH).Subrouter() - if endpointsNeedsProtection(PROMETHEUS_PATH, conf) { + prometheusRouter := mainRouter.PathPrefix(PrometheusPath).Subrouter() + if endpointsNeedsProtection(PrometheusPath, conf) { prometheusRouter.Use(authMiddleware.middleware) if !conf.BasicAuthPossible() { msg := "Basic authentication required for endpoint %s, but credentials not set up properly." - return fmt.Errorf(msg, PROMETHEUS_PATH) + return fmt.Errorf(msg, PrometheusPath) } } // unauthenticated routes