diff --git a/pkg/informers/service/informer_service.go b/pkg/informers/service/informer_service.go index f7729b7d..58f879d6 100644 --- a/pkg/informers/service/informer_service.go +++ b/pkg/informers/service/informer_service.go @@ -5,7 +5,6 @@ import ( toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1" "github.com/codeready-toolchain/registration-service/pkg/application/service" - "github.com/codeready-toolchain/registration-service/pkg/kubeclient/resources" "github.com/codeready-toolchain/toolchain-common/pkg/hash" "k8s.io/apimachinery/pkg/labels" @@ -53,7 +52,7 @@ func (s *ServiceImpl) GetSpace(name string) (*toolchainv1alpha1.Space, error) { func (s *ServiceImpl) GetToolchainStatus() (*toolchainv1alpha1.ToolchainStatus, error) { status := &toolchainv1alpha1.ToolchainStatus{} - namespacedName := types.NamespacedName{Name: resources.ToolchainStatusName, Namespace: s.namespace} + namespacedName := types.NamespacedName{Name: "toolchain-status", Namespace: s.namespace} err := s.client.Get(context.TODO(), namespacedName, status) return status, err } diff --git a/pkg/kubeclient/banneduser.go b/pkg/kubeclient/banneduser.go index e7fd105b..e34733c2 100644 --- a/pkg/kubeclient/banneduser.go +++ b/pkg/kubeclient/banneduser.go @@ -2,18 +2,10 @@ package kubeclient import ( "context" - "fmt" crtapi "github.com/codeready-toolchain/api/api/v1alpha1" "github.com/codeready-toolchain/toolchain-common/pkg/hash" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime/schema" - "k8s.io/client-go/dynamic" -) - -const ( - bannedUserResourcePlural = "bannedusers" + "sigs.k8s.io/controller-runtime/pkg/client" ) type bannedUserClient struct { @@ -50,24 +42,10 @@ func (c *bannedUserClient) listByLabelForHashedValue(labelKey, valueToHash strin // listByLabel returns a BannedUserList containing any BannedUser resources that have a label matching the specified label func (c *bannedUserClient) listByLabel(labelKey, labelValue string) (*crtapi.BannedUserList, error) { - - intf, err := dynamic.NewForConfig(&c.cfg) - if err != nil { + bannedUsers := &crtapi.BannedUserList{} + if err := c.client.List(context.TODO(), bannedUsers, client.InNamespace(c.ns), + client.MatchingLabels{labelKey: labelValue}); err != nil { return nil, err } - - r := schema.GroupVersionResource{Group: "toolchain.dev.openshift.com", Version: "v1alpha1", Resource: bannedUserResourcePlural} - listOptions := metav1.ListOptions{ - LabelSelector: fmt.Sprintf("%s=%s", labelKey, labelValue), - } - - list, err := intf.Resource(r).Namespace(c.ns).List(context.TODO(), listOptions) - if err != nil { - return nil, err - } - - result := &crtapi.BannedUserList{} - - err = c.crtClient.scheme.Convert(list, result, nil) - return result, err + return bannedUsers, nil } diff --git a/pkg/kubeclient/client.go b/pkg/kubeclient/client.go index 48d47217..eb6f345d 100644 --- a/pkg/kubeclient/client.go +++ b/pkg/kubeclient/client.go @@ -5,8 +5,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/runtime/serializer" - "k8s.io/client-go/rest" ) type CRTClient interface { @@ -24,62 +22,28 @@ type V1Alpha1 interface { } // NewCRTRESTClient creates a new REST client for managing Codeready Toolchain resources via the Kubernetes API -func NewCRTRESTClient(cfg *rest.Config, client client.Client, namespace string) (CRTClient, error) { +func NewCRTRESTClient(client client.Client, namespace string) (CRTClient, error) { scheme := runtime.NewScheme() err := crtapi.SchemeBuilder.AddToScheme(scheme) if err != nil { return nil, err } - crtapi.SchemeBuilder.Register(getRegisterObject()...) - - config := *cfg - config.GroupVersion = &crtapi.GroupVersion - config.APIPath = "/apis" - config.ContentType = runtime.ContentTypeJSON - config.NegotiatedSerializer = serializer.WithoutConversionCodecFactory{CodecFactory: serializer.NewCodecFactory(scheme)} - config.UserAgent = rest.DefaultKubernetesUserAgent() - - restClient, err := rest.RESTClientFor(&config) - if err != nil { - return nil, err - } crtRESTClient := &CRTRESTClient{ - RestClient: restClient, - Client: client, - Config: config, - NS: namespace, - Scheme: scheme, + Client: client, + NS: namespace, + Scheme: scheme, } crtRESTClient.v1Alpha1 = &V1Alpha1REST{client: crtRESTClient} return crtRESTClient, nil } -func getRegisterObject() []runtime.Object { - return []runtime.Object{ - &crtapi.UserSignup{}, - &crtapi.UserSignupList{}, - &crtapi.MasterUserRecord{}, - &crtapi.MasterUserRecordList{}, - &crtapi.BannedUser{}, - &crtapi.BannedUserList{}, - &crtapi.ToolchainStatus{}, - &crtapi.ToolchainStatusList{}, - &crtapi.Space{}, - &crtapi.SpaceList{}, - &crtapi.SpaceBinding{}, - &crtapi.SpaceBindingList{}, - } -} - type CRTRESTClient struct { - RestClient rest.Interface - Client client.Client - NS string - Config rest.Config - Scheme *runtime.Scheme - v1Alpha1 *V1Alpha1REST + Client client.Client + NS string + Scheme *runtime.Scheme + v1Alpha1 *V1Alpha1REST } func (c *CRTRESTClient) V1Alpha1() V1Alpha1 { @@ -94,11 +58,9 @@ type V1Alpha1REST struct { func (c *V1Alpha1REST) UserSignups() UserSignupInterface { return &userSignupClient{ crtClient: crtClient{ - restClient: c.client.RestClient, - client: c.client.Client, - ns: c.client.NS, - cfg: c.client.Config, - scheme: c.client.Scheme, + client: c.client.Client, + ns: c.client.NS, + scheme: c.client.Scheme, }, } } @@ -107,11 +69,9 @@ func (c *V1Alpha1REST) UserSignups() UserSignupInterface { func (c *V1Alpha1REST) MasterUserRecords() MasterUserRecordInterface { return &masterUserRecordClient{ crtClient: crtClient{ - restClient: c.client.RestClient, - client: c.client.Client, - ns: c.client.NS, - cfg: c.client.Config, - scheme: c.client.Scheme, + client: c.client.Client, + ns: c.client.NS, + scheme: c.client.Scheme, }, } } @@ -120,11 +80,9 @@ func (c *V1Alpha1REST) MasterUserRecords() MasterUserRecordInterface { func (c *V1Alpha1REST) BannedUsers() BannedUserInterface { return &bannedUserClient{ crtClient: crtClient{ - restClient: c.client.RestClient, - client: c.client.Client, - ns: c.client.NS, - cfg: c.client.Config, - scheme: c.client.Scheme, + client: c.client.Client, + ns: c.client.NS, + scheme: c.client.Scheme, }, } } @@ -133,11 +91,9 @@ func (c *V1Alpha1REST) BannedUsers() BannedUserInterface { func (c *V1Alpha1REST) ToolchainStatuses() ToolchainStatusInterface { return &toolchainStatusClient{ crtClient: crtClient{ - restClient: c.client.RestClient, - client: c.client.Client, - ns: c.client.NS, - cfg: c.client.Config, - scheme: c.client.Scheme, + client: c.client.Client, + ns: c.client.NS, + scheme: c.client.Scheme, }, } } @@ -146,11 +102,9 @@ func (c *V1Alpha1REST) ToolchainStatuses() ToolchainStatusInterface { func (c *V1Alpha1REST) SocialEvents() SocialEventInterface { return &socialeventClient{ crtClient: crtClient{ - restClient: c.client.RestClient, - client: c.client.Client, - ns: c.client.NS, - cfg: c.client.Config, - scheme: c.client.Scheme, + client: c.client.Client, + ns: c.client.NS, + scheme: c.client.Scheme, }, } } @@ -159,11 +113,9 @@ func (c *V1Alpha1REST) SocialEvents() SocialEventInterface { func (c *V1Alpha1REST) Spaces() SpaceInterface { return &spaceClient{ crtClient: crtClient{ - restClient: c.client.RestClient, - client: c.client.Client, - ns: c.client.NS, - cfg: c.client.Config, - scheme: c.client.Scheme, + client: c.client.Client, + ns: c.client.NS, + scheme: c.client.Scheme, }, } } @@ -172,19 +124,15 @@ func (c *V1Alpha1REST) Spaces() SpaceInterface { func (c *V1Alpha1REST) SpaceBindings() SpaceBindingInterface { return &spaceBindingClient{ crtClient: crtClient{ - restClient: c.client.RestClient, - client: c.client.Client, - ns: c.client.NS, - cfg: c.client.Config, - scheme: c.client.Scheme, + client: c.client.Client, + ns: c.client.NS, + scheme: c.client.Scheme, }, } } type crtClient struct { - restClient rest.Interface - client client.Client - ns string - cfg rest.Config - scheme *runtime.Scheme + client client.Client + ns string + scheme *runtime.Scheme } diff --git a/pkg/kubeclient/client_test.go b/pkg/kubeclient/client_test.go index 26f93765..8528e5e3 100644 --- a/pkg/kubeclient/client_test.go +++ b/pkg/kubeclient/client_test.go @@ -4,8 +4,6 @@ import ( "testing" "github.com/codeready-toolchain/toolchain-common/pkg/test" - "k8s.io/client-go/rest" - "github.com/stretchr/testify/require" "github.com/codeready-toolchain/registration-service/pkg/kubeclient" @@ -17,7 +15,7 @@ const ( func TestNewClient(t *testing.T) { // Try creating a new client with an empty config - client, err := kubeclient.NewCRTRESTClient(&rest.Config{}, test.NewFakeClient(t), TestNamespace) + client, err := kubeclient.NewCRTRESTClient(test.NewFakeClient(t), TestNamespace) // Check that there are no errors, and the clients are returned require.NoError(t, err) diff --git a/pkg/kubeclient/mur.go b/pkg/kubeclient/mur.go index 21b34150..098b77af 100644 --- a/pkg/kubeclient/mur.go +++ b/pkg/kubeclient/mur.go @@ -4,7 +4,7 @@ import ( "context" crtapi "github.com/codeready-toolchain/api/api/v1alpha1" - "github.com/codeready-toolchain/registration-service/pkg/kubeclient/resources" + "k8s.io/apimachinery/pkg/types" ) type masterUserRecordClient struct { @@ -18,11 +18,8 @@ type MasterUserRecordInterface interface { // Get returns the MasterUserRecord with the specified name, or an error if something went wrong while attempting to retrieve it func (c *masterUserRecordClient) Get(name string) (*crtapi.MasterUserRecord, error) { result := &crtapi.MasterUserRecord{} - err := c.restClient.Get(). - Namespace(c.ns). - Resource(resources.MurResourcePlural). - Name(name). - Do(context.TODO()). - Into(result) - return result, err + if err := c.client.Get(context.TODO(), types.NamespacedName{Namespace: c.ns, Name: name}, result); err != nil { + return nil, err + } + return result, nil } diff --git a/pkg/kubeclient/resources/names.go b/pkg/kubeclient/resources/names.go deleted file mode 100644 index 7f0f3119..00000000 --- a/pkg/kubeclient/resources/names.go +++ /dev/null @@ -1,13 +0,0 @@ -package resources - -const ( - UserSignupResourcePlural = "usersignups" - MurResourcePlural = "masteruserrecords" - SpaceResourcePlural = "spaces" - SpaceBindingResourcePlural = "spacebindings" - ToolchainStatusPlural = "toolchainstatuses" - ToolchainStatusName = "toolchain-status" - ProxyPluginsPlural = "proxyplugins" - NSTemplateTierPlural = "nstemplatetiers" - BannedUserResourcePlural = "bannedusers" -) diff --git a/pkg/kubeclient/signup.go b/pkg/kubeclient/signup.go index 596f71fe..069f5959 100644 --- a/pkg/kubeclient/signup.go +++ b/pkg/kubeclient/signup.go @@ -5,10 +5,10 @@ import ( "regexp" crtapi "github.com/codeready-toolchain/api/api/v1alpha1" - "github.com/codeready-toolchain/registration-service/pkg/kubeclient/resources" "github.com/codeready-toolchain/toolchain-common/pkg/hash" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/selection" + "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -32,48 +32,28 @@ type UserSignupInterface interface { // If not found then NotFound error returned func (c *userSignupClient) Get(name string) (*crtapi.UserSignup, error) { result := &crtapi.UserSignup{} - err := c.restClient.Get(). - Namespace(c.ns). - Resource(resources.UserSignupResourcePlural). - Name(name). - Do(context.TODO()). - Into(result) - if err != nil { + if err := c.client.Get(context.TODO(), types.NamespacedName{Namespace: c.ns, Name: name}, result); err != nil { return nil, err } - return result, err + return result, nil } // Create creates a new UserSignup resource in the cluster, and returns the resulting UserSignup that was created, or // an error if something went wrong func (c *userSignupClient) Create(obj *crtapi.UserSignup) (*crtapi.UserSignup, error) { - result := &crtapi.UserSignup{} - err := c.restClient.Post(). - Namespace(c.ns). - Resource(resources.UserSignupResourcePlural). - Body(obj). - Do(context.TODO()). - Into(result) - if err != nil { + if err := c.client.Create(context.TODO(), obj); err != nil { return nil, err } - return result, err + return obj, nil } // Update will update an existing UserSignup resource in the cluster, returning an error if something went wrong func (c *userSignupClient) Update(obj *crtapi.UserSignup) (*crtapi.UserSignup, error) { - result := &crtapi.UserSignup{} - err := c.restClient.Put(). - Namespace(c.ns). - Resource(resources.UserSignupResourcePlural). - Name(obj.Name). - Body(obj). - Do(context.TODO()). - Into(result) + err := c.client.Update(context.TODO(), obj) if err != nil { return nil, err } - return result, nil + return obj, nil } // ListActiveSignupsByPhoneNumberOrHash will return a list of non-deactivated UserSignups that have a phone number hash diff --git a/pkg/kubeclient/socialevent.go b/pkg/kubeclient/socialevent.go index b0d75e6b..346a04f7 100644 --- a/pkg/kubeclient/socialevent.go +++ b/pkg/kubeclient/socialevent.go @@ -4,10 +4,7 @@ import ( "context" crtapi "github.com/codeready-toolchain/api/api/v1alpha1" -) - -const ( - socialeventResourcePlural = "socialevents" + "k8s.io/apimachinery/pkg/types" ) type socialeventClient struct { @@ -21,11 +18,8 @@ type SocialEventInterface interface { // Get returns the SocialEvent with the specified name, or an error if something went wrong while attempting to retrieve it func (c *socialeventClient) Get(name string) (*crtapi.SocialEvent, error) { result := &crtapi.SocialEvent{} - err := c.restClient.Get(). - Namespace(c.ns). - Resource(socialeventResourcePlural). - Name(name). - Do(context.TODO()). - Into(result) - return result, err + if err := c.client.Get(context.TODO(), types.NamespacedName{Namespace: c.ns, Name: name}, result); err != nil { + return nil, err + } + return result, nil } diff --git a/pkg/kubeclient/space.go b/pkg/kubeclient/space.go index 73d420b6..79745a99 100644 --- a/pkg/kubeclient/space.go +++ b/pkg/kubeclient/space.go @@ -4,7 +4,7 @@ import ( "context" crtapi "github.com/codeready-toolchain/api/api/v1alpha1" - "github.com/codeready-toolchain/registration-service/pkg/kubeclient/resources" + "k8s.io/apimachinery/pkg/types" ) type spaceClient struct { @@ -18,11 +18,8 @@ type SpaceInterface interface { // List returns the Spaces that match for the provided selector, or an error if something went wrong while attempting to retrieve it func (c *spaceClient) Get(name string) (*crtapi.Space, error) { result := &crtapi.Space{} - err := c.restClient.Get(). - Namespace(c.ns). - Resource(resources.SpaceResourcePlural). - Name(name). - Do(context.TODO()). - Into(result) - return result, err + if err := c.client.Get(context.TODO(), types.NamespacedName{Namespace: c.ns, Name: name}, result); err != nil { + return nil, err + } + return result, nil } diff --git a/pkg/kubeclient/spacebinding.go b/pkg/kubeclient/spacebinding.go index 5cdc3971..f32b60da 100644 --- a/pkg/kubeclient/spacebinding.go +++ b/pkg/kubeclient/spacebinding.go @@ -4,12 +4,8 @@ import ( "context" crtapi "github.com/codeready-toolchain/api/api/v1alpha1" - "github.com/codeready-toolchain/registration-service/pkg/kubeclient/resources" - "github.com/codeready-toolchain/registration-service/pkg/log" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" - "k8s.io/apimachinery/pkg/runtime/schema" - "k8s.io/client-go/dynamic" + "sigs.k8s.io/controller-runtime/pkg/client" ) type spaceBindingClient struct { @@ -23,25 +19,11 @@ type SpaceBindingInterface interface { // List returns the SpaceBindings that match for the provided selector, or an error if something went wrong while attempting to retrieve it func (c *spaceBindingClient) ListSpaceBindings(reqs ...labels.Requirement) ([]crtapi.SpaceBinding, error) { - intf, err := dynamic.NewForConfig(&c.cfg) - if err != nil { - return nil, err - } - selector := labels.NewSelector().Add(reqs...) - r := schema.GroupVersionResource{Group: "toolchain.dev.openshift.com", Version: "v1alpha1", Resource: resources.SpaceBindingResourcePlural} - log.Infof(nil, "Listing SpaceBindings with selector: %v", selector.String()) - listOptions := metav1.ListOptions{ - LabelSelector: selector.String(), - } - - list, err := intf.Resource(r).Namespace(c.ns).List(context.TODO(), listOptions) - if err != nil { - return nil, err - } result := &crtapi.SpaceBindingList{} + err := c.client.List(context.TODO(), result, client.InNamespace(c.ns), + client.MatchingLabelsSelector{Selector: selector}) - err = c.crtClient.scheme.Convert(list, result, nil) return result.Items, err } diff --git a/pkg/kubeclient/toolchainstatus.go b/pkg/kubeclient/toolchainstatus.go index 8cf003b6..0c591899 100644 --- a/pkg/kubeclient/toolchainstatus.go +++ b/pkg/kubeclient/toolchainstatus.go @@ -4,7 +4,7 @@ import ( "context" crtapi "github.com/codeready-toolchain/api/api/v1alpha1" - "github.com/codeready-toolchain/registration-service/pkg/kubeclient/resources" + "k8s.io/apimachinery/pkg/types" ) type toolchainStatusClient struct { @@ -19,14 +19,8 @@ type ToolchainStatusInterface interface { // If not found then NotFound error returned func (c *toolchainStatusClient) Get() (*crtapi.ToolchainStatus, error) { result := &crtapi.ToolchainStatus{} - err := c.restClient.Get(). - Namespace(c.ns). - Resource(resources.ToolchainStatusPlural). - Name(resources.ToolchainStatusName). - Do(context.TODO()). - Into(result) - if err != nil { + if err := c.client.Get(context.TODO(), types.NamespacedName{Namespace: c.ns, Name: "toolchain-status"}, result); err != nil { return nil, err } - return result, err + return result, nil } diff --git a/pkg/server/in_cluster_application.go b/pkg/server/in_cluster_application.go index 067af309..14a60115 100644 --- a/pkg/server/in_cluster_application.go +++ b/pkg/server/in_cluster_application.go @@ -6,7 +6,6 @@ import ( "github.com/codeready-toolchain/registration-service/pkg/application/service/factory" "github.com/codeready-toolchain/registration-service/pkg/configuration" "github.com/codeready-toolchain/registration-service/pkg/kubeclient" - "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -15,12 +14,7 @@ import ( // function to determine which Kubernetes configuration to use to create the REST client that interacts with the // Kubernetes service endpoints. func NewInClusterApplication(client client.Client) (application.Application, error) { - k8sConfig, err := rest.InClusterConfig() - if err != nil { - return nil, err - } - - kubeClient, err := kubeclient.NewCRTRESTClient(k8sConfig, client, configuration.Namespace()) + kubeClient, err := kubeclient.NewCRTRESTClient(client, configuration.Namespace()) if err != nil { return nil, err } diff --git a/test/fake/signup_client.go b/test/fake/signup_client.go index 2639a2d2..47d3501d 100644 --- a/test/fake/signup_client.go +++ b/test/fake/signup_client.go @@ -6,7 +6,6 @@ import ( crtapi "github.com/codeready-toolchain/api/api/v1alpha1" "github.com/codeready-toolchain/toolchain-common/pkg/hash" - "github.com/gofrs/uuid" "github.com/stretchr/testify/require" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"