Skip to content

Commit

Permalink
Merge branch 'master' into runtime_15_reg_service
Browse files Browse the repository at this point in the history
  • Loading branch information
ranakan19 authored Nov 11, 2024
2 parents dea8ed2 + 738daee commit 0313cc2
Show file tree
Hide file tree
Showing 34 changed files with 300 additions and 959 deletions.
8 changes: 5 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/codeready-toolchain/registration-service/pkg/auth"
"github.com/codeready-toolchain/registration-service/pkg/configuration"
"github.com/codeready-toolchain/registration-service/pkg/log"
"github.com/codeready-toolchain/registration-service/pkg/namespaced"
"github.com/codeready-toolchain/registration-service/pkg/proxy"
"github.com/codeready-toolchain/registration-service/pkg/proxy/metrics"
"github.com/codeready-toolchain/registration-service/pkg/server"
Expand Down Expand Up @@ -83,8 +84,9 @@ func main() {
panic(fmt.Sprintf("cannot set captcha credentials: %s", err.Error()))
}
}
nsClient := namespaced.NewClient(cl, configuration.Namespace())

app := server.NewInClusterApplication(cl, configuration.Namespace())
app := server.NewInClusterApplication(nsClient)
// Initialize toolchain cluster cache service
// let's cache the member clusters before we start the services,
// this will speed up the first request
Expand All @@ -106,7 +108,7 @@ func main() {
proxyMetrics := metrics.NewProxyMetrics(proxyRegistry)
proxyMetricsSrv := proxy.StartMetricsServer(proxyRegistry, proxy.ProxyMetricsPort)
// Proxy API server
p, err := proxy.NewProxy(app, proxyMetrics, cluster.GetMemberClusters)
p, err := proxy.NewProxy(nsClient, app, proxyMetrics, cluster.GetMemberClusters)
if err != nil {
panic(errs.Wrap(err, "failed to create proxy"))
}
Expand All @@ -118,7 +120,7 @@ func main() {
regsvcRegistry := prometheus.NewRegistry()
regsvcMetricsSrv, _ := server.StartMetricsServer(regsvcRegistry, server.RegSvcMetricsPort)
regsvcSrv := server.New(app)
err = regsvcSrv.SetupRoutes(proxy.DefaultPort, regsvcRegistry)
err = regsvcSrv.SetupRoutes(proxy.DefaultPort, regsvcRegistry, nsClient)
if err != nil {
panic(err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.20

require (
github.com/aws/aws-sdk-go v1.44.100
github.com/codeready-toolchain/api v0.0.0-20240927104325-b5bfcb3cb1b0
github.com/codeready-toolchain/toolchain-common v0.0.0-20240905135929-d55d86fdd41e
github.com/codeready-toolchain/api v0.0.0-20241009095520-331aa861d43b
github.com/codeready-toolchain/toolchain-common v0.0.0-20241003135627-55e81430602b
github.com/go-logr/logr v1.4.1
github.com/gofrs/uuid v4.2.0+incompatible
github.com/pkg/errors v0.9.1
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWH
github.com/cockroachdb/datadriven v0.0.0-20200714090401-bf6692d28da5/go.mod h1:h6jFvWxBdQXxjopDMZyH2UVceIRfR84bdzbkoKrsWNo=
github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoCr5oaCLELYA=
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
github.com/codeready-toolchain/api v0.0.0-20241009095520-331aa861d43b h1:E0x1LM2BkiS7qm6mAJdaiaWg66bjkOLinL4Dca2tvAA=
github.com/codeready-toolchain/api v0.0.0-20241009095520-331aa861d43b/go.mod h1:ie9p4LenCCS0LsnbWp6/xwpFDdCWYE0KWzUO6Sk1g0E=
github.com/codeready-toolchain/toolchain-common v0.0.0-20241003135627-55e81430602b h1:kPp88xZsAGAo/kF1LV6L1PtM3g7YSY7DNdaLSta4+80=
github.com/codeready-toolchain/toolchain-common v0.0.0-20241003135627-55e81430602b/go.mod h1:kENp9EMqJaoZNvM3BLTk/i+CEteHKrJRAAm0H7L8Z+A=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
Expand Down
5 changes: 0 additions & 5 deletions pkg/application/service/factory/service_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/codeready-toolchain/registration-service/pkg/application/service"
servicecontext "github.com/codeready-toolchain/registration-service/pkg/application/service/context"
"github.com/codeready-toolchain/registration-service/pkg/configuration"
informerservice "github.com/codeready-toolchain/registration-service/pkg/informers/service"
"github.com/codeready-toolchain/registration-service/pkg/log"
"github.com/codeready-toolchain/registration-service/pkg/namespaced"
clusterservice "github.com/codeready-toolchain/registration-service/pkg/proxy/service"
Expand Down Expand Up @@ -52,10 +51,6 @@ func (s *ServiceFactory) defaultServiceContextProducer() servicecontext.ServiceC
}
}

func (s *ServiceFactory) InformerService() service.InformerService {
return informerservice.NewInformerService(s.getContext().Client(), configuration.Namespace())
}

func (s *ServiceFactory) MemberClusterService() service.MemberClusterService {
return clusterservice.NewMemberClusterService(s.getContext().Client(), s.getContext().Services().SignupService())
}
Expand Down
18 changes: 1 addition & 17 deletions pkg/application/service/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,15 @@ import (
"github.com/codeready-toolchain/registration-service/pkg/proxy/access"
"github.com/codeready-toolchain/registration-service/pkg/signup"
"github.com/gin-gonic/gin"
"k8s.io/apimachinery/pkg/labels"
)

type InformerService interface {
GetMasterUserRecord(name string) (*toolchainv1alpha1.MasterUserRecord, error)
GetSpace(name string) (*toolchainv1alpha1.Space, error)
ListSpaceBindings(reqs ...labels.Requirement) ([]toolchainv1alpha1.SpaceBinding, error)
GetNSTemplateTier(name string) (*toolchainv1alpha1.NSTemplateTier, error)
ListBannedUsersByEmail(email string) ([]toolchainv1alpha1.BannedUser, error)
}

type SignupService interface {
Signup(ctx *gin.Context) (*toolchainv1alpha1.UserSignup, error)
GetSignup(ctx *gin.Context, userID, username string) (*signup.Signup, error)
GetSignupFromInformer(ctx *gin.Context, userID, username string, checkUserSignupCompleted bool) (*signup.Signup, error)
GetSignup(ctx *gin.Context, userID, username string, checkUserSignupCompleted bool) (*signup.Signup, error)
GetUserSignupFromIdentifier(userID, username string) (*toolchainv1alpha1.UserSignup, error)
UpdateUserSignup(userSignup *toolchainv1alpha1.UserSignup) (*toolchainv1alpha1.UserSignup, error)
PhoneNumberAlreadyInUse(userID, username, phoneNumberOrHash string) error
}

type SocialEventService interface {
GetEvent(code string) (*toolchainv1alpha1.SocialEvent, error)
}

type VerificationService interface {
InitVerification(ctx *gin.Context, userID, username, e164PhoneNumber, countryCode string) error
VerifyPhoneCode(ctx *gin.Context, userID, username, code string) error
Expand All @@ -40,7 +25,6 @@ type MemberClusterService interface {
}

type Services interface {
InformerService() InformerService
SignupService() SignupService
VerificationService() VerificationService
MemberClusterService() MemberClusterService
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/signup.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (s *Signup) GetHandler(ctx *gin.Context) {
// Get the UserSignup resource from the service by the userID
userID := ctx.GetString(context.SubKey)
username := ctx.GetString(context.UsernameKey)
signupResource, err := s.app.SignupService().GetSignup(ctx, userID, username)
signupResource, err := s.app.SignupService().GetSignup(ctx, userID, username, true)
if err != nil {
log.Error(ctx, err, "error getting UserSignup resource")
crterrors.AbortWithError(ctx, http.StatusInternalServerError, err, "error getting UserSignup resource")
Expand Down
25 changes: 6 additions & 19 deletions pkg/controller/signup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (s *TestSignupSuite) TestSignupGetHandler() {
Reason: "Provisioning",
},
}
svc.MockGetSignup = func(_ *gin.Context, id, _ string) (*signup.Signup, error) {
svc.MockGetSignup = func(_ *gin.Context, id, _ string, _ bool) (*signup.Signup, error) {
if id == userID {
return expected, nil
}
Expand All @@ -234,7 +234,7 @@ func (s *TestSignupSuite) TestSignupGetHandler() {
ctx.Request = req
ctx.Set(context.SubKey, userID)

svc.MockGetSignup = func(_ *gin.Context, _, _ string) (*signup.Signup, error) {
svc.MockGetSignup = func(_ *gin.Context, _, _ string, _ bool) (*signup.Signup, error) {
return nil, nil
}

Expand All @@ -251,7 +251,7 @@ func (s *TestSignupSuite) TestSignupGetHandler() {
ctx.Request = req
ctx.Set(context.SubKey, userID)

svc.MockGetSignup = func(_ *gin.Context, _, _ string) (*signup.Signup, error) {
svc.MockGetSignup = func(_ *gin.Context, _, _ string, _ bool) (*signup.Signup, error) {
return nil, errors.New("oopsie woopsie")
}

Expand Down Expand Up @@ -439,9 +439,6 @@ func (s *TestSignupSuite) TestInitVerificationHandler() {
states.SetVerificationRequired(&us, true)
return &us, nil
},
MockUpdateUserSignup: func(userSignup *crtapi.UserSignup) (userSignup2 *crtapi.UserSignup, e error) {
return userSignup, nil
},
MockPhoneNumberAlreadyInUse: func(_, _, _ string) error {
return nil
},
Expand Down Expand Up @@ -863,20 +860,14 @@ func initActivationCodeVerification(t *testing.T, handler gin.HandlerFunc, usern
}

type FakeSignupService struct {
MockGetSignup func(ctx *gin.Context, userID, username string) (*signup.Signup, error)
MockGetSignupFromInformer func(ctx *gin.Context, userID, username string, checkUserSignupComplete bool) (*signup.Signup, error)
MockGetSignup func(ctx *gin.Context, userID, username string, checkUserSignupComplete bool) (*signup.Signup, error)
MockSignup func(ctx *gin.Context) (*crtapi.UserSignup, error)
MockGetUserSignupFromIdentifier func(userID, username string) (*crtapi.UserSignup, error)
MockUpdateUserSignup func(userSignup *crtapi.UserSignup) (*crtapi.UserSignup, error)
MockPhoneNumberAlreadyInUse func(userID, username, value string) error
}

func (m *FakeSignupService) GetSignup(ctx *gin.Context, userID, username string) (*signup.Signup, error) {
return m.MockGetSignup(ctx, userID, username)
}

func (m *FakeSignupService) GetSignupFromInformer(ctx *gin.Context, userID, username string, checkUserSignupComplete bool) (*signup.Signup, error) {
return m.MockGetSignupFromInformer(ctx, userID, username, checkUserSignupComplete)
func (m *FakeSignupService) GetSignup(ctx *gin.Context, userID, username string, checkUserSignupComplete bool) (*signup.Signup, error) {
return m.MockGetSignup(ctx, userID, username, checkUserSignupComplete)
}

func (m *FakeSignupService) Signup(ctx *gin.Context) (*crtapi.UserSignup, error) {
Expand All @@ -887,10 +878,6 @@ func (m *FakeSignupService) GetUserSignupFromIdentifier(userID, username string)
return m.MockGetUserSignupFromIdentifier(userID, username)
}

func (m *FakeSignupService) UpdateUserSignup(userSignup *crtapi.UserSignup) (*crtapi.UserSignup, error) {
return m.MockUpdateUserSignup(userSignup)
}

func (m *FakeSignupService) PhoneNumberAlreadyInUse(userID, username, e164phoneNumber string) error {
return m.MockPhoneNumberAlreadyInUse(userID, username, e164phoneNumber)
}
12 changes: 7 additions & 5 deletions pkg/controller/usernames.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ package controller
import (
"net/http"

"github.com/codeready-toolchain/registration-service/pkg/application"
toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
crterrors "github.com/codeready-toolchain/registration-service/pkg/errors"
"github.com/codeready-toolchain/registration-service/pkg/log"
"github.com/codeready-toolchain/registration-service/pkg/namespaced"
"github.com/codeready-toolchain/registration-service/pkg/username"
"github.com/gin-gonic/gin"
"k8s.io/apimachinery/pkg/api/errors"
)

// Usernames implements the usernames endpoint, which is invoked for checking if a given username/email exists.
type Usernames struct {
app application.Application
namespaced.Client
}

// NewUsernames returns a new Usernames instance.
func NewUsernames(app application.Application) *Usernames {
func NewUsernames(nsClient namespaced.Client) *Usernames {
return &Usernames{
app: app,
Client: nsClient,
}
}

Expand All @@ -34,7 +35,8 @@ func (s *Usernames) GetHandler(ctx *gin.Context) {
// TODO check if the queryString is an email
// in that case we have to fetch the UserSignup resources with the provided email and the MasterUserRecords associated with those.

murResource, err := s.app.InformerService().GetMasterUserRecord(queryString)
murResource := &toolchainv1alpha1.MasterUserRecord{}
err := s.Get(ctx.Request.Context(), s.NamespacedName(queryString), murResource)
// handle not found error
if errors.IsNotFound(err) {
log.Infof(ctx, "MasterUserRecord resource for: %s not found", queryString)
Expand Down
12 changes: 5 additions & 7 deletions pkg/controller/usernames_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"

"github.com/codeready-toolchain/registration-service/pkg/controller"
"github.com/codeready-toolchain/registration-service/pkg/informers/service"
"github.com/codeready-toolchain/registration-service/pkg/namespaced"
"github.com/codeready-toolchain/registration-service/pkg/username"
"github.com/codeready-toolchain/registration-service/test"
"github.com/codeready-toolchain/registration-service/test/fake"
Expand Down Expand Up @@ -40,11 +40,10 @@ func (s *TestUsernamesSuite) TestUsernamesGetHandler() {

s.Run("success", func() {

fakeInformer := service.NewInformerService(fakeClient, commontest.HostOperatorNs)
s.Application.MockInformerService(fakeInformer)
nsClient := namespaced.NewClient(fakeClient, commontest.HostOperatorNs)

// Create Usernames controller instance.
ctrl := controller.NewUsernames(s.Application)
ctrl := controller.NewUsernames(nsClient)
handler := gin.HandlerFunc(ctrl.GetHandler)

s.Run("usernames found", func() {
Expand Down Expand Up @@ -104,11 +103,10 @@ func (s *TestUsernamesSuite) TestUsernamesGetHandler() {
fakeClient.MockGet = func(_ context.Context, _ client.ObjectKey, _ client.Object, _ ...client.GetOption) error {
return fmt.Errorf("mock error")
}
fakeInformer := service.NewInformerService(fakeClient, commontest.HostOperatorNs)
s.Application.MockInformerService(fakeInformer)
nsClient := namespaced.NewClient(fakeClient, commontest.HostOperatorNs)

// Create Usernames controller instance.
ctrl := controller.NewUsernames(s.Application)
ctrl := controller.NewUsernames(nsClient)
handler := gin.HandlerFunc(ctrl.GetHandler)
s.Run("unable to get mur", func() {
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
Expand Down
76 changes: 0 additions & 76 deletions pkg/informers/service/informer_service.go

This file was deleted.

Loading

0 comments on commit 0313cc2

Please sign in to comment.