Skip to content

Commit

Permalink
fix: build dir now with error and cache
Browse files Browse the repository at this point in the history
Signed-off-by: Smuu <18609909+Smuu@users.noreply.github.com>
  • Loading branch information
smuu committed Dec 6, 2024
1 parent a185a61 commit 10bedaf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
20 changes: 15 additions & 5 deletions pkg/instance/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ func (b *build) SetImage(ctx context.Context, image string, args ...builder.ArgI
return ErrSettingImageNotAllowed.WithParams(b.instance.state.String())
}

buildDir, err := b.getBuildDir()
if err != nil {
return ErrGettingBuildDir.Wrap(err)
}

// Use the builder to build a new image
factory, err := container.NewBuilderFactory(container.BuilderFactoryOptions{
ImageName: image,
BuildContext: b.getBuildDir(),
BuildContext: buildDir,
ImageBuilder: b.instance.ImageBuilder,
Args: args,
Logger: b.instance.Logger,
Expand Down Expand Up @@ -85,9 +90,14 @@ func (b *build) SetGitRepo(ctx context.Context, gitContext builder.GitContext, a
return ErrGettingImageName.Wrap(err)
}

buildDir, err := b.getBuildDir()
if err != nil {
return ErrGettingBuildDir.Wrap(err)
}

factory, err := container.NewBuilderFactory(container.BuilderFactoryOptions{
ImageName: imageName,
BuildContext: b.getBuildDir(),
BuildContext: buildDir,
ImageBuilder: b.instance.ImageBuilder,
Args: args,
Logger: b.instance.Logger,
Expand Down Expand Up @@ -224,12 +234,12 @@ func getImageRegistry(imageName string) (string, error) {
}

// getBuildDir returns the build directory for the instance
func (b *build) getBuildDir() string {
func (b *build) getBuildDir() (string, error) {
tmpDir, err := os.MkdirTemp("", "knuu-build-*")
if err != nil {
return ""
return "", err
}
return filepath.Join(tmpDir, b.instance.name)
return filepath.Join(tmpDir, b.instance.name), nil
}

// addFileToBuilder adds a file to the builder
Expand Down
1 change: 1 addition & 0 deletions pkg/instance/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ var (
ErrSettingGitRepo = errors.New("SettingGitRepo", "setting git repo is only allowed in state 'None'. Current state is '%s'")
ErrGettingBuildContext = errors.New("GettingBuildContext", "error getting build context")
ErrGettingImageName = errors.New("GettingImageName", "error getting image name")
ErrGettingBuildDir = errors.New("GettingBuildDir", "error getting build directory")
ErrSettingImageNotAllowedForSidecars = errors.New("SettingImageNotAllowedForSidecars", "setting image is not allowed for sidecars")
ErrSettingCommand = errors.New("SettingCommand", "setting command is only allowed in state 'Preparing' or 'Committed'. Current state is '%s")
ErrSettingArgsNotAllowed = errors.New("SettingArgsNotAllowed", "setting args is only allowed in state 'Preparing' or 'Committed'. Current state is '%s")
Expand Down
24 changes: 20 additions & 4 deletions pkg/instance/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ func (s *storage) AddFile(src string, dest string, chown string) error {
return s.addFileToInstance(buildDirPath, dest, chown)
}

buildDir, err := s.instance.build.getBuildDir()
if err != nil {
return ErrGettingBuildDir.Wrap(err)
}
s.instance.Logger.WithFields(logrus.Fields{
"file": dest,
"instance": s.instance.name,
"state": s.instance.state,
"build_dir": s.instance.build.getBuildDir(),
"build_dir": buildDir,
}).Debug("added file")
return nil
}
Expand Down Expand Up @@ -91,7 +95,11 @@ func (s *storage) AddFolder(src string, dest string, chown string) error {
if err != nil {
return err
}
dstPath := filepath.Join(s.instance.build.getBuildDir(), dest, relPath)
buildDir, err := s.instance.build.getBuildDir()
if err != nil {
return ErrGettingBuildDir.Wrap(err)
}
dstPath := filepath.Join(buildDir, dest, relPath)

if info.IsDir() {
// create directory at destination path
Expand All @@ -105,11 +113,15 @@ func (s *storage) AddFolder(src string, dest string, chown string) error {
return ErrCopyingFolderToInstance.WithParams(src, s.instance.name).Wrap(err)
}

buildDir, err := s.instance.build.getBuildDir()
if err != nil {
return ErrGettingBuildDir.Wrap(err)
}
s.instance.Logger.WithFields(logrus.Fields{
"folder": dest,
"instance": s.instance.name,
"state": s.instance.state,
"build_dir": s.instance.build.getBuildDir(),
"build_dir": buildDir,
}).Debug("added folder")
return nil
}
Expand Down Expand Up @@ -246,7 +258,11 @@ func (s *storage) validateFileArgs(src, dest, chown string) error {
}

func (s *storage) copyFileToBuildDir(src, dest string) (string, error) {
dstPath := filepath.Join(s.instance.build.getBuildDir(), dest)
buildDir, err := s.instance.build.getBuildDir()
if err != nil {
return "", ErrGettingBuildDir.Wrap(err)
}
dstPath := filepath.Join(buildDir, dest)
if err := os.MkdirAll(filepath.Dir(dstPath), os.ModePerm); err != nil {
return "", ErrCreatingDirectory.Wrap(err)
}
Expand Down

0 comments on commit 10bedaf

Please sign in to comment.