feature/regexp_matching
Karolis Rusenas 2018-09-02 21:13:54 +01:00
parent 31ae67f3ba
commit c37a11f006
5 changed files with 0 additions and 1475 deletions

View File

@ -1,106 +0,0 @@
package kubernetes
import (
"fmt"
"github.com/keel-hq/keel/internal/k8s"
"github.com/keel-hq/keel/types"
"github.com/keel-hq/keel/util/image"
log "github.com/sirupsen/logrus"
)
func (p *Provider) checkUnversionedDeployment(policy types.PolicyType, repo *types.Repository, resource *k8s.GenericResource) (updatePlan *UpdatePlan, shouldUpdateDeployment bool, err error) {
updatePlan = &UpdatePlan{}
eventRepoRef, err := image.Parse(repo.String())
if err != nil {
return
}
labels := resource.GetLabels()
log.WithFields(log.Fields{
"labels": labels,
"name": resource.Name,
"namespace": resource.Namespace,
"kind": resource.Kind(),
"policy": policy,
}).Info("provider.kubernetes.checkVersionedDeployment: keel policy found, checking resource...")
annotations := resource.GetAnnotations()
shouldUpdateDeployment = false
// for idx, c := range deployment.Spec.Template.Spec.Containers {
for idx, c := range resource.Containers() {
containerImageRef, err := image.Parse(c.Image)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"image_name": c.Image,
}).Error("provider.kubernetes: failed to parse image name")
continue
}
log.WithFields(log.Fields{
"name": resource.Name,
"namespace": resource.Namespace,
"kind": resource.Kind(),
"parsed_image_name": containerImageRef.Remote(),
"target_image_name": repo.Name,
"target_tag": repo.Tag,
"policy": policy,
"image": c.Image,
}).Info("provider.kubernetes: checking image")
if containerImageRef.Repository() != eventRepoRef.Repository() {
log.WithFields(log.Fields{
"parsed_image_name": containerImageRef.Remote(),
"target_image_name": repo.Name,
}).Info("provider.kubernetes: images do not match, ignoring")
continue
}
// if poll trigger is used, also checking for matching versions
if _, ok := annotations[types.KeelPollScheduleAnnotation]; ok {
if repo.Tag != containerImageRef.Tag() {
fmt.Printf("tags different, not updating (%s != %s) \n", eventRepoRef.Tag(), containerImageRef.Tag())
continue
}
}
// updating annotations
if matchTag, _ := labels[types.KeelForceTagMatchLabel]; matchTag == "true" {
if containerImageRef.Tag() != eventRepoRef.Tag() {
continue
}
}
// updating spec template annotations
setUpdateTime(resource)
// updating image
if containerImageRef.Registry() == image.DefaultRegistryHostname {
resource.UpdateContainer(idx, fmt.Sprintf("%s:%s", containerImageRef.ShortName(), repo.Tag))
} else {
resource.UpdateContainer(idx, fmt.Sprintf("%s:%s", containerImageRef.Repository(), repo.Tag))
}
shouldUpdateDeployment = true
updatePlan.CurrentVersion = containerImageRef.Tag()
updatePlan.NewVersion = repo.Tag
updatePlan.Resource = resource
log.WithFields(log.Fields{
"parsed_image": containerImageRef.Remote(),
"raw_image_name": c.Image,
"target_image": repo.Name,
"target_image_tag": repo.Tag,
"policy": policy,
}).Info("provider.kubernetes: impacted deployment container found")
}
return updatePlan, shouldUpdateDeployment, nil
}

View File

@ -1,595 +0,0 @@
package kubernetes
import (
"reflect"
"testing"
"time"
"github.com/keel-hq/keel/approvals"
"github.com/keel-hq/keel/extension/notification"
"github.com/keel-hq/keel/internal/k8s"
"github.com/keel-hq/keel/types"
"github.com/keel-hq/keel/util/timeutil"
apps_v1 "k8s.io/api/apps/v1"
"k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestProvider_checkUnversionedDeployment(t *testing.T) {
timeutil.Now = func() time.Time {
return time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)
}
defer func() { timeutil.Now = time.Now }()
type fields struct {
implementer Implementer
sender notification.Sender
approvalManager approvals.Manager
events chan *types.Event
stop chan struct{}
}
type args struct {
policy types.PolicyType
repo *types.Repository
resource *k8s.GenericResource
}
tests := []struct {
name string
fields fields
args args
wantUpdatePlan *UpdatePlan
wantShouldUpdateDeployment bool
wantErr bool
}{
{
name: "force update untagged to latest",
args: args{
policy: types.PolicyTypeForce,
repo: &types.Repository{Name: "gcr.io/v2-namespace/hello-world", Tag: "latest"},
resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{},
Labels: map[string]string{types.KeelPolicyLabel: "all"},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "gcr.io/v2-namespace/hello-world",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
},
wantUpdatePlan: &UpdatePlan{
Resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{},
Labels: map[string]string{types.KeelPolicyLabel: "all"},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "gcr.io/v2-namespace/hello-world:latest",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
NewVersion: "latest",
CurrentVersion: "latest",
},
wantShouldUpdateDeployment: true,
wantErr: false,
},
{
name: "different image name ",
args: args{
policy: types.PolicyTypeForce,
repo: &types.Repository{Name: "gcr.io/v2-namespace/hello-world", Tag: "latest"},
resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{},
Labels: map[string]string{types.KeelPolicyLabel: "all"},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "gcr.io/v2-namespace/goodbye-world:earliest",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
},
wantUpdatePlan: &UpdatePlan{
// Resource: &k8s.GenericResource{},
Resource: nil,
},
wantShouldUpdateDeployment: false,
wantErr: false,
},
{
name: "different tag name for poll image",
args: args{
policy: types.PolicyTypeForce,
repo: &types.Repository{Name: "gcr.io/v2-namespace/hello-world", Tag: "master"},
resource: MustParseGR(&apps_v1.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: "all",
},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "gcr.io/v2-namespace/hello-world:alpha",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
},
wantUpdatePlan: &UpdatePlan{
Resource: nil,
},
wantShouldUpdateDeployment: false,
wantErr: false,
},
{
name: "dockerhub short image name ",
args: args{
policy: types.PolicyTypeForce,
repo: &types.Repository{Name: "karolisr/keel", Tag: "0.2.0"},
resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{},
Labels: map[string]string{types.KeelPolicyLabel: "force"},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "karolisr/keel:latest",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
},
wantUpdatePlan: &UpdatePlan{
Resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{},
Labels: map[string]string{types.KeelPolicyLabel: "force"},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "karolisr/keel:0.2.0",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
NewVersion: "0.2.0",
CurrentVersion: "latest",
},
wantShouldUpdateDeployment: true,
wantErr: false,
},
{
name: "poll trigger, same tag",
args: args{
policy: types.PolicyTypeForce,
repo: &types.Repository{Name: "karolisr/keel", Tag: "master"},
resource: MustParseGR(&apps_v1.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"},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "karolisr/keel:master",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
},
wantUpdatePlan: &UpdatePlan{
Resource: MustParseGR(&apps_v1.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"},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "karolisr/keel:master",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
NewVersion: "master",
CurrentVersion: "master",
},
wantShouldUpdateDeployment: true,
wantErr: false,
},
{
name: "pubsub trigger, force-match, same tag",
args: args{
policy: types.PolicyTypeForce,
repo: &types.Repository{Name: "karolisr/keel", Tag: "latest-staging"},
resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Labels: map[string]string{
types.KeelPolicyLabel: "force",
types.KeelForceTagMatchLabel: "true",
},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "karolisr/keel:latest-staging",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
},
wantUpdatePlan: &UpdatePlan{
Resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Labels: map[string]string{
types.KeelForceTagMatchLabel: "true",
types.KeelPolicyLabel: "force",
},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "karolisr/keel:latest-staging",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
NewVersion: "latest-staging",
CurrentVersion: "latest-staging",
},
wantShouldUpdateDeployment: true,
wantErr: false,
},
{
name: "pubsub trigger, force-match, same tag on eu.gcr.io",
args: args{
policy: types.PolicyTypeForce,
repo: &types.Repository{Host: "eu.gcr.io", Name: "karolisr/keel", Tag: "latest-staging"},
resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Labels: map[string]string{
types.KeelPolicyLabel: "force",
types.KeelForceTagMatchLabel: "true",
},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "eu.gcr.io/karolisr/keel:latest-staging",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
},
wantUpdatePlan: &UpdatePlan{
Resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Labels: map[string]string{
types.KeelForceTagMatchLabel: "true",
types.KeelPolicyLabel: "force",
},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
// "time": timeutil.Now().String(),
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "eu.gcr.io/karolisr/keel:latest-staging",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
NewVersion: "latest-staging",
CurrentVersion: "latest-staging",
},
wantShouldUpdateDeployment: true,
wantErr: false,
},
{
name: "pubsub trigger, force-match, different tag",
args: args{
policy: types.PolicyTypeForce,
repo: &types.Repository{Name: "karolisr/keel", Tag: "latest-staging"},
resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Labels: map[string]string{
types.KeelPolicyLabel: "force",
types.KeelForceTagMatchLabel: "true",
},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "karolisr/keel:latest-acceptance",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
},
wantUpdatePlan: &UpdatePlan{
Resource: nil,
},
wantShouldUpdateDeployment: false,
wantErr: false,
},
{
name: "pubsub trigger, force-match, same tag on eu.gcr.io, daemonset",
args: args{
policy: types.PolicyTypeForce,
repo: &types.Repository{Host: "eu.gcr.io", Name: "karolisr/keel", Tag: "latest-staging"},
resource: MustParseGR(&apps_v1.DaemonSet{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Labels: map[string]string{types.KeelPolicyLabel: "force"},
Annotations: map[string]string{
types.KeelForceTagMatchLabel: "true",
},
},
apps_v1.DaemonSetSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "eu.gcr.io/karolisr/keel:latest-staging",
},
},
},
},
},
apps_v1.DaemonSetStatus{},
}),
},
wantUpdatePlan: &UpdatePlan{
Resource: MustParseGR(&apps_v1.DaemonSet{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{
types.KeelForceTagMatchLabel: "true",
},
Labels: map[string]string{types.KeelPolicyLabel: "force"},
},
apps_v1.DaemonSetSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
// "time": timeutil.Now().String(),
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "eu.gcr.io/karolisr/keel:latest-staging",
},
},
},
},
},
apps_v1.DaemonSetStatus{},
}),
NewVersion: "latest-staging",
CurrentVersion: "latest-staging",
},
wantShouldUpdateDeployment: true,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &Provider{
implementer: tt.fields.implementer,
sender: tt.fields.sender,
approvalManager: tt.fields.approvalManager,
events: tt.fields.events,
stop: tt.fields.stop,
}
gotUpdatePlan, gotShouldUpdateDeployment, err := p.checkUnversionedDeployment(tt.args.policy, tt.args.repo, tt.args.resource)
if (err != nil) != tt.wantErr {
t.Errorf("Provider.checkUnversionedDeployment() error = %#v, wantErr %#v", err, tt.wantErr)
return
}
if gotShouldUpdateDeployment {
ann := gotUpdatePlan.Resource.GetSpecAnnotations()
if ann[types.KeelUpdateTimeAnnotation] != "" {
delete(ann, types.KeelUpdateTimeAnnotation)
gotUpdatePlan.Resource.SetSpecAnnotations(ann)
} else {
t.Errorf("Provider.checkUnversionedDeployment() missing types.KeelUpdateTimeAnnotation annotation")
}
}
if !reflect.DeepEqual(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)
}
})
}
}

View File

@ -1,176 +0,0 @@
package kubernetes
import (
"fmt"
"time"
// "k8s.io/api/core/v1"
// "k8s.io/api/extensions/v1beta1"
"github.com/keel-hq/keel/internal/k8s"
"github.com/keel-hq/keel/types"
"github.com/keel-hq/keel/util/image"
"github.com/keel-hq/keel/util/version"
log "github.com/sirupsen/logrus"
)
// func (p *Provider) checkVersionedDeployment(newVersion *types.Version, policy types.PolicyType, repo *types.Repository, deployment v1beta1.Deployment) (updated v1beta1.Deployment, shouldUpdateDeployment bool, err error) {
func (p *Provider) checkVersionedDeployment(newVersion *types.Version, policy types.PolicyType, repo *types.Repository, resource *k8s.GenericResource) (updatePlan *UpdatePlan, shouldUpdateDeployment bool, err error) {
updatePlan = &UpdatePlan{}
eventRepoRef, err := image.Parse(repo.String())
if err != nil {
return
}
labels := resource.GetLabels()
log.WithFields(log.Fields{
"labels": labels,
"name": resource.Name,
"namespace": resource.Namespace,
"kind": resource.Kind(),
"policy": policy,
}).Info("provider.kubernetes.checkVersionedDeployment: keel policy found, checking resource...")
shouldUpdateDeployment = false
for idx, c := range resource.Containers() {
containerImageRef, err := image.Parse(c.Image)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"image_name": c.Image,
}).Error("provider.kubernetes: failed to parse image name")
continue
}
log.WithFields(log.Fields{
"name": resource.Name,
"namespace": resource.Namespace,
"parsed_image_name": containerImageRef.Remote(),
"kind": resource.Kind(),
"target_image_name": repo.Name,
"target_tag": repo.Tag,
"policy": policy,
"image": c.Image,
}).Info("provider.kubernetes: checking image")
if containerImageRef.Repository() != eventRepoRef.Repository() {
log.WithFields(log.Fields{
"parsed_image_name": containerImageRef.Remote(),
"target_image_name": repo.Name,
}).Info("provider.kubernetes: images do not match, ignoring")
continue
}
// if policy is force, don't bother with version checking
// same with `latest` images, update them to versioned ones
if policy == types.PolicyTypeForce || containerImageRef.Tag() == "latest" {
if matchTag, _ := labels[types.KeelForceTagMatchLabel]; matchTag == "true" {
if containerImageRef.Tag() != eventRepoRef.Tag() {
continue
}
}
if containerImageRef.Registry() == image.DefaultRegistryHostname {
resource.UpdateContainer(idx, fmt.Sprintf("%s:%s", containerImageRef.ShortName(), newVersion.String()))
} else {
resource.UpdateContainer(idx, fmt.Sprintf("%s:%s", containerImageRef.Repository(), newVersion.String()))
}
shouldUpdateDeployment = true
setUpdateTime(resource)
log.WithFields(log.Fields{
"parsed_image": containerImageRef.Remote(),
"raw_image_name": c.Image,
"target_image": repo.Name,
"target_image_tag": repo.Tag,
"policy": policy,
}).Info("provider.kubernetes: impacted deployment container found")
updatePlan.CurrentVersion = containerImageRef.Tag()
updatePlan.NewVersion = newVersion.Original
updatePlan.Resource = resource
// success, moving to next container
continue
}
currentVersion, err := version.GetVersionFromImageName(c.Image)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"container_image": c.Image,
"container_image_tag": containerImageRef.Tag(),
"keel_policy": policy,
}).Error("provider.kubernetes: failed to get image version, is it tagged as semver?")
continue
}
log.WithFields(log.Fields{
"labels": labels,
"name": resource.Name,
"namespace": resource.Namespace,
"image": c.Image,
"current_version": currentVersion.String(),
"policy": policy,
}).Info("provider.kubernetes: current image version")
shouldUpdateContainer, err := version.ShouldUpdate(currentVersion, newVersion, policy)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"new_version": newVersion.String(),
"current_version": currentVersion.String(),
"keel_policy": policy,
}).Error("provider.kubernetes: got error while checking whether deployment should be updated")
continue
}
log.WithFields(log.Fields{
"labels": labels,
"name": resource.Name,
"namespace": resource.Namespace,
"image": c.Image,
"current_version": currentVersion.String(),
"new_version": newVersion.String(),
"policy": policy,
"should_update": shouldUpdateContainer,
}).Info("provider.kubernetes: checked version, deciding whether to update")
if shouldUpdateContainer {
if containerImageRef.Registry() == image.DefaultRegistryHostname {
resource.UpdateContainer(idx, fmt.Sprintf("%s:%s", containerImageRef.ShortName(), newVersion.String()))
} else {
resource.UpdateContainer(idx, fmt.Sprintf("%s:%s", containerImageRef.Repository(), newVersion.String()))
}
// marking this deployment for update
shouldUpdateDeployment = true
setUpdateTime(resource)
updatePlan.CurrentVersion = currentVersion.Original
updatePlan.NewVersion = newVersion.Original
updatePlan.Resource = resource
log.WithFields(log.Fields{
"parsed_image": containerImageRef.Remote(),
"raw_image_name": c.Image,
"target_image": repo.Name,
"target_image_tag": repo.Tag,
"policy": policy,
}).Info("provider.kubernetes: impacted deployment container found")
}
}
return updatePlan, shouldUpdateDeployment, nil
}
func setUpdateTime(resource *k8s.GenericResource) {
specAnnotations := resource.GetSpecAnnotations()
specAnnotations[types.KeelUpdateTimeAnnotation] = time.Now().String()
resource.SetSpecAnnotations(specAnnotations)
}

View File

@ -1,530 +0,0 @@
package kubernetes
import (
"reflect"
"testing"
"github.com/keel-hq/keel/approvals"
"github.com/keel-hq/keel/extension/notification"
"github.com/keel-hq/keel/internal/k8s"
"github.com/keel-hq/keel/types"
"github.com/keel-hq/keel/util/version"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apps_v1 "k8s.io/api/apps/v1"
"k8s.io/api/core/v1"
)
func unsafeGetVersion(ver string) *types.Version {
v, err := version.GetVersion(ver)
if err != nil {
panic(err)
}
return v
}
func TestProvider_checkVersionedDeployment(t *testing.T) {
type fields struct {
implementer Implementer
sender notification.Sender
approvalManager approvals.Manager
events chan *types.Event
stop chan struct{}
}
type args struct {
newVersion *types.Version
policy types.PolicyType
repo *types.Repository
resource *k8s.GenericResource
}
tests := []struct {
name string
fields fields
args args
wantUpdatePlan *UpdatePlan
wantShouldUpdateDeployment bool
wantErr bool
}{
{
name: "standard version bump",
args: args{
newVersion: unsafeGetVersion("1.1.2"),
policy: types.PolicyTypeAll,
repo: &types.Repository{Name: "gcr.io/v2-namespace/hello-world", Tag: "1.1.2"},
resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{},
Labels: map[string]string{types.KeelPolicyLabel: "all"},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "gcr.io/v2-namespace/hello-world:1.1.1",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
},
wantUpdatePlan: &UpdatePlan{
Resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{},
Labels: map[string]string{types.KeelPolicyLabel: "all"},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "gcr.io/v2-namespace/hello-world:1.1.2",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
NewVersion: "1.1.2",
CurrentVersion: "1.1.1",
},
wantShouldUpdateDeployment: true,
wantErr: false,
},
{
name: "staging pre-release",
args: args{
newVersion: unsafeGetVersion("v1.1.2-staging"),
policy: types.PolicyTypeMinor,
repo: &types.Repository{Name: "gcr.io/v2-namespace/hello-prerelease", Tag: "v1.1.2-staging"},
resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{},
Labels: map[string]string{types.KeelPolicyLabel: "minor"},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "gcr.io/v2-namespace/hello-prerelease:v1.1.1",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
},
wantUpdatePlan: &UpdatePlan{},
wantShouldUpdateDeployment: false,
wantErr: false,
},
{
name: "normal new tag while there's pre-release",
args: args{
newVersion: unsafeGetVersion("v1.1.2"),
policy: types.PolicyTypeMinor,
repo: &types.Repository{Name: "gcr.io/v2-namespace/hello-prerelease", Tag: "v1.1.2"},
resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{},
Labels: map[string]string{types.KeelPolicyLabel: "minor"},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "gcr.io/v2-namespace/hello-prerelease:v1.1.1-staging",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
},
wantUpdatePlan: &UpdatePlan{},
wantShouldUpdateDeployment: false,
wantErr: false,
},
{
name: "standard ignore version bump",
args: args{
newVersion: unsafeGetVersion("1.1.1"),
policy: types.PolicyTypeAll,
repo: &types.Repository{Name: "gcr.io/v2-namespace/hello-world", Tag: "1.1.1"},
resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{},
Labels: map[string]string{types.KeelPolicyLabel: "all"},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "gcr.io/v2-namespace/hello-world:1.1.1",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
},
wantUpdatePlan: &UpdatePlan{
Resource: nil,
NewVersion: "",
CurrentVersion: "",
},
wantShouldUpdateDeployment: false,
wantErr: false,
},
{
name: "multiple containers, version bump one",
args: args{
newVersion: unsafeGetVersion("1.1.2"),
policy: types.PolicyTypeAll,
repo: &types.Repository{Name: "gcr.io/v2-namespace/hello-world", Tag: "1.1.2"},
resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{},
Labels: map[string]string{types.KeelPolicyLabel: "all"},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "gcr.io/v2-namespace/hello-world:1.1.1",
},
v1.Container{
Image: "yo-world:1.1.1",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
},
wantUpdatePlan: &UpdatePlan{
Resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{},
Labels: map[string]string{types.KeelPolicyLabel: "all"},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "gcr.io/v2-namespace/hello-world:1.1.2",
},
v1.Container{
Image: "yo-world:1.1.1",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
NewVersion: "1.1.2",
CurrentVersion: "1.1.1",
},
wantShouldUpdateDeployment: true,
wantErr: false,
},
{
name: "force update untagged container",
args: args{
newVersion: unsafeGetVersion("1.1.2"),
policy: types.PolicyTypeForce,
repo: &types.Repository{Name: "gcr.io/v2-namespace/hello-world", Tag: "1.1.2"},
resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{},
Labels: map[string]string{types.KeelPolicyLabel: "force"},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "gcr.io/v2-namespace/hello-world:latest",
},
v1.Container{
Image: "yo-world:1.1.1",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
},
wantUpdatePlan: &UpdatePlan{
Resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{},
Labels: map[string]string{types.KeelPolicyLabel: "force"},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "gcr.io/v2-namespace/hello-world:1.1.2",
},
v1.Container{
Image: "yo-world:1.1.1",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
NewVersion: "1.1.2",
CurrentVersion: "latest",
},
wantShouldUpdateDeployment: true,
wantErr: false,
},
{
name: "force update untagged container - match tag",
args: args{
newVersion: unsafeGetVersion("1.1.2"),
policy: types.PolicyTypeForce,
repo: &types.Repository{Name: "gcr.io/v2-namespace/hello-world", Tag: "1.1.2"},
resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{},
Labels: map[string]string{
types.KeelPolicyLabel: "force",
types.KeelForceTagMatchLabel: "true",
},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "gcr.io/v2-namespace/hello-world:1.1.2",
},
v1.Container{
Image: "yo-world:1.1.1",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
},
wantUpdatePlan: &UpdatePlan{
Resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{},
Labels: map[string]string{
types.KeelPolicyLabel: "force",
types.KeelForceTagMatchLabel: "true",
},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "gcr.io/v2-namespace/hello-world:1.1.2",
},
v1.Container{
Image: "yo-world:1.1.1",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
NewVersion: "1.1.2",
CurrentVersion: "1.1.2",
},
wantShouldUpdateDeployment: true,
wantErr: false,
},
{
name: "don't force update untagged container - match tag",
args: args{
newVersion: unsafeGetVersion("1.1.3"),
policy: types.PolicyTypeForce,
repo: &types.Repository{Name: "gcr.io/v2-namespace/hello-world", Tag: "1.1.3"},
resource: MustParseGR(&apps_v1.Deployment{
meta_v1.TypeMeta{},
meta_v1.ObjectMeta{
Name: "dep-1",
Namespace: "xxxx",
Annotations: map[string]string{},
Labels: map[string]string{
types.KeelPolicyLabel: "force",
types.KeelForceTagMatchLabel: "true",
},
},
apps_v1.DeploymentSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{
"this": "that",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Image: "gcr.io/v2-namespace/hello-world:1.1.2",
},
v1.Container{
Image: "yo-world:1.1.1",
},
},
},
},
},
apps_v1.DeploymentStatus{},
}),
},
wantUpdatePlan: &UpdatePlan{
Resource: nil,
NewVersion: "",
CurrentVersion: "",
},
wantShouldUpdateDeployment: false,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &Provider{
implementer: tt.fields.implementer,
sender: tt.fields.sender,
approvalManager: tt.fields.approvalManager,
events: tt.fields.events,
stop: tt.fields.stop,
}
gotUpdatePlan, gotShouldUpdateDeployment, err := p.checkVersionedDeployment(tt.args.newVersion, tt.args.policy, tt.args.repo, tt.args.resource)
if (err != nil) != tt.wantErr {
t.Errorf("Provider.checkVersionedDeployment() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotShouldUpdateDeployment {
ann := gotUpdatePlan.Resource.GetSpecAnnotations()
_, ok := ann[types.KeelUpdateTimeAnnotation]
if ok {
delete(ann, types.KeelUpdateTimeAnnotation)
gotUpdatePlan.Resource.SetSpecAnnotations(ann)
} else {
t.Errorf("Provider.checkVersionedDeployment() missing types.KeelUpdateTimeAnnotation annotation")
}
}
if !reflect.DeepEqual(gotUpdatePlan, tt.wantUpdatePlan) {
t.Errorf("Provider.checkVersionedDeployment() gotUpdatePlan = %v, want %v", gotUpdatePlan, tt.wantUpdatePlan)
}
if gotShouldUpdateDeployment != tt.wantShouldUpdateDeployment {
t.Errorf("Provider.checkVersionedDeployment() gotShouldUpdateDeployment = %v, want %v", gotShouldUpdateDeployment, tt.wantShouldUpdateDeployment)
}
})
}
}

View File

@ -1,68 +0,0 @@
// generated by jsonenums -type=PolicyType; DO NOT EDIT
package types
import (
"encoding/json"
"fmt"
)
var (
_PolicyTypeNameToValue = map[string]PolicyType{
"PolicyTypeNone": PolicyTypeNone,
"PolicyTypeAll": PolicyTypeAll,
"PolicyTypeMajor": PolicyTypeMajor,
"PolicyTypeMinor": PolicyTypeMinor,
"PolicyTypePatch": PolicyTypePatch,
"PolicyTypeForce": PolicyTypeForce,
}
_PolicyTypeValueToName = map[PolicyType]string{
PolicyTypeNone: "PolicyTypeNone",
PolicyTypeAll: "PolicyTypeAll",
PolicyTypeMajor: "PolicyTypeMajor",
PolicyTypeMinor: "PolicyTypeMinor",
PolicyTypePatch: "PolicyTypePatch",
PolicyTypeForce: "PolicyTypeForce",
}
)
func init() {
var v PolicyType
if _, ok := interface{}(v).(fmt.Stringer); ok {
_PolicyTypeNameToValue = map[string]PolicyType{
interface{}(PolicyTypeNone).(fmt.Stringer).String(): PolicyTypeNone,
interface{}(PolicyTypeAll).(fmt.Stringer).String(): PolicyTypeAll,
interface{}(PolicyTypeMajor).(fmt.Stringer).String(): PolicyTypeMajor,
interface{}(PolicyTypeMinor).(fmt.Stringer).String(): PolicyTypeMinor,
interface{}(PolicyTypePatch).(fmt.Stringer).String(): PolicyTypePatch,
interface{}(PolicyTypeForce).(fmt.Stringer).String(): PolicyTypeForce,
}
}
}
// MarshalJSON is generated so PolicyType satisfies json.Marshaler.
func (r PolicyType) MarshalJSON() ([]byte, error) {
if s, ok := interface{}(r).(fmt.Stringer); ok {
return json.Marshal(s.String())
}
s, ok := _PolicyTypeValueToName[r]
if !ok {
return nil, fmt.Errorf("invalid PolicyType: %d", r)
}
return json.Marshal(s)
}
// UnmarshalJSON is generated so PolicyType satisfies json.Unmarshaler.
func (r *PolicyType) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("PolicyType should be a string, got %s", data)
}
v, ok := _PolicyTypeNameToValue[s]
if !ok {
return fmt.Errorf("invalid PolicyType %q", s)
}
*r = v
return nil
}