Skip to content

Commit

Permalink
Add k8s events support
Browse files Browse the repository at this point in the history
Create k8s events when a pod is successfully evicted.

Fixes kubernetes-sigs#166.
  • Loading branch information
seanmalloy committed Oct 18, 2019
1 parent c42670e commit a171df3
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/descheduler/evictions/evictions.go
Expand Up @@ -18,10 +18,13 @@ package evictions

import (
"fmt"
"time"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/api/events/v1beta1"
policy "k8s.io/api/policy/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
apimachinery "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"

Expand All @@ -47,7 +50,16 @@ func EvictPod(client clientset.Interface, pod *v1.Pod, policyGroupVersion string
}
err := client.Policy().Evictions(eviction.Namespace).Evict(eviction)

event := &v1beta1.Event{
EventTime: apimachinery.NewMicroTime(time.Now()),
Note: "pod evicted by sigs.k8s.io/descheduler",
Reason: "pod evictecd by sigs.k8s.io/descheduler",
Regarding: v1.ObjectReference{Kind: "pod", Namespace: pod.Namespace, Name: pod.Name},
Type: "Normal",
}

if err == nil {
client.EventsV1beta1().Events(pod.Namespace).Create(event)
return true, nil
} else if apierrors.IsTooManyRequests(err) {
return false, fmt.Errorf("error when evicting pod (ignoring) %q: %v", pod.Name, err)
Expand Down

0 comments on commit a171df3

Please sign in to comment.