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

Commit

Permalink
Support reading legacy AML
Browse files Browse the repository at this point in the history
Signed-off-by: Darren Shepherd <darren@acorn.io>
  • Loading branch information
ibuildthecloud committed Sep 29, 2023
1 parent 80968a8 commit fdabf2d
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 21 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/AlecAivazis/survey/v2 v2.3.6
github.com/acorn-io/aml v0.0.0-20230929053759-b8bb55610fce
github.com/acorn-io/aml/cli v0.0.0-20230929054110-b12c9629b109
github.com/acorn-io/aml/legacy v0.0.0-20230929054110-b12c9629b109
github.com/acorn-io/aml/legacy v0.0.0-20230929081514-1e9f3394432e
github.com/acorn-io/baaah v0.0.0-20230831185433-be0115009281
github.com/acorn-io/mink v0.0.0-20230804175412-8d121aae112c
github.com/acorn-io/namegenerator v0.0.0-20220915160418-9e3d5a0ffe78
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ github.com/acorn-io/aml v0.0.0-20230929053759-b8bb55610fce h1:KNSZ1c6BScVjZ0awkS
github.com/acorn-io/aml v0.0.0-20230929053759-b8bb55610fce/go.mod h1:I1qN++bfN+6sOV/FiACluqj2Eu5YaHOy82cTAnjLMf0=
github.com/acorn-io/aml/cli v0.0.0-20230929054110-b12c9629b109 h1:CNiK/y3JKcevLOYqcFDDGU4PWgs1Ehpxnpt63UBsYuo=
github.com/acorn-io/aml/cli v0.0.0-20230929054110-b12c9629b109/go.mod h1:x7Y6lLMTqMD65KWjgVaGem5tQP2ZeevOQiaps9epJj4=
github.com/acorn-io/aml/legacy v0.0.0-20230929054110-b12c9629b109 h1:WLdIyqFpqDCxbDFjwoFsBXOzy3gSySzjpteJqPRuLSs=
github.com/acorn-io/aml/legacy v0.0.0-20230929054110-b12c9629b109/go.mod h1:XnJZSZq/tG/jWPE/tmm2zy90gOZrJRIaOyKpoMulxfE=
github.com/acorn-io/aml/legacy v0.0.0-20230929081514-1e9f3394432e h1:W67DG9AcoNvBwIOR9OFUCZlSJBaHuvM2kXQ2+C6EnLk=
github.com/acorn-io/aml/legacy v0.0.0-20230929081514-1e9f3394432e/go.mod h1:XnJZSZq/tG/jWPE/tmm2zy90gOZrJRIaOyKpoMulxfE=
github.com/acorn-io/baaah v0.0.0-20230831185433-be0115009281 h1:3/Avpd1rOQqwARqrey5nd7QK6/E3ryT81b3tuSRr9wU=
github.com/acorn-io/baaah v0.0.0-20230831185433-be0115009281/go.mod h1:1KSGxZt0E2MDedJESKUUYtxCwsJ3A+xZiw2QD8cVbjU=
github.com/acorn-io/cmd v0.0.0-20230929053520-ebe1b9879b38 h1:oJMGvI702ZW5L0JjJfGV9ekzU2IqqTGjmAQl4gkO6Ro=
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/internal.acorn.io/v1/appimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type AppImage struct {
BuildArgs *GenericMap `json:"buildArgs,omitempty"`
Profiles []string `json:"profiles,omitempty"`
VCS VCS `json:"vcs,omitempty"`

AcornfileV0 bool `json:"acornfileV0,omitempty"`
}

type VCS struct {
Expand Down
67 changes: 55 additions & 12 deletions pkg/appdefinition/appdefinition.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ import (

"github.com/acorn-io/aml"
"github.com/acorn-io/aml/cli/pkg/amlreadhelper"
amllegacy "github.com/acorn-io/aml/legacy"
"github.com/acorn-io/baaah/pkg/typed"
v1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
"sigs.k8s.io/yaml"
)

const (
IconFile = "icon"
ReadmeFile = "README"
AcornCueFile = "Acornfile"
ImageDataFile = "images.json"
VCSDataFile = "vcs.json"
BuildDataFile = "build.json"
messageSuffix = ", you may need to define the image/build in the images section of the Acornfile"
IconFile = "icon"
ReadmeFile = "README"
AcornfileFile = "Acornfile.v1"
AcornfileV0File = "Acornfile"
ImageDataFile = "images.json"
VCSDataFile = "vcs.json"
BuildDataFile = "build.json"
messageSuffix = ", you may need to define the image/build in the images section of the Acornfile"
)

var (
Expand All @@ -42,18 +44,28 @@ type DataFiles struct {

type AppDefinition struct {
data []byte
acornfileV0 bool
imageDatas []v1.ImagesData
hasImageData bool
args map[string]any
profiles []string
}

func FromAppImage(appImage *v1.AppImage) (*AppDefinition, error) {
appDef, err := NewAppDefinition([]byte(appImage.Acornfile))
if err != nil {
return nil, err
func FromAppImage(appImage *v1.AppImage) (appDef *AppDefinition, err error) {
if appImage.AcornfileV0 {
appDef, err = NewLegacyAppDefinition([]byte(appImage.Acornfile))
if err != nil {
return nil, err
}
} else {
appDef, err = NewAppDefinition([]byte(appImage.Acornfile))
if err != nil {
return nil, err
}
}

appDef.acornfileV0 = appImage.AcornfileV0

appDef = appDef.WithImageData(appImage.ImageData)
return appDef, err
}
Expand All @@ -65,6 +77,7 @@ func (a *AppDefinition) clone() AppDefinition {
hasImageData: a.hasImageData,
args: a.args,
profiles: a.profiles,
acornfileV0: a.acornfileV0,
}
}

Expand All @@ -75,6 +88,18 @@ func (a *AppDefinition) WithImageData(imageData v1.ImagesData) *AppDefinition {
return &result
}

func NewLegacyAppDefinition(data []byte) (*AppDefinition, error) {
appDef := &AppDefinition{
data: data,
acornfileV0: true,
}
_, err := appDef.AppSpec()
if err != nil {
return nil, err
}
return appDef, nil
}

func NewAppDefinition(data []byte) (*AppDefinition, error) {
appDef := &AppDefinition{
data: data,
Expand Down Expand Up @@ -148,7 +173,18 @@ func (a *AppDefinition) getData() []byte {
return append(a.data, def...)
}

func (a *AppDefinition) decodeLegacy(out any) error {
return amllegacy.NewDecoder(bytes.NewReader(a.data), amllegacy.Options{
Args: a.args,
Profiles: a.profiles,
Acornfile: true,
}).Decode(out)
}

func (a *AppDefinition) decode(out any) error {
if a.acornfileV0 {
return a.decodeLegacy(out)
}
f, err := fs.Open(schemaFile)
if err != nil {
// this shouldn't happen, this an embedded FS
Expand Down Expand Up @@ -364,12 +400,19 @@ func AppImageFromTar(reader io.Reader) (*v1.AppImage, *DataFiles, error) {
}

switch header.Name {
case AcornCueFile:
case AcornfileFile:
data, err := io.ReadAll(tar)
if err != nil {
return nil, nil, err
}
result.Acornfile = string(data)
case AcornfileV0File:
data, err := io.ReadAll(tar)
if err != nil {
return nil, nil, err
}
result.Acornfile = string(data)
result.AcornfileV0 = true
case ImageDataFile:
err := json.NewDecoder(tar).Decode(&result.ImageData)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/build/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func getContextFromAppImage(dataFiles appdefinition.DataFiles, appImage *v1.AppI
}
}

if err := addFile(tempDir, appdefinition.AcornCueFile, appImage.Acornfile); err != nil {
if err := addFile(tempDir, appdefinition.AcornfileFile, appImage.Acornfile); err != nil {
return "", err
}
if err := addFile(tempDir, appdefinition.ImageDataFile, imageData); err != nil {
Expand Down
19 changes: 15 additions & 4 deletions pkg/imagedetails/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,26 @@ type Details struct {
Params *v1.ParamSpec `json:"params,omitempty"`
}

func ParseDetails(acornfile string, deployArgs map[string]any, profiles []string) (*Details, error) {
func ParseDetails(acornfile string, acornfileV0 bool, deployArgs map[string]any, profiles []string) (*Details, error) {
result := &Details{
DeployArgs: v1.NewGenericMap(deployArgs),
Profiles: profiles,
}

appDef, err := appdefinition.NewAppDefinition([]byte(acornfile))
if err != nil {
return nil, err
var (
appDef *appdefinition.AppDefinition
err error
)
if acornfileV0 {
appDef, err = appdefinition.NewLegacyAppDefinition([]byte(acornfile))
if err != nil {
return nil, err
}
} else {
appDef, err = appdefinition.NewAppDefinition([]byte(acornfile))
if err != nil {
return nil, err
}
}

if len(deployArgs) > 0 || len(profiles) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/imagedetails/imagedetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func getImageDetails(ctx context.Context, c kclient.Client, namespace, imageName
return nil, err
}

details, err := ParseDetails(appImageWithData.AppImage.Acornfile, opts.DeployArgs, opts.Profiles)
details, err := ParseDetails(appImageWithData.AppImage.Acornfile, appImageWithData.AppImage.AcornfileV0, opts.DeployArgs, opts.Profiles)
if err != nil {
return &apiv1.ImageDetails{
ObjectMeta: metav1.ObjectMeta{
Expand Down
6 changes: 6 additions & 0 deletions pkg/openapi/generated/openapi_generated.go

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

0 comments on commit fdabf2d

Please sign in to comment.