Skip to content

Commit

Permalink
Merge pull request #27 from rusenask/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
rusenask authored Jul 4, 2017
2 parents caddc23 + 8671102 commit 65abc3b
Show file tree
Hide file tree
Showing 981 changed files with 212,552 additions and 119 deletions.
4 changes: 4 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package constants

// DefaultDockerRegistry - default docker registry
const DefaultDockerRegistry = "https://index.docker.io"
13 changes: 9 additions & 4 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import:
version: a94a7ac054dc76e5ab6cf170a9d82faba8aaf33f
subpackages:
- status
- package: github.com/coreos/go-semver
- package: github.com/sirupsen/logrus
subpackages:
- semver
- formatters
- formatters/logstash
- package: github.com/gorilla/mux
- package: github.com/urfave/negroni
- package: golang.org/x/net
Expand All @@ -46,7 +47,11 @@ import:
- pkg/apis/extensions/v1beta1
- rest
- tools/clientcmd
- package: github.com/docker/distribution
version: ^2.6.1
subpackages:
- digest
- package: github.com/Masterminds/semver
version: ^1.3.0
- package: github.com/sirupsen/logrus
version: ^1.0.0
- package: github.com/opencontainers/go-digest
version: ^1.0.0-rc0
14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (

"github.com/rusenask/keel/provider"
"github.com/rusenask/keel/provider/kubernetes"
"github.com/rusenask/keel/registry"
"github.com/rusenask/keel/trigger/http"
"github.com/rusenask/keel/trigger/poll"
"github.com/rusenask/keel/trigger/pubsub"
"github.com/rusenask/keel/types"
"github.com/rusenask/keel/version"
Expand All @@ -20,6 +22,7 @@ import (
// gcloud pubsub related config
const (
EnvTriggerPubSub = "PUBSUB" // set to 1 or something to enable pub/sub trigger
EnvTriggerPoll = "POLL" // set to 1 or something to enable poll trigger
EnvProjectID = "PROJECT_ID"
)

Expand Down Expand Up @@ -154,6 +157,17 @@ func setupTriggers(ctx context.Context, k8sImplementer kubernetes.Implementer, p
go subManager.Start(ctx)
}

if os.Getenv(EnvTriggerPoll) != "" {

registryClient := registry.New()
watcher := poll.NewRepositoryWatcher(providers, registryClient)
pollManager := poll.NewPollManager(k8sImplementer, watcher)

// start poll manager, will finish with ctx
go watcher.Start(ctx)
go pollManager.Start(ctx)
}

teardown = func() {
whs.Stop()
}
Expand Down
36 changes: 36 additions & 0 deletions provider/kubernetes/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package kubernetes

import (
"strings"
)

func addImageToPull(annotations map[string]string, image string) map[string]string {
existing, ok := annotations[forceUpdateImageAnnotation]
if ok {
// check if it's already there
if shouldPullImage(annotations, image) {
// skipping
return annotations
}

annotations[forceUpdateImageAnnotation] = existing + "," + image
return annotations
}
annotations[forceUpdateImageAnnotation] = image
return annotations
}

func shouldPullImage(annotations map[string]string, image string) bool {
imagesStr, ok := annotations[forceUpdateImageAnnotation]
if !ok {
return false
}

images := strings.Split(imagesStr, ",")
for _, img := range images {
if img == image {
return true
}
}
return false
}
76 changes: 76 additions & 0 deletions provider/kubernetes/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package kubernetes

import (
"reflect"
"testing"
)

func Test_addImageToPull(t *testing.T) {
type args struct {
annotations map[string]string
image string
}
tests := []struct {
name string
args args
want map[string]string
}{
{
name: "empty",
args: args{annotations: make(map[string]string), image: "whatever"},
want: map[string]string{forceUpdateImageAnnotation: "whatever"},
},
{
name: "not empty",
args: args{annotations: map[string]string{forceUpdateImageAnnotation: "foo"}, image: "bar"},
want: map[string]string{forceUpdateImageAnnotation: "foo,bar"},
},
{
name: "not empty with same image",
args: args{annotations: map[string]string{forceUpdateImageAnnotation: "foo"}, image: "foo"},
want: map[string]string{forceUpdateImageAnnotation: "foo"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := addImageToPull(tt.args.annotations, tt.args.image); !reflect.DeepEqual(got, tt.want) {
t.Errorf("addImageToPull() = %v, want %v", got, tt.want)
}
})
}
}

func Test_shouldPullImage(t *testing.T) {
type args struct {
annotations map[string]string
image string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "should pull single image",
args: args{annotations: map[string]string{forceUpdateImageAnnotation: "bar"}, image: "bar"},
want: true,
},
{
name: "should pull multiple image",
args: args{annotations: map[string]string{forceUpdateImageAnnotation: "foo,bar,whatever"}, image: "bar"},
want: true,
},
{
name: "should not pull multiple image",
args: args{annotations: map[string]string{forceUpdateImageAnnotation: "foo,bar,whatever"}, image: "alpha"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := shouldPullImage(tt.args.annotations, tt.args.image); got != tt.want {
t.Errorf("shouldPullImage() = %v, want %v", got, tt.want)
}
})
}
}
Loading

0 comments on commit 65abc3b

Please sign in to comment.