Skip to content

Commit

Permalink
WIP: Writing custom values to yaml manifest
Browse files Browse the repository at this point in the history
TODO: Writing custom values to the Dockerfile
TODO: Handling boolean and integer types

Signed-off-by: joshuabezaleel <[email protected]>
  • Loading branch information
joshuabezaleel committed Feb 8, 2022
1 parent 151d9b9 commit f0ba80f
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cmd/porter/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func buildBundleBuildCommand(p *porter.Porter) *cobra.Command {
porter build --version 0.1.0
porter build --file path/to/porter.yaml
porter build --dir path/to/build/context
porter build --custom test-mode=true --custom version=0.2.0
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.Validate(p)
Expand All @@ -78,6 +79,8 @@ func buildBundleBuildCommand(p *porter.Porter) *cobra.Command {
"Path to the build context directory where all bundle assets are located.")
f.StringVar(&opts.Driver, "driver", porter.BuildDriverDefault,
fmt.Sprintf("Experimental. Driver for building the invocation image. Allowed values are: %s", strings.Join(porter.BuildDriverAllowedValues, ", ")))
f.StringSliceVar(&opts.Customs, "custom", nil,
"Define an individual key-value pair for custom section in the form of NAME=VALUE. May be specified multiple times.")

// Allow configuring the --driver flag with build-driver, to avoid conflicts with other commands
cmd.Flag("driver").Annotations = map[string][]string{
Expand Down
2 changes: 2 additions & 0 deletions docs/content/cli/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ porter build [flags]
porter build --version 0.1.0
porter build --file path/to/porter.yaml
porter build --dir path/to/build/context
porter build --custom test-mode=true --custom version=0.2.0
```

### Options

```
--custom strings Define an individual key-value pair for custom section in the form of NAME=VALUE. May be specified multiple times.
-d, --dir string Path to the build context directory where all bundle assets are located.
--driver string Experimental. Driver for building the invocation image. Allowed values are: docker, buildkit (default "docker")
-f, --file porter.yaml Path to the Porter manifest. Defaults to porter.yaml in the current directory.
Expand Down
2 changes: 2 additions & 0 deletions docs/content/cli/bundles_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ porter bundles build [flags]
porter build --version 0.1.0
porter build --file path/to/porter.yaml
porter build --dir path/to/build/context
porter build --custom test-mode=true --custom version=0.2.0
```

### Options

```
--custom strings Define an individual key-value pair for custom section in the form of NAME=VALUE. May be specified multiple times.
-d, --dir string Path to the build context directory where all bundle assets are located.
--driver string Experimental. Driver for building the invocation image. Allowed values are: docker, buildkit (default "docker")
-f, --file porter.yaml Path to the Porter manifest. Defaults to porter.yaml in the current directory.
Expand Down
4 changes: 4 additions & 0 deletions pkg/build/buildkit/buildx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"

Expand Down Expand Up @@ -37,6 +38,8 @@ func NewBuilder(cxt *portercontext.Context) *Builder {
}

func (b *Builder) BuildInvocationImage(manifest *manifest.Manifest) error {
log.Printf("p.Manifest on buildx.go = %+v", manifest)

fmt.Fprintf(b.Out, "\nStarting Invocation Image Build (%s) =======> \n", manifest.Image)
cli, err := command.NewDockerCli()
if err != nil {
Expand Down Expand Up @@ -68,6 +71,7 @@ func (b *Builder) BuildInvocationImage(manifest *manifest.Manifest) error {
},
BuildArgs: map[string]string{
"BUNDLE_DIR": build.BUNDLE_DIR,
"JOBEL": "JOBEL",
},
Session: []session.Attachable{authprovider.NewDockerAuthProvider(b.Err)},
},
Expand Down
6 changes: 6 additions & 0 deletions pkg/build/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io/ioutil"
"log"
"path/filepath"

"get.porter.sh/porter/pkg/build"
Expand All @@ -30,6 +31,10 @@ func NewBuilder(cxt *portercontext.Context) *Builder {
}

func (b *Builder) BuildInvocationImage(manifest *manifest.Manifest) error {
log.Printf("p.Manifest on build/docker.go = %+v", manifest)

a := "JOBEL"

fmt.Fprintf(b.Out, "\nStarting Invocation Image Build (%s) =======> \n", manifest.Image)
buildOptions := types.ImageBuildOptions{
SuppressOutput: false,
Expand All @@ -39,6 +44,7 @@ func (b *Builder) BuildInvocationImage(manifest *manifest.Manifest) error {
Dockerfile: filepath.ToSlash(build.DOCKER_FILE),
BuildArgs: map[string]*string{
"BUNDLE_DIR": &build.BUNDLE_DIR,
"JOBEL": &a,
},
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/parameters/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package parameters

import (
"fmt"
"log"
"strings"

"github.com/cnabio/cnab-go/schema"
Expand All @@ -24,18 +25,22 @@ const (
func ParseVariableAssignments(params []string) (map[string]string, error) {
variables := make(map[string]string)
for _, p := range params {
log.Println(p)

parts := strings.SplitN(p, "=", 2)
if len(parts) < 2 {
return nil, fmt.Errorf("invalid parameter (%s), must be in name=value format", p)
}
log.Println(parts)

variable := strings.TrimSpace(parts[0])
if variable == "" {
return nil, fmt.Errorf("invalid parameter (%s), variable name is required", p)
}
log.Println(variable)
value := strings.TrimSpace(parts[1])

log.Println(value)
variables[variable] = value
}

Expand Down
29 changes: 29 additions & 0 deletions pkg/porter/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package porter

import (
"fmt"
"log"
"os"

"get.porter.sh/porter/pkg/build"
"get.porter.sh/porter/pkg/cnab"
configadapter "get.porter.sh/porter/pkg/cnab/config-adapter"
"get.porter.sh/porter/pkg/config"
"get.porter.sh/porter/pkg/mixin"
"get.porter.sh/porter/pkg/parameters"
"get.porter.sh/porter/pkg/printer"
"github.com/Masterminds/semver/v3"
"github.com/opencontainers/go-digest"
Expand All @@ -25,6 +27,12 @@ type BuildOptions struct {

// Driver to use when building the invocation image.
Driver string

// Customs is the unparsed list of NAME=VALUE custom inputs set on the command line.
Customs []string

// parsedCustoms is the parsed set of custom inputs from Customs.
parsedCustoms map[string]string
}

const BuildDriverDefault = config.BuildDriverDocker
Expand Down Expand Up @@ -52,9 +60,27 @@ func (o *BuildOptions) Validate(p *Porter) error {
// This would be less awkward if we didn't do an automatic build during publish
p.Data.BuildDriver = o.Driver

err := o.parseCustomInputs()
if err != nil {
return err
}

return o.bundleFileOptions.Validate(p.Context)
}

func (o *BuildOptions) parseCustomInputs() error {
log.Println(o.Customs)

p, err := parameters.ParseVariableAssignments(o.Customs)
if err != nil {
return err
}

o.parsedCustoms = p

return nil
}

func stringSliceContains(allowedValues []string, value string) bool {
for _, allowed := range allowedValues {
if value == allowed {
Expand Down Expand Up @@ -117,6 +143,9 @@ func (p *Porter) Build(opts BuildOptions) error {
}

builder := p.GetBuilder()

log.Printf("p.Manifest on build.go = %+v", p.Manifest)

return errors.Wrap(builder.BuildInvocationImage(p.Manifest), "unable to build CNAB invocation image")
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/porter/generateManifest.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package porter

import (
"fmt"
"regexp"

"get.porter.sh/porter/pkg/build"
"get.porter.sh/porter/pkg/yaml"
"github.com/pkg/errors"
Expand Down Expand Up @@ -41,5 +44,18 @@ func (p *Porter) generateInternalManifest(opts BuildOptions) error {
}
}

numberRegex := regexp.MustCompile(`\d`)

if opts.parsedCustoms != nil {
for k, v := range opts.parsedCustoms {
if v != "true" && v != "false" && !numberRegex.MatchString(v) {
v = fmt.Sprintf("\"%s\"", v)
}
if err = e.SetValue("custom."+k, v); err != nil {
return err
}
}
}

return e.WriteFile(build.LOCAL_MANIFEST)
}
4 changes: 2 additions & 2 deletions pkg/porter/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ type BundleActionOptions struct {
sharedOptions
BundlePullOptions
AllowDockerHostAccess bool
NoLogs bool
NoLogs bool

bundleRef *cnab.BundleReference
}

Expand Down

0 comments on commit f0ba80f

Please sign in to comment.