Skip to content
This repository was archived by the owner on Jul 15, 2025. It is now read-only.

Conversation

@kmendell
Copy link
Member

@kmendell kmendell commented Feb 22, 2025

Summary by Sourcery

This pull request adds a garbage collection feature to the application, allowing users to initiate garbage collection on Docker-hosted registries. It also refactors the Docker service to use the HTTP API, providing more configuration options and improving compatibility.

New Features:

  • Introduces a garbage collection feature for Docker-hosted registries, allowing users to clean up unused layers and free up storage space.

Enhancements:

  • Refactors the Docker service to use the Docker HTTP API instead of a socket connection, improving compatibility and configuration options.
  • Adds logging for garbage collection operations, providing better visibility into the process and potential issues.

Build:

  • Adds new environment variables for configuring the Docker HTTP API, including the API version, host, port, and registry container name.

Documentation:

  • Updates the README with instructions on configuring the new Docker HTTP API environment variables and enabling the garbage collection feature.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Feb 22, 2025

Reviewer's Guide by Sourcery

This pull request introduces a garbage collection feature for Docker-hosted registries, allowing users to initiate the process directly from the UI. It also refactors the Docker service to use the HTTP API, improving configuration flexibility. A new API endpoint /api/garbage-collect was added to initiate the garbage collection process. A new GarbageCollectButton component was added to the UI. The Docker service was refactored to use the Docker HTTP API instead of a socket connection, allowing for more flexible configuration. The .env.example file was updated to include new environment variables for configuring the Docker HTTP API connection.

Sequence diagram for Garbage Collection

sequenceDiagram
    actor User
    participant Browser
    participant API Endpoint
    participant DockerService
    participant DockerRegistry

    User->>Browser: Clicks 'Garbage Collect' button
    Browser->>API Endpoint: POST /api/garbage-collect
    API Endpoint->>DockerService: Calls runGarbageCollection()
    DockerService->>DockerRegistry: Creates exec instance
    DockerService->>DockerRegistry: Starts exec instance
    DockerService->>DockerRegistry: Gets exec stream
    DockerService->>DockerRegistry: Inspects exec instance
    DockerRegistry-->>DockerService: Returns result
    DockerService-->>API Endpoint: Returns success/failure
    API Endpoint-->>Browser: Returns JSON response
    Browser-->>User: Displays notification
Loading

File-Level Changes

Change Details Files
Refactors the Docker service to use the Docker HTTP API instead of a socket connection, allowing for more flexible configuration.
  • Removes socket path configuration.
  • Adds HTTP host and port configuration.
  • Updates the base URL to use HTTP.
  • Removes platform-specific logic for socket paths.
  • Updates axios configuration to remove socket path.
  • Adds logging for Docker service initialization.
src/lib/utils/garbagecollect.ts
Adds a 'Garbage Collect' button to the UI that triggers a garbage collection process on the Docker registry.
  • Adds a new GarbageCollectButton component.
  • Conditionally renders the button based on the PUBLIC_ENABLE_GARBAGE_COLLECT environment variable.
  • Implements the runGarbageCollect function to call the garbage collection API.
  • Adds loading state and UI feedback using svelte-sonner toasts.
  • Adds the GarbageCollectButton component to the main page.
src/routes/+page.svelte
src/lib/components/GarbageCollectButton.svelte
Introduces an API endpoint /api/garbage-collect to initiate the garbage collection process.
  • Creates a new API endpoint at /api/garbage-collect.
  • Initializes the DockerService.
  • Calls the runGarbageCollection method.
  • Returns a JSON response indicating success or failure.
  • Adds error handling and logging.
src/routes/api/garbage-collect/+server.ts
Updates the garbage collection logic to use the Docker HTTP API and stream the output.
  • Updates the runGarbageCollection method to use the HTTP API.
  • Adds a new getExecStream method to stream the output of the exec instance.
  • Updates the garbage collection command to include the -m flag.
  • Adds logging for the garbage collection process.
  • Returns the output and error streams in case of failure.
src/lib/utils/garbagecollect.ts
Updates the .env.example file to include new environment variables for configuring the Docker HTTP API connection and enabling the garbage collect button.
  • Adds PRIVATE_DOCKER_API_VERSION, PRIVATE_DOCKER_HTTP_HOST, PRIVATE_DOCKER_HTTP_PORT, and PRIVATE_DOCKER_REGISTRY_CONTAINER variables.
  • Adds PUBLIC_ENABLE_GARBAGE_COLLECT variable.
.env.example

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@kmendell kmendell marked this pull request as ready for review February 22, 2025 02:45
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @kmendell - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a configuration option to enable/disable the garbage collection feature.
  • Ensure that the Docker API version is configurable and validated.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Pid: number;
}

interface ExecStreamResponse {
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (complexity): Consider simplifying getExecStream by using an async iterator to handle the streaming data, which improves readability and reduces nested callbacks.

You can simplify getExecStream by replacing the nested event listeners with an async iterator over the streaming data. This reduces nested callbacks and improves readability while keeping the same functionality.

For example, change the method to this:

private async getExecStream(execId: string): Promise<ExecStreamResponse> {
  try {
    const response = await axios.post(
      `${this.baseUrl}/exec/${execId}/start`,
      { Detach: false, Tty: false },
      { ...this.getAxiosConfig(), responseType: 'stream' }
    );

    let output = '';
    for await (const chunk of response.data) {
      output += chunk.toString();
    }
    // Note: Errors in the stream will cause the iteration to throw
    return { output, error: '' };
  } catch (error) {
    logger.error('Failed to get exec stream:', error);
    throw error;
  }
}

This approach uses the async iterator of the stream, eliminating the manual event listener setup and reducing cognitive overhead.

@kmendell kmendell added this to the 0.23.0 milestone Feb 22, 2025
@kmendell
Copy link
Member Author

@sourcery-ai review

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @kmendell - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a health check endpoint to verify the Docker daemon is accessible before attempting garbage collection.
  • It would be useful to include the garbage collection logs in the UI for better transparency.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Pid: number;
}

interface ExecStreamResponse {
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (complexity): Consider using async iteration to handle the stream in getExecStream instead of manually wrapping it in a Promise.

Consider refactoring the manual Promise wrapping in getExecStream using async iteration on the stream. This reduces nesting and clarifies control flow. For example:

private async getExecStream(execId: string): Promise<ExecStreamResponse> {
  try {
    const response = await axios.post(
      `${this.baseUrl}/exec/${execId}/start`,
      {
        Detach: false,
        Tty: false,
      },
      {
        ...this.getAxiosConfig(),
        responseType: 'stream',
      }
    );

    let output = '';
    // Async iterate over stream chunks
    for await (const chunk of response.data) {
      output += chunk.toString();
    }
    return { output, error: '' };
  } catch (error) {
    logger.error('Failed to get exec stream:', error);
    throw error;
  }
}

This refactoring leverages the built-in async iterator support on streams and removes the need for manually handling data, end, and error events, making the code cleaner and easier to maintain.

@kmendell kmendell removed this from the 0.23.0 milestone Feb 23, 2025
@github-actions
Copy link

🐳 Docker image built and pushed!

Image: ghcr.io/kmendell/svelocker-ui:pr-46

You can pull and run this image with:

docker pull ghcr.io/kmendell/svelocker-ui:pr-46
docker run -p 3000:3000 ghcr.io/kmendell/svelocker-ui:pr-46

@ofkm ofkm deleted a comment from github-actions bot Feb 28, 2025
@github-actions
Copy link

🐳 Docker image built and pushed!

Image: ghcr.io/kmendell/svelocker-ui:pr-46

You can pull and run this image with:

docker pull ghcr.io/kmendell/svelocker-ui:pr-46
docker run -p 3000:3000 ghcr.io/kmendell/svelocker-ui:pr-46

@kmendell kmendell marked this pull request as draft March 17, 2025 06:46
@kmendell kmendell closed this Mar 28, 2025
@kmendell kmendell deleted the garbage-collect branch April 12, 2025 03:11
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants