CreatePullSecret creates an image pull secret for a registry
(ctx devspacecontext.Context, options *PullSecretOptions)
| 48 | |
| 49 | // CreatePullSecret creates an image pull secret for a registry |
| 50 | func (r *client) CreatePullSecret(ctx devspacecontext.Context, options *PullSecretOptions) error { |
| 51 | pullSecretName := options.Secret |
| 52 | if pullSecretName == "" { |
| 53 | pullSecretName = GetRegistryAuthSecretName(options.RegistryURL) |
| 54 | } |
| 55 | |
| 56 | registryURL := options.RegistryURL |
| 57 | if registryURL == "hub.docker.com" || registryURL == "" { |
| 58 | registryURL = "https://index.docker.io/v1/" |
| 59 | } |
| 60 | |
| 61 | authToken := options.PasswordOrToken |
| 62 | if options.Username != "" { |
| 63 | authToken = options.Username + ":" + authToken |
| 64 | } |
| 65 | |
| 66 | email := options.Email |
| 67 | if email == "" { |
| 68 | email = "noreply@devspace.sh" |
| 69 | } |
| 70 | |
| 71 | err := wait.PollUntilContextTimeout(ctx.Context(), time.Second, time.Second*30, true, func(ctxPollUntil context.Context) (bool, error) { |
| 72 | secret, err := ctx.KubeClient().KubeClient().CoreV1().Secrets(options.Namespace).Get(ctxPollUntil, pullSecretName, metav1.GetOptions{}) |
| 73 | if err != nil { |
| 74 | if kerrors.IsNotFound(err) { |
| 75 | // Create the pull secret |
| 76 | secret, err := newPullSecret(pullSecretName, registryURL, authToken, email) |
| 77 | if err != nil { |
| 78 | return false, err |
| 79 | } |
| 80 | |
| 81 | _, err = ctx.KubeClient().KubeClient().CoreV1().Secrets(options.Namespace).Create(ctxPollUntil, secret, metav1.CreateOptions{}) |
| 82 | if err != nil { |
| 83 | if kerrors.IsAlreadyExists(err) { |
| 84 | // Retry |
| 85 | return false, nil |
| 86 | } |
| 87 | |
| 88 | return false, errors.Wrap(err, "create pull secret") |
| 89 | } |
| 90 | |
| 91 | ctx.Log().Donef("Created image pull secret %s/%s", options.Namespace, pullSecretName) |
| 92 | return true, nil |
| 93 | } else { |
| 94 | // Retry |
| 95 | return false, nil |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | dockerConfigJSON, err := fromPullSecretData(secret.Data) |
| 100 | if err != nil { |
| 101 | return false, err |
| 102 | } |
| 103 | |
| 104 | existingEntry := dockerConfigJSON.Auths[registryURL] |
| 105 | updatedEntry := newDockerConfigEntry(authToken, email) |
| 106 | if hasChanges(existingEntry, updatedEntry) { |
| 107 | // Update secret entry |
no test coverage detected