deploy with "keel.sh/match-tag" annotations

pull/191/head
Adrien Delorme 2018-04-16 10:59:01 +02:00
parent 1aea4ba9c7
commit a8d85483ff
3 changed files with 120 additions and 6 deletions

View File

@ -14,7 +14,7 @@ import (
func (p *Provider) checkUnversionedDeployment(policy types.PolicyType, repo *types.Repository, deployment v1beta1.Deployment) (updatePlan *UpdatePlan, shouldUpdateDeployment bool, err error) {
updatePlan = &UpdatePlan{}
eventRepoRef, err := image.Parse(repo.Name)
eventRepoRef, err := image.Parse(repo.String())
if err != nil {
return
}
@ -70,6 +70,14 @@ func (p *Provider) checkUnversionedDeployment(policy types.PolicyType, repo *typ
}
}
// updating annotations
annotations := deployment.GetAnnotations()
if _, ok := annotations[types.KeelForceTagMatchLabel]; ok {
if containerImageRef.Tag() != eventRepoRef.Tag() {
continue
}
}
// updating image
if containerImageRef.Registry() == image.DefaultRegistryHostname {
c.Image = fmt.Sprintf("%s:%s", containerImageRef.ShortName(), repo.Tag)
@ -81,8 +89,6 @@ func (p *Provider) checkUnversionedDeployment(policy types.PolicyType, repo *typ
// marking this deployment for update
shouldUpdateDeployment = true
// updating annotations
annotations := deployment.GetAnnotations()
// updating digest if available
if repo.Digest != "" {

View File

@ -271,6 +271,104 @@ func TestProvider_checkUnversionedDeployment(t *testing.T) {
wantShouldUpdateDeployment: true,
wantErr: false,
},
{
name: "poll trigger, force-match, same tag",
args: args{
policy: types.PolicyTypeForce,
repo: &types.Repository{Name: "karolisr/keel", Tag: "latest-staging"},
deployment: v1beta1.Deployment{
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",
},
},
v1beta1.DeploymentSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "karolisr/keel:latest-staging",
},
},
},
},
},
v1beta1.DeploymentStatus{},
},
},
wantUpdatePlan: &UpdatePlan{
Deployment: v1beta1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{
types.KeelPollScheduleAnnotation: types.KeelPollDefaultSchedule,
types.KeelForceTagMatchLabel: "yup",
forceUpdateImageAnnotation: "karolisr/keel:latest-staging",
},
Labels: map[string]string{types.KeelPolicyLabel: "force"},
},
v1beta1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "karolisr/keel:latest-staging",
},
},
},
},
},
v1beta1.DeploymentStatus{},
},
NewVersion: "latest-staging",
CurrentVersion: "latest-staging",
},
wantShouldUpdateDeployment: true,
wantErr: false,
},
{
name: "poll trigger, force-match, different tag",
args: args{
policy: types.PolicyTypeForce,
repo: &types.Repository{Name: "karolisr/keel", Tag: "latest-staging"},
deployment: v1beta1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{types.KeelPollScheduleAnnotation: types.KeelPollDefaultSchedule},
Labels: map[string]string{types.KeelPolicyLabel: "force"},
},
v1beta1.DeploymentSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "karolisr/keel:latest-acceptance",
},
},
},
},
},
v1beta1.DeploymentStatus{},
},
},
wantUpdatePlan: &UpdatePlan{
Deployment: v1beta1.Deployment{},
},
wantShouldUpdateDeployment: false,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -283,14 +381,14 @@ func TestProvider_checkUnversionedDeployment(t *testing.T) {
}
gotUpdatePlan, gotShouldUpdateDeployment, err := p.checkUnversionedDeployment(tt.args.policy, tt.args.repo, tt.args.deployment)
if (err != nil) != tt.wantErr {
t.Errorf("Provider.checkUnversionedDeployment() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("Provider.checkUnversionedDeployment() error = %#v, wantErr %#v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotUpdatePlan, tt.wantUpdatePlan) {
t.Errorf("Provider.checkUnversionedDeployment() gotUpdatePlan = %v, want %v", gotUpdatePlan, tt.wantUpdatePlan)
t.Errorf("Provider.checkUnversionedDeployment() gotUpdatePlan = %#v, want %#v", gotUpdatePlan, tt.wantUpdatePlan)
}
if gotShouldUpdateDeployment != tt.wantShouldUpdateDeployment {
t.Errorf("Provider.checkUnversionedDeployment() gotShouldUpdateDeployment = %v, want %v", gotShouldUpdateDeployment, tt.wantShouldUpdateDeployment)
t.Errorf("Provider.checkUnversionedDeployment() gotShouldUpdateDeployment = %#v, want %#v", gotShouldUpdateDeployment, tt.wantShouldUpdateDeployment)
}
})
}

View File

@ -25,6 +25,8 @@ const KeelPolicyLabel = "keel.sh/policy"
// changes
const KeelTriggerLabel = "keel.sh/trigger"
const KeelForceTagMatchLabel = "keel.sh/match-tag"
// KeelPollScheduleAnnotation - optional variable to setup custom schedule for polling, defaults to @every 10m
const KeelPollScheduleAnnotation = "keel.sh/pollSchedule"
@ -67,6 +69,14 @@ type Repository struct {
Digest string `json:"digest"` // optional digest field
}
// String gives you team/repo[:tag] identifier
func (r *Repository) String() string {
if r.Tag != "" {
return r.Name + ":" + r.Tag
}
return r.Name
}
// Event - holds information about new event from trigger
type Event struct {
Repository Repository `json:"repository,omitempty"`