Skip to content

Commit

Permalink
Merge branch 'main' into mojtaba/514-headless-services
Browse files Browse the repository at this point in the history
  • Loading branch information
mojtaba-esk authored Oct 15, 2024
2 parents a7f3913 + f86e86c commit fb06a44
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 34 deletions.
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 {
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
}
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
1 change: 1 addition & 0 deletions pkg/instance/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,5 @@ var (
ErrInstanceNameAlreadyExists = errors.New("InstanceNameAlreadyExists", "instance name '%s' already exists")
ErrSettingSidecarName = errors.New("SettingSidecarName", "error setting sidecar name with prefix '%s' for instance '%s'")
ErrGettingServiceEndpointNotAllowed = errors.New("GettingServiceEndpointNotAllowed", "getting service endpoint is only allowed in state 'Started'. Current state is '%s'")
ErrCannotCloneInstance = errors.New("CannotCloneInstance", "cannot clone instance '%s' in state '%s'")
)
6 changes: 5 additions & 1 deletion pkg/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,14 @@ func (i *Instance) CloneWithSuffix(suffix string) (*Instance, error) {
}

// CloneWithName creates a clone of the instance with a given name
// This function can only be called in the state 'Committed'
// This function can only be called in the state 'Committed' or 'Stopped'
// When cloning an instance that is a sidecar, the clone will be not a sidecar
// When cloning an instance with sidecars, the sidecars will be cloned as well
func (i *Instance) CloneWithName(name string) (*Instance, error) {
if !i.IsInState(StateCommitted, StateStopped) {
return nil, ErrCannotCloneInstance.WithParams(i.name, i.state)
}

clonedSidecars, err := i.sidecars.clone(name)
if err != nil {
return nil, err
Expand Down
13 changes: 11 additions & 2 deletions pkg/sidecars/tshark/tshark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,16 @@ func TestTsharkValidateConfig(t *testing.T) {
}

func TestTsharkClone(t *testing.T) {
testInstance, err := instance.New("testInstance", &system.SystemDependencies{})
testInstance, err := instance.New("testInstance",
&system.SystemDependencies{
Logger: logrus.New(),
})
require.NoError(t, err)

err = testInstance.Build().SetImage(context.Background(), "testImage")
require.NoError(t, err)

err = testInstance.Build().Commit(context.Background())
require.NoError(t, err)

tshark := &Tshark{
Expand All @@ -184,8 +193,8 @@ func TestTsharkClone(t *testing.T) {
UploadInterval: time.Minute * 5,
instance: testInstance,
}

clonePrefixName := "test-clone-prefix"

clone, err := tshark.Clone(clonePrefixName)
require.NoError(t, err)

Expand Down

0 comments on commit fb06a44

Please sign in to comment.