Skip to content

Commit

Permalink
Merge pull request #8 from leoarpin/fix-downscale-pending
Browse files Browse the repository at this point in the history
Fix: Downscale claim staying in pending
  • Loading branch information
Whisper40 authored Nov 20, 2023
2 parents 8b3965e + e2c1ba4 commit 63e391e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
2 changes: 1 addition & 1 deletion artifacts/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ spec:
serviceAccountName: kotary
containers:
- name: kotary
image: cagip/kotary:v0.24.0
image: cagip/kotary:v1.24.0
imagePullPolicy: Always
envFrom:
- configMapRef:
Expand Down
3 changes: 3 additions & 0 deletions internal/controller/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (c *Controller) syncHandlerClaim(key string) error {
}

utils.ClaimCounter.WithLabelValues("success").Inc()
klog.Infof("< RequestQuotaClaim '%s' ACCEPTED >", claim.Name)

// Everything went well
return nil
Expand Down Expand Up @@ -146,6 +147,7 @@ func (c *Controller) updateResourceQuotaClaimStatus(claim *cagipv1.ResourceQuota

// Update claim phase to Rejected with a msg
func (c *Controller) claimRejected(claim *cagipv1.ResourceQuotaClaim, msg string) (err error) {
klog.Infof("< RequestQuotaClaim '%s' set to REJECTED >", claim.Name)
// Notify via an event
c.recorder.Event(claim, v1Core.EventTypeWarning, cagipv1.PhaseRejected, msg)
// Update ResourceQuotaClaim Status to Rejected Phase
Expand All @@ -156,6 +158,7 @@ func (c *Controller) claimRejected(claim *cagipv1.ResourceQuotaClaim, msg string

// Update claim phase to Pending with a msg
func (c *Controller) claimPending(claim *cagipv1.ResourceQuotaClaim, msg string) (err error) {
klog.Infof("< RequestQuotaClaim '%s' set to PENDING >", claim.Name)
// Notify via an event
c.recorder.Event(claim, v1Core.EventTypeWarning, cagipv1.PhasePending, msg)
// Update ResourceQuotaClaim Status to Rejected Phase
Expand Down
66 changes: 62 additions & 4 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func NewController(
resourceQuotaClaimInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueResourceQuotaClaim,
UpdateFunc: func(old, new interface{}) {
klog.Infof("============= RequestQuotaClaim informer is invoqued =============")
newQuotaClaim := new.(*cagipv1.ResourceQuotaClaim)
oldQuotaClaim := old.(*cagipv1.ResourceQuotaClaim)
if newQuotaClaim.ResourceVersion == oldQuotaClaim.ResourceVersion {
Expand All @@ -134,10 +135,19 @@ func NewController(
namespaceInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueNamespace,
UpdateFunc: func(old, new interface{}) {
klog.Infof("============= Namespace Informer is invoqued =============")
controller.enqueueNamespace(new)
},
})

//Set up an event handler for pod deletions to handle changes in Resource Used
podsInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
DeleteFunc: func(obj interface{}) {
klog.Infof("============= Pods informer is invoqued (delete) =============")
controller.handlePod(obj)
},
})

return controller
}

Expand Down Expand Up @@ -228,6 +238,7 @@ func (c *Controller) processNextWorkClaim() bool {
defer c.resourceQuotaClaimWorkQueue.Done(obj)
var key string
var ok bool
klog.Infof("Working on %s quotaclaim", key)
// We expect strings to come off the resourceQuotaClaimWorkQueue. These are of the
// form namespace/name. We do this as the delayed nature of the
// resourceQuotaClaimWorkQueue means the items in the informer cache may actually be
Expand Down Expand Up @@ -340,23 +351,70 @@ func (c *Controller) handleObject(obj interface{}) {
utilruntime.HandleError(fmt.Errorf("error decoding object tombstone, invalid type"))
return
}
klog.V(4).Infof("Recovered deleted object '%s' from tombstone", object.GetName())
klog.Infof("Recovered deleted object '%s' from tombstone", object.GetName())
}
klog.V(4).Infof("Processing object: %s", object.GetName())
klog.Infof("Processing object: %s", object.GetName())
if ownerRef := metav1.GetControllerOf(object); ownerRef != nil {
// If this object is not owned by a ResourceQuotaClaims, we should not do anything more
// with it.
if ownerRef.Kind != "ResourceQuotaClaims" {
klog.Infof("not owned by a ResourceQuotaClaims")
return
}

claim, err := c.resourceQuotaClaimLister.ResourceQuotaClaims(object.GetNamespace()).Get(ownerRef.Name)
if err != nil {
klog.V(4).Infof("ignoring orphaned object '%s' of ResourceQuotaClaims '%s'", object.GetSelfLink(), ownerRef.Name)
klog.Infof("ignoring orphaned object '%s' of ResourceQuotaClaims '%s'", object.GetSelfLink(), ownerRef.Name)
return
}

klog.Infof("Processing claim: %s", claim)
c.enqueueResourceQuotaClaim(claim)
return
}
}

// handlePod will take a pod interface in argument and try to find all its Claims that are still not
// treated and enqueue them to be processed.
func (c *Controller) handlePod(obj interface{}) {

//get the pod object
var object metav1.Object
var ok bool
if object, ok = obj.(metav1.Object); !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
utilruntime.HandleError(fmt.Errorf("error decoding object, invalid type"))
return
}
object, ok = tombstone.Obj.(metav1.Object)
if !ok {
utilruntime.HandleError(fmt.Errorf("error decoding object tombstone, invalid type"))
return
}
klog.Infof("Recovered deleted object '%s' from tombstone", object.GetName())
}
//retrieve the namespace of the pod
podNamespace := object.GetNamespace()

klog.Infof("Processing pod: %s (%s)", object.GetName(), podNamespace)

// Empty selector
selector, _ := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{})

//retrieve the list of current quotaclaims in the namespace
quotaClaims, err := c.resourceQuotaClaimLister.ResourceQuotaClaims(podNamespace).List(selector)

if err != nil {
klog.Infof("error while getting quotaclaims: %s", err)
return
}

//iterate through the list and enqueue claims to be treated
for _, claim := range quotaClaims {
notReject := claim.Status.Phase != cagipv1.PhaseRejected
notAccepted := claim.Status.Phase != cagipv1.PhaseAccepted
if notReject && notAccepted {
c.enqueueResourceQuotaClaim(claim)
}
}
}

0 comments on commit 63e391e

Please sign in to comment.