Skip to content

Commit

Permalink
simplify secret lookup on service create
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Hazlett <[email protected]>
  • Loading branch information
ehazlett committed Nov 9, 2016
1 parent ab5f829 commit 6bbc35a
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions command/service/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ func parseSecretString(secretString string) (string, string, error) {
// parseSecrets retrieves the secrets from the requested names and converts
// them to secret references to use with the spec
func parseSecrets(client client.APIClient, requestedSecrets []string) ([]*swarmtypes.SecretReference, error) {
lookupSecretNames := []string{}
neededSecrets := make(map[string]*swarmtypes.SecretReference)
secretRefs := make(map[string]*swarmtypes.SecretReference)
ctx := context.Background()

neededLookup := map[string]string{}
for _, secret := range requestedSecrets {
n, t, err := parseSecretString(secret)
if err != nil {
Expand All @@ -60,14 +58,15 @@ func parseSecrets(client client.APIClient, requestedSecrets []string) ([]*swarmt
Target: t,
}

lookupSecretNames = append(lookupSecretNames, n)
neededLookup[t] = n
neededSecrets[t] = secretRef
if _, exists := secretRefs[t]; exists {
return nil, fmt.Errorf("duplicate secret target for %s not allowed", n)
}
secretRefs[t] = secretRef
}

args := filters.NewArgs()
for _, s := range lookupSecretNames {
args.Add("names", s)
for _, s := range secretRefs {
args.Add("names", s.SecretName)
}

secrets, err := client.SecretList(ctx, types.SecretListOptions{
Expand All @@ -84,21 +83,16 @@ func parseSecrets(client client.APIClient, requestedSecrets []string) ([]*swarmt

addedSecrets := []*swarmtypes.SecretReference{}

for target, secretName := range neededLookup {
id, ok := foundSecrets[secretName]
if !ok {
return nil, fmt.Errorf("secret not found: %s", secretName)
}

secretRef, ok := neededSecrets[target]
for _, ref := range secretRefs {
id, ok := foundSecrets[ref.SecretName]
if !ok {
return nil, fmt.Errorf("secret reference not found: %s", secretName)
return nil, fmt.Errorf("secret not found: %s", ref.SecretName)
}

// set the id for the ref to properly assign in swarm
// since swarm needs the ID instead of the name
secretRef.SecretID = id
addedSecrets = append(addedSecrets, secretRef)
ref.SecretID = id
addedSecrets = append(addedSecrets, ref)
}

return addedSecrets, nil
Expand Down

0 comments on commit 6bbc35a

Please sign in to comment.