From 88bb4253f3a0ff90d1ed54f15e5d54acbb51424b Mon Sep 17 00:00:00 2001 From: Hari Date: Wed, 21 Jun 2023 17:30:21 +0530 Subject: [PATCH] feat: add a field to allow force rebuilding of the image (#1051) Signed-off-by: Harikrishnan Balagopal --- environment/environment.go | 8 ++++---- environment/peercontainer.go | 31 ++++++++++++++++++++++++------- types/environment/container.go | 5 +++-- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/environment/environment.go b/environment/environment.go index e022b6578..f6611f238 100644 --- a/environment/environment.go +++ b/environment/environment.go @@ -91,7 +91,7 @@ func NewEnvironment(envInfo EnvInfo, grpcQAReceiver net.Addr) (env *Environment, if !common.IsPresent(envInfo.EnvPlatformConfig.Platforms, runtime.GOOS) && envInfo.EnvPlatformConfig.Container.Image == "" { return nil, fmt.Errorf("platform '%s' is not supported", runtime.GOOS) } - c := envInfo.EnvPlatformConfig.Container + containerInfo := envInfo.EnvPlatformConfig.Container tempPath, err := os.MkdirTemp(common.TempPath, "environment-"+envInfo.Name+"-*") if err != nil { return env, fmt.Errorf("failed to create the temporary directory. Error: %w", err) @@ -103,14 +103,14 @@ func NewEnvironment(envInfo EnvInfo, grpcQAReceiver net.Addr) (env *Environment, TempPathsMap: map[string]string{}, active: true, } - if c.Image == "" { + if containerInfo.Image == "" { env.Env, err = NewLocal(envInfo, grpcQAReceiver) if err != nil { return env, fmt.Errorf("failed to create the local environment. Error: %w", err) } return env, nil } - envVariableName := common.MakeStringEnvNameCompliant(c.Image) + envVariableName := common.MakeStringEnvNameCompliant(containerInfo.Image) // TODO: replace below signalling mechanism with `prefersLocalExecutuion: true` in the transformer.yaml // Check if image is part of the current environment. // It will be set as environment variable with root as base path of move2kube @@ -139,7 +139,7 @@ func NewEnvironment(envInfo EnvInfo, grpcQAReceiver net.Addr) (env *Environment, } } if env.Env == nil { - env.Env, err = NewPeerContainer(envInfo, grpcQAReceiver, c, envInfo.SpawnContainers) + env.Env, err = NewPeerContainer(envInfo, grpcQAReceiver, containerInfo, envInfo.SpawnContainers) if err != nil { return env, fmt.Errorf("failed to create the peer container environment. Error: %w", err) } diff --git a/environment/peercontainer.go b/environment/peercontainer.go index 417e2af46..5b38f7edc 100644 --- a/environment/peercontainer.go +++ b/environment/peercontainer.go @@ -57,17 +57,34 @@ func NewPeerContainer(envInfo EnvInfo, grpcQAReceiver net.Addr, containerInfo en return nil, fmt.Errorf("failed to get the container engine. Error: %w", err) } if containerInfo.WorkingDir == "" { - containerInfo.WorkingDir = filepath.Join(string(filepath.Separator), types.AppNameShort) + containerInfo.WorkingDir = "/" + types.AppNameShort } peerContainer := &PeerContainer{ - EnvInfo: envInfo, - OriginalImage: containerInfo.Image, - ContainerInfo: containerInfo, - GRPCQAReceiver: grpcQAReceiver, + EnvInfo: envInfo, + OriginalImage: containerInfo.Image, + ContainerInfo: containerInfo, + GRPCQAReceiver: grpcQAReceiver, + WorkspaceSource: "/" + DefaultWorkspaceDir, + } + if containerInfo.ImageBuild.ForceRebuild { + logrus.Debugf("force rebuilding the image. containerInfo: %#v", containerInfo) + imageBuildContext := filepath.Join(envInfo.Context, peerContainer.ContainerInfo.ImageBuild.Context) + if err := cengine.BuildImage( + peerContainer.OriginalImage, + imageBuildContext, + peerContainer.ContainerInfo.ImageBuild.Dockerfile, + ); err != nil { + return nil, fmt.Errorf( + "failed to build the container image '%s' using the context directory '%s' and the Dockerfile at path '%s' . Error: %w", + peerContainer.OriginalImage, + imageBuildContext, + peerContainer.ContainerInfo.ImageBuild.Dockerfile, + err, + ) + } } - peerContainer.WorkspaceSource = filepath.Join(string(filepath.Separator), DefaultWorkspaceDir) peerContainer.ContainerInfo.Image = peerContainer.ContainerInfo.Image + strings.ToLower(envInfo.Name+uniuri.NewLen(5)) - logrus.Debug("trying to create a new image with the input data") + logrus.Debugf("trying to create a new image '%s' with the input data", peerContainer.ContainerInfo.Image) if err := cengine.CopyDirsIntoImage( peerContainer.OriginalImage, peerContainer.ContainerInfo.Image, diff --git a/types/environment/container.go b/types/environment/container.go index 9e9e84e5e..f7faf6907 100644 --- a/types/environment/container.go +++ b/types/environment/container.go @@ -34,6 +34,7 @@ type Container struct { // ImageBuild stores container build information type ImageBuild struct { - Dockerfile string `yaml:"dockerfile"` // Default : Look for Dockerfile in the same folder - Context string `yaml:"context"` // Default : Same folder as the yaml + ForceRebuild bool `yaml:"forceRebuild"` // Force rebuild the image even if it exists + Dockerfile string `yaml:"dockerfile"` // Default : Look for Dockerfile in the same folder + Context string `yaml:"context"` // Default : Same folder as the yaml }