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

First implementation of DockerRegitry module #1165

Closed

Conversation

davidesalerno
Copy link

@davidesalerno davidesalerno commented May 10, 2023

What does this PR do?

This PR is adding the new Docker Registry module. This module is wrapping the registry image and it will be useful for those who needs to interact with a container registry to check if images are pushed or not after a build (i.e. local integration test of projects like kaniko or buildah).

@davidesalerno davidesalerno requested a review from a team as a code owner May 10, 2023 13:25
@netlify
Copy link

netlify bot commented May 10, 2023

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit 8e54e79
🔍 Latest deploy log https://app.netlify.com/sites/testcontainers-go/deploys/649c8e523e6ec70008b63a90
😎 Deploy Preview https://deploy-preview-1165--testcontainers-go.netlify.app/modules/dockerregistry
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@davidesalerno davidesalerno force-pushed the docker-registry-module branch 2 times, most recently from 5e5b445 to 98072f6 Compare May 10, 2023 13:31
Copy link
Member

@mdelapenya mdelapenya left a comment

Choose a reason for hiding this comment

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

Great contribution! Not sure if you already took a look at the existing tests including an authenticated docker registry (please see https://github.com/testcontainers/testcontainers-go/blob/main/docker_auth_test.go#L240), which I think it could be a great addition to this module, something like adding a customizer for WithAuthentication or similar.

Wdyt?

.github/workflows/module-registry.yml Outdated Show resolved Hide resolved
mkdocs.yml Outdated Show resolved Hide resolved
@mdelapenya
Copy link
Member

Not to mention that we could eventually merge this PR at this state, without the authentication, and create a follow-up PR with that work. At the end of the day, the module won't be available until the next release

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

modules/registry/go.mod Outdated Show resolved Hide resolved
@davidesalerno davidesalerno force-pushed the docker-registry-module branch 4 times, most recently from 68403a3 to 61ff364 Compare June 13, 2023 07:05
@mdelapenya
Copy link
Member

Hi @davidesalerno did you take a look at my comment here #1165 (review)?

@davidesalerno
Copy link
Author

Hi @davidesalerno did you take a look at my comment here #1165 (review)?

Yes, I've just added a customizer to support an authenticated registry but I'm having some issue in testing it due to issu #279.

Could you help me in test that is working making this unit test working?

return &DockerRegistryContainer{Container: container}, nil
}

func WithAuthentication(dockerAuthConfig string) testcontainers.CustomizeRequestOption {
Copy link
Member

Choose a reason for hiding this comment

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

Let's define the expectations on how to use this module: that would a user expect if the registry runs with authentication?

I'd try to start with a container request like the one in this test: https://github.com/testcontainers/testcontainers-go/blob/main/docker_auth_test.go#L275-L299, allowing customising the auth file.

Then, the test for the module should check that's is posible to log in into that registry.

Does it make sense to you?

Copy link
Author

Choose a reason for hiding this comment

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

Yes it is making sense to me.

Do we want also a realm as a parameter?

Copy link
Member

Choose a reason for hiding this comment

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

I do not have a strong opinion... I think we can start simple, and iterate with a follow-up PR for the realm if you want. Or if you see it important, add it here. What is your preference? At the same time, if you consider it important for users of the module, then we can include it since the beginning.

@davidesalerno davidesalerno force-pushed the docker-registry-module branch from df08171 to 9a5ef0d Compare June 28, 2023 17:14
@davidesalerno davidesalerno force-pushed the docker-registry-module branch from 9a5ef0d to 3811c5b Compare June 28, 2023 17:33
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 8 Code Smells

No Coverage information No Coverage information
11.0% 11.0% Duplication

@@ -68,6 +68,7 @@ nav:
- modules/pulsar.md
- modules/redis.md
- modules/redpanda.md
- modules/dockerregistry.md
Copy link
Member

Choose a reason for hiding this comment

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

@@ -138,6 +138,12 @@ updates:
interval: monthly
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
Copy link
Member

Choose a reason for hiding this comment

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

func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*DockerRegistryContainer, error) {
req := testcontainers.ContainerRequest{
Image: "registry:2",
ExposedPorts: []string{"5000:5000/tcp"},
Copy link
Member

Choose a reason for hiding this comment

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

I think we do not want to always force a fixed port for the container:

Suggested change
ExposedPorts: []string{"5000:5000/tcp"},
ExposedPorts: []string{"5000/tcp"},


}

// WithData customizer that will retrieve a data directory and mount it inside the registry container
Copy link
Member

Choose a reason for hiding this comment

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

Do we need a Docker mount for it? I wonder if copying the files/dir into the container, using the req.Files attribute would be something to consider.

@eddumelendez is there a preference when creating modules: using mounts or just copying files before the container start? 🤔

Copy link
Member

Choose a reason for hiding this comment

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

The preference is to copy files.

Copy link
Member

Choose a reason for hiding this comment

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

@davidesalerno then I'd advocate for converting all the Mounts into Files 🙏

Comment on lines +91 to +96
// WithImage customizer that will override the registry image used
func WithImage(image string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) {
req.Image = image
}
}
Copy link
Member

Choose a reason for hiding this comment

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

The core library already exposes a testcontainers.WithImage option, which can be used in the modules.

Suggested change
// WithImage customizer that will override the registry image used
func WithImage(image string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) {
req.Image = image
}
}

func TestDockerRegistryWithCustomImage(t *testing.T) {
ctx := context.Background()

container, err := RunContainer(ctx, WithImage("docker.io/registry:latest"))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
container, err := RunContainer(ctx, WithImage("docker.io/registry:latest"))
container, err := RunContainer(ctx, testcontainers.WithImage("docker.io/registry:latest"))

_, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
}
func getRegistryPortAndAddress(t *testing.T, err error, container *DockerRegistryContainer, ctx context.Context) (nat.Port, string) {
Copy link
Member

Choose a reason for hiding this comment

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

I'd create a method at the registry container level, so that any user would be able to get both. Please take a look at how mysql or postgres modules expose the ConnectionString

ctx := context.Background()
wd, err := os.Getwd()
assert.NoError(t, err)
container, err := RunContainer(ctx, WithAuthentication(wd+"/../../testdata/auth"), WithData(wd+"/../../testdata/data"), WithImage("docker.io/registry:latest"))
Copy link
Member

Choose a reason for hiding this comment

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

I'd copy/duplicate the resources that exist in the core library to the module, to avoid relative paths and decouple the module from the upstream core library just a little bit more.

We could decide later if we want to test that feature in the core or in the module.

@@ -0,0 +1,11 @@
//go:build tools
Copy link
Member

Choose a reason for hiding this comment

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

Please take a look at #1303, which removed the dependency with gotestsum at the package dependency level: it's now a responsibility of our CI to install it, simplifying our dependency tree.

Therefore, this file should be removed

@@ -0,0 +1,56 @@
name: DockerRegistry module pipeline
Copy link
Member

Choose a reason for hiding this comment

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

Please take a look at #1414: this module file has been removed in favour of an array of modules in the ci.yml workflow.


}

// WithData customizer that will retrieve a data directory and mount it inside the registry container
Copy link
Member

Choose a reason for hiding this comment

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

@davidesalerno then I'd advocate for converting all the Mounts into Files 🙏

@mdelapenya
Copy link
Member

@davidesalerno we have sent #2341 creating a module for the docker registry. It supersedes this one, so I marked it to close this one.

I'd thank you a lot if you could take a look to that PR and come with your feedback, so it should cover your use case 🙏

Cheers!

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