utils.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package secret
  2. import (
  3. "github.com/docker/docker/api/types"
  4. "github.com/docker/docker/api/types/filters"
  5. "github.com/docker/docker/api/types/swarm"
  6. "github.com/docker/docker/client"
  7. "golang.org/x/net/context"
  8. )
  9. func getSecretsByName(ctx context.Context, client client.APIClient, names []string) ([]swarm.Secret, error) {
  10. args := filters.NewArgs()
  11. for _, n := range names {
  12. args.Add("names", n)
  13. }
  14. return client.SecretList(ctx, types.SecretListOptions{
  15. Filters: args,
  16. })
  17. }
  18. func getCliRequestedSecretIDs(ctx context.Context, client client.APIClient, names []string) ([]string, error) {
  19. ids := names
  20. // attempt to lookup secret by name
  21. secrets, err := getSecretsByName(ctx, client, ids)
  22. if err != nil {
  23. return nil, err
  24. }
  25. lookup := make(map[string]struct{})
  26. for _, id := range ids {
  27. lookup[id] = struct{}{}
  28. }
  29. if len(secrets) > 0 {
  30. ids = []string{}
  31. for _, s := range secrets {
  32. if _, ok := lookup[s.Spec.Annotations.Name]; ok {
  33. ids = append(ids, s.ID)
  34. }
  35. }
  36. }
  37. return ids, nil
  38. }