Skip to content
This repository has been archived by the owner on May 6, 2022. It is now read-only.

Commit

Permalink
Buildable ServiceClass control loop
Browse files Browse the repository at this point in the history
  • Loading branch information
eriknelson committed Apr 30, 2018
1 parent 43901be commit 270a0bf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
11 changes: 5 additions & 6 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,12 @@ func NewController(
UpdateFunc: controller.serviceBrokerUpdate,
DeleteFunc: controller.serviceBrokerDelete,
})
// ERIK TODO: Uncomment when the controllers are brought in
controller.serviceClassLister = serviceClassInformer.Lister()
//serviceClassInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
//AddFunc: controller.serviceClassAdd,
//UpdateFunc: controller.serviceClassUpdate,
//DeleteFunc: controller.serviceClassDelete,
//})
serviceClassInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.serviceClassAdd,
UpdateFunc: controller.serviceClassUpdate,
DeleteFunc: controller.serviceClassDelete,
})
controller.servicePlanLister = servicePlanInformer.Lister()
//servicePlanInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
//AddFunc: controller.servicePlanAdd,
Expand Down
57 changes: 31 additions & 26 deletions pkg/controller/controller_serviceclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,64 +20,69 @@ import (
"github.com/golang/glog"
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1"

"github.com/kubernetes-incubator/service-catalog/pkg/pretty"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/tools/cache"
)

// Service class handlers and control-loop

func (c *controller) clusterServiceClassAdd(obj interface{}) {
func (c *controller) serviceClassAdd(obj interface{}) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
glog.Errorf("Couldn't get key for object %+v: %v", obj, err)
return
}
c.clusterServiceClassQueue.Add(key)
c.serviceClassQueue.Add(key)
}

func (c *controller) clusterServiceClassUpdate(oldObj, newObj interface{}) {
c.clusterServiceClassAdd(newObj)
func (c *controller) serviceClassUpdate(oldObj, newObj interface{}) {
c.serviceClassAdd(newObj)
}

func (c *controller) clusterServiceClassDelete(obj interface{}) {
serviceClass, ok := obj.(*v1beta1.ClusterServiceClass)
func (c *controller) serviceClassDelete(obj interface{}) {
serviceClass, ok := obj.(*v1beta1.ServiceClass)
if serviceClass == nil || !ok {
return
}

glog.V(4).Infof("Received delete event for ServiceClass %v; no further processing will occur", serviceClass.Name)
}

// reconcileServiceClassKey reconciles a ClusterServiceClass due to controller resync
// or an event on the ClusterServiceClass. Note that this is NOT the main
// reconciliation loop for ClusterServiceClass. ClusterServiceClasses are primarily
// reconciled in a separate flow when a ClusterServiceBroker is reconciled.
func (c *controller) reconcileClusterServiceClassKey(key string) error {
plan, err := c.clusterServiceClassLister.Get(key)
// reconcileServiceClassKey reconciles a ServiceClass due to controller resync
// or an event on the ServiceClass. Note that this is NOT the main
// reconciliation loop for ServiceClass. ServiceClasses are primarily
// reconciled in a separate flow when a ServiceBroker is reconciled.
func (c *controller) reconcileServiceClassKey(key string) error {
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return err
}
pcb := pretty.NewContextBuilder(pretty.ServiceClass, namespace, name)
class, err := c.serviceClassLister.ServiceClasses(namespace).Get(name)
if errors.IsNotFound(err) {
glog.Infof("ClusterServiceClass %q: Not doing work because it has been deleted", key)
glog.Info(pcb.Message("Not doing work because the ServiceClass has been deleted"))
return nil
}
if err != nil {
glog.Infof("ClusterServiceClass %q: Unable to retrieve object from store: %v", key, err)
glog.Infof(pcb.Message("Unable to retrieve"))
return err
}

return c.reconcileClusterServiceClass(plan)
return c.reconcileServiceClass(class)
}

func (c *controller) reconcileClusterServiceClass(serviceClass *v1beta1.ClusterServiceClass) error {
glog.Infof("ClusterServiceClass %q (ExternalName: %q): processing", serviceClass.Name, serviceClass.Spec.ExternalName)
func (c *controller) reconcileServiceClass(serviceClass *v1beta1.ServiceClass) error {
pcb := pretty.NewContextBuilder(pretty.ServiceClass, serviceClass.Namespace, serviceClass.Name)
glog.Info(pcb.Message("Processing"))

if !serviceClass.Status.RemovedFromBrokerCatalog {
return nil
}

glog.Infof("ClusterServiceClass %q (ExternalName: %q): has been removed from broker catalog; determining whether there are instances remaining", serviceClass.Name, serviceClass.Spec.ExternalName)
glog.Info(pcb.Message("Removed from broker catalog; determining whether there are instances remaining"))

serviceInstances, err := c.findServiceInstancesOnClusterServiceClass(serviceClass)
serviceInstances, err := c.findServiceInstancesOnServiceClass(serviceClass)
if err != nil {
return err
}
Expand All @@ -86,16 +91,16 @@ func (c *controller) reconcileClusterServiceClass(serviceClass *v1beta1.ClusterS
return nil
}

glog.Infof("ClusterServiceClass %q (ExternalName: %q): has been removed from broker catalog and has zero instances remaining; deleting", serviceClass.Name, serviceClass.Spec.ExternalName)
return c.serviceCatalogClient.ClusterServiceClasses().Delete(serviceClass.Name, &metav1.DeleteOptions{})
glog.Info(pcb.Message("Removed from broker catalog and has zero instances remaining; deleting"))
return c.serviceCatalogClient.ServiceClasses(serviceClass.Namespace).Delete(serviceClass.Name, &metav1.DeleteOptions{})
}

func (c *controller) findServiceInstancesOnClusterServiceClass(serviceClass *v1beta1.ClusterServiceClass) (*v1beta1.ServiceInstanceList, error) {
func (c *controller) findServiceInstancesOnServiceClass(serviceClass *v1beta1.ServiceClass) (*v1beta1.ServiceInstanceList, error) {
fieldSet := fields.Set{
"spec.clusterServiceClassRef.name": serviceClass.Name,
"spec.serviceClassRef.name": serviceClass.Name,
}
fieldSelector := fields.SelectorFromSet(fieldSet).String()
listOpts := metav1.ListOptions{FieldSelector: fieldSelector}

return c.serviceCatalogClient.ServiceInstances(metav1.NamespaceAll).List(listOpts)
return c.serviceCatalogClient.ServiceInstances(serviceClass.Namespace).List(listOpts)
}

0 comments on commit 270a0bf

Please sign in to comment.