Skip to content
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

Refactors service catalog and discovery client code #4272

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: 1 addition & 1 deletion pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func ListInProject(client *occlient.Client) ([]string, error) {
// Get all ServiceInstances with the "app" label
// Okay, so there is an edge-case here.. if Service Catalog is *not* enabled in the cluster, we shouldn't error out..
// however, we should at least warn the user.
serviceInstanceAppNames, err := client.GetServiceInstanceLabelValues(applabels.ApplicationLabel, applabels.ApplicationLabel)
serviceInstanceAppNames, err := client.GetKubeClient().ListServiceInstanceLabelValues(applabels.ApplicationLabel, applabels.ApplicationLabel)
if err != nil {
klog.V(4).Infof("Unable to list Service Catalog instances: %s", err)
} else {
Expand Down
6 changes: 3 additions & 3 deletions pkg/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func ListServices(client *occlient.Client) (ServiceTypeList, error) {
// ListOperatorServices fetches a list of Operators from the cluster and
// returns only those Operators which are successfully installed on the cluster
func ListOperatorServices(client *kclient.Client) (*olm.ClusterServiceVersionList, error) {
allCsvs, err := client.GetClusterServiceVersionList()
allCsvs, err := client.ListClusterServiceVersion()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -345,12 +345,12 @@ func SearchService(client *occlient.Client, name string) (ServiceTypeList, error
func getClusterCatalogServices(client *occlient.Client) ([]ServiceType, error) {
var classNames []ServiceType

classes, err := client.GetClusterServiceClasses()
classes, err := client.GetKubeClient().ListClusterServiceClasses()
if err != nil {
return nil, errors.Wrap(err, "unable to get cluster service classes")
}

planListItems, err := client.GetAllClusterServicePlans()
planListItems, err := client.GetKubeClient().ListClusterServicePlans()
if err != nil {
return nil, errors.Wrap(err, "Unable to get service plans")
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/kclient/fakeclient.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package kclient

import (
fakeKubeClientset "k8s.io/client-go/kubernetes/fake"

fakeServiceCatalogClientSet "github.com/kubernetes-sigs/service-catalog/pkg/client/clientset_generated/clientset/fake"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
fakeKubeClientset "k8s.io/client-go/kubernetes/fake"
)

// FakeClientset holds fake ClientSets
// this is returned by FakeNew to access methods of fake client sets
type FakeClientset struct {
Kubernetes *fakeKubeClientset.Clientset
Kubernetes *fakeKubeClientset.Clientset
ServiceCatalogClientSet *fakeServiceCatalogClientSet.Clientset
}

// FakeNew creates new fake client for testing
Expand All @@ -23,6 +24,9 @@ func FakeNew() (*Client, *FakeClientset) {
fkclientset.Kubernetes = fakeKubeClientset.NewSimpleClientset()
client.KubeClient = fkclientset.Kubernetes

fkclientset.ServiceCatalogClientSet = fakeServiceCatalogClientSet.NewSimpleClientset()
client.serviceCatalogClient = fkclientset.ServiceCatalogClientSet.ServicecatalogV1beta1()

return &client, &fkclientset
}

Expand Down
63 changes: 56 additions & 7 deletions pkg/kclient/kclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"strings"
"time"

servicecatalogclienset "github.com/kubernetes-sigs/service-catalog/pkg/client/clientset_generated/clientset/typed/servicecatalog/v1beta1"
"github.com/openshift/odo/pkg/util"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/discovery"
appsclientset "k8s.io/client-go/kubernetes/typed/apps/v1"
"k8s.io/klog"

Expand All @@ -33,16 +36,19 @@ Consult your Kubernetes distribution's documentation for more details

// Client is a collection of fields used for client configuration and interaction
type Client struct {
KubeClient kubernetes.Interface
KubeConfig clientcmd.ClientConfig
KubeClientConfig *rest.Config
Namespace string
OperatorClient *operatorsclientset.OperatorsV1alpha1Client
appsClient appsclientset.AppsV1Interface
KubeClient kubernetes.Interface
KubeConfig clientcmd.ClientConfig
KubeClientConfig *rest.Config
Namespace string
OperatorClient *operatorsclientset.OperatorsV1alpha1Client
appsClient appsclientset.AppsV1Interface
serviceCatalogClient servicecatalogclienset.ServicecatalogV1beta1Interface
// DynamicClient interacts with client-go's `dynamic` package. It is used
// to dynamically create service from an operator. It can take an arbitrary
// yaml and create k8s/OpenShift resource from it.
DynamicClient dynamic.Interface
DynamicClient dynamic.Interface
discoveryClient discovery.DiscoveryInterface
supportedResources map[string]bool
}

// New creates a new client
Expand Down Expand Up @@ -93,6 +99,16 @@ func NewForConfig(config clientcmd.ClientConfig) (client *Client, err error) {
}
client.appsClient = appsClient

client.serviceCatalogClient, err = servicecatalogclienset.NewForConfig(client.KubeClientConfig)
if err != nil {
return nil, err
}

client.discoveryClient, err = discovery.NewDiscoveryClientForConfig(client.KubeClientConfig)
if err != nil {
return nil, err
}

return client, nil
}

Expand Down Expand Up @@ -178,3 +194,36 @@ func (c *Client) GeneratePortForwardReq(podName string) *rest.Request {
Name(podName).
SubResource("portforward")
}

func (c *Client) SetDiscoveryInterface(client discovery.DiscoveryInterface) {
c.discoveryClient = client
}

func (c *Client) IsResourceSupported(apiGroup, apiVersion, resourceName string) (bool, error) {
if c.supportedResources == nil {
c.supportedResources = make(map[string]bool, 7)
}
groupVersion := metav1.GroupVersion{Group: apiGroup, Version: apiVersion}.String()

supported, found := c.supportedResources[groupVersion]
if !found {
list, err := c.discoveryClient.ServerResourcesForGroupVersion(groupVersion)
if err != nil {
if kerrors.IsNotFound(err) {
supported = false
} else {
// don't record, just attempt again next time in case it's a transient error
return false, err
}
} else {
for _, resources := range list.APIResources {
if resources.Name == resourceName {
supported = true
break
}
}
}
c.supportedResources[groupVersion] = supported
}
return supported, nil
}
33 changes: 21 additions & 12 deletions pkg/kclient/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

olm "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
"github.com/pkg/errors"
kerrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"
)
Expand All @@ -18,29 +19,37 @@ const (
apiVersion = "odo.dev/v1alpha1"
)

// GetClusterServiceVersionList returns a list of CSVs in the cluster
// IsSBRSupported checks if resource of type service binding request present on the cluster
func (c *Client) IsSBRSupported() (bool, error) {
return c.IsResourceSupported("apps.openshift.io", "v1alpha1", "servicebindingrequests")
}

// IsCSVSupported checks if resource of type service binding request present on the cluster
func (c *Client) IsCSVSupported() (bool, error) {
return c.IsResourceSupported("operators.coreos.com", "v1alpha1", "clusterserviceversions")
}

// ListClusterServiceVersion returns a list of CSVs in the cluster
// It is equivalent to doing `oc get csvs` using oc cli
func (c *Client) GetClusterServiceVersionList() (*olm.ClusterServiceVersionList, error) {
func (c *Client) ListClusterServiceVersion() (*olm.ClusterServiceVersionList, error) {
dharmit marked this conversation as resolved.
Show resolved Hide resolved
klog.V(3).Infof("Fetching list of operators installed in cluster")
csvs, err := c.OperatorClient.ClusterServiceVersions(c.Namespace).List(v1.ListOptions{})
if err != nil {
if kerrors.IsNotFound(err) {
return &olm.ClusterServiceVersionList{}, ErrNoSuchOperator
}
return &olm.ClusterServiceVersionList{}, err
}
return csvs, nil
}

// GetClusterServiceVersion returns a particular CSV from a list of CSVs
func (c *Client) GetClusterServiceVersion(name string) (olm.ClusterServiceVersion, error) {
csvs, err := c.GetClusterServiceVersionList()
csv, err := c.OperatorClient.ClusterServiceVersions(c.Namespace).Get(name, v1.GetOptions{})
if err != nil {
return olm.ClusterServiceVersion{}, err
}
for _, item := range csvs.Items {
if item.Name == name {
return item, nil
}
}
return olm.ClusterServiceVersion{}, ErrNoSuchOperator
return *csv, nil
}

// GetCustomResourcesFromCSV returns a list of CRs provided by an operator/CSV.
Expand All @@ -53,7 +62,7 @@ func (c *Client) GetCustomResourcesFromCSV(csv *olm.ClusterServiceVersion) *[]ol
// given keyword then return it
func (c *Client) SearchClusterServiceVersionList(name string) (*olm.ClusterServiceVersionList, error) {
var result []olm.ClusterServiceVersion
csvs, err := c.GetClusterServiceVersionList()
csvs, err := c.ListClusterServiceVersion()
if err != nil {
return &olm.ClusterServiceVersionList{}, errors.Wrap(err, "unable to list services")
}
Expand Down Expand Up @@ -83,7 +92,7 @@ func (c *Client) SearchClusterServiceVersionList(name string) (*olm.ClusterServi
// GetCustomResource returns the CR matching the name
func (c *Client) GetCustomResource(customResource string) (*olm.CRDDescription, error) {
// Get all csvs in the namespace
csvs, err := c.GetClusterServiceVersionList()
csvs, err := c.ListClusterServiceVersion()
if err != nil {
return &olm.CRDDescription{}, err
}
Expand All @@ -105,7 +114,7 @@ func (c *Client) GetCustomResource(customResource string) (*olm.CRDDescription,

// GetCSVWithCR returns the CSV (Operator) that contains the CR (service)
func (c *Client) GetCSVWithCR(name string) (*olm.ClusterServiceVersion, error) {
csvs, err := c.GetClusterServiceVersionList()
csvs, err := c.ListClusterServiceVersion()
if err != nil {
return &olm.ClusterServiceVersion{}, errors.Wrap(err, "unable to list services")
}
Expand Down
Loading