ParseSecrets retrieves the secrets with the requested names and fills secret IDs into the secret references.
(ctx context.Context, apiClient client.SecretAPIClient, requestedSecrets []*swarm.SecretReference)
| 11 | // ParseSecrets retrieves the secrets with the requested names and fills |
| 12 | // secret IDs into the secret references. |
| 13 | func ParseSecrets(ctx context.Context, apiClient client.SecretAPIClient, requestedSecrets []*swarm.SecretReference) ([]*swarm.SecretReference, error) { |
| 14 | if len(requestedSecrets) == 0 { |
| 15 | return []*swarm.SecretReference{}, nil |
| 16 | } |
| 17 | |
| 18 | secretRefs := make(map[string]*swarm.SecretReference) |
| 19 | |
| 20 | for _, secret := range requestedSecrets { |
| 21 | if _, exists := secretRefs[secret.File.Name]; exists { |
| 22 | return nil, fmt.Errorf("duplicate secret target for %s not allowed", secret.SecretName) |
| 23 | } |
| 24 | secretRef := new(swarm.SecretReference) |
| 25 | *secretRef = *secret |
| 26 | secretRefs[secret.File.Name] = secretRef |
| 27 | } |
| 28 | |
| 29 | args := make(client.Filters) |
| 30 | for _, s := range secretRefs { |
| 31 | args.Add("name", s.SecretName) |
| 32 | } |
| 33 | |
| 34 | res, err := apiClient.SecretList(ctx, client.SecretListOptions{ |
| 35 | Filters: args, |
| 36 | }) |
| 37 | if err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | |
| 41 | foundSecrets := make(map[string]string) |
| 42 | for _, secret := range res.Items { |
| 43 | foundSecrets[secret.Spec.Annotations.Name] = secret.ID |
| 44 | } |
| 45 | |
| 46 | addedSecrets := make([]*swarm.SecretReference, 0, len(secretRefs)) |
| 47 | for _, ref := range secretRefs { |
| 48 | id, ok := foundSecrets[ref.SecretName] |
| 49 | if !ok { |
| 50 | return nil, fmt.Errorf("secret not found: %s", ref.SecretName) |
| 51 | } |
| 52 | |
| 53 | // set the id for the ref to properly assign in swarm |
| 54 | // since swarm needs the ID instead of the name |
| 55 | ref.SecretID = id |
| 56 | addedSecrets = append(addedSecrets, ref) |
| 57 | } |
| 58 | |
| 59 | return addedSecrets, nil |
| 60 | } |
| 61 | |
| 62 | // ParseConfigs retrieves the configs from the requested names and converts |
| 63 | // them to config references to use with the spec |
no test coverage detected
searching dependent graphs…