Skip to content

Commit

Permalink
Merge pull request #686 from FabianKramm/driver-refactor
Browse files Browse the repository at this point in the history
refactor: improve dockerless & cache agent binaries
  • Loading branch information
FabianKramm authored Sep 8, 2023
2 parents 2509d41 + de3eec6 commit a52d4b8
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 12 deletions.
26 changes: 23 additions & 3 deletions cmd/agent/container/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (cmd *SetupContainerCmd) Run(ctx context.Context) error {
}

// do dockerless build
err = dockerlessBuild(ctx, workspaceInfo, tunnelClient, log)
err = dockerlessBuild(ctx, setupInfo, workspaceInfo, tunnelClient, log)
if err != nil {
return fmt.Errorf("dockerless build: %w", err)
}
Expand Down Expand Up @@ -203,7 +203,13 @@ func fillContainerEnv(setupInfo *config.Result) error {
return nil
}

func dockerlessBuild(ctx context.Context, workspaceInfo *provider2.AgentWorkspaceInfo, client tunnel.TunnelClient, log log.Logger) error {
func dockerlessBuild(
ctx context.Context,
setupInfo *config.Result,
workspaceInfo *provider2.AgentWorkspaceInfo,
client tunnel.TunnelClient,
log log.Logger,
) error {
if os.Getenv("DOCKERLESS") != "true" {
return nil
}
Expand Down Expand Up @@ -266,12 +272,22 @@ func dockerlessBuild(ctx context.Context, workspaceInfo *provider2.AgentWorkspac
args = append(args, "--build-arg", "TARGETOS="+runtime.GOOS)
args = append(args, "--build-arg", "TARGETARCH="+runtime.GOARCH)

// ignore mounts
args = append(args, "--ignore-path", setupInfo.SubstitutionContext.ContainerWorkspaceFolder)
for _, m := range setupInfo.MergedConfig.Mounts {
// check if there already, then we don't touch it
files, err := os.ReadDir(m.Target)
if err == nil && len(files) > 0 {
args = append(args, "--ignore-path", m.Target)
}
}

// write output to log
writer := log.Writer(logrus.InfoLevel, false)
defer writer.Close()

// start building
log.Infof("Start dockerless building with kaniko...")
log.Infof("Start dockerless building %s %s", "/.dockerless/dockerless", strings.Join(args, " "))
cmd := exec.CommandContext(ctx, "/.dockerless/dockerless", args...)
cmd.Stdout = writer
cmd.Stderr = writer
Expand Down Expand Up @@ -309,6 +325,10 @@ func dockerlessBuild(ctx context.Context, workspaceInfo *provider2.AgentWorkspac
}

func parseIgnorePaths(ignorePaths string) []string {
if strings.TrimSpace(ignorePaths) == "" {
return nil
}

retPaths := []string{}
splitted := strings.Split(ignorePaths, ",")
for _, s := range splitted {
Expand Down
76 changes: 67 additions & 9 deletions pkg/binaries/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func DownloadBinaries(binaries map[string][]*provider2.ProviderBinary, targetFol

// check if binary is correct
targetFolder := filepath.Join(targetFolder, strings.ToLower(binaryName))
binaryPath := verifyBinary(binary, targetFolder)
if binaryPath != "" {
binaryPath := getBinaryPath(binary, targetFolder)
if verifyBinary(binaryPath, binary.Checksum) || fromCache(binary, targetFolder, log) {
retBinaries[binaryName] = binaryPath
continue
}
Expand All @@ -103,6 +103,7 @@ func DownloadBinaries(binaries map[string][]*provider2.ProviderBinary, targetFol
}
}

toCache(binary, binaryPath, log)
retBinaries[binaryName] = binaryPath
break
}
Expand All @@ -118,23 +119,76 @@ func DownloadBinaries(binaries map[string][]*provider2.ProviderBinary, targetFol
return retBinaries, nil
}

func verifyBinary(binary *provider2.ProviderBinary, targetFolder string) string {
func toCache(binary *provider2.ProviderBinary, binaryPath string, log log.Logger) {
if !isRemotePath(binary.Path) {
return
}

cachedBinaryPath := getCachedBinaryPath(binary.Path)
err := os.MkdirAll(filepath.Dir(cachedBinaryPath), 0777)
if err != nil {
return
}

err = copy.File(binaryPath, cachedBinaryPath, 0755)
if err != nil {
log.Warnf("Error copying binary to cache: %v", err)
return
}
}

func fromCache(binary *provider2.ProviderBinary, targetFolder string, log log.Logger) bool {
if !isRemotePath(binary.Path) {
return false
}

binaryPath := getBinaryPath(binary, targetFolder)
cachedBinaryPath := getCachedBinaryPath(binary.Path)
if !verifyBinary(cachedBinaryPath, binary.Checksum) {
return false
}

err := os.MkdirAll(path.Dir(binaryPath), 0755)
if err != nil {
log.Warnf("Error creating directory %s: %v", path.Dir(binaryPath), err)
return false
}

err = copy.File(cachedBinaryPath, binaryPath, 0755)
if err != nil {
log.Warnf("Error copying cached binary from %s to %s: %v", cachedBinaryPath, binaryPath, err)
return false
}

err = os.Chmod(binaryPath, 0755)
if err != nil {
log.Warnf("Error chmod binary %s: %v", binaryPath, err)
return false
}

return true
}

func getCachedBinaryPath(url string) string {
return filepath.Join(os.TempDir(), "devpod-binaries", hash.String(url)[:16])
}

func verifyBinary(binaryPath, checksum string) bool {
_, err := os.Stat(binaryPath)
if err != nil {
return ""
return false
}

// verify checksum
if binary.Checksum != "" {
if checksum != "" {
fileHash, err := hash.File(binaryPath)
if err != nil || !strings.EqualFold(fileHash, binary.Checksum) {
if err != nil || !strings.EqualFold(fileHash, checksum) {
_ = os.Remove(binaryPath)
return ""
return false
}
}

return binaryPath
return true
}

func getBinaryPath(binary *provider2.ProviderBinary, targetFolder string) string {
Expand All @@ -143,7 +197,7 @@ func getBinaryPath(binary *provider2.ProviderBinary, targetFolder string) string
}

// check if download
if !strings.HasPrefix(binary.Path, "http://") && !strings.HasPrefix(binary.Path, "https://") {
if !isRemotePath(binary.Path) {
return localTargetPath(binary, targetFolder)
}

Expand All @@ -164,6 +218,10 @@ func getBinaryPath(binary *provider2.ProviderBinary, targetFolder string) string
return path.Join(filepath.ToSlash(targetFolder), name)
}

func isRemotePath(path string) bool {
return strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://")
}

func downloadBinary(binaryName string, binary *provider2.ProviderBinary, targetFolder string, log log.Logger) (string, error) {
// check if local
_, err := os.Stat(binary.Path)
Expand Down

0 comments on commit a52d4b8

Please sign in to comment.