Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1807 from ibuildthecloud/table-out
Browse files Browse the repository at this point in the history
Fix build cache issues
  • Loading branch information
ibuildthecloud authored Jun 27, 2023
2 parents c0b37d4 + ebd5f0a commit 98d234e
Show file tree
Hide file tree
Showing 41 changed files with 416 additions and 224 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# syntax=docker/dockerfile:1.3-labs

FROM ghcr.io/acorn-io/images-mirror/tonistiigi/binfmt:qemu-v6.2.0 AS binfmt
FROM ghcr.io/acorn-io/images-mirror/moby/buildkit:v0.10.6 AS buildkit
FROM ghcr.io/acorn-io/images-mirror/moby/buildkit:v0.11.6 AS buildkit
FROM ghcr.io/acorn-io/images-mirror/registry:2.8.1 AS registry
FROM ghcr.io/acorn-io/images-mirror/rancher/klipper-lb:v0.3.5 AS klipper-lb

Expand Down
1 change: 1 addition & 0 deletions docs/docs/100-reference/01-command-line/acorn_install.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ acorn install
--allow-traffic-from-namespace strings Namespaces that are allowed to send network traffic to all Acorn apps
--allow-user-annotation strings Allow these annotations to propagate to dependent objects, no effect if --ignore-user-labels-and-annotations not true
--allow-user-label strings Allow these labels to propagate to dependent objects, no effect if --ignore-user-labels-and-annotations not true
--allow-user-metadata-namespace strings Allow these namespaces to propagate labels and annotations to dependent objects, no effect if --ignore-user-labels-and-annotations not true
--api-server-replicas int acorn-api deployment replica count
--auto-upgrade-interval string For apps configured with automatic upgrades enabled, the interval at which to check for new versions. Upgrade intervals configured at the application level cannot be smaller than this. (default '5m' - 5 minutes)
--aws-identity-provider-arn string ARN of cluster's OpenID Connect provider registered in AWS
Expand Down
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ require (
github.com/aws/aws-sdk-go-v2/config v1.18.23
github.com/aws/aws-sdk-go-v2/service/iam v1.19.10
github.com/containerd/console v1.0.3
github.com/containerd/containerd v1.6.10
github.com/containerd/containerd v1.6.20
github.com/denisbrodbeck/machineid v1.0.1
github.com/docker/cli v23.0.5+incompatible
github.com/docker/docker-credential-helpers v0.7.0
github.com/go-acme/lego/v4 v4.9.1
Expand All @@ -35,7 +36,7 @@ require (
github.com/gorilla/websocket v1.5.0
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de
github.com/loft-sh/devspace v1.1.1-0.20221217093921-7604c5857f98
github.com/moby/buildkit v0.10.6
github.com/moby/buildkit v0.11.6
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.1.0-rc3
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
Expand All @@ -48,7 +49,7 @@ require (
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
github.com/tonistiigi/fsutil v0.0.0-20220315205639-9ed612626da3
github.com/tonistiigi/fsutil v0.0.0-20230407161946-9e7a6df48576
github.com/wI2L/jsondiff v0.3.0
golang.org/x/crypto v0.9.0
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc
Expand Down Expand Up @@ -102,7 +103,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/cockroachdb/apd/v2 v2.0.2 // indirect
github.com/containerd/continuity v0.3.0 // indirect
github.com/containerd/continuity v0.3.1-0.20230206214859-2a963a2f56e8 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.14.3 // indirect
github.com/containerd/typeurl v1.0.2 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
Expand Down Expand Up @@ -195,8 +196,9 @@ require (
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/locker v1.0.1 // indirect
github.com/moby/patternmatcher v0.5.0 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/sys/signal v0.6.0 // indirect
github.com/moby/sys/signal v0.7.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand Down
116 changes: 24 additions & 92 deletions go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pkg/apis/api.acorn.io/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ type Config struct {
IgnoreUserLabelsAndAnnotations *bool `json:"ignoreUserLabelsAndAnnotations" name:"ignore-user-labels-and-annotations" usage:"Don't propagate user-defined labels and annotations to dependent objects"`
AllowUserLabels []string `json:"allowUserLabels" name:"allow-user-label" usage:"Allow these labels to propagate to dependent objects, no effect if --ignore-user-labels-and-annotations not true"`
AllowUserAnnotations []string `json:"allowUserAnnotations" name:"allow-user-annotation" usage:"Allow these annotations to propagate to dependent objects, no effect if --ignore-user-labels-and-annotations not true"`
AllowUserMetadataNamespaces []string `json:"allowUserMetadataNamespaces" name:"allow-user-metadata-namespace" usage:"Allow these namespaces to propagate labels and annotations to dependent objects, no effect if --ignore-user-labels-and-annotations not true"`
WorkloadMemoryDefault *int64 `json:"workloadMemoryDefault" name:"workload-memory-default" quantity:"true" usage:"Set the default memory for acorn workloads. Accepts binary suffixes (Ki, Mi, Gi, etc) and \".\" and \"_\" seperators (default 0)" short:"m"`
WorkloadMemoryMaximum *int64 `json:"workloadMemoryMaximum" name:"workload-memory-maximum" quantity:"true" usage:"Set the maximum memory for acorn workloads. Accepts binary suffixes (Ki, Mi, Gi, etc) and \".\" and \"_\" seperators (default 0)"`
UseCustomCABundle *bool `json:"useCustomCABundle" name:"use-custom-ca-bundle" usage:"Use CA bundle for admin supplied secret for all acorn control plane components. Defaults to false."`
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/api.acorn.io/v1/zz_generated.deepcopy.go

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

3 changes: 3 additions & 0 deletions pkg/apis/internal.acorn.io/v1/appstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func (e *ExpressionError) String() string {
if e.DependencyNotFound == nil {
return "error [" + e.Error + "] expression [" + e.Expression + "]"
}
if e.Expression != "" {
return fmt.Sprintf("missing %s [%s] from expression [%s]", e.DependencyNotFound.DependencyType, e.DependencyNotFound.Name, e.Expression)
}
return fmt.Sprintf("missing %s [%s]", e.DependencyNotFound.DependencyType, e.DependencyNotFound.Name)
}

Expand Down
13 changes: 7 additions & 6 deletions pkg/apis/internal.acorn.io/v1/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,13 @@ type AcornImageBuildInstance struct {
}

type AcornImageBuildInstanceSpec struct {
BuilderName string `json:"builderName,omitempty" wrangler:"required"`
Acornfile string `json:"acornfile,omitempty"`
Profiles []string `json:"profiles,omitempty"`
Platforms []Platform `json:"platforms,omitempty"`
Args GenericMap `json:"args,omitempty"`
VCS VCS `json:"vcs,omitempty"`
ContextCacheKey string `json:"contextCacheKey,omitempty"`
BuilderName string `json:"builderName,omitempty" wrangler:"required"`
Acornfile string `json:"acornfile,omitempty"`
Profiles []string `json:"profiles,omitempty"`
Platforms []Platform `json:"platforms,omitempty"`
Args GenericMap `json:"args,omitempty"`
VCS VCS `json:"vcs,omitempty"`
}

type AcornImageBuildInstanceStatus struct {
Expand Down
9 changes: 6 additions & 3 deletions pkg/apis/internal.acorn.io/v1/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,6 @@ func (in *VolumeBinding) UnmarshalJSON(data []byte) error {

func impliedSecretsForContainer(app *AppSpec, container Container) {
for _, env := range container.Environment {
if strings.Contains(env.Secret.Name, ".") {
continue
}
if _, ok := app.Secrets[env.Secret.Name]; env.Secret.Name != "" && !ok {
app.Secrets[env.Secret.Name] = Secret{
Type: "opaque",
Expand Down Expand Up @@ -406,6 +403,9 @@ func addImpliedResources(in *AppSpec) error {

for _, a := range in.Acorns {
for _, volumeBinding := range a.Volumes {
if strings.Contains(volumeBinding.Volume, ".") {
continue
}
if _, ok := in.Volumes[volumeBinding.Volume]; !ok {
in.Volumes[volumeBinding.Volume] = VolumeRequest{
Size: volumeBinding.Size,
Expand All @@ -414,6 +414,9 @@ func addImpliedResources(in *AppSpec) error {
}
}
for _, secretBinding := range a.Secrets {
if strings.Contains(secretBinding.Secret, ".") {
continue
}
if _, ok := in.Secrets[secretBinding.Secret]; !ok {
in.Secrets[secretBinding.Secret] = Secret{
Type: "opaque",
Expand Down
6 changes: 2 additions & 4 deletions pkg/appdefinition/lookup.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package appdefinition

import (
"reflect"
"strings"

v1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
Expand Down Expand Up @@ -93,12 +92,11 @@ func findAcornImage(imageData v1.ImagesData, image string, acornBuild *v1.AcornB
if testBuild == nil {
continue
}
if !reflect.DeepEqual(*acornBuild, *testBuild) {
if !equality.Semantic.DeepEqual(*acornBuild, *testBuild) {
continue
}
if build.ImageKey != "" {
image, ok := imageData.Acorns[build.ImageKey]
return image.Image, ok
return findImageInImageData(imageData, build.ImageKey)
}
return image, image != ""
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type buildContext struct {
func Build(ctx context.Context, messages buildclient.Messages, pushRepo, buildNamespace string, opts v1.AcornImageBuildInstanceSpec, keychain authn.Keychain, remoteOpts ...remote.Option) (*v1.AppImage, error) {
remoteKc := NewRemoteKeyChain(messages, keychain)
buildContext := &buildContext{
ctx: ctx,
ctx: buildkit.WithContextCacheKey(ctx, opts.ContextCacheKey),
cwd: "",
pushRepo: pushRepo,
buildNamespace: buildNamespace,
Expand Down
22 changes: 21 additions & 1 deletion pkg/build/buildkit/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,34 @@ package buildkit

import (
"context"
"encoding/json"
"fmt"
"path/filepath"

v1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
"github.com/acorn-io/runtime/pkg/build/authprovider"
"github.com/acorn-io/runtime/pkg/buildclient"
"github.com/acorn-io/runtime/pkg/digest"
cplatforms "github.com/containerd/containerd/platforms"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/uuid"
buildkit "github.com/moby/buildkit/client"
"github.com/moby/buildkit/session"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/sirupsen/logrus"
)

type cacheKey struct{}

func WithContextCacheKey(ctx context.Context, key string) context.Context {
return context.WithValue(ctx, cacheKey{}, key)
}

func getCacheKey(ctx context.Context) string {
v, _ := ctx.Value(cacheKey{}).(string)
return v
}

func Build(ctx context.Context, pushRepo string, local bool, cwd string, platforms []v1.Platform, build v1.Build, messages buildclient.Messages, keychain authn.Keychain) ([]v1.Platform, []string, error) {
bkc, err := buildkit.New(ctx, "")
if err != nil {
Expand Down Expand Up @@ -50,9 +64,15 @@ func Build(ctx context.Context, pushRepo string, local bool, cwd string, platfor
}
}

buildData, _ := json.Marshal(build)
sharedKey := digest.SHA256(getCacheKey(ctx), cwd, string(buildData), fmt.Sprint(local))
logrus.Debugf("sharedKey=[%s] cacheKey=[%s] cwd=[%s], buildData=[%s] local=[%v]",
sharedKey, getCacheKey(ctx), cwd, buildData, local)

for _, platform := range platforms {
options := buildkit.SolveOpt{
Frontend: "dockerfile.v0",
SharedKey: sharedKey,
Frontend: "dockerfile.v0",
FrontendAttrs: map[string]string{
"target": build.Target,
"filename": dockerfileName,
Expand Down
1 change: 1 addition & 0 deletions pkg/buildclient/filesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (f *FileServer) DiffCopy(server filesync.FileSync_DiffCopyServer) error {
err := f.messages.Send(&Message{
FileSessionID: sessionID,
SyncOptions: &SyncOptions{
Compress: true,
Context: f.context,
Dockerfile: f.dockerfilePath,
DockerfileContents: f.dockerfileContents,
Expand Down
17 changes: 11 additions & 6 deletions pkg/buildclient/filesyncclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/moby/buildkit/session/filesync"
"github.com/sirupsen/logrus"
"github.com/tonistiigi/fsutil"
"github.com/tonistiigi/fsutil/types"
fstypes "github.com/tonistiigi/fsutil/types"
"google.golang.org/grpc/metadata"
Expand All @@ -22,6 +23,7 @@ var (
)

type fileSyncClient struct {
compress bool
sessionID string
messages Messages
msg <-chan *Message
Expand Down Expand Up @@ -54,8 +56,9 @@ func newFileSyncClient(ctx context.Context, cwd, sessionID string, messages Mess
keyExporterMetaPrefix: opts.ExporterMetaPrefix,
}

logrus.Tracef("starting file sync client %s", sessionID)
logrus.Tracef("starting file sync client %s, compress=%v", sessionID, opts.Compress)
fsClient := &fileSyncClient{
compress: opts.Compress,
sessionID: sessionID,
messages: messages,
tempDir: tempDir,
Expand Down Expand Up @@ -104,7 +107,7 @@ func createFileMapInput(cwd string, opts *SyncOptions) (string, map[string]strin
}, nil
}

func prepareSyncedDirs(localDirs map[string]string, dirNames []string, followPaths []string) ([]filesync.SyncedDir, error) {
func prepareSyncedDirs(localDirs map[string]string, dirNames []string, followPaths []string) (filesync.StaticDirSource, error) {
for localDirName, d := range localDirs {
fi, err := os.Stat(d)
if os.IsNotExist(err) {
Expand Down Expand Up @@ -141,19 +144,20 @@ func prepareSyncedDirs(localDirs map[string]string, dirNames []string, followPat
}
}
}
resetUIDAndGID := func(p string, st *fstypes.Stat) bool {
resetUIDAndGID := func(p string, st *fstypes.Stat) fsutil.MapResult {
st.Uid = 0
st.Gid = 0
return true
return fsutil.MapResultKeep
}

dirs := make([]filesync.SyncedDir, 0, len(localDirs))
dirs := make(filesync.StaticDirSource, len(localDirs))
for name, d := range localDirs {
dirs = append(dirs, filesync.SyncedDir{Name: name, Dir: d, Map: resetUIDAndGID})
dirs[name] = filesync.SyncedDir{Dir: d, Map: resetUIDAndGID}
}

return dirs, nil
}

func (s *fileSyncClient) Send(obj *types.Packet) error {
return s.SendMsg(obj)
}
Expand Down Expand Up @@ -188,6 +192,7 @@ func (s *fileSyncClient) Close() {

func (s *fileSyncClient) SendMsg(m interface{}) error {
return s.messages.Send(&Message{
Compress: s.compress,
FileSessionID: s.sessionID,
Packet: m.(*types.Packet),
})
Expand Down
Loading

0 comments on commit 98d234e

Please sign in to comment.