-
Notifications
You must be signed in to change notification settings - Fork 9
/
alpine.go
63 lines (51 loc) · 1.97 KB
/
alpine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package genbinary
import (
"context"
"strings"
"github.com/moby/buildkit/client/llb"
"golang.org/x/exp/slices"
"namespacelabs.dev/foundation/internal/artifacts/oci"
"namespacelabs.dev/foundation/internal/build"
"namespacelabs.dev/foundation/internal/build/buildkit"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/dependencies/pins"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/llbutil"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
func BuildAlpine(loc pkggraph.Location, plan *schema.ImageBuildPlan_AlpineBuild) build.Spec {
return &buildAlpine{loc: loc, plan: plan}
}
type buildAlpine struct {
loc pkggraph.Location
plan *schema.ImageBuildPlan_AlpineBuild
}
func (b *buildAlpine) BuildImage(ctx context.Context, env pkggraph.SealedContext, conf build.Configuration) (compute.Computable[oci.Image], error) {
var image string
if b.plan.Version != "" {
image = "docker.io/library/alpine@" + b.plan.Version
} else {
var err error
image, err = pins.CheckDefault("alpine")
if err != nil {
return nil, err
}
}
if conf.TargetPlatform() == nil {
return nil, fnerrors.InternalError("alpine builds require a platform")
}
packages := slices.Clone(b.plan.Package)
slices.Sort(packages)
state := llbutil.Image(image, *conf.TargetPlatform()).
Run(llb.Shlexf("apk add --no-cache %s", strings.Join(packages, " "))).Root()
def, err := buildkit.MarshalForTarget(ctx, state, conf)
if err != nil {
return nil, fnerrors.InternalError("failed to marshal llb plan: %w", err)
}
return buildkit.BuildDefinitionToImage(buildkit.DeferClient(env.Configuration(), conf.TargetPlatform()), conf, def), nil
}
func (b *buildAlpine) PlatformIndependent() bool { return false }