Skip to content

feat: add new uiconfig route #530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
4 changes: 4 additions & 0 deletions pkg/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@
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)

Check warning on line 147 in pkg/configuration/configuration.go

View check run for this annotation

Codecov / codecov/patch

pkg/configuration/configuration.go#L146-L147

Added lines #L146 - L147 were not covered by tests
}

type AnalyticsConfig struct {
c toolchainv1alpha1.RegistrationServiceAnalyticsConfig
}
Expand Down
33 changes: 33 additions & 0 deletions pkg/controller/uiconfig.go
Original file line number Diff line number Diff line change
@@ -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)
}
67 changes: 67 additions & 0 deletions pkg/controller/uiconfig_test.go
Original file line number Diff line number Diff line change
@@ -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")
})
})
}
2 changes: 2 additions & 0 deletions pkg/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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() {
Expand Down
Loading