From dc5ae8107af28a22ea930922f7387226c8d55479 Mon Sep 17 00:00:00 2001 From: Moji Date: Mon, 14 Oct 2024 11:29:27 +0330 Subject: [PATCH] chore: pass a custom logger from user for the builder factory (#571) * chore: pass a custom logger from user for the builder factory * fix: imagebuilder empty check * chore: applied feedback --- pkg/container/docker.go | 84 +++++++++++++++++++++++++++-------------- pkg/container/errors.go | 4 ++ pkg/instance/build.go | 16 +++++++- 3 files changed, 73 insertions(+), 31 deletions(-) diff --git a/pkg/container/docker.go b/pkg/container/docker.go index ec35d50..c63cbc7 100644 --- a/pkg/container/docker.go +++ b/pkg/container/docker.go @@ -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 } @@ -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 } @@ -94,6 +108,10 @@ func (f *BuilderFactory) PushBuilderImage(ctx context.Context, imageName string) return ErrFailedToWriteDockerfile.Wrap(err) } + if f.imageBuilder == nil { + return ErrImageBuilderNotSet + } + logs, err := f.imageBuilder.Build(ctx, &builder.BuilderOptions{ ImageName: f.imageNameTo, Destination: f.imageNameTo, // in docker the image name and destination are the same @@ -101,15 +119,7 @@ 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, - }) - + f.logDebugWithQuotesDisabled("build logs: ", logs) return err } @@ -129,7 +139,11 @@ 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) + + if f.imageBuilder == nil { + return ErrImageBuilderNotSet + } logs, err := f.imageBuilder.Build(ctx, &builder.BuilderOptions{ ImageName: imageName, @@ -139,16 +153,7 @@ 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) - - logrus.SetFormatter(&logrus.TextFormatter{ - DisableQuote: qStatus, - }) + f.logDebugWithQuotesDisabled("build logs: ", logs) return err } @@ -184,7 +189,28 @@ 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.Logger == nil { + return ErrLoggerEmpty + } + return nil +} + +func (f *BuilderFactory) logDebugWithQuotesDisabled(args ...interface{}) { + lf := f.logger.Formatter.(*logrus.TextFormatter) + qStatus := lf.DisableQuote + lf.DisableQuote = true + f.logger.Debug(args...) + lf.DisableQuote = qStatus +} diff --git a/pkg/container/errors.go b/pkg/container/errors.go index ad866c3..bfeec0a 100644 --- a/pkg/container/errors.go +++ b/pkg/container/errors.go @@ -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") + ErrImageBuilderNotSet = errors.New("ImageBuilderNotSet", "image builder is not set") + ErrLoggerEmpty = errors.New("LoggerEmpty", "logger is empty") ) diff --git a/pkg/instance/build.go b/pkg/instance/build.go index ac9c96e..aa1bb54 100644 --- a/pkg/instance/build.go +++ b/pkg/instance/build.go @@ -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) } @@ -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) }