Use ULID for app IDs and Docker label for container lookup
- Replace UUID with ULID for app ID generation (lexicographically sortable) - Remove container_id column from apps table (migration 002) - Add upaas.id Docker label to identify containers by app ID - Implement FindContainerByAppID in Docker client to query by label - Update handlers and deploy service to use label-based container lookup - Show system-managed upaas.id label in UI with editing disabled Container association is now determined dynamically via Docker label rather than stored in the database, making the system more resilient to container recreation or external changes.
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/docker/docker/api/types/mount"
|
||||
"github.com/docker/docker/api/types/network"
|
||||
"github.com/docker/docker/client"
|
||||
@@ -309,6 +310,51 @@ func (c *Client) IsContainerHealthy(
|
||||
return inspect.State.Health.Status == "healthy", nil
|
||||
}
|
||||
|
||||
// LabelUpaasID is the Docker label key used to identify containers managed by upaas.
|
||||
const LabelUpaasID = "upaas.id"
|
||||
|
||||
// ContainerInfo contains basic information about a container.
|
||||
type ContainerInfo struct {
|
||||
ID string
|
||||
Running bool
|
||||
}
|
||||
|
||||
// FindContainerByAppID finds a container by the upaas.id label.
|
||||
// Returns nil if no container is found.
|
||||
//
|
||||
//nolint:nilnil // returning nil,nil is idiomatic for "not found"
|
||||
func (c *Client) FindContainerByAppID(
|
||||
ctx context.Context,
|
||||
appID string,
|
||||
) (*ContainerInfo, error) {
|
||||
if c.docker == nil {
|
||||
return nil, ErrNotConnected
|
||||
}
|
||||
|
||||
filterArgs := filters.NewArgs()
|
||||
filterArgs.Add("label", LabelUpaasID+"="+appID)
|
||||
|
||||
containers, err := c.docker.ContainerList(ctx, container.ListOptions{
|
||||
All: true,
|
||||
Filters: filterArgs,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list containers: %w", err)
|
||||
}
|
||||
|
||||
if len(containers) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Return the first matching container
|
||||
ctr := containers[0]
|
||||
|
||||
return &ContainerInfo{
|
||||
ID: ctr.ID,
|
||||
Running: ctr.State == "running",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// cloneConfig holds configuration for a git clone operation.
|
||||
type cloneConfig struct {
|
||||
repoURL string
|
||||
|
||||
Reference in New Issue
Block a user