Skip to content

Commit

Permalink
testing, interval schedule moved to annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
rusenask committed Jul 4, 2017
1 parent 3311300 commit 616e43a
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 19 deletions.
5 changes: 5 additions & 0 deletions provider/kubernetes/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func Test_addImageToPull(t *testing.T) {
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) {
Expand Down
112 changes: 105 additions & 7 deletions provider/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,16 +508,17 @@ func TestGetImpactedUntaggedImage(t *testing.T) {
v1beta1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Labels: map[string]string{types.KeelPolicyLabel: "all"},
Name: "dep-1",
Namespace: "xxxx",
Labels: map[string]string{types.KeelPolicyLabel: "all"},
Annotations: map[string]string{},
},
v1beta1.DeploymentSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "gcr.io/v2-namespace/hello-world",
Image: "gcr.io/v2-namespace/foo-world",
},
},
},
Expand All @@ -528,9 +529,10 @@ func TestGetImpactedUntaggedImage(t *testing.T) {
v1beta1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-2",
Namespace: "xxxx",
Labels: map[string]string{types.KeelPolicyLabel: "all"},
Name: "dep-2",
Namespace: "xxxx",
Annotations: map[string]string{},
Labels: map[string]string{types.KeelPolicyLabel: "all"},
},
v1beta1.DeploymentSpec{
Template: v1.PodTemplateSpec{
Expand Down Expand Up @@ -583,3 +585,99 @@ func TestGetImpactedUntaggedImage(t *testing.T) {
}

}

// test to check whether we get impacted deployment when it's untagged (we should)
func TestGetImpactedUntaggedOneImage(t *testing.T) {
fp := &fakeImplementer{}
fp.namespaces = &v1.NamespaceList{
Items: []v1.Namespace{
v1.Namespace{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{Name: "xxxx"},
v1.NamespaceSpec{},
v1.NamespaceStatus{},
},
},
}
fp.deploymentList = &v1beta1.DeploymentList{
Items: []v1beta1.Deployment{
v1beta1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Labels: map[string]string{types.KeelPolicyLabel: "all"},
Annotations: map[string]string{},
},
v1beta1.DeploymentSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "gcr.io/v2-namespace/hello-world",
},
},
},
},
},
v1beta1.DeploymentStatus{},
},
v1beta1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-2",
Namespace: "xxxx",
Annotations: map[string]string{},
Labels: map[string]string{types.KeelPolicyLabel: "all"},
},
v1beta1.DeploymentSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "gcr.io/v2-namespace/hello-world:1.1.1",
},
},
},
},
},
v1beta1.DeploymentStatus{},
},
},
}

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

// creating "new version" event
repo := &types.Repository{
Name: "gcr.io/v2-namespace/hello-world",
Tag: "1.1.2",
}

deps, err := provider.impactedDeployments(repo)
if err != nil {
t.Errorf("failed to get deployments: %s", err)
}

if len(deps) != 2 {
t.Errorf("expected to find 2 deployment but found %s", len(deps))
}

found := false
for _, c := range deps[0].Spec.Template.Spec.Containers {

containerImageName := versionreg.ReplaceAllString(c.Image, "")

if containerImageName == repo.Name {
found = true
}
}

if !found {
t.Errorf("couldn't find expected deployment in impacted deployment list")
}

}
10 changes: 6 additions & 4 deletions provider/kubernetes/versioned_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ func (p *Provider) checkVersionedDeployment(newVersion *types.Version, policy ty
}

// if policy is force, don't bother with version checking
if policy == types.PolicyTypeForce {
// same with `latest` images, update them to versioned ones
if policy == types.PolicyTypeForce || conatinerImageRef.Tag() == "latest" {
c = updateContainer(c, conatinerImageRef, newVersion.String())

deployment.Spec.Template.Spec.Containers[idx] = c
Expand Down Expand Up @@ -94,9 +95,10 @@ func (p *Provider) checkVersionedDeployment(newVersion *types.Version, policy ty
currentVersion, err := version.GetVersionFromImageName(c.Image)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"image_name": c.Image,
"keel_policy": policy,
"error": err,
"container_image": c.Image,
"container_image_tag": conatinerImageRef.Tag(),
"keel_policy": policy,
}).Error("provider.kubernetes: failed to get image version, is it tagged as semver?")
continue
}
Expand Down
4 changes: 2 additions & 2 deletions trigger/poll/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ func (s *DefaultManager) scan(ctx context.Context) error {

// checkDeployment - checks whether we are already watching for this deployment
func (s *DefaultManager) checkDeployment(deployment *v1beta1.Deployment) error {
labels := deployment.GetLabels()
annotations := deployment.GetAnnotations()

for _, c := range deployment.Spec.Template.Spec.Containers {

schedule, ok := labels[types.KeelPollScheduleLabel]
schedule, ok := annotations[types.KeelPollScheduleAnnotation]
if ok {
_, err := cron.Parse(schedule)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions trigger/poll/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package poll
import (
"testing"

"github.com/rusenask/keel/util/image"
"github.com/rusenask/keel/provider"
"github.com/rusenask/keel/registry"
"github.com/rusenask/keel/types"
"github.com/rusenask/keel/util/image"
)

// ======== fake registry client for testing =======
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestWatchTagJob(t *testing.T) {

submitted := fp.submitted[0]

if submitted.Repository.Name != "docker.io/foo/bar:1.1" {
if submitted.Repository.Name != "index.docker.io/foo/bar:1.1" {
t.Errorf("unexpected event repository name: %s", submitted.Repository.Name)
}

Expand Down Expand Up @@ -105,7 +105,7 @@ func TestWatchTagJobLatest(t *testing.T) {

submitted := fp.submitted[0]

if submitted.Repository.Name != "docker.io/foo/bar:latest" {
if submitted.Repository.Name != "index.docker.io/foo/bar:latest" {
t.Errorf("unexpected event repository name: %s", submitted.Repository.Name)
}

Expand Down
4 changes: 2 additions & 2 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const KeelPolicyLabel = "keel.sh/policy"
// changes
const KeelTriggerLabel = "keel.sh/trigger"

// KeelPollScheduleLabel - optional variable to setup custom schedule for polling, defaults to @every 10m
const KeelPollScheduleLabel = "keel.sh/pollSchedule"
// KeelPollScheduleAnnotation - optional variable to setup custom schedule for polling, defaults to @every 10m
const KeelPollScheduleAnnotation = "keel.sh/pollSchedule"

// KeelPollDefaultSchedule - defaul polling schedule
const KeelPollDefaultSchedule = "@every 1m"
Expand Down
7 changes: 6 additions & 1 deletion types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ func TestParsePolicy(t *testing.T) {
{
name: "random",
args: args{policy: "rand"},
want: PolicyTypeUnknown,
want: PolicyTypeNone,
},
{
name: "force",
args: args{policy: "force"},
want: PolicyTypeForce,
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 616e43a

Please sign in to comment.