Skip to content

Commit

Permalink
chore: pass a custom logger from user for the builder factory
Browse files Browse the repository at this point in the history
  • Loading branch information
mojtaba-esk committed Oct 8, 2024
1 parent 339f749 commit 904f9f7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 29 deletions.
77 changes: 50 additions & 27 deletions pkg/container/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,34 @@ type BuilderFactory struct {
dockerFileInstructions []string
buildContext string
args []builder.ArgInterface
logger *logrus.Logger
}

type BuilderFactoryOptions struct {
ImageName string
BuildContext string
ImageBuilder builder.Builder
Args []builder.ArgInterface
Logger *logrus.Logger
}

// NewBuilderFactory creates a new instance of BuilderFactory.
func NewBuilderFactory(imageName, buildContext string, imageBuilder builder.Builder, args []builder.ArgInterface) (*BuilderFactory, error) {
if err := os.MkdirAll(buildContext, 0755); err != nil {
func NewBuilderFactory(opts BuilderFactoryOptions) (*BuilderFactory, error) {
if err := verifyOptions(opts); err != nil {
return nil, err
}

if err := os.MkdirAll(opts.BuildContext, 0755); err != nil {
return nil, ErrFailedToCreateContextDir.Wrap(err)
}

return &BuilderFactory{
imageNameFrom: imageName,
dockerFileInstructions: []string{"FROM " + imageName},
buildContext: buildContext,
imageBuilder: imageBuilder,
args: args,
imageNameFrom: opts.ImageName,
dockerFileInstructions: []string{"FROM " + opts.ImageName},
buildContext: opts.BuildContext,
imageBuilder: opts.ImageBuilder,
args: opts.Args,
logger: opts.Logger,
}, nil
}

Expand Down Expand Up @@ -73,7 +87,7 @@ func (f *BuilderFactory) Changed() bool {
// The image is identified by the provided name.
func (f *BuilderFactory) PushBuilderImage(ctx context.Context, imageName string) error {
if !f.Changed() {
logrus.Debugf("No changes made to image %s, skipping push", f.imageNameFrom)
f.logger.Debugf("No changes made to image %s, skipping push", f.imageNameFrom)
return nil
}

Expand Down Expand Up @@ -101,14 +115,11 @@ func (f *BuilderFactory) PushBuilderImage(ctx context.Context, imageName string)
Args: f.args,
})

qStatus := logrus.TextFormatter{}.DisableQuote
logrus.SetFormatter(&logrus.TextFormatter{
DisableQuote: true,
})
logrus.Debug("build logs: ", logs)
logrus.SetFormatter(&logrus.TextFormatter{
DisableQuote: qStatus,
})
lf := f.logger.Formatter.(*logrus.TextFormatter)
qStatus := lf.DisableQuote
lf.DisableQuote = true
f.logger.Debug("build logs: ", logs)
lf.DisableQuote = qStatus

return err
}
Expand All @@ -129,7 +140,7 @@ func (f *BuilderFactory) BuildImageFromGitRepo(ctx context.Context, gitCtx build
return ErrFailedToGetDefaultCacheOptions.Wrap(err)
}

logrus.Debugf("Building image %s from git repo %s", imageName, gitCtx.Repo)
f.logger.Debugf("Building image %s from git repo %s", imageName, gitCtx.Repo)

logs, err := f.imageBuilder.Build(ctx, &builder.BuilderOptions{
ImageName: imageName,
Expand All @@ -139,16 +150,12 @@ func (f *BuilderFactory) BuildImageFromGitRepo(ctx context.Context, gitCtx build
Args: f.args,
})

qStatus := logrus.TextFormatter{}.DisableQuote
logrus.SetFormatter(&logrus.TextFormatter{
DisableQuote: true,
})

logrus.Debug("build logs: ", logs)
lf := f.logger.Formatter.(*logrus.TextFormatter)
qStatus := lf.DisableQuote
lf.DisableQuote = true
f.logger.Debug("build logs: ", logs)
lf.DisableQuote = qStatus

logrus.SetFormatter(&logrus.TextFormatter{
DisableQuote: qStatus,
})
return err
}

Expand Down Expand Up @@ -184,7 +191,23 @@ func (f *BuilderFactory) GenerateImageHash() (string, error) {
return "", ErrHashingBuildContext.Wrap(err)
}

logrus.Debug("Generated image hash: ", fmt.Sprintf("%x", hasher.Sum(nil)))
f.logger.Debug("Generated image hash: ", fmt.Sprintf("%x", hasher.Sum(nil)))

return fmt.Sprintf("%x", hasher.Sum(nil)), nil
}

func verifyOptions(opts BuilderFactoryOptions) error {
if opts.ImageName == "" {
return ErrImageNameEmpty
}
if opts.BuildContext == "" {
return ErrBuildContextEmpty
}
if opts.ImageBuilder == nil {
return ErrImageBuilderEmpty
}
if opts.Logger == nil {
return ErrLoggerEmpty
}
return nil
}
4 changes: 4 additions & 0 deletions pkg/container/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ var (
ErrReadingFile = errors.New("ReadingFile", "error reading file: %s")
ErrHashingFile = errors.New("HashingFile", "error hashing file %s")
ErrHashingBuildContext = errors.New("HashingBuildContext", "error hashing build context")
ErrImageNameEmpty = errors.New("ImageNameEmpty", "image name is empty")
ErrBuildContextEmpty = errors.New("BuildContextEmpty", "build context is empty")
ErrImageBuilderEmpty = errors.New("ImageBuilderEmpty", "image builder is empty")
ErrLoggerEmpty = errors.New("LoggerEmpty", "logger is empty")
)
16 changes: 14 additions & 2 deletions pkg/instance/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ func (b *build) SetImage(ctx context.Context, image string, args ...builder.ArgI
}

// Use the builder to build a new image
factory, err := container.NewBuilderFactory(image, b.getBuildDir(), b.instance.ImageBuilder, args)
factory, err := container.NewBuilderFactory(container.BuilderFactoryOptions{
ImageName: image,
BuildContext: b.getBuildDir(),
ImageBuilder: b.instance.ImageBuilder,
Args: args,
Logger: b.instance.Logger,
})
if err != nil {
return ErrCreatingBuilder.Wrap(err)
}
Expand All @@ -80,7 +86,13 @@ func (b *build) SetGitRepo(ctx context.Context, gitContext builder.GitContext, a
return ErrGettingImageName.Wrap(err)
}

factory, err := container.NewBuilderFactory(imageName, b.getBuildDir(), b.instance.ImageBuilder, args)
factory, err := container.NewBuilderFactory(container.BuilderFactoryOptions{
ImageName: imageName,
BuildContext: b.getBuildDir(),
ImageBuilder: b.instance.ImageBuilder,
Args: args,
Logger: b.instance.Logger,
})
if err != nil {
return ErrCreatingBuilder.Wrap(err)
}
Expand Down

0 comments on commit 904f9f7

Please sign in to comment.