This is an automated email from the ASF dual-hosted git repository. tsato pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit eca1ef38efe62191036d362e2b674cdcca47c00f Author: Tadayoshi Sato <[email protected]> AuthorDate: Thu Jul 28 22:46:16 2022 +0900 chore(cli): further refactor kamel local cmds --- pkg/cmd/local_build.go | 51 ++++++++++++++++------------------------- pkg/cmd/local_run.go | 6 ++--- pkg/cmd/local_util.go | 44 +++++++++++++++-------------------- pkg/cmd/local_util_container.go | 34 +++++++++++++++++++-------- pkg/util/docker/docker.go | 3 +-- pkg/util/docker/docker_base.go | 4 ++-- 6 files changed, 69 insertions(+), 73 deletions(-) diff --git a/pkg/cmd/local_build.go b/pkg/cmd/local_build.go index 181f58979..bc506c27b 100644 --- a/pkg/cmd/local_build.go +++ b/pkg/cmd/local_build.go @@ -159,6 +159,7 @@ func (o *localBuildCmdOptions) init(args []string) error { func (o *localBuildCmdOptions) run(cmd *cobra.Command, args []string) error { var dependenciesList, propertyFilesList []string routeFiles := args + if !o.BaseImage { dependencies, err := getDependencies(o.Context, args, o.Dependencies, o.MavenRepositories, true) if err != nil { @@ -177,56 +178,44 @@ func (o *localBuildCmdOptions) run(cmd *cobra.Command, args []string) error { dependenciesList = dependencies propertyFilesList = propertyFiles + // Integration directory can only be used when building an integration image or when we just + // build the integration without also building the image. A local build of the integration is + // represented by all the files that define the integration: dependencies, properties, and routes. if o.IntegrationDirectory != "" { - // Create dependencies subdirectory. - localDependenciesDirectory := getCustomDependenciesDir(o.IntegrationDirectory) - - // Copy dependencies in persistent IntegrationDirectory/dependencies - dependenciesList, err = copyIntegrationFilesToDirectory(dependencies, localDependenciesDirectory) + localDependenciesDir := getCustomDependenciesDir(o.IntegrationDirectory) + dependenciesList, err = copyIntegrationFilesToDirectory(dependencies, localDependenciesDir) if err != nil { return err } - // Once dependencies have been copied to local folder, we can exit. if o.DependenciesOnly { + // Once dependencies have been copied to local folder, we can exit. return nil } - // Create dependencies subdirectory. - localPropertiesDirectory := getCustomPropertiesDir(o.IntegrationDirectory) - - // Copy dependencies in persistent IntegrationDirectory/dependencies - propertyFilesList, err = copyIntegrationFilesToDirectory(propertyFiles, localPropertiesDirectory) + localPropertiesDir := getCustomPropertiesDir(o.IntegrationDirectory) + propertyFilesList, err = copyIntegrationFilesToDirectory(propertyFiles, localPropertiesDir) if err != nil { return err } - // Save routes. - localRoutesDirectory := getCustomRoutesDir(o.IntegrationDirectory) - - // Copy routes in persistent IntegrationDirectory/dependencies - routeFiles, err = copyIntegrationFilesToDirectory(args, localRoutesDirectory) + localRoutesDir := getCustomRoutesDir(o.IntegrationDirectory) + routeFiles, err = copyIntegrationFilesToDirectory(args, localRoutesDir) if err != nil { return err } - } - } - - // Integration directory can only be used when building an integration image or when we just - // build the integration without also building the image. A local build of the integration is - // represented by all the files that define the integration: dependencies, properties and routes. - // The only case in which we should not execute the integration image creation is when we want to - // just output the files that comprise the integration locally. - if o.IntegrationDirectory != "" && o.Image == "" { - return nil + // The only case in which we should not execute the integration image creation is when we want to + // just output the files that comprise the integration locally. + if o.Image == "" { + return nil + } + } } - // Create and build integration image. - err := createAndBuildIntegrationImage(o.Context, o.ContainerRegistry, o.BaseImage, - o.Image, propertyFilesList, dependenciesList, routeFiles, false, - cmd.OutOrStdout(), cmd.ErrOrStderr()) - if err != nil { + if err := createAndBuildIntegrationImage(o.Context, o.ContainerRegistry, o.BaseImage, o.Image, + propertyFilesList, dependenciesList, routeFiles, false, + cmd.OutOrStdout(), cmd.ErrOrStderr()); err != nil { return err } diff --git a/pkg/cmd/local_run.go b/pkg/cmd/local_run.go index 951e455f3..62e53205a 100644 --- a/pkg/cmd/local_run.go +++ b/pkg/cmd/local_run.go @@ -224,9 +224,9 @@ func (o *localRunCmdOptions) run(cmd *cobra.Command, args []string) error { // If this is a containerized local run, create, build and run the container image. if o.Containerize { - // Create and build integration image. - err := createAndBuildIntegrationImage(o.Context, "", false, o.Image, propertyFiles, dependencies, routes, hasIntegrationDir, cmd.OutOrStdout(), cmd.ErrOrStderr()) - if err != nil { + if err := createAndBuildIntegrationImage(o.Context, "", false, o.Image, + propertyFiles, dependencies, routes, hasIntegrationDir, + cmd.OutOrStdout(), cmd.ErrOrStderr()); err != nil { return err } diff --git a/pkg/cmd/local_util.go b/pkg/cmd/local_util.go index 67ea7f905..bc0d26619 100644 --- a/pkg/cmd/local_util.go +++ b/pkg/cmd/local_util.go @@ -357,8 +357,9 @@ func updateIntegrationProperties(properties []string, propertyFiles []string, ha // Relocate properties files to this integration's property directory. relocatedPropertyFiles := []string{} + dir := getLocalPropertiesDir() for _, propertyFile := range propertyFiles { - relocatedPropertyFile := path.Join(getLocalPropertiesDir(), path.Base(propertyFile)) + relocatedPropertyFile := path.Join(dir, path.Base(propertyFile)) if _, err := util.CopyFile(propertyFile, relocatedPropertyFile); err != nil { return nil, err } @@ -368,7 +369,7 @@ func updateIntegrationProperties(properties []string, propertyFiles []string, ha if !hasIntegrationDir { // Output list of properties to property file if any CLI properties were given. if len(properties) > 0 { - propertyFilePath := path.Join(getLocalPropertiesDir(), "CLI.properties") + propertyFilePath := path.Join(dir, "CLI.properties") if err := ioutil.WriteFile(propertyFilePath, []byte(strings.Join(properties, "\n")), 0o600); err != nil { return nil, err } @@ -382,22 +383,21 @@ func updateIntegrationProperties(properties []string, propertyFiles []string, ha func updateIntegrationDependencies(dependencies []string) error { // Create dependencies directory under Maven working directory. // This ensures that dependencies will be removed after they are not needed. - err := createLocalDependenciesDirectory() - if err != nil { + if err := createLocalDependenciesDirectory(); err != nil { return err } // Relocate dependencies files to this integration's dependencies directory + dir := getLocalDependenciesDir() for _, dependency := range dependencies { var targetPath string basePath := util.SubstringFrom(dependency, util.QuarkusDependenciesBaseDirectory) if basePath != "" { - targetPath = path.Join(getLocalDependenciesDir(), basePath) + targetPath = path.Join(dir, basePath) } else { - targetPath = path.Join(getLocalDependenciesDir(), path.Base(dependency)) + targetPath = path.Join(dir, path.Base(dependency)) } - _, err = util.CopyFile(dependency, targetPath) - if err != nil { + if _, err := util.CopyFile(dependency, targetPath); err != nil { return err } } @@ -406,14 +406,13 @@ func updateIntegrationDependencies(dependencies []string) error { } func updateIntegrationRoutes(routes []string) error { - err := createLocalRoutesDirectory() - if err != nil { + if err := createLocalRoutesDirectory(); err != nil { return err } + dir := getLocalRoutesDir() for _, route := range routes { - _, err = util.CopyFile(route, path.Join(getLocalRoutesDir(), path.Base(route))) - if err != nil { + if _, err := util.CopyFile(route, path.Join(dir, path.Base(route))); err != nil { return err } } @@ -422,8 +421,7 @@ func updateIntegrationRoutes(routes []string) error { } func updateQuarkusDirectory() error { - err := createLocalQuarkusDirectory() - if err != nil { + if err := createLocalQuarkusDirectory(); err != nil { return err } @@ -434,8 +432,7 @@ func updateQuarkusDirectory() error { } func updateAppDirectory() error { - err := createLocalAppDirectory() - if err != nil { + if err := createLocalAppDirectory(); err != nil { return err } @@ -446,8 +443,7 @@ func updateAppDirectory() error { } func updateLibDirectory() error { - err := createLocalLibDirectory() - if err != nil { + if err := createLocalLibDirectory(); err != nil { return err } @@ -467,8 +463,7 @@ func copyIntegrationFilesToDirectory(files []string, directory string) ([]string relocatedFilesList := []string{} for _, filePath := range files { newFilePath := path.Join(directory, path.Base(filePath)) - _, err := util.CopyFile(filePath, newFilePath) - if err != nil { + if _, err := util.CopyFile(filePath, newFilePath); err != nil { return relocatedFilesList, err } relocatedFilesList = append(relocatedFilesList, newFilePath) @@ -492,8 +487,7 @@ func copyQuarkusAppFiles(localDependenciesDirectory string, localQuarkusDir stri if strings.HasSuffix(file, ".dat") || strings.HasSuffix(file, "-bytecode.jar") { source := path.Join(localDependenciesDirectory, file) destination := path.Join(localQuarkusDir, file) - _, err = util.CopyFile(source, destination) - if err != nil { + if _, err = util.CopyFile(source, destination); err != nil { return err } } @@ -516,8 +510,7 @@ func copyLibFiles(localDependenciesDirectory string, localLibDirectory string) e for _, dependencyJar := range fileNames { source := path.Join(localDependenciesDirectory, dependencyJar) destination := path.Join(localLibDirectory, dependencyJar) - _, err = util.CopyFile(source, destination) - if err != nil { + if _, err = util.CopyFile(source, destination); err != nil { return err } } @@ -540,8 +533,7 @@ func copyAppFile(localDependenciesDirectory string, localAppDirectory string) er if strings.HasPrefix(dependencyJar, "camel-k-integration-") { source := path.Join(localDependenciesDirectory, dependencyJar) destination := path.Join(localAppDirectory, dependencyJar) - _, err = util.CopyFile(source, destination) - if err != nil { + if _, err = util.CopyFile(source, destination); err != nil { return err } } diff --git a/pkg/cmd/local_util_container.go b/pkg/cmd/local_util_container.go index e03824b7c..e2bcdd191 100644 --- a/pkg/cmd/local_util_container.go +++ b/pkg/cmd/local_util_container.go @@ -84,11 +84,20 @@ func setDockerEnvVars(envVars []string) { } func createAndBuildBaseImage(ctx context.Context, stdout, stderr io.Writer) error { + // This ensures the Dockerfile for the base image will not end up in an undesired location. + if docker.BaseWorkingDirectory == "" { + return errors.New("base directory that holds the base image Dockerfile has not been set correctly") + } + // Create the base image Docker file. if err := docker.CreateBaseImageDockerFile(); err != nil { return err } + return buildBaseImage(ctx, stdout, stderr) +} + +func buildBaseImage(ctx context.Context, stdout, stderr io.Writer) error { // Get the Docker command arguments for building the base image and create the command. args := docker.BuildBaseImageArgs() cmd := exec.CommandContext(ctx, "docker", args...) @@ -108,15 +117,8 @@ func createAndBuildBaseImage(ctx context.Context, stdout, stderr io.Writer) erro return nil } -func createAndBuildIntegrationImage(ctx context.Context, containerRegistry string, justBaseImage bool, image string, - propertyFiles []string, dependencies []string, routes []string, startsFromLocalFolder bool, stdout, stderr io.Writer) error { - // This ensures the Dockerfile for the base image will not end up in an undesired location. - if docker.BaseWorkingDirectory == "" { - return errors.New("base directory that holds the base image Dockerfile has not been set correctly") - } - +func setupDockerRegistry(containerRegistry string, image string, justBaseImage bool) error { docker.RegistryName = containerRegistry - // If we build a normal image, i.e. not the base image, we need to parse // the location where images will be pushed. if !justBaseImage { @@ -124,10 +126,19 @@ func createAndBuildIntegrationImage(ctx context.Context, containerRegistry strin if err != nil { return err } - docker.RegistryName = registryName } + return nil +} + +func createAndBuildIntegrationImage(ctx context.Context, containerRegistry string, justBaseImage bool, image string, + propertyFiles []string, dependencies []string, routes []string, startsFromLocalFolder bool, + stdout, stderr io.Writer) error { + if err := setupDockerRegistry(containerRegistry, image, justBaseImage); err != nil { + return err + } + // Create the Dockerfile and build the base image. if err := createAndBuildBaseImage(ctx, stdout, stderr); err != nil { return err @@ -179,6 +190,11 @@ func createAndBuildIntegrationImage(ctx context.Context, containerRegistry strin return err } + return buildIntegrationImage(ctx, image, startsFromLocalFolder, stdout, stderr) +} + +func buildIntegrationImage(ctx context.Context, image string, startsFromLocalFolder bool, + stdout, stderr io.Writer) error { // Get the Docker command arguments for building the base image and create the command. args := docker.BuildIntegrationImageArgs(image, MavenWorkingDirectory) cmd := exec.CommandContext(ctx, "docker", args...) diff --git a/pkg/util/docker/docker.go b/pkg/util/docker/docker.go index a053434c9..eca77307f 100644 --- a/pkg/util/docker/docker.go +++ b/pkg/util/docker/docker.go @@ -39,8 +39,7 @@ func CreateBaseImageDockerFile() error { // Write <BaseWorkingDirectory>/Dockerfile baseDockerFilePath := path.Join(BaseWorkingDirectory, "Dockerfile") - err := util.WriteToFile(baseDockerFilePath, strings.Join(dockerFile, "\n")) - if err != nil { + if err := util.WriteToFile(baseDockerFilePath, strings.Join(dockerFile, "\n")); err != nil { return err } diff --git a/pkg/util/docker/docker_base.go b/pkg/util/docker/docker_base.go index b1e032fbe..5fba82e95 100644 --- a/pkg/util/docker/docker_base.go +++ b/pkg/util/docker/docker_base.go @@ -40,7 +40,7 @@ var IntegrationWorkingDirectory = "" var NetworkName = "host" // Internal variables. -var ( +const ( dockerEndpointSeparator = "/" containerFileSeparator = "/" latestTag = "latest" @@ -174,7 +174,7 @@ func GetFullDockerImage(dockerImageName string, tag string) string { return strings.Join(fullImagePath, dockerEndpointSeparator) } -// GetBaseImagePath --. +// GetBaseImagePath returns Docker base image path. func GetBaseImagePath() string { return RegistryName + dockerEndpointSeparator + BaseImageName }
