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

Fix: use container name consistent with Docker Compose API #633

Merged
merged 9 commits into from
Dec 24, 2021

Conversation

r00tu53r
Copy link
Contributor

  • Docker compose v1 (python) and v2 (Go) use
    different service naming conventions. Set
    service name based on the version.

Confirmed with docker compose source and tests:

https://github.com/docker/compose/blob/v2/pkg/compose/compose.go#L36

* Docker compose v1 (python) and v2 (Go) use
  different service naming conventions. Set
  service name based on the version.
@elasticmachine
Copy link
Collaborator

elasticmachine commented Dec 22, 2021

❕ Build Aborted

There is a new build on-going so the previous on-going builds have been aborted.

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview

Expand to view the summary

Build stats

  • Reason: Aborted from #11

  • Start Time: 2021-12-24T10:35:54.061+0000

  • Duration: 18 min 19 sec

  • Commit: 42416ef

Test stats 🧪

Test Results
Failed 0
Passed 237
Skipped 0
Total 237

Steps errors 1

Expand to view the steps failures

Check
  • Took 5 min 33 sec . View more details here
  • Description: make build test-stack-command-8x check-git-clean

🤖 GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

Copy link
Contributor

@mtojek mtojek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution, Sai! I left a few comments, so let me know what do you think :)

@@ -340,7 +345,29 @@ func (p *Project) runDockerComposeCmd(opts dockerComposeOptions) error {
return cmd.Run()
}

func (p *Project) DockerComposeVersion() (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this method is used only by ContainerName, do you think we could make it package-visible and access it only in ContainerName?

This way you would prevent adjusting all calling code. If we need this condition in other places, we can refactor it then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I have refactored this to make it package-visible. To avoid calling docker-compose --version each time ContainerName is invoked I've stored the value in a project variable.

internal/compose/compose.go Outdated Show resolved Hide resolved
internal/compose/compose.go Outdated Show resolved Hide resolved
internal/compose/compose.go Outdated Show resolved Hide resolved
@mtojek
Copy link
Contributor

mtojek commented Dec 22, 2021

BTW CI reported some problems with this PR.

Copy link
Contributor

@mtojek mtojek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @r00tu53r, did you forget to push your branch? I don't see any changes applied, but comments were marked as resolved.

internal/compose/compose.go Outdated Show resolved Hide resolved
internal/compose/compose.go Outdated Show resolved Hide resolved
internal/compose/compose.go Outdated Show resolved Hide resolved
@r00tu53r
Copy link
Contributor Author

r00tu53r commented Dec 23, 2021

@mtojek sorry I forgot to push the changes. I've resolved all of the above issues.

Copy link
Contributor

@mtojek mtojek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a few suggestions, let me know what do you think.

@@ -340,7 +343,38 @@ func (p *Project) runDockerComposeCmd(opts dockerComposeOptions) error {
return cmd.Run()
}

func (p *Project) dockerComposeVersion() (*semver.Version, error) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: empty line (cane be removed)

// ContainerName method the container name for the service.
func (p *Project) ContainerName(serviceName string) string {
return fmt.Sprintf("%s_%s_1", p.name, serviceName)
if p.dcMajorVersion == "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be cleaner to keep the ContainerName function as simple as possible:

if p.dockerComposeV1 {
   return fmt.Sprintf("%s_%s_1", p.name, serviceName)
}
return fmt.Sprintf("%s-%s-1", p.name, serviceName)

Then, the logic determining p.dockerComposeV1 can be moved to the NewProject.

dockerComposeV1 can be bool, as the Compose V2 seems to deprecate V1 in the future.

internal/compose/compose.go Outdated Show resolved Hide resolved
name,
paths,
name: name,
dcMajorVersion: "",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As suggested in the other comment, dcMajorVersion can be replaced with dockerComposeV1 (bool).

Then in the NewProject, you can evaluate its value.

BTW I hope that the Project isn't created somewhere just with {}.

@mtojek mtojek changed the title Set service naming per docker compose version Fix: use container name consistent with Docker Compose API Dec 23, 2021
Copy link
Contributor

@mtojek mtojek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two small nit-picks are left, but it looks fine now!

Let me know what do you think about them and then I will merge this PR.

name,
paths,
name: name,
dockerComposeV1: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: You could also skip this property here and set it only when ve.Major == 1 or undefined. But it would the same outcome :)

Copy link
Contributor Author

@r00tu53r r00tu53r Dec 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't quite get what you meant. Did you mean we could use the semver.Version (*Version) inside the Project and use ver.Major() in ContainerName instead ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, you're setting the property dockerComposeV1: true by default. But you could adjust it to be the other way around, so just assume:

c := Project{
   name: name,
   composeFilePaths: paths
}

and then just enable it for two cases:

if ver.Major() == 1 {
   c.dockerComposeV1 = true
}
	if err != nil {
		logger.Errorf("Unable to determine docker-compose version: %v. Defaulting to 1.x", err)
                 c.dockerComposeV1 = true
		return &c, nil
	}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's see if I have permissions to push it here, so that we don't need to bother you anymore :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@r00tu53r
Copy link
Contributor Author

/test

@mtojek
Copy link
Contributor

mtojek commented Dec 24, 2021

/test

@mtojek
Copy link
Contributor

mtojek commented Dec 24, 2021

Error seems to be unrelated. I will merge this PR.

@mtojek mtojek merged commit ef32056 into elastic:master Dec 24, 2021
@elasticmachine
Copy link
Collaborator

💔 Build Failed

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2021-12-24T10:51:59.175+0000

  • Duration: 22 min 50 sec

  • Commit: 42416ef

Test stats 🧪

Test Results
Failed 0
Passed 451
Skipped 0
Total 451

Steps errors 1

Expand to view the steps failures

Check
  • Took 5 min 1 sec . View more details here
  • Description: make build test-stack-command-8x check-git-clean

🤖 GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants