Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: pass a custom logger from user for the builder factory #571

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 55 additions & 29 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 All @@ -94,22 +108,18 @@ 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
BuildContext: builder.DirContext{Path: f.buildContext}.BuildContext(),
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
}

Expand All @@ -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,
Expand All @@ -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
}

Expand Down Expand Up @@ -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 {
mojtaba-esk marked this conversation as resolved.
Show resolved Hide resolved
if opts.ImageName == "" {
MSevey marked this conversation as resolved.
Show resolved Hide resolved
return ErrImageNameEmpty
}
if opts.BuildContext == "" {
return ErrBuildContextEmpty
}
if opts.Logger == nil {
mojtaba-esk marked this conversation as resolved.
Show resolved Hide resolved
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
}
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")
ErrImageBuilderNotSet = errors.New("ImageBuilderNotSet", "image builder is not set")
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
Loading