2016-09-08 13:11:39 -04:00
|
|
|
package idresolver
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2017-03-30 20:15:54 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2017-05-08 13:51:30 -04:00
|
|
|
"github.com/docker/docker/client"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// IDResolver provides ID to Name resolution.
|
|
|
|
type IDResolver struct {
|
|
|
|
client client.APIClient
|
|
|
|
noResolve bool
|
|
|
|
cache map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new IDResolver.
|
2023-11-20 11:38:50 -05:00
|
|
|
func New(apiClient client.APIClient, noResolve bool) *IDResolver {
|
2016-09-08 13:11:39 -04:00
|
|
|
return &IDResolver{
|
2023-11-20 11:38:50 -05:00
|
|
|
client: apiClient,
|
2016-09-08 13:11:39 -04:00
|
|
|
noResolve: noResolve,
|
|
|
|
cache: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 12:04:36 -05:00
|
|
|
func (r *IDResolver) get(ctx context.Context, t any, id string) (string, error) {
|
2016-12-06 21:57:22 -05:00
|
|
|
switch t.(type) {
|
2016-09-08 13:11:39 -04:00
|
|
|
case swarm.Node:
|
|
|
|
node, _, err := r.client.NodeInspectWithRaw(ctx, id)
|
|
|
|
if err != nil {
|
2023-11-20 08:53:40 -05:00
|
|
|
// TODO(thaJeztah): should error-handling be more specific, or is it ok to ignore any error?
|
|
|
|
return id, nil //nolint:nilerr // ignore nil-error being returned, as this is a best-effort.
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
if node.Spec.Annotations.Name != "" {
|
|
|
|
return node.Spec.Annotations.Name, nil
|
|
|
|
}
|
|
|
|
if node.Description.Hostname != "" {
|
|
|
|
return node.Description.Hostname, nil
|
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
case swarm.Service:
|
2017-03-30 20:15:54 -04:00
|
|
|
service, _, err := r.client.ServiceInspectWithRaw(ctx, id, types.ServiceInspectOptions{})
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
2023-11-20 08:53:40 -05:00
|
|
|
// TODO(thaJeztah): should error-handling be more specific, or is it ok to ignore any error?
|
|
|
|
return id, nil //nolint:nilerr // ignore nil-error being returned, as this is a best-effort.
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
return service.Spec.Annotations.Name, nil
|
|
|
|
default:
|
2017-03-09 13:23:45 -05:00
|
|
|
return "", errors.Errorf("unsupported type")
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve will attempt to resolve an ID to a Name by querying the manager.
|
|
|
|
// Results are stored into a cache.
|
|
|
|
// If the `-n` flag is used in the command-line, resolution is disabled.
|
2023-11-20 12:04:36 -05:00
|
|
|
func (r *IDResolver) Resolve(ctx context.Context, t any, id string) (string, error) {
|
2016-09-08 13:11:39 -04:00
|
|
|
if r.noResolve {
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
if name, ok := r.cache[id]; ok {
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
name, err := r.get(ctx, t, id)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
r.cache[id] = name
|
|
|
|
return name, nil
|
|
|
|
}
|