-
Notifications
You must be signed in to change notification settings - Fork 88
/
troubleshoot.go
140 lines (117 loc) · 5.59 KB
/
troubleshoot.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
package registry
import (
"encoding/base64"
"fmt"
"path"
"strings"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
kotsregistry "github.com/replicatedhq/kots/pkg/docker/registry"
"github.com/replicatedhq/kots/pkg/registry/types"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
)
// UpdateCollectorSpecsWithRegistryData takes an array of collectors and some environment data (local registry info and license, etc)
// any image that needs to be rewritten to be compatible with the local registry settings or proxy pull
// will be updated and replaced in the spec. any required image pull secret will be automatically
// inserted into the spec
// an error is returned if anything failed, but the collectors param can always be used after calling (assuming no error)
func UpdateCollectorSpecsWithRegistryData(collectors []*troubleshootv1beta2.Collect, localRegistryInfo types.RegistrySettings, knownImages []kotsv1beta1.InstallationImage, license *kotsv1beta1.License) ([]*troubleshootv1beta2.Collect, error) {
// if there's a local registry, always attach that image pull secret for all, and
// always rewrite
updatedCollectors := make([]*troubleshootv1beta2.Collect, len(collectors))
if localRegistryInfo.IsValid() {
for idx, collect := range collectors {
if collect.Run != nil {
run := collect.Run
run.Image = rewriteImage(localRegistryInfo.Hostname, localRegistryInfo.Namespace, run.Image)
pullSecrets, err := kotsregistry.PullSecretForRegistries([]string{localRegistryInfo.Hostname}, localRegistryInfo.Username, localRegistryInfo.Password, run.Namespace, "")
if err != nil {
return nil, errors.Wrap(err, "failed to generate pull secret for registry")
}
run.ImagePullSecret = &troubleshootv1beta2.ImagePullSecrets{
SecretType: "kubernetes.io/dockerconfigjson",
Data: map[string]string{
".dockerconfigjson": base64.StdEncoding.EncodeToString(pullSecrets.AdminConsoleSecret.Data[".dockerconfigjson"]),
},
}
collect.Run = run
updatedCollectors[idx] = collect
} else if collect.RegistryImages != nil {
pullSecrets, err := kotsregistry.PullSecretForRegistries([]string{localRegistryInfo.Hostname}, localRegistryInfo.Username, localRegistryInfo.Password, collect.RegistryImages.Namespace, "")
if err != nil {
return nil, errors.Wrap(err, "failed to generate pull secret for registry")
}
collect.RegistryImages.ImagePullSecrets = &troubleshootv1beta2.ImagePullSecrets{
SecretType: "kubernetes.io/dockerconfigjson",
Data: map[string]string{
".dockerconfigjson": base64.StdEncoding.EncodeToString(pullSecrets.AdminConsoleSecret.Data[".dockerconfigjson"]),
},
}
images := []string{}
for _, knownImage := range knownImages {
image := rewriteImage(localRegistryInfo.Hostname, localRegistryInfo.Namespace, knownImage.Image)
images = append(images, image)
}
collect.RegistryImages.Images = images
updatedCollectors[idx] = collect
} else {
updatedCollectors[idx] = collect
}
}
return updatedCollectors, nil
}
registryProxyInfo := kotsregistry.ProxyEndpointFromLicense(license)
// for all known private images, rewrite to the replicated proxy and add license image pull secret
for idx, collect := range collectors {
// all collectors that include images in the spec should have an if / else statement here
if collect.Run != nil {
for _, knownImage := range knownImages {
if knownImage.Image == collect.Run.Image && knownImage.IsPrivate {
run := collect.Run
// if it's the replicated registry, no change, just add image pull secret
registryHost := strings.Split(run.Image, "/")[0]
if registryHost != registryProxyInfo.Registry {
tag := strings.Split(run.Image, ":")
run.Image = kotsregistry.MakeProxiedImageURL(registryProxyInfo.Proxy, license.Spec.AppSlug, run.Image)
if len(tag) > 1 {
run.Image = fmt.Sprintf("%s:%s", run.Image, tag[len(tag)-1])
}
pullSecrets, err := kotsregistry.PullSecretForRegistries([]string{registryProxyInfo.Proxy}, license.Spec.LicenseID, license.Spec.LicenseID, run.Namespace, "")
if err != nil {
return nil, errors.Wrap(err, "failed to generate pull secret for proxy registry")
}
run.ImagePullSecret = &troubleshootv1beta2.ImagePullSecrets{
SecretType: "kubernetes.io/dockerconfigjson",
Data: map[string]string{
".dockerconfigjson": base64.StdEncoding.EncodeToString(pullSecrets.AdminConsoleSecret.Data[".dockerconfigjson"]),
},
}
collect.Run = run
} else {
pullSecrets, err := kotsregistry.PullSecretForRegistries([]string{registryProxyInfo.Registry}, license.Spec.LicenseID, license.Spec.LicenseID, run.Namespace, "")
if err != nil {
return nil, errors.Wrap(err, "failed to generate pull secret for replicated registry")
}
run.ImagePullSecret = &troubleshootv1beta2.ImagePullSecrets{
SecretType: "kubernetes.io/dockerconfigjson",
Data: map[string]string{
".dockerconfigjson": base64.StdEncoding.EncodeToString(pullSecrets.AdminConsoleSecret.Data[".dockerconfigjson"]),
},
}
collect.Run = run
}
collectors[idx].Run = run
}
}
updatedCollectors[idx] = collect
} else {
updatedCollectors[idx] = collect
}
}
return updatedCollectors, nil
}
func rewriteImage(newHost string, newNamespace string, image string) string {
imageParts := strings.Split(image, "/")
imageNameWithOptionalTag := imageParts[len(imageParts)-1]
return path.Join(newHost, newNamespace, imageNameWithOptionalTag)
}