Skip to content

Commit

Permalink
feat: add a field to allow force rebuilding of the image (#1051)
Browse files Browse the repository at this point in the history
Signed-off-by: Harikrishnan Balagopal <[email protected]>
  • Loading branch information
HarikrishnanBalagopal authored Jun 21, 2023
1 parent 60f8f3e commit 88bb425
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
8 changes: 4 additions & 4 deletions environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
31 changes: 24 additions & 7 deletions environment/peercontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions types/environment/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 88bb425

Please sign in to comment.