Skip to content

Commit

Permalink
checking for annotation changes before any DD API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasreed committed Jan 9, 2020
1 parent 00e1b5f commit 431930f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
Binary file added BuildRun_Testing_DD_Account
Binary file not shown.
11 changes: 7 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
log "github.com/sirupsen/logrus"
ddapi "github.com/zorkian/go-datadog-api"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type ruleset struct {
Expand All @@ -51,10 +52,12 @@ type Annotation struct {

// An Event represents an update of a Kubernetes object and contains metadata about the update.
type Event struct {
Key string // A key identifying the object. This is in the format <object-type>/<object-name>
EventType string // The type of event - update, delete, or create
Namespace string // The namespace of the event's object
ResourceType string // The type of resource that was updated.
EventType string // The type of event - update, delete, or create
Key string // A key identifying the object. This is in the format <object-type>/<object-name>
Namespace string // The namespace of the event's object
OldMeta *metav1.ObjectMeta // Metadata from old kubernetes object in update or delete events
NewMeta *metav1.ObjectMeta // Metadata from new kubernetes object in update or add events
ResourceType string // The type of resource that was updated.
}

// Config represents the application configuration.
Expand Down
10 changes: 10 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,12 @@ func createController(kubeClient kubernetes.Interface, informer cache.SharedInde
log.Errorf("Error handling add event")
return
}
objMeta := objectMeta(obj)
evt.EventType = "create"
evt.ResourceType = resource
evt.Namespace = objectMeta(obj).Namespace
evt.NewMeta = &objMeta
evt.OldMeta = new(metav1.ObjectMeta)
log.Infof("%s/%s has been added.", resource, evt.Key)
wq.Add(evt)
},
Expand All @@ -191,9 +194,12 @@ func createController(kubeClient kubernetes.Interface, informer cache.SharedInde
log.Errorf("Error handling delete event")
return
}
objMeta := objectMeta(obj)
evt.EventType = "delete"
evt.ResourceType = resource
evt.Namespace = objectMeta(obj).Namespace
evt.NewMeta = new(metav1.ObjectMeta)
evt.OldMeta = &objMeta
log.Infof("%s/%s has been deleted.", resource, evt.Key)
wq.Add(evt)
},
Expand All @@ -205,9 +211,13 @@ func createController(kubeClient kubernetes.Interface, informer cache.SharedInde
log.Errorf("Error handling update event")
return
}
oldMeta := objectMeta(old)
newMeta := objectMeta(new)
evt.EventType = "update"
evt.ResourceType = resource
evt.Namespace = objectMeta(new).Namespace
evt.OldMeta = &oldMeta
evt.NewMeta = &newMeta
log.Infof("%s/%s has been updated.", resource, evt.Key)
wq.Add(evt)
},
Expand Down
9 changes: 9 additions & 0 deletions pkg/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package handler
import (
"bytes"
"fmt"
"reflect"
"regexp"
"strings"
"text/template"
Expand All @@ -40,6 +41,14 @@ func OnUpdate(obj interface{}, event config.Event) {
return
}

oldMeta := *event.OldMeta
newMeta := *event.NewMeta

if reflect.DeepEqual(oldMeta.Annotations, newMeta.Annotations) {
log.Infof("Old annotations match new, not updating: %s", event.Key)
return
}

switch t := obj.(type) {
case *appsv1.Deployment:
OnDeploymentChanged(obj.(*appsv1.Deployment), event)
Expand Down

0 comments on commit 431930f

Please sign in to comment.