-
Notifications
You must be signed in to change notification settings - Fork 88
/
registry.go
227 lines (195 loc) · 6.79 KB
/
registry.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package registry
import (
"bufio"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
"github.com/replicatedhq/kots/pkg/k8sutil"
"github.com/replicatedhq/kots/pkg/kotsadm"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/registry/types"
"github.com/replicatedhq/kots/pkg/reporting"
"github.com/replicatedhq/kots/pkg/rewrite"
"github.com/replicatedhq/kots/pkg/store"
corev1 "k8s.io/api/core/v1"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// RewriteImages will use the app (a) and send the images to the registry specified. It will create patches for these
// and create a new version of the application
// the caller is responsible for deleting the appDir returned
func RewriteImages(appID string, sequence int64, hostname string, username string, password string, namespace string, isReadOnly bool, configValues *kotsv1beta1.ConfigValues) (appDir string, finalError error) {
if err := store.GetStore().SetTaskStatus("image-rewrite", "Updating registry settings", "running"); err != nil {
return "", errors.Wrap(err, "failed to set task status")
}
finishedCh := make(chan struct{})
defer close(finishedCh)
go func() {
for {
select {
case <-time.After(time.Second):
if err := store.GetStore().UpdateTaskStatusTimestamp("image-rewrite"); err != nil {
logger.Error(err)
}
case <-finishedCh:
return
}
}
}()
defer func() {
if finalError == nil {
if err := store.GetStore().ClearTaskStatus("image-rewrite"); err != nil {
logger.Error(err)
}
} else {
// do not show the stack trace to the user
causeErr := errors.Cause(finalError)
if err := store.GetStore().SetTaskStatus("image-rewrite", causeErr.Error(), "failed"); err != nil {
logger.Error(err)
}
}
}()
// get the archive and store it in a temporary location
appDir, err := ioutil.TempDir("", "kotsadm")
if err != nil {
return "", errors.Wrap(err, "failed to create temp dir")
}
// appDir is returned
err = store.GetStore().GetAppVersionArchive(appID, sequence, appDir)
if err != nil {
return "", errors.Wrap(err, "failed to get app version archive")
}
installation, err := kotsutil.LoadInstallationFromPath(filepath.Join(appDir, "upstream", "userdata", "installation.yaml"))
if err != nil {
return "", errors.Wrap(err, "failed to load installation from path")
}
license, err := kotsutil.LoadLicenseFromPath(filepath.Join(appDir, "upstream", "userdata", "license.yaml"))
if err != nil {
return "", errors.Wrap(err, "failed to load license from path")
}
if configValues == nil {
previousConfigValues, err := kotsutil.LoadConfigValuesFromFile(filepath.Join(appDir, "upstream", "userdata", "config.yaml"))
if err != nil && !os.IsNotExist(errors.Cause(err)) {
return "", errors.Wrap(err, "failed to load config values from path")
}
configValues = previousConfigValues
}
// get the downstream names only
downstreams, err := store.GetStore().ListDownstreamsForApp(appID)
if err != nil {
return "", errors.Wrap(err, "failed to list downstreams")
}
downstreamNames := []string{}
for _, d := range downstreams {
downstreamNames = append(downstreamNames, d.Name)
}
a, err := store.GetStore().GetApp(appID)
if err != nil {
return "", errors.Wrap(err, "failed to get app")
}
// dev_namespace makes the dev env work
appNamespace := os.Getenv("POD_NAMESPACE")
if os.Getenv("KOTSADM_TARGET_NAMESPACE") != "" {
appNamespace = os.Getenv("KOTSADM_TARGET_NAMESPACE")
}
pipeReader, pipeWriter := io.Pipe()
go func() {
scanner := bufio.NewScanner(pipeReader)
for scanner.Scan() {
if err := store.GetStore().SetTaskStatus("image-rewrite", scanner.Text(), "running"); err != nil {
logger.Error(err)
}
}
pipeReader.CloseWithError(scanner.Err())
}()
options := rewrite.RewriteOptions{
RootDir: appDir,
UpstreamURI: fmt.Sprintf("replicated://%s", license.Spec.AppSlug),
UpstreamPath: filepath.Join(appDir, "upstream"),
Installation: installation,
Downstreams: downstreamNames,
CreateAppDir: false,
ExcludeKotsKinds: true,
License: license,
ConfigValues: configValues,
K8sNamespace: appNamespace,
ReportWriter: pipeWriter,
IsAirgap: a.IsAirgap,
RegistryEndpoint: hostname,
RegistryUsername: username,
RegistryPassword: password,
RegistryNamespace: namespace,
RegistryIsReadOnly: isReadOnly,
AppSlug: a.Slug,
IsGitOps: a.IsGitOps,
AppSequence: a.CurrentSequence + 1, // sequence +1 because this is the current latest sequence, not the sequence that the rendered version will be saved as
ReportingInfo: reporting.GetReportingInfo(a.ID),
// TODO: pass in as arguments if this is ever called from CLI
HTTPProxyEnvValue: os.Getenv("HTTP_PROXY"),
HTTPSProxyEnvValue: os.Getenv("HTTPS_PROXY"),
NoProxyEnvValue: os.Getenv("NO_PROXY"),
}
options.CopyImages = true
if isReadOnly {
options.CopyImages = false
}
if err := rewrite.Rewrite(options); err != nil {
return "", errors.Wrap(err, "failed to rewrite images")
}
return appDir, nil
}
func HasKurlRegistry() (bool, error) {
clientset, err := k8sutil.GetClientset()
if err != nil {
return false, errors.Wrap(err, "failed to get k8s clientset")
}
registryCredsSecret, err := clientset.CoreV1().Secrets(metav1.NamespaceDefault).Get(context.TODO(), "registry-creds", metav1.GetOptions{})
if kuberneteserrors.IsNotFound(err) {
return false, nil
}
if err != nil {
// this is not an error, it could be rbac
// don't even log it, normal operations
return false, nil
}
if registryCredsSecret != nil {
if registryCredsSecret.Type == corev1.SecretTypeDockerConfigJson {
return true, nil
}
}
return false, nil
}
func GetKotsadmRegistry() (*types.RegistrySettings, error) {
clientset, err := k8sutil.GetClientset()
if err != nil {
return nil, errors.Wrap(err, "failed to get k8s clientset")
}
namespace := os.Getenv("POD_NAMESPACE")
kotsadmOptions, err := kotsadm.GetKotsadmOptionsFromCluster(namespace, clientset)
if err != nil {
return nil, errors.Wrap(err, "failed to get kotsadm options from cluster")
}
registry := kotsadmOptions.OverrideRegistry
registryNamespace := kotsadmOptions.OverrideNamespace
hostParts := strings.Split(kotsadmOptions.OverrideRegistry, "/")
if len(hostParts) == 2 {
registry = hostParts[0]
registryNamespace = hostParts[1]
}
registrySettings := types.RegistrySettings{
Hostname: registry,
Namespace: registryNamespace,
Username: kotsadmOptions.Username,
Password: kotsadmOptions.Password,
IsReadOnly: kotsadmOptions.IsReadOnly,
}
return ®istrySettings, nil
}