Skip to content

Commit

Permalink
Adds support for hook weight. Closes #1396
Browse files Browse the repository at this point in the history
  • Loading branch information
alexec committed Apr 29, 2019
1 parent a121245 commit 83527a2
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 13 deletions.
3 changes: 3 additions & 0 deletions controller/sync_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ func (sc *syncContext) getHooks(hookTypes ...appv1.HookType) ([]*unstructured.Un
}
}
hooks = append(hooks, hook)

hook.GetAnnotations()
}

return hooks, nil
}

Expand Down
77 changes: 64 additions & 13 deletions test/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import (
"encoding/json"
"fmt"

appsv1 "k8s.io/api/apps/v1"
apiv1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -45,12 +46,7 @@ var PodManifest = []byte(`
`)

func NewPod() *unstructured.Unstructured {
var un unstructured.Unstructured
err := json.Unmarshal(PodManifest, &un)
if err != nil {
panic(err)
}
return &un
return un(PodManifest)
}

var ServiceManifest = []byte(`
Expand All @@ -77,8 +73,12 @@ var ServiceManifest = []byte(`
`)

func NewService() *unstructured.Unstructured {
return un(ServiceManifest)
}

func un(bytes []byte) *unstructured.Unstructured {
var un unstructured.Unstructured
err := json.Unmarshal(ServiceManifest, &un)
err := json.Unmarshal(bytes, &un)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -127,12 +127,7 @@ var DeploymentManifest = []byte(`
`)

func NewDeployment() *unstructured.Unstructured {
var un unstructured.Unstructured
err := json.Unmarshal(DeploymentManifest, &un)
if err != nil {
panic(err)
}
return &un
return un(DeploymentManifest)
}

func DemoDeployment() *appsv1.Deployment {
Expand Down Expand Up @@ -213,3 +208,59 @@ func NewFakeProjLister(objects ...runtime.Object) applister.AppProjectNamespaceL
defer cancel()
return factory.Argoproj().V1alpha1().AppProjects().Lister().AppProjects(FakeArgoCDNamespace)
}
func NewHook() *unstructured.Unstructured {
return un([]byte(`{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "my-hook",
"annotations": {
"argocd.argoproj.io/hook": "PostSync",
"argocd.argoproj.io/hook-delete-policy": "HookSucceeded"
}
},
"spec": {
"restartPolicy": "Never",
"containers": [
{
"name": "main"
}
],
"image": "alpine:latest",
"command": [
"sh",
"-c",
"sleep 10"
]
}
}`))
}

func NewHookWithWeight(hookWeight string) *unstructured.Unstructured {
return un([]byte(fmt.Sprintf(`{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "my-hook",
"annotations": {
"argocd.argoproj.io/hook": "PostSync",
"argocd.argoproj.io/hook-weight": "%s",
"argocd.argoproj.io/hook-delete-policy": "HookSucceeded"
}
},
"spec": {
"restartPolicy": "Never",
"containers": [
{
"name": "main"
}
],
"image": "alpine:latest",
"command": [
"sh",
"-c",
"sleep 10"
]
}
}`, hookWeight)))
}
20 changes: 20 additions & 0 deletions util/hook/hook.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package hook

import (
"strconv"
"strings"

log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/argoproj/argo-cd/common"
Expand Down Expand Up @@ -53,3 +55,21 @@ func IsArgoHook(obj *unstructured.Unstructured) bool {
}
return false
}

// IsArgoHook indicates if the supplied object is an Argo CD application lifecycle hook
// (vs. a normal, synced application resource)
func Weight(obj *unstructured.Unstructured) int {

text := obj.GetAnnotations()["argocd.argoproj.io/hook-weight"]

if text == "" {
return 0
}

hookWeight, err := strconv.Atoi(text)
if err != nil {
log.WithFields(log.Fields{"text": text, "err": err}).Warn("failed to convert hook weight, ignoring")
}

return hookWeight
}
21 changes: 21 additions & 0 deletions util/hook/hook_util.go → util/hook/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/argoproj/argo-cd/test"
)
Expand All @@ -30,3 +31,23 @@ func TestIsHook(t *testing.T) {
pod.SetAnnotations(map[string]string{"argocd.argoproj.io/hook": "Unknown"})
assert.False(t, IsHook(pod))
}

func TestWeight(t *testing.T) {
tests := []struct {
name string
obj *unstructured.Unstructured
want int
}{
{"TestDefaultWeight", test.NewHook(), 0},
{"TestPositiveWeight", test.NewHookWithWeight("1"), 1},
{"TestNegativeWeight", test.NewHookWithWeight("-1"), -1},
{"TestGarbageWeight", test.NewHookWithWeight("foo"), 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Weight(tt.obj); got != tt.want {
t.Errorf("Weight() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 83527a2

Please sign in to comment.