Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default process #520

Merged
merged 10 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

var (
Platform = newApisMustParse([]string{"0.3", "0.4", "0.5", "0.6"}, nil)
Buildpack = newApisMustParse([]string{"0.2", "0.3", "0.4", "0.5"}, nil)
Buildpack = newApisMustParse([]string{"0.2", "0.3", "0.4", "0.5", "0.6"}, nil)
)

type APIs struct {
Expand Down
92 changes: 76 additions & 16 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (b *Builder) Build() (*BuildMetadata, error) {
return nil, err
}

procMap := processMap{}
processMap := newProcessMap()
plan := b.Plan
var bom []BOMEntry
var slices []layers.Slice
Expand All @@ -94,10 +94,23 @@ func (b *Builder) Build() (*BuildMetadata, error) {
return nil, err
}

updateDefaultProcesses(br.Processes, api.MustParse(bp.API), b.PlatformAPI)

bom = append(bom, br.BOM...)
labels = append(labels, br.Labels...)
plan = plan.filter(br.MetRequires)
procMap.add(br.Processes)

replacedDefault := processMap.add(br.Processes)
if err != nil {
return nil, err
}
yaelharel marked this conversation as resolved.
Show resolved Hide resolved

if replacedDefault != "" {
warning := fmt.Sprintf("Warning: redefining the following default process type with a process not marked as default: %s", replacedDefault)
if _, err := b.Out.Write([]byte(warning)); err != nil {
return nil, err
}
}
slices = append(slices, br.Slices...)
}

Expand All @@ -106,16 +119,31 @@ func (b *Builder) Build() (*BuildMetadata, error) {
bom[i].convertMetadataToVersion()
}
}
procList := processMap.list()

return &BuildMetadata{
BOM: bom,
Buildpacks: b.Group.Group,
Labels: labels,
Processes: procMap.list(),
Slices: slices,
BOM: bom,
Buildpacks: b.Group.Group,
Labels: labels,
Processes: procList,
Slices: slices,
BuildpackDefaultProcessType: processMap.defaultType,
}, nil
}

// we set default = true for web processes when platformAPI >= 0.6 and buildpackAPI < 0.6
func updateDefaultProcesses(processes []launch.Process, buildpackAPI *api.Version, platformAPI *api.Version) {
if platformAPI.Compare(api.MustParse("0.6")) < 0 || buildpackAPI.Compare(api.MustParse("0.6")) >= 0 {
return
}

for i := range processes {
if processes[i].Type == "web" {
processes[i].Default = true
}
}
}

func (b *Builder) BuildConfig() (BuildConfig, error) {
appDir, err := filepath.Abs(b.AppDir)
if err != nil {
Expand Down Expand Up @@ -175,25 +203,57 @@ func containsEntry(metRequires []string, entry BuildPlanEntry) bool {
return false
}

type processMap map[string]launch.Process
type processMap struct {
typeToProcess map[string]launch.Process
defaultType string
}

func newProcessMap() processMap {
return processMap{
typeToProcess: make(map[string]launch.Process),
defaultType: "",
}
}

func (m processMap) add(l []launch.Process) {
for _, proc := range l {
m[proc.Type] = proc
// This function adds the processes from listToAdd to processMap
// it sets m.defaultType to the last default process
// if a non-default process overrides a default process, it returns its type and unset m.defaultType
func (m *processMap) add(listToAdd []launch.Process) string {
natalieparellano marked this conversation as resolved.
Show resolved Hide resolved
result := ""
for _, procToAdd := range listToAdd {
if procToAdd.Default {
m.defaultType = procToAdd.Type
result = ""
} else {
existingProc, ok := m.typeToProcess[procToAdd.Type]
if ok && existingProc.Type == m.defaultType { // existingProc.Default = true
yaelharel marked this conversation as resolved.
Show resolved Hide resolved
// non-default process overrides a default process
m.defaultType = ""
result = procToAdd.Type
}
}
m.typeToProcess[procToAdd.Type] = procToAdd
}

return result
}

// list returns a sorted array of processes.
// The array is sorted based on the process types.
// The list is sorted for reproducibility.
yaelharel marked this conversation as resolved.
Show resolved Hide resolved
func (m processMap) list() []launch.Process {
var keys []string
for key := range m {
keys = append(keys, key)
for proc := range m.typeToProcess {
keys = append(keys, proc)
}
sort.Strings(keys)
procs := []launch.Process{}
result := []launch.Process{}
for _, key := range keys {
procs = append(procs, m[key])
processWithNoDefault := m.typeToProcess[key]
processWithNoDefault.Default = false // we set the default to false so it won't be part of metadata.toml
natalieparellano marked this conversation as resolved.
Show resolved Hide resolved
result = append(result, processWithNoDefault)
}
return procs
return result
}

func (bom *BOMEntry) convertMetadataToVersion() {
Expand Down
Loading