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

Commit

Permalink
Add function builder support
Browse files Browse the repository at this point in the history
Signed-off-by: Darren Shepherd <[email protected]>
  • Loading branch information
ibuildthecloud committed Jan 6, 2024
1 parent 407ca5d commit 3dc0733
Show file tree
Hide file tree
Showing 20 changed files with 365 additions and 127 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ go 1.21.5
require (
cuelang.org/go v0.6.0
github.com/AlecAivazis/survey/v2 v2.3.6
github.com/acorn-io/aml v0.0.0-20240104225813-e936fb3bc7b6
github.com/acorn-io/aml/cli v0.0.0-20231113171943-4844e2f3e1a2
github.com/acorn-io/aml v0.0.0-20240106192317-21afc7320c77
github.com/acorn-io/aml/cli v0.0.0-20240106192317-21afc7320c77
github.com/acorn-io/baaah v0.0.0-20240105013849-c4f82d7a5a41
github.com/acorn-io/broadcaster v0.0.0-20240105011354-bfadd4a7b45d
github.com/acorn-io/function-builder v0.0.0-20240106051830-f0d368d2382c
github.com/acorn-io/mink v0.0.0-20240105015834-b1f7af4fadea
github.com/acorn-io/namegenerator v0.0.0-20220915160418-9e3d5a0ffe78
github.com/acorn-io/schemer v0.0.0-20240105014212-9739d5485208
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,18 @@ github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpH
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
github.com/acomagu/bufpipe v1.0.4 h1:e3H4WUzM3npvo5uv95QuJM3cQspFNtFBzvJ2oNjKIDQ=
github.com/acomagu/bufpipe v1.0.4/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=
github.com/acorn-io/aml v0.0.0-20240104225813-e936fb3bc7b6 h1:rRaCOn7EkIxkfDZieDbm6HlpUIHIN8DpVZfGMIbohA0=
github.com/acorn-io/aml v0.0.0-20240104225813-e936fb3bc7b6/go.mod h1:z/Tz4f5dWHj/F5THru0+IQBIq31Xq06S5b7YpPkV7K0=
github.com/acorn-io/aml/cli v0.0.0-20231113171943-4844e2f3e1a2 h1:CtflOEPAvtpALuC3SM1WApsfTuECXcDuq25E3fZaPdg=
github.com/acorn-io/aml/cli v0.0.0-20231113171943-4844e2f3e1a2/go.mod h1:D4tWmJlLdsmMbQ/MI4T+Tj4j2PjgTAdde2QDGkeWH20=
github.com/acorn-io/aml v0.0.0-20240106192317-21afc7320c77 h1:MFaoLSKZj1z9c+U7S0GGkS7LXfMeBfQTgGRGVs1HwMg=
github.com/acorn-io/aml v0.0.0-20240106192317-21afc7320c77/go.mod h1:z/Tz4f5dWHj/F5THru0+IQBIq31Xq06S5b7YpPkV7K0=
github.com/acorn-io/aml/cli v0.0.0-20240106192317-21afc7320c77 h1:KdXktTqCv1OlmkFYl7+P11Dh0NDiYnQFblNDdLXPhT4=
github.com/acorn-io/aml/cli v0.0.0-20240106192317-21afc7320c77/go.mod h1:tTw5qUW+S3cAoYepBLFdLNqaRV/DHV3YhHFhB2FzZhE=
github.com/acorn-io/baaah v0.0.0-20240105013849-c4f82d7a5a41 h1:ZzzhWJ2ZP1QQlibd9pll9UTfmitIGReW0x3VNNBg5Qw=
github.com/acorn-io/baaah v0.0.0-20240105013849-c4f82d7a5a41/go.mod h1:13nTO3svO8zTD3j9E5c86tCtK5YrKsK5sxca4Lwkbc0=
github.com/acorn-io/broadcaster v0.0.0-20240105011354-bfadd4a7b45d h1:hfpNQkJ4I2b8+DbMr8m97gG67ku0uPsMzUfskVu3cHU=
github.com/acorn-io/broadcaster v0.0.0-20240105011354-bfadd4a7b45d/go.mod h1:WF6FYrEqW0+ZtY5OKb21JhSL0aeL5VJoVrm+u0d4gOE=
github.com/acorn-io/cmd v0.0.0-20230929053520-ebe1b9879b38 h1:oJMGvI702ZW5L0JjJfGV9ekzU2IqqTGjmAQl4gkO6Ro=
github.com/acorn-io/cmd v0.0.0-20230929053520-ebe1b9879b38/go.mod h1:bo9ONX4kagbQmXcG4bnfoK53MBFFtbUZ5fR7s9NfS+M=
github.com/acorn-io/function-builder v0.0.0-20240106051830-f0d368d2382c h1:xfOXFsu5BBsZg6B+xkD4mrP1vacp+WofRTfReSYpS1A=
github.com/acorn-io/function-builder v0.0.0-20240106051830-f0d368d2382c/go.mod h1:yf8qvacJItEV62UVTgNoTTnZHF/1/kqu+eU0CNf9hLw=
github.com/acorn-io/mink v0.0.0-20240105015834-b1f7af4fadea h1:HyH8nh7zZ7qZeWOjBGtK/iAI5gCu/c5iSnN703hlHHY=
github.com/acorn-io/mink v0.0.0-20240105015834-b1f7af4fadea/go.mod h1:EmoDPUYsPyBEhEpSC+LpRGUs2GeSI5nPOTa7WO7jwlc=
github.com/acorn-io/namegenerator v0.0.0-20220915160418-9e3d5a0ffe78 h1:5zs9L/CXNkuTdJSbhFWczAorbmx67nqlqswx5CQi7XI=
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/internal.acorn.io/v1/appspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Build struct {
BaseImage string `json:"baseImage,omitempty"`
ContextDirs map[string]string `json:"contextDirs,omitempty"`
BuildArgs map[string]string `json:"buildArgs,omitempty"`
WatchFiles []string `json:"watchFiles,omitempty"`
}

func (in Build) BaseBuild() Build {
Expand Down Expand Up @@ -636,6 +637,7 @@ type Container struct {
Files Files `json:"files,omitempty"`
Image string `json:"image,omitempty"`
Build *Build `json:"build,omitempty"`
Src string `json:"src,omitempty"`
Command CommandSlice `json:"command,omitempty"`
Interactive bool `json:"interactive,omitempty"`
Entrypoint CommandSlice `json:"entrypoint,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/internal.acorn.io/v1/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var (
type ContainerImageBuilderSpec struct {
Image string `json:"image,omitempty"`
Build *Build `json:"build,omitempty"`
Src string `json:"src,omitempty"`
// Sidecars is only populated for non-sidecar containers
Sidecars map[string]ContainerImageBuilderSpec `json:"sidecars,omitempty"`
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/apis/internal.acorn.io/v1/imageinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
)

type ContainerData struct {
Image string `json:"image,omitempty"`
Sidecars map[string]ImageData `json:"sidecars,omitempty"`
Image string `json:"image,omitempty"`
Sidecars map[string]ImageData `json:"sidecars,omitempty"`
AcornfileFragment string `json:"acornfileFragment,omitempty"`
}

type ImageData struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/internal.acorn.io/v1/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ func checkForDuplicateNames(in *AppSpec) error {
return nil
}

func addImpliedResources(in *AppSpec) error {
func AddImpliedResources(in *AppSpec) error {
if in.Volumes == nil {
in.Volumes = map[string]VolumeRequest{}
}
Expand Down Expand Up @@ -549,7 +549,7 @@ func (in *AppSpec) UnmarshalJSON(data []byte) error {
return err
}

if err := addImpliedResources(in); err != nil {
if err := AddImpliedResources(in); err != nil {
return err
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/internal.acorn.io/v1/zz_generated.deepcopy.go

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

9 changes: 6 additions & 3 deletions pkg/appdefinition/acornfile-schema.acorn
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ let types: {
additionalContexts?: StringMap
dockerfile?: string
target?: string
watchFiles: [string]
}

EnvVars: StringArray || StringMap
Expand Down Expand Up @@ -60,6 +61,7 @@ let types: {
NameDescription
Metadata

src?: string
match "consumes": string || [string]
match "dirs|directories": {
string: FunctionDir
Expand Down Expand Up @@ -211,6 +213,10 @@ let types: {

ContainerBase: {
ContainerCommon

// 1 or both of image or build is required
image?: string
build?: string || Build
match "dirs|directories": {
string: ContainerDir
}
Expand All @@ -224,9 +230,6 @@ let types: {
files?: {
string: FileContent
}
// 1 or both of image or build is required
image?: string
build?: string || Build
entrypoint?: string || [string]
match "command|cmd": string || [string]
match "env|environment": EnvVars
Expand Down
44 changes: 43 additions & 1 deletion pkg/appdefinition/appdefinition.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,39 @@ func (a *AppDefinition) imagesData() (result v1.ImagesData) {
return
}

func getFragment(imagesData v1.ImagesData, imageID string) string {
for _, check := range []map[string]v1.ContainerData{
imagesData.Containers,
imagesData.Functions,
imagesData.Jobs,
} {
for _, imageData := range check {
if imageData.Image == imageID && imageData.AcornfileFragment != "" {
return imageData.AcornfileFragment
}
}
}
return ""
}

func overlayFragment(con *v1.Container, imagesData v1.ImagesData, imageID, serviceName string) error {
fragment := getFragment(imagesData, imageID)
if fragment == "" {
return nil
}

containerData, err := json.Marshal(con)
if err != nil {
return err
}

strData := fmt.Sprintf(`
let serviceName: "%s"
{ %s } + { %s }`, serviceName, containerData, fragment)

return aml.Unmarshal([]byte(strData), con)
}

func (a *AppDefinition) AppSpec() (*v1.AppSpec, error) {
spec := &v1.AppSpec{}
if err := a.decode(spec); err != nil {
Expand All @@ -231,6 +264,9 @@ func (a *AppDefinition) AppSpec() (*v1.AppSpec, error) {

for containerName, conSpec := range spec.Containers {
if image, ok := GetImageReferenceForServiceName(containerName, spec, imagesData); ok {
if err := overlayFragment(&conSpec, imagesData, image, containerName); err != nil {
return nil, err
}
conSpec.Image, conSpec.Build = assignImage(conSpec.Image, conSpec.Build, image)
} else {
return nil, fmt.Errorf("failed to find image for container [%s] in Acornfile"+messageSuffix, containerName)
Expand All @@ -248,6 +284,9 @@ func (a *AppDefinition) AppSpec() (*v1.AppSpec, error) {

for functionName, conSpec := range spec.Functions {
if image, ok := GetImageReferenceForServiceName(functionName, spec, imagesData); ok {
if err := overlayFragment(&conSpec, imagesData, image, functionName); err != nil {
return nil, err
}
conSpec.Image, conSpec.Build = assignImage(conSpec.Image, conSpec.Build, image)
} else {
return nil, fmt.Errorf("failed to find image for function [%s] in Acornfile"+messageSuffix, functionName)
Expand All @@ -265,6 +304,9 @@ func (a *AppDefinition) AppSpec() (*v1.AppSpec, error) {

for containerName, conSpec := range spec.Jobs {
if image, ok := GetImageReferenceForServiceName(containerName, spec, imagesData); ok {
if err := overlayFragment(&conSpec, imagesData, image, containerName); err != nil {
return nil, err
}
conSpec.Image, conSpec.Build = assignImage(conSpec.Image, conSpec.Build, image)
} else {
return nil, fmt.Errorf("failed to find image for job [%s] in Acornfile"+messageSuffix, containerName)
Expand Down Expand Up @@ -314,7 +356,7 @@ func (a *AppDefinition) AppSpec() (*v1.AppSpec, error) {
spec.Services[serviceName] = serviceSpec
}

return spec, nil
return spec, v1.AddImpliedResources(spec)
}

func addContainerFiles(fileSet map[string]bool, builds map[string]v1.ContainerImageBuilderSpec, cwd string) {
Expand Down
106 changes: 42 additions & 64 deletions pkg/appdefinition/appdefinition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1090,87 +1090,65 @@ images: {
t.Fatal(err)
}

assert.Equal(t, &v1.BuilderSpec{
autogold.Expect(&v1.BuilderSpec{
Containers: map[string]v1.ContainerImageBuilderSpec{
"image": {
Image: "image-image",
Sidecars: map[string]v1.ContainerImageBuilderSpec{
"side": {
Image: "image-image-side",
},
},
},
"build": {
Build: &v1.Build{
Context: ".",
Dockerfile: "Dockerfile",
WatchFiles: []string{},
},
Sidecars: map[string]v1.ContainerImageBuilderSpec{
"side": {
Build: &v1.Build{
Context: ".",
Dockerfile: "Dockerfile",
},
},
},
Sidecars: map[string]v1.ContainerImageBuilderSpec{"side": {Build: &v1.Build{
Context: ".",
Dockerfile: "Dockerfile",
WatchFiles: []string{},
}}},
},
"buildcontext": {
Build: &v1.Build{
Context: ".",
Dockerfile: "Dockerfile",
ContextDirs: map[string]string{
"/var/tmp": "./foo/bar",
},
},
Sidecars: map[string]v1.ContainerImageBuilderSpec{
"side": {
Build: &v1.Build{
Context: ".",
Dockerfile: "Dockerfile",
ContextDirs: map[string]string{
"/var/tmp": "./foo/bar",
},
},
},
Context: ".",
Dockerfile: "Dockerfile",
ContextDirs: map[string]string{"/var/tmp": "./foo/bar"},
WatchFiles: []string{},
},
Sidecars: map[string]v1.ContainerImageBuilderSpec{"side": {Build: &v1.Build{
Context: ".",
Dockerfile: "Dockerfile",
ContextDirs: map[string]string{"/var/tmp": "./foo/bar"},
WatchFiles: []string{},
}}},
},
"image": {
Image: "image-image",
Sidecars: map[string]v1.ContainerImageBuilderSpec{"side": {Image: "image-image-side"}},
},
"imagecontext": {
Image: "imagecontext-image",
Build: &v1.Build{
BaseImage: "imagecontext-image",
Context: ".",
Dockerfile: "Dockerfile",
ContextDirs: map[string]string{
"/var/tmp": "./foo/bar",
},
Context: ".",
Dockerfile: "Dockerfile",
BaseImage: "imagecontext-image",
ContextDirs: map[string]string{"/var/tmp": "./foo/bar"},
},
Sidecars: map[string]v1.ContainerImageBuilderSpec{
"side": {
Image: "imagecontext-image-side",
Build: &v1.Build{
BaseImage: "imagecontext-image-side",
Context: ".",
Dockerfile: "Dockerfile",
ContextDirs: map[string]string{
"/var/tmp": "./foo/bar",
},
},
Sidecars: map[string]v1.ContainerImageBuilderSpec{"side": {
Image: "imagecontext-image-side",
Build: &v1.Build{
Context: ".",
Dockerfile: "Dockerfile",
BaseImage: "imagecontext-image-side",
ContextDirs: map[string]string{"/var/tmp": "./foo/bar"},
},
},
}},
},
},
Images: map[string]v1.ImageBuilderSpec{
"ibuild": {
AcornBuild: &v1.AcornBuild{
Context: ".",
Acornfile: "Acornfile",
},
},
"iimage": {
Image: "images-image-image",
},
"ibuild": {AcornBuild: &v1.AcornBuild{
Context: ".",
Acornfile: "Acornfile",
}},
"iimage": {Image: "images-image-image"},
},
}, buildSpec)
}).Equal(t, buildSpec)

app := appImage.WithImageData(v1.ImagesData{
Containers: map[string]v1.ContainerData{
Expand Down Expand Up @@ -3039,7 +3017,7 @@ func TestNestedScopedLabels(t *testing.T) {

func TestFunction(t *testing.T) {
appImage, err := NewAppDefinition([]byte(`functions: foo: {
image: "foo:latest"
src: "somewhere"
}`))
if err != nil {
t.Fatal(err)
Expand All @@ -3052,11 +3030,11 @@ func TestFunction(t *testing.T) {
}

autogold.Expect(v1.Container{
Image: "foo:latest",
Src: "somewhere",
}).Equal(t, appSpec.Functions["foo"])

_, err = NewAppDefinition([]byte(`functions: foo: {
image: "foo:latest"
src: "somewhere"
}
containers: foo: {
image: "foo:latest"
Expand Down
5 changes: 3 additions & 2 deletions pkg/build/assemble.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ func digestOnlyContainers(data map[string]v1.ContainerData) (map[string]v1.Conta
return nil, err
}
result[k] = v1.ContainerData{
Image: t.DigestStr(),
Sidecars: sidecars,
Image: t.DigestStr(),
AcornfileFragment: v.AcornfileFragment,
Sidecars: sidecars,
}
}
if len(result) == 0 {
Expand Down
Loading

0 comments on commit 3dc0733

Please sign in to comment.