Skip to content

Commit

Permalink
test updates, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rusenask committed Apr 20, 2018
1 parent 1229d3e commit 2ac1790
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 29 deletions.
23 changes: 10 additions & 13 deletions internal/k8s/cache.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package k8s

import (
"fmt"
"sort"
"sync"
)
Expand Down Expand Up @@ -34,19 +33,18 @@ func (cc *genericResourceCache) Add(grs ...*GenericResource) {
}
cc.Lock()
sort.Sort(genericResource(cc.values))
for _, c := range grs {
cc.add(c)
for _, gr := range grs {
cc.add(gr)
}
cc.Unlock()
}

// add adds c to the cache. If c is already present, the cached value of c is overwritten.
// invariant: cc.values should be sorted on entry.
func (cc *genericResourceCache) add(c *GenericResource) {
i := sort.Search(len(cc.values), func(i int) bool { return cc.values[i].Name >= c.Name })
if i < len(cc.values) && cc.values[i].Name == c.Name {
i := sort.Search(len(cc.values), func(i int) bool { return cc.values[i].Identifier >= c.Identifier })
if i < len(cc.values) && cc.values[i].Identifier == c.Identifier {
// c is already present, replace
fmt.Println("object added: ", c.Name)
cc.values[i] = c
} else {
// c is not present, append
Expand All @@ -58,24 +56,23 @@ func (cc *genericResourceCache) add(c *GenericResource) {

// Remove removes the named entry from the cache. If the entry
// is not present in the cache, the operation is a no-op.
func (cc *genericResourceCache) Remove(names ...string) {
if len(names) == 0 {
func (cc *genericResourceCache) Remove(identifiers ...string) {
if len(identifiers) == 0 {
return
}
cc.Lock()
sort.Sort(genericResource(cc.values))
for _, n := range names {
for _, n := range identifiers {
cc.remove(n)
fmt.Println("object removed: ", n)
}
cc.Unlock()
}

// remove removes the named entry from the cache.
// invariant: cc.values should be sorted on entry.
func (cc *genericResourceCache) remove(name string) {
i := sort.Search(len(cc.values), func(i int) bool { return cc.values[i].Name >= name })
if i < len(cc.values) && cc.values[i].Name == name {
func (cc *genericResourceCache) remove(identifier string) {
i := sort.Search(len(cc.values), func(i int) bool { return cc.values[i].Identifier >= identifier })
if i < len(cc.values) && cc.values[i].Identifier == identifier {
// c is present, remove
cc.values = append(cc.values[:i], cc.values[i+1:]...)
}
Expand Down
40 changes: 38 additions & 2 deletions internal/k8s/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package k8s

import (
"fmt"
"reflect"
"strings"

apps_v1 "k8s.io/api/apps/v1"
core_v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -29,7 +31,7 @@ func (c genericResource) Swap(i, j int) {
}

func (c genericResource) Less(i, j int) bool {
return c[i].Name < c[j].Name
return c[i].Identifier < c[j].Identifier
}

// NewGenericResource - create new generic k8s resource
Expand All @@ -39,7 +41,7 @@ func NewGenericResource(obj interface{}) (*GenericResource, error) {
case *apps_v1.Deployment, *apps_v1.StatefulSet, *apps_v1.DaemonSet:
// ok
default:
return nil, fmt.Errorf("unsupported resource")
return nil, fmt.Errorf("unsupported resource type: %v", reflect.TypeOf(obj).Kind())
}

gr := &GenericResource{
Expand All @@ -53,6 +55,10 @@ func NewGenericResource(obj interface{}) (*GenericResource, error) {
return gr, nil
}

func (r *GenericResource) String() string {
return fmt.Sprintf("%s/%s/%s images: %s", r.Kind(), r.Namespace, r.Name, strings.Join(r.GetImages(), ", "))
}

// GetIdentifier returns resource identifier
func (r *GenericResource) GetIdentifier() string {
switch obj := r.obj.(type) {
Expand Down Expand Up @@ -136,6 +142,36 @@ func (r *GenericResource) SetLabels(labels map[string]string) {
return
}

// GetSpecAnnotations - get resource spec template annotations
func (r *GenericResource) GetSpecAnnotations() (annotations map[string]string) {
switch obj := r.obj.(type) {
case *apps_v1.Deployment:
a := obj.Spec.Template.GetAnnotations()
if a == nil {
return make(map[string]string)
}
return a
case *apps_v1.StatefulSet:
return obj.Spec.Template.GetAnnotations()
case *apps_v1.DaemonSet:
return obj.Spec.Template.GetAnnotations()
}
return
}

// SetSpecAnnotations - set resource spec template annotations
func (r *GenericResource) SetSpecAnnotations(annotations map[string]string) {
switch obj := r.obj.(type) {
case *apps_v1.Deployment:
obj.Spec.Template.SetAnnotations(annotations)
case *apps_v1.StatefulSet:
obj.Spec.Template.SetAnnotations(annotations)
case *apps_v1.DaemonSet:
obj.Spec.Template.SetAnnotations(annotations)
}
return
}

// GetAnnotations - get resource annotations
func (r *GenericResource) GetAnnotations() (annotations map[string]string) {
switch obj := r.obj.(type) {
Expand Down
4 changes: 0 additions & 4 deletions internal/k8s/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func (t *Translator) OnAdd(obj interface{}) {
t.Errorf("OnAdd failed to add resource %T: %#v", obj, obj)
return
}
t.Infof("added %s %s", gr.Kind(), gr.Name)
t.GenericResourceCache.Add(gr)
}

Expand All @@ -28,8 +27,6 @@ func (t *Translator) OnUpdate(oldObj, newObj interface{}) {
t.Errorf("OnUpdate failed to update resource %T: %#v", newObj, newObj)
return
}

t.Infof("updated %s %s", gr.Kind(), gr.Name)
t.GenericResourceCache.Add(gr)
}

Expand All @@ -39,6 +36,5 @@ func (t *Translator) OnDelete(obj interface{}) {
t.Errorf("OnDelete failed to delete resource %T: %#v", obj, obj)
return
}
t.Infof("deleted %s %s", gr.Kind(), gr.Name)
t.GenericResourceCache.Remove(gr.GetIdentifier())
}
1 change: 0 additions & 1 deletion provider/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ func (p *Provider) createUpdatePlans(event *types.Event) ([]*UpdatePlan, error)

newVersion, err := version.GetVersion(event.Repository.Tag)
if err != nil {

plan, update, errCheck := checkUnversionedRelease(&event.Repository, release.Namespace, release.Name, release.Chart, release.Config)
if errCheck != nil {
log.WithFields(log.Fields{
Expand Down
9 changes: 0 additions & 9 deletions provider/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ import (

apps_v1 "k8s.io/api/apps/v1"
"k8s.io/api/core/v1"
// "k8s.io/api/extensions/apps_v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
core_v1 "k8s.io/client-go/kubernetes/typed/core/v1"

log "github.com/sirupsen/logrus"
)

type fakeProvider struct {
Expand Down Expand Up @@ -225,7 +222,6 @@ func MustParseGRS(objs []*apps_v1.Deployment) []*k8s.GenericResource {
var err error
var gr *k8s.GenericResource
gr, err = k8s.NewGenericResource(obj)
log.Infof("step 1: new gr parsed: %s, %s", gr.Identifier, gr.GetLabels())
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -292,11 +288,6 @@ func TestGetImpacted(t *testing.T) {

grs := MustParseGRS(deps)
grc := &k8s.GenericResourceCache{}

for _, gr := range grs {
log.Infof("step 2:current resrouce: %s labels: %s", gr.Identifier, gr.GetLabels())
}

grc.Add(grs...)

provider, err := NewProvider(fp, &fakeSender{}, approver(), grc)
Expand Down
4 changes: 4 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const KeelPolicyLabel = "keel.sh/policy"
// changes
const KeelTriggerLabel = "keel.sh/trigger"

// KeelForceTagMatchLabel - label that checks whether tags match before force updating
const KeelForceTagMatchLabel = "keel.sh/match-tag"

// KeelPollScheduleAnnotation - optional variable to setup custom schedule for polling, defaults to @every 10m
Expand All @@ -43,6 +44,9 @@ const KeelNotificationChanAnnotation = "keel.sh/notify"
// KeelMinimumApprovalsLabel - min approvals
const KeelMinimumApprovalsLabel = "keel.sh/approvals"

// KeelUpdateTimeAnnotation - update time
const KeelUpdateTimeAnnotation = "keel.sh/update-time"

// KeelApprovalDeadlineLabel - approval deadline
const KeelApprovalDeadlineLabel = "keel.sh/approvalDeadline"

Expand Down

0 comments on commit 2ac1790

Please sign in to comment.