Skip to content

Commit

Permalink
Merge pull request #185 from keel-hq/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
rusenask committed Apr 4, 2018
2 parents 58d04ba + 28c1e02 commit 1aea4ba
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 13 deletions.
15 changes: 12 additions & 3 deletions provider/kubernetes/force_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,25 @@ func (p *Provider) forceUpdate(deployment *v1beta1.Deployment) (err error) {

for index, pod := range podList.Items {

var gp int64

if pod.DeletionGracePeriodSeconds != nil {
gp = *pod.DeletionGracePeriodSeconds
}
if gracePeriod != 0 {
gp = gracePeriod
}

log.WithFields(log.Fields{
"selector": selector,
"pod": pod.Name,
"namespace": deployment.Namespace,
"deployment": deployment.Name,
"grace_period": fmt.Sprint(gracePeriod),
"grace_period": fmt.Sprint(gp),
}).Info("provider.kubernetes: deleting pod to force pull...")

err = p.implementer.DeletePod(deployment.Namespace, pod.Name, &meta_v1.DeleteOptions{
GracePeriodSeconds: &gracePeriod,
GracePeriodSeconds: &gp,
})
if err != nil {
log.WithFields(log.Fields{
Expand All @@ -55,7 +64,7 @@ func (p *Provider) forceUpdate(deployment *v1beta1.Deployment) (err error) {

// sleep between pod restarts but not if there aren't more left
if index < len(podList.Items)-1 {
time.Sleep(time.Duration(podDeleteDelay))
time.Sleep(time.Duration(podDeleteDelay) * time.Second)
}
}

Expand Down
60 changes: 60 additions & 0 deletions provider/kubernetes/force_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kubernetes

import (
"testing"
"time"

"github.com/keel-hq/keel/types"
"k8s.io/api/core/v1"
Expand Down Expand Up @@ -69,3 +70,62 @@ func TestForceUpdate(t *testing.T) {
t.Errorf("wrong name: %s", fp.deletedPods[1].Name)
}
}

func TestForceUpdateDelay(t *testing.T) {

fp := &fakeImplementer{}

dep := &v1beta1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "deployment-1",
Namespace: "xx",
Labels: map[string]string{types.KeelPolicyLabel: "all"},
Annotations: map[string]string{types.KeelPodDeleteDelay: "300"},
},
v1beta1.DeploymentSpec{},
v1beta1.DeploymentStatus{},
}

fp.podList = &v1.PodList{
Items: []v1.Pod{
v1.Pod{
ObjectMeta: meta_v1.ObjectMeta{
Name: "1",
Namespace: "xx",
},
},
v1.Pod{
ObjectMeta: meta_v1.ObjectMeta{
Name: "2",
Namespace: "xx",
},
},
},
}

provider, err := NewProvider(fp, &fakeSender{}, approver())
if err != nil {
t.Fatalf("failed to get provider: %s", err)
}

go func() {
err = provider.forceUpdate(dep)
if err != nil {
t.Fatalf("failed to force update: %s", err)
}
}()

time.Sleep(100 * time.Millisecond)

if len(fp.deletedPods) != 1 {
t.Errorf("expected to get 1 deleted pods, another one should be delayed")
}

if fp.deletedPods[0].Namespace != "xx" {
t.Errorf("wrong namespace: %s", fp.deletedPods[0].Namespace)
}
if fp.deletedPods[0].Name != "1" {
t.Errorf("wrong name: %s", fp.deletedPods[0].Name)
}
}
10 changes: 9 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,12 @@ Before starting to work on some big or medium features - raise an issue [here](h

### Developing Keel

If you wish to work on Keel itself, you will need Go 1.8+ installed. Make sure you put Keel into correct Gopath and `go build` (dependency management is done through [dep](https://github.com/golang/dep)).
If you wish to work on Keel itself, you will need Go 1.9+ installed. Make sure you put Keel into correct Gopath and `go build` (dependency management is done through [dep](https://github.com/golang/dep)).

To test Keel while developing:

1. Launch a Kubernetes cluster like Minikube or Docker for Mac with Kubernetes.
2. Change config to use it: `kubectl config use-context docker-for-desktop`
3. Build Keel from `cmd/keel` directory.
4. Start Keel with: `keel --no-incluster`. This will use Kubeconfig from your home.

26 changes: 17 additions & 9 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ const KeelApprovalDeadlineDefault = 24
// during force deploy
const KeelPodDeleteDelay = "keel.sh/forceDelay"

//KeelPodMaxDelay defines maximum delay in seconds between deleting pods
const KeelPodMaxDelay int64 = 600

// KeelPodTerminationGracePeriod - optional grace period during
// pod termination
const KeelPodTerminationGracePeriod = "keel.sh/gracePeriod"
Expand Down Expand Up @@ -218,19 +221,24 @@ func ParsePodDeleteDelay(annotations map[string]string) int64 {
return delay
}
delayStr, ok := annotations[KeelPodDeleteDelay]
if ok {
if !ok {
return delay
}

g, err := strconv.Atoi(delayStr)
if err != nil {
return delay
}
g, err := strconv.Atoi(delayStr)
if err != nil {
return delay
}

if g > 0 && g < 600 {
return int64(g)
}
if g < 1 {
return delay
}

if int64(g) > KeelPodMaxDelay {
return KeelPodMaxDelay
}
return int64(g)

return delay
}

// ParsePodTerminationGracePeriod - parses pod termination time in seconds
Expand Down
44 changes: 44 additions & 0 deletions types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,47 @@ func TestParseEventNotificationChannels(t *testing.T) {
})
}
}

func TestParsePodDeleteDelay(t *testing.T) {
type args struct {
annotations map[string]string
}
tests := []struct {
name string
args args
want int64
}{
{
name: "not specified",
args: args{map[string]string{}},
want: 0,
},
{
name: "string",
args: args{map[string]string{KeelPodDeleteDelay: "aa"}},
want: 0,
},
{
name: "10",
args: args{map[string]string{KeelPodDeleteDelay: "10"}},
want: 10,
},
{
name: "-10",
args: args{map[string]string{KeelPodDeleteDelay: "-10"}},
want: 0,
},
{
name: "over max",
args: args{map[string]string{KeelPodDeleteDelay: "50000"}},
want: KeelPodMaxDelay,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ParsePodDeleteDelay(tt.args.annotations); got != tt.want {
t.Errorf("ParsePodDeleteDelay() = %v, want %v", got, tt.want)
}
})
}
}
20 changes: 20 additions & 0 deletions util/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,26 @@ func TestShouldUpdate(t *testing.T) {
want: false,
wantErr: false,
},
{
name: "parsed prerelease patch increase, policy minor, no prerelease",
args: args{
current: MustParse("v1.0.0"),
new: MustParse("v1.0.1-metadata"),
policy: types.PolicyTypeMinor,
},
want: false,
wantErr: false,
},
{
name: "parsed prerelease minor increase, policy minor, both have metadata",
args: args{
current: MustParse("v1.0.0-metadata"),
new: MustParse("v1.0.1-metadata"),
policy: types.PolicyTypeMinor,
},
want: true,
wantErr: false,
},
{
name: "prerelease patch increase, policy minor",
args: args{
Expand Down

0 comments on commit 1aea4ba

Please sign in to comment.