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

Commit

Permalink
Add functions to synthesize Image resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmoor committed Sep 14, 2018
1 parent 227402d commit f719ff4
Show file tree
Hide file tree
Showing 6 changed files with 419 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions pkg/reconciler/buildtemplate/resources/imagecache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright 2018 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resources

import (
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/knative/pkg/kmeta"

"github.com/knative/build/pkg/apis/build/v1alpha1"
"github.com/knative/build/pkg/reconciler/buildtemplate/resources/names"
caching "github.com/knative/caching/pkg/apis/caching/v1alpha1"
)

// Note: namespace is passed separately because this may be used for
// cluster-scoped stuff as well.
func makeImageCachesFromSpec(
namespace string,
bt names.ImageCacheable,
) []caching.Image {
var caches []caching.Image

// Avoid duplicates.
images := make(map[string]struct{})

for index, container := range bt.TemplateSpec().Steps {
// TODO(mattmoor): Consider substituting default values to
// get more caching when substitutions are used in image names.
if strings.Contains(container.Image, "$") {
// Skip image names containing substitutions.
continue
}
if _, ok := images[container.Image]; ok {
continue
}
images[container.Image] = struct{}{}

caches = append(caches, caching.Image{
ObjectMeta: metav1.ObjectMeta{
Name: names.ImageCache(bt, index),
Namespace: namespace,
Labels: kmeta.MakeVersionLabels(bt),
OwnerReferences: []metav1.OwnerReference{*kmeta.NewOwnerReference(bt)},
},
Spec: caching.ImageSpec{
Image: container.Image,
},
})
}
return caches
}

func MakeImageCaches(bt *v1alpha1.BuildTemplate) []caching.Image {
return makeImageCachesFromSpec(bt.Namespace, bt)
}
235 changes: 235 additions & 0 deletions pkg/reconciler/buildtemplate/resources/imagecache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
/*
Copyright 2018 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resources

import (
"testing"

"github.com/google/go-cmp/cmp"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/knative/build/pkg/apis/build/v1alpha1"
caching "github.com/knative/caching/pkg/apis/caching/v1alpha1"
)

func TestMakeImageCache(t *testing.T) {
boolTrue := true

tests := []struct {
name string
bt *v1alpha1.BuildTemplate
want []caching.Image
}{{
name: "no container",
bt: &v1alpha1.BuildTemplate{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar",
UID: "1234",
ResourceVersion: "asdf",
},
Spec: v1alpha1.BuildTemplateSpec{},
},
}, {
name: "single container",
bt: &v1alpha1.BuildTemplate{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar",
UID: "1234",
ResourceVersion: "asdf",
},
Spec: v1alpha1.BuildTemplateSpec{
Steps: []corev1.Container{{
Image: "busybox",
}},
},
},
want: []caching.Image{{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar-asdf-00000",
Labels: map[string]string{
"controller": "1234",
"version": "asdf",
},
OwnerReferences: []metav1.OwnerReference{{
APIVersion: "build.knative.dev/v1alpha1",
Kind: "BuildTemplate",
Name: "bar",
UID: "1234",
Controller: &boolTrue,
BlockOwnerDeletion: &boolTrue,
}},
},
Spec: caching.ImageSpec{
Image: "busybox",
},
}},
}, {
name: "duplicate container",
bt: &v1alpha1.BuildTemplate{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar",
UID: "1234",
ResourceVersion: "asdf",
},
Spec: v1alpha1.BuildTemplateSpec{
Steps: []corev1.Container{{
Image: "busybox",
}, {
Image: "busybox",
}},
},
},
want: []caching.Image{{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar-asdf-00000",
Labels: map[string]string{
"controller": "1234",
"version": "asdf",
},
OwnerReferences: []metav1.OwnerReference{{
APIVersion: "build.knative.dev/v1alpha1",
Kind: "BuildTemplate",
Name: "bar",
UID: "1234",
Controller: &boolTrue,
BlockOwnerDeletion: &boolTrue,
}},
},
Spec: caching.ImageSpec{
Image: "busybox",
},
}},
}, {
name: "multiple containers",
bt: &v1alpha1.BuildTemplate{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar",
UID: "1234",
ResourceVersion: "asdf",
},
Spec: v1alpha1.BuildTemplateSpec{
Steps: []corev1.Container{{
Image: "busybox",
}, {
Image: "helloworld",
}, {
Image: "busybox",
}},
},
},
want: []caching.Image{{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar-asdf-00000",
Labels: map[string]string{
"controller": "1234",
"version": "asdf",
},
OwnerReferences: []metav1.OwnerReference{{
APIVersion: "build.knative.dev/v1alpha1",
Kind: "BuildTemplate",
Name: "bar",
UID: "1234",
Controller: &boolTrue,
BlockOwnerDeletion: &boolTrue,
}},
},
Spec: caching.ImageSpec{
Image: "busybox",
},
}, {
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar-asdf-00001",
Labels: map[string]string{
"controller": "1234",
"version": "asdf",
},
OwnerReferences: []metav1.OwnerReference{{
APIVersion: "build.knative.dev/v1alpha1",
Kind: "BuildTemplate",
Name: "bar",
UID: "1234",
Controller: &boolTrue,
BlockOwnerDeletion: &boolTrue,
}},
},
Spec: caching.ImageSpec{
Image: "helloworld",
},
}},
}, {
name: "containers with substitutions",
bt: &v1alpha1.BuildTemplate{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar",
UID: "1234",
ResourceVersion: "asdf",
},
Spec: v1alpha1.BuildTemplateSpec{
Parameters: []v1alpha1.ParameterSpec{{
Name: "TAG",
}},
Steps: []corev1.Container{{
Image: "busybox",
}, {
Image: "helloworld:${TAG}",
}, {
Image: "busybox",
}},
},
},
want: []caching.Image{{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar-asdf-00000",
Labels: map[string]string{
"controller": "1234",
"version": "asdf",
},
OwnerReferences: []metav1.OwnerReference{{
APIVersion: "build.knative.dev/v1alpha1",
Kind: "BuildTemplate",
Name: "bar",
UID: "1234",
Controller: &boolTrue,
BlockOwnerDeletion: &boolTrue,
}},
},
Spec: caching.ImageSpec{
Image: "busybox",
},
}},
}}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := MakeImageCaches(test.bt)
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("MakeImageCache (-want, +got) = %v", diff)
}
})
}
}
18 changes: 18 additions & 0 deletions pkg/reconciler/buildtemplate/resources/names/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Copyright 2018 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package names holds simple functions for synthesizing resource names.
package names
38 changes: 38 additions & 0 deletions pkg/reconciler/buildtemplate/resources/names/names.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2018 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package names

import (
"fmt"

"github.com/knative/pkg/kmeta"

"github.com/knative/build/pkg/apis/build/v1alpha1"
)

type ImageCacheable interface {
kmeta.OwnerRefable

v1alpha1.Template
}

func ImageCache(tmpl ImageCacheable, step int) string {
return fmt.Sprintf("%s-%s-%05d",
tmpl.GetObjectMeta().GetName(),
tmpl.GetObjectMeta().GetResourceVersion(),
step)
}
Loading

0 comments on commit f719ff4

Please sign in to comment.