-
Notifications
You must be signed in to change notification settings - Fork 88
/
registry.go
226 lines (195 loc) · 6.56 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
package registry
import (
"bufio"
"context"
_ "embed"
"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"
"github.com/replicatedhq/kots/pkg/util"
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(errors.Wrap(err, "failed to clear image rewrite task status"))
}
} 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(errors.Wrap(err, "failed to set image rewrite task status as failed"))
}
}
}()
// 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")
}
kotsKinds, err := kotsutil.LoadKotsKindsFromPath(appDir)
if err != nil {
return "", errors.Wrap(err, "failed to load kotskinds from path")
}
if configValues == nil {
configValues = kotsKinds.ConfigValues
}
// 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 := util.PodNamespace
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())
}()
nextAppSequence, err := store.GetStore().GetNextAppSequence(a.ID)
if err != nil {
return "", errors.Wrap(err, "failed to get next app sequence")
}
options := rewrite.RewriteOptions{
RootDir: appDir,
UpstreamURI: fmt.Sprintf("replicated://%s", kotsKinds.License.Spec.AppSlug),
UpstreamPath: filepath.Join(appDir, "upstream"),
Installation: &kotsKinds.Installation,
Downstreams: downstreamNames,
CreateAppDir: false,
ExcludeKotsKinds: true,
License: kotsKinds.License,
ConfigValues: configValues,
KotsApplication: &kotsKinds.KotsApplication,
K8sNamespace: appNamespace,
ReportWriter: pipeWriter,
IsAirgap: a.IsAirgap,
RegistryEndpoint: hostname,
RegistryUsername: username,
RegistryPassword: password,
RegistryNamespace: namespace,
RegistryIsReadOnly: isReadOnly,
AppID: a.ID,
AppSlug: a.Slug,
IsGitOps: a.IsGitOps,
AppSequence: nextAppSequence,
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 := util.PodNamespace
registryConfig, err := kotsadm.GetRegistryConfigFromCluster(namespace, clientset)
if err != nil {
return nil, errors.Wrap(err, "failed to get kotsadm options from cluster")
}
registry := registryConfig.OverrideRegistry
registryNamespace := registryConfig.OverrideNamespace
hostParts := strings.Split(registryConfig.OverrideRegistry, "/")
if len(hostParts) == 2 {
registry = hostParts[0]
registryNamespace = hostParts[1]
}
registrySettings := types.RegistrySettings{
Hostname: registry,
Namespace: registryNamespace,
Username: registryConfig.Username,
Password: registryConfig.Password,
IsReadOnly: registryConfig.IsReadOnly,
}
return ®istrySettings, nil
}