Skip to content

Commit

Permalink
Tests seem to pass
Browse files Browse the repository at this point in the history
Signed-off-by: Natalie Arellano <[email protected]>
  • Loading branch information
natalieparellano committed Oct 16, 2023
1 parent a029735 commit d6036e4
Show file tree
Hide file tree
Showing 21 changed files with 343 additions and 157 deletions.
8 changes: 3 additions & 5 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ import (
"os"
"time"

"github.com/pivotal/kpack/pkg/secret"

"github.com/Masterminds/semver/v3"
"github.com/sigstore/cosign/v2/cmd/cosign/cli/sign"
ociremote "github.com/sigstore/cosign/v2/pkg/oci/remote"

"github.com/pivotal/kpack/pkg/cosign"

"go.uber.org/zap"
"golang.org/x/sync/errgroup"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -46,6 +42,7 @@ import (
"github.com/pivotal/kpack/pkg/client/informers/externalversions"
"github.com/pivotal/kpack/pkg/cnb"
"github.com/pivotal/kpack/pkg/config"
"github.com/pivotal/kpack/pkg/cosign"
"github.com/pivotal/kpack/pkg/dockercreds/k8sdockercreds"
"github.com/pivotal/kpack/pkg/duckbuilder"
"github.com/pivotal/kpack/pkg/flaghelpers"
Expand All @@ -64,6 +61,7 @@ import (
"github.com/pivotal/kpack/pkg/reconciler/lifecycle"
"github.com/pivotal/kpack/pkg/reconciler/sourceresolver"
"github.com/pivotal/kpack/pkg/registry"
"github.com/pivotal/kpack/pkg/secret"
)

const (
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -324,5 +324,3 @@ require (
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace github.com/AdamKorcz/go-fuzz-headers-1 => github.com/AdamKorcz/go-fuzz-headers-1 v0.0.0-20230919221257-8b5d3ce2d11d
10 changes: 0 additions & 10 deletions pkg/apis/build/v1alpha2/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,6 @@ func (b *Build) builtWithStack(runImage string) bool {
return lastBuildRunImageRef.Identifier() == builderRunImageRef.Identifier()
}

func (b *Build) builtWithBuildpacks(buildpacks corev1alpha1.BuildpackMetadataList) bool {
for _, bp := range b.Status.BuildMetadata {
if !buildpacks.Include(bp) {
return false
}
}

return true
}

func (b *Build) additionalBuildNeeded() bool {
_, ok := b.Annotations[BuildNeededAnnotation]
return ok
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/build/v1alpha2/build_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (bs *BuildSpec) convertFrom(from *v1alpha1.BuildSpec) {

func (bs *BuildStatus) convertFrom(from *v1alpha1.BuildStatus) {
bs.Status = from.Status
bs.BuildMetadata = from.BuildMetadata
bs.BuildMetadataBuildpacks = from.BuildMetadata
bs.Stack = from.Stack
bs.LatestImage = from.LatestImage
bs.PodName = from.PodName
Expand All @@ -90,7 +90,7 @@ func (bs *BuildStatus) convertFrom(from *v1alpha1.BuildStatus) {

func (bs *BuildStatus) convertTo(to *v1alpha1.BuildStatus) {
to.Status = bs.Status
to.BuildMetadata = bs.BuildMetadata
to.BuildMetadata = bs.BuildMetadataBuildpacks
to.Stack = bs.Stack
to.LatestImage = bs.LatestImage
to.PodName = bs.PodName
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/build/v1alpha2/build_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func testBuildConversion(t *testing.T, when spec.G, it spec.S) {
ObservedGeneration: 0,
Conditions: nil,
},
BuildMetadata: corev1alpha1.BuildpackMetadataList{},
BuildMetadataBuildpacks: corev1alpha1.BuildpackMetadataList{},
Stack: corev1alpha1.BuildStack{
RunImage: "some-run",
ID: "some-id",
Expand Down
12 changes: 6 additions & 6 deletions pkg/apis/build/v1alpha2/build_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ const (

var (
PrepareCommand = "/cnb/process/build-init"
AnalyzeCommand = "/cnb/lifecycle/analyzer"
DetectCommand = "/cnb/lifecycle/detector"
RestoreCommand = "/cnb/lifecycle/restorer"
BuildCommand = "/cnb/lifecycle/builder"
ExportCommand = "/cnb/lifecycle/exporter"
AnalyzeCommand = "/cnb/lifecycle/analyzer"
DetectCommand = "/cnb/lifecycle/detector"
RestoreCommand = "/cnb/lifecycle/restorer"
BuildCommand = "/cnb/lifecycle/builder"
ExportCommand = "/cnb/lifecycle/exporter"
CompletionCommand = "/cnb/process/completion"
RebaseCommand = "/cnb/process/rebase"
)
Expand Down Expand Up @@ -760,7 +760,7 @@ func (b *Build) useImageExtensions(pod *corev1.Pod) {
container.SecurityContext.RunAsUser = intPointer(0)
container.SecurityContext.RunAsGroup = intPointer(0)
container.SecurityContext.RunAsNonRoot = boolPointer(false)
container.SecurityContext.Capabilities = &corev1.Capabilities{Add: []corev1.Capability{"SETGID", "SETUID"}} // TODO: check if this is needed if not using kind
container.SecurityContext.Capabilities = &corev1.Capabilities{Add: []corev1.Capability{"SETGID", "SETUID"}}
}
pod.Spec.InitContainers[idx] = container
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/apis/build/v1alpha2/build_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,13 @@ type BuildStack struct {

// +k8s:openapi-gen=true
type BuildStatus struct {
corev1alpha1.Status `json:",inline"`
BuildMetadata corev1alpha1.BuildpackMetadataList `json:"buildMetadata,omitempty"`
Stack corev1alpha1.BuildStack `json:"stack,omitempty"`
LatestImage string `json:"latestImage,omitempty"`
LatestCacheImage string `json:"latestCacheImage,omitempty"`
PodName string `json:"podName,omitempty"`
corev1alpha1.Status `json:",inline"`
BuildMetadataBuildpacks corev1alpha1.BuildpackMetadataList `json:"buildMetadata,omitempty"`
BuildMetadataExtensions corev1alpha1.BuildpackMetadataList `json:"buildMetadataExtensions,omitempty"`
Stack corev1alpha1.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"`
// +listType
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/build/v1alpha2/builder_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type BuilderResource interface {
BuildBuilderSpec() corev1alpha1.BuildBuilderSpec
Ready() bool
BuildpackMetadata() corev1alpha1.BuildpackMetadataList
ExtensionMetadata() corev1alpha1.BuildpackMetadataList
RunImage() string
GetKind() string
ConditionReadyMessage() string
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/build/v1alpha2/image_builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
BuildReasonConfig = "CONFIG"
BuildReasonCommit = "COMMIT"
BuildReasonBuildpack = "BUILDPACK"
BuildReasonExtension = "EXTENSION"
BuildReasonStack = "STACK"
BuildReasonTrigger = "TRIGGER"
)
Expand Down
27 changes: 16 additions & 11 deletions pkg/apis/build/v1alpha2/image_builds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func testImageBuilds(t *testing.T, when spec.G, it spec.S) {
LatestImage: "some/builder@sha256:builder-digest",
Kind: BuilderKind,
BuilderReady: true,
BuilderMetadata: []corev1alpha1.BuildpackMetadata{
BuilderMetadataBuildpacks: []corev1alpha1.BuildpackMetadata{
{Id: "buildpack.matches", Version: "1"},
},
LatestRunImage: "some.registry.io/run-image@sha256:67e3de2af270bf09c02e9a644aeb7e87e6b3c049abe6766bf6b6c3728a83e7fb",
Expand All @@ -84,7 +84,7 @@ func testImageBuilds(t *testing.T, when spec.G, it spec.S) {
},
},
},
BuildMetadata: []corev1alpha1.BuildpackMetadata{
BuildMetadataBuildpacks: []corev1alpha1.BuildpackMetadata{
{Id: "buildpack.matches", Version: "1"},
},
Stack: corev1alpha1.BuildStack{
Expand Down Expand Up @@ -371,14 +371,15 @@ func testImageBuilds(t *testing.T, when spec.G, it spec.S) {
}

type TestBuilderResource struct {
BuilderReady bool
BuilderMetadata []corev1alpha1.BuildpackMetadata
ImagePullSecrets []corev1.LocalObjectReference
Kind string
LatestImage string
LatestRunImage string
Name string
Namespace string
BuilderReady bool
BuilderMetadataBuildpacks []corev1alpha1.BuildpackMetadata
BuilderMetadataExtensions []corev1alpha1.BuildpackMetadata
ImagePullSecrets []corev1.LocalObjectReference
Kind string
LatestImage string
LatestRunImage string
Name string
Namespace string
}

func (t TestBuilderResource) ConditionReadyMessage() string {
Expand All @@ -397,7 +398,11 @@ func (t TestBuilderResource) Ready() bool {
}

func (t TestBuilderResource) BuildpackMetadata() corev1alpha1.BuildpackMetadataList {
return t.BuilderMetadata
return t.BuilderMetadataBuildpacks
}

func (t TestBuilderResource) ExtensionMetadata() corev1alpha1.BuildpackMetadataList {
return t.BuilderMetadataExtensions
}

func (t TestBuilderResource) RunImage() string {
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/build/v1alpha2/zz_generated.deepcopy.go

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

6 changes: 3 additions & 3 deletions pkg/buildchange/buildpack_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
corev1alpha1 "github.com/pivotal/kpack/pkg/apis/core/v1alpha1"
)

func NewBuildpackChange(oldBuildpacks, newBuildpacks []corev1alpha1.BuildpackInfo) Change {
func NewBuildpackChange(oldInfos, newInfos []corev1alpha1.BuildpackInfo) Change {
return buildpackChange{
old: oldBuildpacks,
new: newBuildpacks,
old: oldInfos,
new: newInfos,
}
}

Expand Down
40 changes: 40 additions & 0 deletions pkg/buildchange/extension_change.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package buildchange

import (
"sort"

"github.com/google/go-cmp/cmp"

buildapi "github.com/pivotal/kpack/pkg/apis/build/v1alpha2"
corev1alpha1 "github.com/pivotal/kpack/pkg/apis/core/v1alpha1"
)

func NewExtensionChange(oldInfos, newInfos []corev1alpha1.BuildpackInfo) Change {
return extensionChange{
old: oldInfos,
new: newInfos,
}
}

type extensionChange struct {
old []corev1alpha1.BuildpackInfo
new []corev1alpha1.BuildpackInfo
}

func (b extensionChange) Reason() buildapi.BuildReason { return buildapi.BuildReasonExtension }

func (b extensionChange) IsBuildRequired() (bool, error) {
sort.Slice(b.old, func(i, j int) bool {
return b.old[i].Id < b.old[j].Id
})
sort.Slice(b.new, func(i, j int) bool {
return b.new[i].Id < b.new[j].Id
})
return !cmp.Equal(b.old, b.new), nil
}

func (b extensionChange) Old() interface{} { return b.old }

func (b extensionChange) New() interface{} { return b.new }

func (b extensionChange) Priority() buildapi.BuildPriority { return buildapi.BuildPriorityLow }
34 changes: 25 additions & 9 deletions pkg/cnb/build_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

type BuildMetadata struct {
BuildpackMetadata corev1alpha1.BuildpackMetadataList `json:"buildpackMetadata"`
ExtensionMetadata corev1alpha1.BuildpackMetadataList `json:"extensionMetadata"`
LatestCacheImage string `json:"latestCacheImage"`
LatestImage string `json:"latestImage"`
StackID string `json:"stackID"`
Expand All @@ -43,7 +44,8 @@ func (r *RemoteMetadataRetriever) GetBuildMetadata(builtImageRef, cacheTag strin
cacheImageRef, _ := r.getCacheImage(cacheTag, keychain) // if getting cache fails, use empty cache

return &BuildMetadata{
BuildpackMetadata: buildMetadataFromBuiltImage(buildImg),
BuildpackMetadata: buildpackMetadataFromBuiltImage(buildImg),
ExtensionMetadata: extensionMetadataFromBuiltImage(buildImg),
LatestImage: buildImg.identifier,
LatestCacheImage: cacheImageRef,
StackRunImage: buildImg.stack.RunImage,
Expand Down Expand Up @@ -103,6 +105,7 @@ func readBuiltImage(appImage ggcrv1.Image, appImageId string) (builtImage, error
return builtImage{
identifier: appImageId,
buildpackMetadata: buildMetadata.Buildpacks,
extensionMetadata: buildMetadata.Extensions,
stack: builtImageStack{
RunImage: baseImageRef.Context().String() + "@" + runImageRef.Identifier(),
ID: stackId,
Expand All @@ -113,6 +116,7 @@ func readBuiltImage(appImage ggcrv1.Image, appImageId string) (builtImage, error
type builtImage struct {
identifier string
buildpackMetadata []lifecyclebuildpack.GroupElement
extensionMetadata []lifecyclebuildpack.GroupElement
stack builtImageStack
}

Expand All @@ -126,16 +130,28 @@ type RunImageAppMetadata struct {
Reference string `json:"reference" toml:"reference"`
}

func buildMetadataFromBuiltImage(image builtImage) corev1alpha1.BuildpackMetadataList {
bpMetadata := make([]corev1alpha1.BuildpackMetadata, 0, len(image.buildpackMetadata))
for _, metadata := range image.buildpackMetadata {
bpMetadata = append(bpMetadata, corev1alpha1.BuildpackMetadata{
Id: metadata.ID,
Version: metadata.Version,
Homepage: metadata.Homepage,
func buildpackMetadataFromBuiltImage(image builtImage) corev1alpha1.BuildpackMetadataList {
ret := make([]corev1alpha1.BuildpackMetadata, 0, len(image.buildpackMetadata))
for _, m := range image.buildpackMetadata {
ret = append(ret, corev1alpha1.BuildpackMetadata{
Id: m.ID,
Version: m.Version,
Homepage: m.Homepage,
})
}
return bpMetadata
return ret
}

func extensionMetadataFromBuiltImage(image builtImage) corev1alpha1.BuildpackMetadataList {
ret := make([]corev1alpha1.BuildpackMetadata, 0, len(image.buildpackMetadata))
for _, m := range image.buildpackMetadata {
ret = append(ret, corev1alpha1.BuildpackMetadata{
Id: m.ID,
Version: m.Version,
Homepage: m.Homepage,
})
}
return ret
}

func CompressBuildMetadata(metadata *BuildMetadata) ([]byte, error) {
Expand Down
5 changes: 4 additions & 1 deletion pkg/duckbuilder/duck_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ func (b *DuckBuilder) BuildBuilderSpec() corev1alpha1.BuildBuilderSpec {
}
}

// TODO: add for extensions?
func (b *DuckBuilder) BuildpackMetadata() corev1alpha1.BuildpackMetadataList {
return b.Status.BuilderMetadataBuildpacks
}

func (b *DuckBuilder) ExtensionMetadata() corev1alpha1.BuildpackMetadataList {
return b.Status.BuilderMetadataExtensions
}

func (b *DuckBuilder) RunImage() string {
return b.Status.Stack.RunImage
}
Expand Down
25 changes: 14 additions & 11 deletions pkg/reconciler/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,8 @@ package build
import (
"context"
"encoding/json"

"github.com/google/go-containerregistry/pkg/authn"
buildapi "github.com/pivotal/kpack/pkg/apis/build/v1alpha2"
corev1alpha1 "github.com/pivotal/kpack/pkg/apis/core/v1alpha1"
"github.com/pivotal/kpack/pkg/buildchange"
"github.com/pivotal/kpack/pkg/buildpod"
"github.com/pivotal/kpack/pkg/client/clientset/versioned"
buildinformers "github.com/pivotal/kpack/pkg/client/informers/externalversions/build/v1alpha2"
buildlisters "github.com/pivotal/kpack/pkg/client/listers/build/v1alpha2"
"github.com/pivotal/kpack/pkg/cnb"
"github.com/pivotal/kpack/pkg/reconciler"
"github.com/pivotal/kpack/pkg/registry"
"github.com/pkg/errors"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
Expand All @@ -27,6 +18,17 @@ import (
"k8s.io/client-go/tools/cache"
"knative.dev/pkg/controller"
"knative.dev/pkg/logging/logkey"

buildapi "github.com/pivotal/kpack/pkg/apis/build/v1alpha2"
corev1alpha1 "github.com/pivotal/kpack/pkg/apis/core/v1alpha1"
"github.com/pivotal/kpack/pkg/buildchange"
"github.com/pivotal/kpack/pkg/buildpod"
"github.com/pivotal/kpack/pkg/client/clientset/versioned"
buildinformers "github.com/pivotal/kpack/pkg/client/informers/externalversions/build/v1alpha2"
buildlisters "github.com/pivotal/kpack/pkg/client/listers/build/v1alpha2"
"github.com/pivotal/kpack/pkg/cnb"
"github.com/pivotal/kpack/pkg/reconciler"
"github.com/pivotal/kpack/pkg/registry"
)

const (
Expand Down Expand Up @@ -167,7 +169,8 @@ func (c *Reconciler) reconcile(ctx context.Context, build *buildapi.Build) error
return errors.Wrap(err, "failed to get build metadata from build pod")
}
}
build.Status.BuildMetadata = buildMetadata.BuildpackMetadata
build.Status.BuildMetadataBuildpacks = buildMetadata.BuildpackMetadata
build.Status.BuildMetadataExtensions = buildMetadata.ExtensionMetadata
build.Status.LatestImage = buildMetadata.LatestImage
build.Status.LatestCacheImage = buildMetadata.LatestCacheImage
build.Status.Stack.RunImage = buildMetadata.StackRunImage
Expand Down
Loading

0 comments on commit d6036e4

Please sign in to comment.