-
Notifications
You must be signed in to change notification settings - Fork 12
/
pullsecrets.go
154 lines (128 loc) · 3.49 KB
/
pullsecrets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package kubernetes
import (
"context"
"encoding/json"
"fmt"
"github.com/loft-sh/devpod-provider-kubernetes/pkg/docker"
perrors "github.com/pkg/errors"
k8sv1 "k8s.io/api/core/v1"
)
func (k *KubernetesDriver) EnsurePullSecret(
ctx context.Context,
pullSecretName string,
dockerImage string,
) (bool, error) {
k.Log.Debugf("Ensure pull secrets")
host, err := GetRegistryFromImageName(dockerImage)
if err != nil {
return false, fmt.Errorf("get registry from image name: %w", err)
}
dockerCredentials, err := docker.GetAuthConfig(host)
if err != nil || dockerCredentials == nil || dockerCredentials.Username == "" || dockerCredentials.Secret == "" {
k.Log.Debugf("Couldn't retrieve credentials for registry: %s", host)
return false, nil
}
if k.secretExists(ctx, pullSecretName) {
if !k.shouldRecreateSecret(ctx, dockerCredentials, pullSecretName, host) {
k.Log.Debugf("Pull secret '%s' already exists and is up to date", pullSecretName)
return true, nil
}
k.Log.Debugf("Pull secret '%s' already exists, but is outdated. Recreating...", pullSecretName)
err := k.DeletePullSecret(ctx, pullSecretName)
if err != nil {
return false, err
}
}
err = k.createPullSecret(ctx, pullSecretName, dockerCredentials)
if err != nil {
return false, err
}
k.Log.Infof("Pull secret '%s' created", pullSecretName)
return true, nil
}
func (k *KubernetesDriver) ReadSecretContents(
ctx context.Context,
pullSecretName string,
host string,
) (string, error) {
args := []string{
"get",
"secret",
pullSecretName,
"-o", "json",
}
out, err := k.buildCmd(ctx, args).CombinedOutput()
if err != nil {
return "", perrors.Wrapf(err, "delete pull secret: %s", string(out))
}
var secret k8sv1.Secret
err = json.Unmarshal(out, &secret)
if err != nil {
return "", perrors.Wrap(err, "unmarshal secret")
}
return DecodeAuthTokenFromPullSecret(secret, host)
}
func (k *KubernetesDriver) DeletePullSecret(
ctx context.Context,
pullSecretName string) error {
if !k.secretExists(ctx, pullSecretName) {
return nil
}
args := []string{
"delete",
"secret",
pullSecretName,
}
out, err := k.buildCmd(ctx, args).CombinedOutput()
if err != nil {
return perrors.Wrapf(err, "delete pull secret: %s", string(out))
}
return nil
}
func (k *KubernetesDriver) shouldRecreateSecret(ctx context.Context, dockerCredentials *docker.Credentials, pullSecretName, host string) bool {
existingAuthToken, err := k.ReadSecretContents(ctx, pullSecretName, host)
if err != nil {
return true
}
return existingAuthToken != dockerCredentials.AuthToken()
}
func (k *KubernetesDriver) secretExists(
ctx context.Context,
pullSecretName string,
) bool {
args := []string{
"get",
"secret",
pullSecretName,
}
_, err := k.buildCmd(ctx, args).CombinedOutput()
if err != nil {
return false
}
return true
}
func (k *KubernetesDriver) createPullSecret(
ctx context.Context,
pullSecretName string,
dockerCredentials *docker.Credentials,
) error {
authToken := dockerCredentials.AuthToken()
email := "noreply@loft.sh"
encodedSecretData, err := PreparePullSecretData(dockerCredentials.ServerURL, authToken, email)
if err != nil {
return perrors.Wrap(err, "prepare pull secret data")
}
args := []string{
"create",
"secret",
"generic",
pullSecretName,
"--type", string(k8sv1.SecretTypeDockerConfigJson),
"--from-literal", encodedSecretData,
}
out, err := k.buildCmd(ctx, args).CombinedOutput()
if err != nil {
return perrors.Wrapf(err, "create pull secret: %s", string(out))
}
return nil
}