Skip to content

Commit

Permalink
Merge pull request #201 from keel-hq/feature/ecr_registry_auth
Browse files Browse the repository at this point in the history
gr fixes
  • Loading branch information
rusenask authored Apr 24, 2018
2 parents 689c921 + b4003ae commit 5bc49d5
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 13 deletions.
31 changes: 23 additions & 8 deletions internal/k8s/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ func (r *GenericResource) GetResource() interface{} {
func (r *GenericResource) GetLabels() (labels map[string]string) {
switch obj := r.obj.(type) {
case *apps_v1.Deployment:
return obj.GetLabels()
return getOrInitialise(obj.GetLabels())
case *apps_v1.StatefulSet:
return obj.GetLabels()
return getOrInitialise(obj.GetLabels())
case *apps_v1.DaemonSet:
return obj.GetLabels()
return getOrInitialise(obj.GetLabels())
}
return
}
Expand Down Expand Up @@ -152,9 +152,17 @@ func (r *GenericResource) GetSpecAnnotations() (annotations map[string]string) {
}
return a
case *apps_v1.StatefulSet:
return obj.Spec.Template.GetAnnotations()
a := obj.Spec.Template.GetAnnotations()
if a == nil {
return make(map[string]string)
}
return a
case *apps_v1.DaemonSet:
return obj.Spec.Template.GetAnnotations()
a := obj.Spec.Template.GetAnnotations()
if a == nil {
return make(map[string]string)
}
return a
}
return
}
Expand All @@ -172,15 +180,22 @@ func (r *GenericResource) SetSpecAnnotations(annotations map[string]string) {
return
}

func getOrInitialise(a map[string]string) map[string]string {
if a == nil {
return make(map[string]string)
}
return a
}

// GetAnnotations - get resource annotations
func (r *GenericResource) GetAnnotations() (annotations map[string]string) {
switch obj := r.obj.(type) {
case *apps_v1.Deployment:
return obj.GetAnnotations()
return getOrInitialise(obj.GetAnnotations())
case *apps_v1.StatefulSet:
return obj.GetAnnotations()
return getOrInitialise(obj.GetAnnotations())
case *apps_v1.DaemonSet:
return obj.GetAnnotations()
return getOrInitialise(obj.GetAnnotations())
}
return
}
Expand Down
3 changes: 3 additions & 0 deletions internal/k8s/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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 @@ -27,6 +28,7 @@ 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 @@ -36,5 +38,6 @@ 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())
}
12 changes: 7 additions & 5 deletions provider/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ var kubernetesVersionedUpdatesCounter = prometheus.NewCounterVec(
Name: "kubernetes_versioned_updates_total",
Help: "How many versioned deployments were updated, partitioned by deployment name.",
},
[]string{"deployment"},
[]string{"kubernetes"},
)

var kubernetesUnversionedUpdatesCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "kubernetes_unversioned_updates_total",
Help: "How many unversioned deployments were updated, partitioned by deployment name.",
},
[]string{"deployment"},
[]string{"kubernetes"},
)

func init() {
Expand Down Expand Up @@ -261,15 +261,16 @@ func (p *Provider) updateDeployments(plans []*UpdatePlan) (updated []*k8s.Generi
resource.SetAnnotations(annotations)

err = p.implementer.Update(resource)
kubernetesVersionedUpdatesCounter.With(prometheus.Labels{resource.Kind(): fmt.Sprintf("%s/%s", resource.Namespace, resource.Name)}).Inc()
kubernetesVersionedUpdatesCounter.With(prometheus.Labels{"kubernetes": fmt.Sprintf("%s/%s", resource.Namespace, resource.Name)}).Inc()
// }
if err != nil {
log.WithFields(log.Fields{
"error": err,
"namespace": resource.Namespace,
"deployment": resource.Name,
"kind": resource.Kind(),
"update": fmt.Sprintf("%s->%s", plan.CurrentVersion, plan.NewVersion),
}).Error("provider.kubernetes: got error while update deployment")
}).Error("provider.kubernetes: got error while updating resource")

p.sender.Send(types.EventNotification{
Name: "update resource",
Expand All @@ -294,8 +295,9 @@ func (p *Provider) updateDeployments(plans []*UpdatePlan) (updated []*k8s.Generi

log.WithFields(log.Fields{
"name": resource.Name,
"kind": resource.Kind(),
"namespace": resource.Namespace,
}).Info("provider.kubernetes: deployment updated")
}).Info("provider.kubernetes: resource updated")
updated = append(updated, resource)
}

Expand Down
72 changes: 72 additions & 0 deletions provider/kubernetes/unversioned_updates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,78 @@ func TestProvider_checkUnversionedDeployment(t *testing.T) {
wantShouldUpdateDeployment: false,
wantErr: false,
},
{
name: "poll trigger, force-match, same tag on eu.gcr.io, daemonset",
args: args{
policy: types.PolicyTypeForce,
repo: &types.Repository{Host: "eu.gcr.io", Name: "karolisr/keel", Tag: "latest-staging"},
resource: MustParseGR(&apps_v1.DaemonSet{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Labels: map[string]string{types.KeelPolicyLabel: "force"},
Annotations: map[string]string{
types.KeelPollScheduleAnnotation: types.KeelPollDefaultSchedule,
types.KeelForceTagMatchLabel: "yup",
},
},
apps_v1.DaemonSetSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "eu.gcr.io/karolisr/keel:latest-staging",
},
},
},
},
},
apps_v1.DaemonSetStatus{},
}),
},
wantUpdatePlan: &UpdatePlan{
Resource: MustParseGR(&apps_v1.DaemonSet{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{
types.KeelPollScheduleAnnotation: types.KeelPollDefaultSchedule,
types.KeelForceTagMatchLabel: "yup",
},
Labels: map[string]string{types.KeelPolicyLabel: "force"},
},
apps_v1.DaemonSetSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
// "time": timeutil.Now().String(),
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "eu.gcr.io/karolisr/keel:latest-staging",
},
},
},
},
},
apps_v1.DaemonSetStatus{},
}),
NewVersion: "latest-staging",
CurrentVersion: "latest-staging",
},
wantShouldUpdateDeployment: true,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 5bc49d5

Please sign in to comment.