getNodeContainer: regex to allow k3d-prefixed or non-prefixed name
- fixes #495 - additionally brings in a fix for a nil-pointer exception in case someone inputs a regex that would result in multiple containers being returned
This commit is contained in:
parent
2fe56a9732
commit
dbf3ff8172
@ -140,6 +140,7 @@ func pullImage(ctx context.Context, docker *client.Client, image string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getNodeContainer(ctx context.Context, node *k3d.Node) (*types.Container, error) {
|
func getNodeContainer(ctx context.Context, node *k3d.Node) (*types.Container, error) {
|
||||||
|
|
||||||
// (0) create docker client
|
// (0) create docker client
|
||||||
docker, err := GetDockerClient()
|
docker, err := GetDockerClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -153,8 +154,12 @@ func getNodeContainer(ctx context.Context, node *k3d.Node) (*types.Container, er
|
|||||||
for k, v := range node.Labels {
|
for k, v := range node.Labels {
|
||||||
filters.Add("label", fmt.Sprintf("%s=%s", k, v))
|
filters.Add("label", fmt.Sprintf("%s=%s", k, v))
|
||||||
}
|
}
|
||||||
// See https://github.com/moby/moby/issues/29997 for explanation around initial /
|
|
||||||
filters.Add("name", fmt.Sprintf("^/?%s$", node.Name)) // regex filtering for exact name match
|
// regex filtering for exact name match
|
||||||
|
// Assumptions:
|
||||||
|
// -> container names start with a / (see https://github.com/moby/moby/issues/29997)
|
||||||
|
// -> user input may or may not have the "k3d-" prefix
|
||||||
|
filters.Add("name", fmt.Sprintf("^/?(%s-)?%s$", k3d.DefaultObjectNamePrefix, node.Name))
|
||||||
|
|
||||||
containers, err := docker.ContainerList(ctx, types.ContainerListOptions{
|
containers, err := docker.ContainerList(ctx, types.ContainerListOptions{
|
||||||
Filters: filters,
|
Filters: filters,
|
||||||
@ -165,8 +170,7 @@ func getNodeContainer(ctx context.Context, node *k3d.Node) (*types.Container, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(containers) > 1 {
|
if len(containers) > 1 {
|
||||||
log.Errorln("Failed to get a single container")
|
return nil, fmt.Errorf("Failed to get a single container for name '%s'. Found: %d", node.Name, len(containers))
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(containers) == 0 {
|
if len(containers) == 0 {
|
||||||
|
Loading…
Reference in New Issue
Block a user