Skip to content
This repository has been archived by the owner on Nov 6, 2019. It is now read-only.

Commit

Permalink
Add pinned version filter for ip (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
҈҈҈Luiz Branco authored Oct 8, 2018
1 parent 400a4f0 commit 745a142
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 11 deletions.
14 changes: 14 additions & 0 deletions apis/v1alpha1/image_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ func (m *ImagePolicyMatchMapping) Map(from string) (string, error) {
// ImagePolicyFilter will define how we can filter where images come from
type ImagePolicyFilter struct {
GitHub *v1.ObjectReference `json:"github,omitempty"`
Pinned *SemVerRelease `json:"pinned,omitempty"`
}

// ContainerRegistry will define how to fetch images from a container registry.
Expand Down Expand Up @@ -254,6 +255,19 @@ var filterValidationSchema = v1beta1.JSONSchemaProps{
{
Required: []string{"github"},
},
{
Required: []string{"pinned"},
Properties: map[string]v1beta1.JSONSchemaProps{
"pinned": {
Required: []string{"version"},
Properties: map[string]v1beta1.JSONSchemaProps{
"version": {
Type: "string",
},
},
},
},
},
},
}

Expand Down
36 changes: 26 additions & 10 deletions internal/imagepolicy/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,6 @@ func (c *Controller) run(ctx context.Context) {
func (c *Controller) syncPolicy(obj interface{}) error {
ip := obj.(*v1alpha1.ImagePolicy).DeepCopy()

repo, err := getGithubRepository(c.patcher, ip)
if err != nil {
c.logger.Printf("Could not retrieve GithubRepository for %s: %s", ip.Name, err)
return nil
}

registry, err := getRegistry(c.patcher, ip)
if err != nil {
c.logger.Printf("Could not retrieve registry for %s: %s", ip.Name, err)
Expand All @@ -152,10 +146,32 @@ func (c *Controller) syncPolicy(obj interface{}) error {
return nil
}

ip.Status.Releases, err = filterImages(ip.Spec.Image, ip.Spec.Match, repo, registry, vp)
if err != nil {
c.logger.Printf("Could not filter images for %s: %s", ip.Name, err)
return nil
switch {
case ip.Spec.Filter.GitHub != nil:
repo, err := getGithubRepository(c.patcher, ip)
if err != nil {
c.logger.Printf("Could not retrieve GithubRepository for %s: %s", ip.Name, err)
return nil
}

ip.Status.Releases, err = filterImages(ip.Spec.Image, ip.Spec.Match, repo, registry, vp)
if err != nil {
c.logger.Printf("Could not filter images for %s: %s", ip.Name, err)
return nil
}

case ip.Spec.Filter.Pinned != nil:
pinned := ip.Spec.Filter.Pinned

r := v1alpha1.Release{
SemVer: pinned,
Level: vp.Spec.SemVer.Level,
Image: ip.Spec.Image + ":" + pinned.Version,
}

ip.Status.Releases = []v1alpha1.Release{r}
default:
return errors.New("image spec filter not defined")
}

// need to specify types again until we resolve the mapping issue
Expand Down
84 changes: 83 additions & 1 deletion internal/imagepolicy/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,76 @@ func TestController_SyncPolicy(t *testing.T) {
},
},
},
{
scenario: "when using a pinned version",
policy: &v1alpha1.ImagePolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "ip-test",
},
Spec: v1alpha1.ImagePolicySpec{
Filter: v1alpha1.ImagePolicyFilter{
Pinned: &v1alpha1.SemVerRelease{
Version: "1.2.3",
},
},
VersioningPolicy: v1.ObjectReference{
Namespace: "vp",
},
ContainerRegistry: &v1alpha1.ContainerRegistry{
Name: "mock",
ImagePullSecrets: []v1.LocalObjectReference{
{
Name: "secret",
},
},
},
},
},
patcher: &mockPatchClient{
GetFn: func(v interface{}, ns, name string) error {
if ns == "vp" {
vp := v.(*v1alpha1.VersioningPolicy)
vp.Spec = v1alpha1.VersioningPolicySpec{
SemVer: &v1alpha1.SemVerSource{
Level: v1alpha1.SemVerLevelRelease,
},
}

return nil
}

return nil
},
ApplyFn: func(runtime.Object, ...patcher.OptionFunc) ([]byte, error) {
return nil, nil
},
},
},
{
scenario: "when no filter is defined",
policy: &v1alpha1.ImagePolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "ip-test",
},
Spec: v1alpha1.ImagePolicySpec{
Filter: v1alpha1.ImagePolicyFilter{},
ContainerRegistry: &v1alpha1.ContainerRegistry{
Name: "mock",
ImagePullSecrets: []v1.LocalObjectReference{
{
Name: "secret",
},
},
},
},
},
patcher: &mockPatchClient{
GetFn: func(v interface{}, ns, name string) error {
return nil
},
},
err: errors.New("image spec filter not defined"),
},
{
scenario: "when github repo fails",
policy: &v1alpha1.ImagePolicy{
Expand All @@ -73,11 +143,23 @@ func TestController_SyncPolicy(t *testing.T) {
Namespace: "websites",
},
},
ContainerRegistry: &v1alpha1.ContainerRegistry{
Name: "mock",
ImagePullSecrets: []v1.LocalObjectReference{
{
Name: "secret",
},
},
},
},
},
patcher: &mockPatchClient{
GetFn: func(v interface{}, ns, name string) error {
return errors.New("github repo not found")
if ns == "websites" {
return errors.New("github repo not found")
}

return nil
},
},
log: "Could not retrieve GithubRepository for ip-test: github repo not found\n",
Expand Down

0 comments on commit 745a142

Please sign in to comment.