This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2105 from ibuildthecloud/depot
Depot integration
- Loading branch information
Showing
8 changed files
with
256 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package buildkit | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/acorn-io/runtime/pkg/build/depot" | ||
"github.com/moby/buildkit/client" | ||
) | ||
|
||
var ( | ||
depotToken = os.Getenv("DEPOT_TOKEN") | ||
depotProject = os.Getenv("DEPOT_PROJECT_ID") | ||
) | ||
|
||
func newClient(ctx context.Context, image, platform string) (*client.Client, func(error), error) { | ||
if depotToken != "" && depotProject != "" { | ||
return depot.Client(ctx, depotProject, depotToken, image, platform) | ||
} | ||
bkc, err := client.New(ctx, "") | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
return bkc, func(_ error) { | ||
_ = bkc.Close() | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package depot | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/depot/depot-go/build" | ||
"github.com/depot/depot-go/machine" | ||
cliv1 "github.com/depot/depot-go/proto/depot/cli/v1" | ||
buildkit "github.com/moby/buildkit/client" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func Client(ctx context.Context, project, token, image, platform string) (*buildkit.Client, func(error), error) { | ||
b, err := newBuilder(ctx, project, token, image, platform) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return b.client, b.Close, nil | ||
} | ||
|
||
type builder struct { | ||
project string | ||
token string | ||
machine *machine.Machine | ||
build *build.Build | ||
client *buildkit.Client | ||
} | ||
|
||
func (b *builder) Close(err error) { | ||
if b.machine != nil { | ||
if err := b.machine.Release(); err != nil { | ||
logrus.Errorf("failed to release machine: %v", err) | ||
} | ||
b.machine = nil | ||
} | ||
if b.build != nil { | ||
b.build.Finish(err) | ||
b.build = nil | ||
} | ||
if b.client != nil { | ||
_ = b.client.Close() | ||
b.client = nil | ||
} | ||
} | ||
|
||
func newBuilder(ctx context.Context, project, token, image, platform string) (_ *builder, returnErr error) { | ||
if strings.Contains(platform, "arm64") { | ||
platform = "arm64" | ||
} else { | ||
platform = "amd64" | ||
} | ||
|
||
req := &cliv1.CreateBuildRequest{ | ||
ProjectId: project, | ||
Options: []*cliv1.BuildOptions{ | ||
{ | ||
Command: cliv1.Command_COMMAND_BUILD, | ||
Tags: []string{image}, | ||
}, | ||
}, | ||
} | ||
|
||
build, err := build.NewBuild(ctx, req, token) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create depot build: %w", err) | ||
} | ||
defer func() { | ||
if returnErr != nil { | ||
build.Finish(returnErr) | ||
} | ||
}() | ||
|
||
ctx, cancel := context.WithCancel(ctx) | ||
defer func() { | ||
if returnErr != nil { | ||
cancel() | ||
} | ||
}() | ||
|
||
buildkitMachine, err := machine.Acquire(ctx, build.ID, build.Token, platform) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer func() { | ||
if returnErr != nil { | ||
_ = buildkitMachine.Release() | ||
} | ||
}() | ||
|
||
timeoutCtx, timeoutCancel := context.WithTimeout(ctx, 5*time.Minute) | ||
defer timeoutCancel() | ||
|
||
buildkitClient, err := buildkitMachine.Connect(timeoutCtx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &builder{ | ||
project: project, | ||
token: token, | ||
machine: buildkitMachine, | ||
build: &build, | ||
client: buildkitClient, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package builder | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/acorn-io/baaah/pkg/router" | ||
"github.com/acorn-io/runtime/pkg/config" | ||
"github.com/acorn-io/runtime/pkg/system" | ||
corev1 "k8s.io/api/core/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
kclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func getDepotKey(ctx context.Context, c kclient.Client, namespace string) (string, string, error) { | ||
cfg, err := config.Get(ctx, c) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
if *cfg.InternalRegistryPrefix == "" { | ||
return "", "", nil | ||
} | ||
|
||
sec := &corev1.Secret{} | ||
if err := c.Get(ctx, router.Key(namespace, "depot-builder-key"), sec); apierrors.IsNotFound(err) { | ||
if err := c.Get(ctx, router.Key(system.ImagesNamespace, "depot-builder-key"), sec); apierrors.IsNotFound(err) { | ||
return "", "", nil | ||
} | ||
return "", "", nil | ||
} else if err != nil { | ||
return "", "", err | ||
} | ||
return string(sec.Data["token"]), string(sec.Data["projectId"]), nil | ||
} |
Oops, something went wrong.