From ec1ce60ba9ceb9b7390aaf8b0ff0286ea48a1e0e Mon Sep 17 00:00:00 2001 From: Devtools Date: Mon, 5 May 2025 15:27:14 +0200 Subject: [PATCH 1/2] add uiconfig route --- go.mod | 2 + go.sum | 4 +- pkg/configuration/configuration.go | 4 ++ pkg/controller/uiconfig.go | 33 +++++++++++++++ pkg/controller/uiconfig_test.go | 67 ++++++++++++++++++++++++++++++ pkg/server/routes.go | 2 + 6 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 pkg/controller/uiconfig.go create mode 100644 pkg/controller/uiconfig_test.go diff --git a/go.mod b/go.mod index 6ddedcc6..8ac97b9d 100644 --- a/go.mod +++ b/go.mod @@ -174,3 +174,5 @@ require ( sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect sigs.k8s.io/yaml v1.4.0 // indirect ) + +replace github.com/codeready-toolchain/api => github.com/mfrancisc/api v0.0.0-20250505125344-2add993deb95 diff --git a/go.sum b/go.sum index dcae6933..4de4d524 100644 --- a/go.sum +++ b/go.sum @@ -49,8 +49,6 @@ github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtM github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU= github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= -github.com/codeready-toolchain/api v0.0.0-20250313170542-4e3c4147cb80 h1:wh41dZ7VkyClQWiMJbMypku6xTxtxNZTgQJUAcodgUo= -github.com/codeready-toolchain/api v0.0.0-20250313170542-4e3c4147cb80/go.mod h1:shTTQ+bYWinbdF/oK+ly4DO5jdXtIASZ+uPZElsmsKg= github.com/codeready-toolchain/toolchain-common v0.0.0-20250313203311-0bce6563576f h1:qBUZ/xBNgDMf5Y0tObJ85/9GcDSgtUf8WFfCrNSxaZ4= github.com/codeready-toolchain/toolchain-common v0.0.0-20250313203311-0bce6563576f/go.mod h1:0dopWh1MWi9eg2d5RAd4uSUmXfDMqz4Yk2oNPHF15+Y= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= @@ -263,6 +261,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg= github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k= +github.com/mfrancisc/api v0.0.0-20250505125344-2add993deb95 h1:7wYZgMB8leE6s+j69HGcaQ6TAFWrxiSYDsfqgbteWYU= +github.com/mfrancisc/api v0.0.0-20250505125344-2add993deb95/go.mod h1:shTTQ+bYWinbdF/oK+ly4DO5jdXtIASZ+uPZElsmsKg= github.com/migueleliasweb/go-github-mock v0.0.18 h1:0lWt9MYmZQGnQE2rFtjlft/YtD6hzxuN6JJRFpujzEI= github.com/migueleliasweb/go-github-mock v0.0.18/go.mod h1:CcgXcbMoRnf3rRVHqGssuBquZDIcaplxL2W6G+xs7kM= github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ= diff --git a/pkg/configuration/configuration.go b/pkg/configuration/configuration.go index 0460fdb7..704befca 100644 --- a/pkg/configuration/configuration.go +++ b/pkg/configuration/configuration.go @@ -143,6 +143,10 @@ func (r RegistrationServiceConfig) Verification() VerificationConfig { return VerificationConfig{c: r.cfg.Host.RegistrationService.Verification, secrets: r.secrets} } +func (r RegistrationServiceConfig) UICanaryDeploymentWeight() int { + return commonconfig.GetInt(r.cfg.Host.RegistrationService.UICanaryDeploymentWeight, 20) +} + type AnalyticsConfig struct { c toolchainv1alpha1.RegistrationServiceAnalyticsConfig } diff --git a/pkg/controller/uiconfig.go b/pkg/controller/uiconfig.go new file mode 100644 index 00000000..14d420ce --- /dev/null +++ b/pkg/controller/uiconfig.go @@ -0,0 +1,33 @@ +package controller + +import ( + "net/http" + + "github.com/codeready-toolchain/registration-service/pkg/configuration" + "github.com/gin-gonic/gin" +) + +type UIConfigResponse struct { + // Holds to weight specifying up to how many users ( in percentage ) should use the new UI. + // NOTE: this is a temporary parameter, it will be removed once we switch all the users to the new UI. + UICanaryDeploymentWeight int `json:"uiCanaryDeploymentWeight"` +} + +// UIConfig implements the ui config endpoint, which is invoked to +// retrieve the config for the ui. +type UIConfig struct { +} + +// NewAuthConfig returns a new AuthConfig instance. +func NewUIConfig() *UIConfig { + return &UIConfig{} +} + +// GetHandler returns raw auth config content for UI. +func (uic *UIConfig) GetHandler(ctx *gin.Context) { + cfg := configuration.GetRegistrationServiceConfig() + configRespData := UIConfigResponse{ + UICanaryDeploymentWeight: cfg.UICanaryDeploymentWeight(), + } + ctx.JSON(http.StatusOK, configRespData) +} diff --git a/pkg/controller/uiconfig_test.go b/pkg/controller/uiconfig_test.go new file mode 100644 index 00000000..a6f7c720 --- /dev/null +++ b/pkg/controller/uiconfig_test.go @@ -0,0 +1,67 @@ +package controller + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + testconfig "github.com/codeready-toolchain/toolchain-common/pkg/test/config" + + "github.com/codeready-toolchain/registration-service/pkg/configuration" + "github.com/codeready-toolchain/registration-service/test" + + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +type TestUIConfigSuite struct { + test.UnitTestSuite +} + +func TestRunUIConfigSuite(t *testing.T) { + suite.Run(t, &TestUIConfigSuite{test.UnitTestSuite{}}) +} + +func (s *TestUIConfigSuite) TestUIConfigHandler() { + // Create a request to pass to our handler. We don't have any query parameters for now, so we'll + // pass 'nil' as the third parameter. + req, err := http.NewRequest(http.MethodGet, "/api/v1/uiconfig", nil) + require.NoError(s.T(), err) + + // Check if the config is set to testing mode, so the handler may use this. + assert.True(s.T(), configuration.IsTestingMode(), "testing mode not set correctly to true") + s.OverrideApplicationDefault(testconfig.RegistrationService(). + RegistrationServiceURL("https://signup.domain.com")) + defer s.DefaultConfig() + cfg := configuration.GetRegistrationServiceConfig() + + // Create handler instance. + uiConfigCtrl := NewUIConfig() + handler := gin.HandlerFunc(uiConfigCtrl.GetHandler) + + s.Run("valid json config", func() { + + // We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response. + rr := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(rr) + ctx.Request = req + + handler(ctx) + + // Check the status code is what we expect. + require.Equal(s.T(), http.StatusOK, rr.Code) + + // Check the response body is what we expect. + // get config values from endpoint response + var data *UIConfigResponse + err = json.Unmarshal(rr.Body.Bytes(), &data) + require.NoError(s.T(), err) + + s.Run("uiCanaryDeploymentWeight", func() { + assert.Equal(s.T(), cfg.UICanaryDeploymentWeight(), data.UICanaryDeploymentWeight, "wrong 'UICanaryDeploymentWeight' in uiconfig response") + }) + }) +} diff --git a/pkg/server/routes.go b/pkg/server/routes.go index 07172148..67da2097 100644 --- a/pkg/server/routes.go +++ b/pkg/server/routes.go @@ -59,6 +59,7 @@ func (srv *RegistrationServer) SetupRoutes(proxyPort string, reg *prometheus.Reg analyticsCtrl := controller.NewAnalytics() signupCtrl := controller.NewSignup(srv.application) usernamesCtrl := controller.NewUsernames(nsClient) + uiConfigCtrl := controller.NewUIConfig() // unsecured routes unsecuredV1 := srv.router.Group("/api/v1") @@ -95,6 +96,7 @@ func (srv *RegistrationServer) SetupRoutes(proxyPort string, reg *prometheus.Reg securedV1.GET("/signup/verification/:code", signupCtrl.VerifyPhoneCodeHandler) // TODO: also provide a `POST /signup/verification/phone-code` +deprecate this one + migrate UI? securedV1.POST("/signup/verification/activation-code", signupCtrl.VerifyActivationCodeHandler) securedV1.GET("/usernames/:username", usernamesCtrl.GetHandler) + securedV1.GET("/uiconfig", uiConfigCtrl.GetHandler) // if we are in testing mode, we also add a secured health route for testing if configuration.IsTestingMode() { From 3bb3b536d310186febcdad7408550b7ddc9e0dad Mon Sep 17 00:00:00 2001 From: Devtools Date: Tue, 6 May 2025 21:19:30 +0200 Subject: [PATCH 2/2] update api --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index b210f831..98942f74 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ toolchain go1.22.12 require ( github.com/aws/aws-sdk-go v1.44.100 - github.com/codeready-toolchain/api v0.0.0-20250506092100-39b4862e1271 + github.com/codeready-toolchain/api v0.0.0-20250506183835-ee4a91d0bca4 github.com/codeready-toolchain/toolchain-common v0.0.0-20250506093954-2b65ad3a2e12 github.com/go-logr/logr v1.4.2 github.com/gofrs/uuid v4.2.0+incompatible diff --git a/go.sum b/go.sum index 4ddfb03d..9a3894ff 100644 --- a/go.sum +++ b/go.sum @@ -51,8 +51,8 @@ github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtM github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU= github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= -github.com/codeready-toolchain/api v0.0.0-20250506092100-39b4862e1271 h1:AHrFr/aPuJ4+0zHw4sFXcfMA92kChy12JAPS5bLODlw= -github.com/codeready-toolchain/api v0.0.0-20250506092100-39b4862e1271/go.mod h1:20258od6i5+jP406Z76YaI2ow/vc7URwsDU2bokpkRE= +github.com/codeready-toolchain/api v0.0.0-20250506183835-ee4a91d0bca4 h1:m2OsrXpH1MieIg65FQQl3EV0oAyqNfIAYhW2TQnOrWY= +github.com/codeready-toolchain/api v0.0.0-20250506183835-ee4a91d0bca4/go.mod h1:20258od6i5+jP406Z76YaI2ow/vc7URwsDU2bokpkRE= github.com/codeready-toolchain/toolchain-common v0.0.0-20250506093954-2b65ad3a2e12 h1:w54sojJJ8PsHZzK1mC+/EUBrQ9F2sC/k7JUVc8LSqK4= github.com/codeready-toolchain/toolchain-common v0.0.0-20250506093954-2b65ad3a2e12/go.mod h1:TrMvD0sP69wI6Rouzfs7OsOUSj4CGn/ZiIdiDBAFQjk= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=