Skip to content

Commit

Permalink
Move changes to buildapi from v1alpha1 to v1alpha2
Browse files Browse the repository at this point in the history
Co-authored-by: Ralf Pannemans <ralf.pannemans@sap.com>
  • Loading branch information
modulo11 and c0d1ngm0nk3y committed Jul 22, 2021
1 parent 3150804 commit a54eebd
Show file tree
Hide file tree
Showing 24 changed files with 386 additions and 387 deletions.
11 changes: 0 additions & 11 deletions pkg/apis/build/v1alpha1/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,6 @@ func (b *Build) BuiltImage() string {
return b.Status.LatestImage
}

func (b *Build) CacheImage() string {
if b == nil {
return ""
}
if !b.IsSuccess() {
return ""
}

return b.Status.LatestCacheImage
}

func (b *Build) IsSuccess() bool {
if b == nil {
return false
Expand Down
23 changes: 6 additions & 17 deletions pkg/apis/build/v1alpha1/build_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/kmeta"
)

Expand Down Expand Up @@ -163,28 +164,17 @@ func (b *Build) BuildPod(images BuildPodImages, secrets []corev1.Secret, taints
}

var cacheArgs []string
var exportCacheArgs []string
var cacheVolumes []corev1.VolumeMount

if (b.Spec.Cache.ImageTag == "" && b.Spec.Cache.VolumeName == "") || config.OS == "windows" {
if b.Spec.CacheName == "" || config.OS == "windows" {
cacheArgs = nil
cacheVolumes = nil
} else if len(b.Spec.Cache.ImageTag) > 0 {
if b.Spec.LastBuild != nil && b.Spec.LastBuild.CacheImage != "" {
cacheArgs = []string{fmt.Sprintf("-cache-image=%s", b.Spec.LastBuild.CacheImage)}
} else {
cacheArgs = []string{fmt.Sprintf("-cache-image=%s", b.Spec.Cache.ImageTag)}
}
exportCacheArgs = []string{fmt.Sprintf("-cache-image=%s", b.Spec.Cache.ImageTag)}
cacheVolumes = nil
} else {
cacheArgs = []string{"-cache-dir=/cache"}
exportCacheArgs = cacheArgs
cacheVolumes = []corev1.VolumeMount{cacheVolume}
}

return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
ObjectMeta: v1.ObjectMeta{
Name: b.PodName(),
Namespace: b.Namespace,
Labels: combine(b.Labels, map[string]string{
Expand Down Expand Up @@ -350,7 +340,6 @@ func (b *Build) BuildPod(images BuildPodImages, secrets []corev1.Secret, taints
}, cacheArgs),
VolumeMounts: append([]corev1.VolumeMount{
layersVolume,
homeVolume,
}, cacheVolumes...),
Env: []corev1.EnvVar{
{
Expand Down Expand Up @@ -399,7 +388,7 @@ func (b *Build) BuildPod(images BuildPodImages, secrets []corev1.Secret, taints
"-group=/layers/group.toml",
"-analyzed=/layers/analyzed.toml",
"-project-metadata=/layers/project-metadata.toml"},
exportCacheArgs,
cacheArgs,
func() []string {
if platformAPI == "0.3" {
return nil
Expand Down Expand Up @@ -652,14 +641,14 @@ func (b *Build) rebasePod(secrets []corev1.Secret, images BuildPodImages, config
}

func (b *Build) cacheVolume(os string) []corev1.Volume {
if b.Spec.Cache.VolumeName == "" || os == "windows" {
if b.Spec.CacheName == "" || os == "windows" {
return []corev1.Volume{}
}

return []corev1.Volume{{
Name: cacheDirName,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ClaimName: b.Spec.Cache.VolumeName},
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ClaimName: b.Spec.CacheName},
},
}}
}
Expand Down
72 changes: 3 additions & 69 deletions pkg/apis/build/v1alpha1/build_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ func testBuildPod(t *testing.T, when spec.G, it spec.S) {
Revision: "gitrev1234",
},
},
Cache: &buildapi.BuildCacheConfig{
VolumeName: "some-cache-name",
},
CacheName: "some-cache-name",
Bindings: []buildapi.Binding{
{
Name: "database",
Expand Down Expand Up @@ -583,7 +581,6 @@ func testBuildPod(t *testing.T, when spec.G, it spec.S) {
assert.Equal(t, pod.Spec.InitContainers[3].Image, builderImage)
assert.Equal(t, []string{
"layers-dir",
"home-dir",
"cache-dir",
}, names(pod.Spec.InitContainers[3].VolumeMounts))

Expand Down Expand Up @@ -671,72 +668,9 @@ func testBuildPod(t *testing.T, when spec.G, it spec.S) {
}, pod.Spec.Volumes[0])
})

when("registry cache is requested", func() {
podWithVolumeCache, _ := build.BuildPod(config, nil, nil, buildPodBuilderConfig)
build.Spec.Cache.VolumeName = ""
build.Spec.Cache.ImageTag = "test-cache-image"

it("creates a pod without cache volume", func() {
podWithImageCache, err := build.BuildPod(config, nil, nil, buildPodBuilderConfig)
require.NoError(t, err)

assert.Len(t, podWithImageCache.Spec.Volumes, len(podWithVolumeCache.Spec.Volumes)-1)
})

it("adds the cache to analyze container", func() {
podWithImageCache, err := build.BuildPod(config, nil, nil, buildPodBuilderConfig)
require.NoError(t, err)

analyzeContainer := podWithImageCache.Spec.InitContainers[2]
assert.Contains(t, analyzeContainer.Args, "-cache-image=test-cache-image")
})
it("adds the cache to restore container", func() {
podWithImageCache, err := build.BuildPod(config, nil, nil, buildPodBuilderConfig)
require.NoError(t, err)

restoreContainer := podWithImageCache.Spec.InitContainers[3]
assert.Contains(t, restoreContainer.Args, "-cache-image=test-cache-image")
})
it("adds the cache to export container", func() {
podWithImageCache, err := build.BuildPod(config, nil, nil, buildPodBuilderConfig)
require.NoError(t, err)

exportContainer := podWithImageCache.Spec.InitContainers[5]
assert.Contains(t, exportContainer.Args, "-cache-image=test-cache-image")
})
})

when("Tag is empty", func() {
var pod *corev1.Pod
var err error
build.Spec.Cache.ImageTag = ""

it("does not add the cache to analyze container", func() {
pod, err = build.BuildPod(config, nil, nil, buildPodBuilderConfig)
require.NoError(t, err)

analyzeContainer := pod.Spec.InitContainers[2]
assert.NotContains(t, analyzeContainer.Args, "-cache-image")
})
it("does not add the cache to restore container", func() {
pod, err = build.BuildPod(config, nil, nil, buildPodBuilderConfig)
require.NoError(t, err)

restoreContainer := pod.Spec.InitContainers[3]
assert.NotContains(t, restoreContainer.Args, "-cache-image")
})
it("does not add the cache to export container", func() {
pod, err = build.BuildPod(config, nil, nil, buildPodBuilderConfig)
require.NoError(t, err)

exportContainer := pod.Spec.InitContainers[5]
assert.NotContains(t, exportContainer.Args, "-cache-image")
})
})

when("CacheName is empty", func() {
podWithCache, _ := build.BuildPod(config, nil, nil, buildPodBuilderConfig)
build.Spec.Cache.VolumeName = ""
build.Spec.CacheName = ""

it("creates a pod without cache volume", func() {
pod, err := build.BuildPod(config, nil, nil, buildPodBuilderConfig)
Expand Down Expand Up @@ -1351,7 +1285,7 @@ func testBuildPod(t *testing.T, when spec.G, it spec.S) {
buildPodBuilderConfigLinux := buildPodBuilderConfig.DeepCopy()
buildPodBuilderConfigLinux.OS = "linux"
podWithCache, _ := build.BuildPod(config, nil, nil, *buildPodBuilderConfigLinux)
build.Spec.Cache.VolumeName = "non-empty"
build.Spec.CacheName = "non-empty"

pod, err := build.BuildPod(config, nil, nil, buildPodBuilderConfig)
require.NoError(t, err)
Expand Down
23 changes: 8 additions & 15 deletions pkg/apis/build/v1alpha1/build_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ type BuildBuilderSpec struct {
// +k8s:openapi-gen=true
type BuildSpec struct {
// +listType
Tags []string `json:"tags,omitempty"`
Builder BuildBuilderSpec `json:"builder,omitempty"`
ServiceAccount string `json:"serviceAccount,omitempty"`
Source SourceConfig `json:"source"`
Cache *BuildCacheConfig `json:"cache,omitempty"`
Tags []string `json:"tags,omitempty"`
Builder BuildBuilderSpec `json:"builder,omitempty"`
ServiceAccount string `json:"serviceAccount,omitempty"`
Source SourceConfig `json:"source"`
CacheName string `json:"cacheName,omitempty"`
// +listType
Bindings Bindings `json:"bindings,omitempty"`
// +listType
Expand All @@ -69,26 +69,20 @@ type BuildSpec struct {
Notary *NotaryConfig `json:"notary,omitempty"`
}

type BuildCacheConfig struct {
VolumeName string `json:"volumeName,omitempty"`
ImageTag string `json:"imageTag,omitempty"`
}

// +k8s:openapi-gen=true
type Bindings []Binding

// +k8s:openapi-gen=true
type Binding struct {
Name string `json:"name,omitempty"`
Name string `json:"name",omitempty"`
MetadataRef *corev1.LocalObjectReference `json:"metadataRef,omitempty"`
SecretRef *corev1.LocalObjectReference `json:"secretRef,omitempty"`
}

// +k8s:openapi-gen=true
type LastBuild struct {
Image string `json:"image,omitempty"`
CacheImage string `json:"cacheImage,omitempty"`
StackId string `json:"stackId,omitempty"`
Image string `json:"image,omitempty"`
StackId string `json:"stackId,omitempty"`
}

// +k8s:openapi-gen=true
Expand All @@ -103,7 +97,6 @@ type BuildStatus struct {
BuildMetadata BuildpackMetadataList `json:"buildMetadata,omitempty"`
Stack BuildStack `json:"stack,omitempty"`
LatestImage string `json:"latestImage,omitempty"`
LatestCacheImage string `json:"latestCacheImage,omitempty"`
PodName string `json:"podName,omitempty"`
// +listType
StepStates []corev1.ContainerState `json:"stepStates,omitempty"`
Expand Down
37 changes: 9 additions & 28 deletions pkg/apis/build/v1alpha1/image_builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (

type BuildReason string

func (im *Image) Build(sourceResolver *SourceResolver, builder BuilderResource, latestBuild *Build, reasons, changes string, nextBuildNumber int64) *Build {
func (im *Image) Build(sourceResolver *SourceResolver, builder BuilderResource, latestBuild *Build, reasons, changes, cacheName string, nextBuildNumber int64) *Build {
buildNumber := strconv.Itoa(int(nextBuildNumber))

return &Build{
Expand Down Expand Up @@ -57,35 +57,13 @@ func (im *Image) Build(sourceResolver *SourceResolver, builder BuilderResource,
Resources: im.Resources(),
ServiceAccount: im.Spec.ServiceAccount,
Source: sourceResolver.SourceConfig(),
Cache: im.getBuildCacheConfig(),
CacheName: im.Status.BuildCacheName,
LastBuild: lastBuild(latestBuild),
Notary: im.Spec.Notary,
},
}
}

func (is *ImageSpec) NeedVolumeCache() bool {
return is.Cache != nil && is.Cache.Volume != nil && is.Cache.Volume.Request != nil
}

func (is *ImageSpec) NeedRegistryCache() bool {
return is.Cache != nil && is.Cache.Registry != nil && len(is.Cache.Registry.Tag) > 0
}

func (im *Image) getBuildCacheConfig() *BuildCacheConfig {
buildCacheConfig := BuildCacheConfig{}

if im.Spec.NeedRegistryCache() {
buildCacheConfig.ImageTag = im.Spec.Cache.Registry.Tag
}

if im.Spec.NeedVolumeCache() {
buildCacheConfig.VolumeName = im.Status.BuildCacheName
}

return &buildCacheConfig
}

func lastBuild(latestBuild *Build) *LastBuild {
if latestBuild == nil {
return nil
Expand All @@ -96,9 +74,8 @@ func lastBuild(latestBuild *Build) *LastBuild {
}

return &LastBuild{
Image: latestBuild.BuiltImage(),
CacheImage: latestBuild.CacheImage(),
StackId: latestBuild.Stack(),
Image: latestBuild.BuiltImage(),
StackId: latestBuild.Stack(),
}
}

Expand Down Expand Up @@ -134,6 +111,10 @@ func (im *Image) CacheName() string {
return kmeta.ChildName(im.Name, "-cache")
}

func (im *Image) NeedCache() bool {
return im.Spec.CacheSize != nil
}

func (im *Image) BuildCache() *corev1.PersistentVolumeClaim {
return &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -148,7 +129,7 @@ func (im *Image) BuildCache() *corev1.PersistentVolumeClaim {
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceStorage: *im.Spec.Cache.Volume.Request,
corev1.ResourceStorage: *im.Spec.CacheSize,
},
},
},
Expand Down
Loading

0 comments on commit a54eebd

Please sign in to comment.