setConfigs does double duty: it both sets the ConfigReferences of the service, and it sets the service CredentialSpec. This is because there is an interplay between the CredentialSpec and the Config it depends on.
(ctx context.Context, apiClient client.ConfigAPIClient, service *swarm.ServiceSpec, opts *serviceOptions)
| 153 | // service, and it sets the service CredentialSpec. This is because there is an |
| 154 | // interplay between the CredentialSpec and the Config it depends on. |
| 155 | func setConfigs(ctx context.Context, apiClient client.ConfigAPIClient, service *swarm.ServiceSpec, opts *serviceOptions) error { |
| 156 | specifiedConfigs := opts.configs.Value() |
| 157 | // if the user has requested to use a Config, for the CredentialSpec add it |
| 158 | // to the specifiedConfigs as a RuntimeTarget. |
| 159 | if cs := opts.credentialSpec.Value(); cs != nil && cs.Config != "" { |
| 160 | specifiedConfigs = append(specifiedConfigs, &swarm.ConfigReference{ |
| 161 | ConfigName: cs.Config, |
| 162 | Runtime: &swarm.ConfigReferenceRuntimeTarget{}, |
| 163 | }) |
| 164 | } |
| 165 | if len(specifiedConfigs) > 0 { |
| 166 | // parse and validate configs |
| 167 | configs, err := ParseConfigs(ctx, apiClient, specifiedConfigs) |
| 168 | if err != nil { |
| 169 | return err |
| 170 | } |
| 171 | service.TaskTemplate.ContainerSpec.Configs = configs |
| 172 | // if we have a CredentialSpec Config, find its ID and rewrite the |
| 173 | // field on the spec |
| 174 | // |
| 175 | // we check the opts instead of the service directly because there are |
| 176 | // a few layers of nullable objects in the service, which is a PITA |
| 177 | // to traverse, but the existence of the option implies that those are |
| 178 | // non-null. |
| 179 | if cs := opts.credentialSpec.Value(); cs != nil && cs.Config != "" { |
| 180 | for _, config := range configs { |
| 181 | if config.ConfigName == cs.Config { |
| 182 | service.TaskTemplate.ContainerSpec.Privileges.CredentialSpec.Config = config.ConfigID |
| 183 | // we've found the right config, no need to keep iterating |
| 184 | // through the rest of them. |
| 185 | break |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return nil |
| 192 | } |
searching dependent graphs…