-
Notifications
You must be signed in to change notification settings - Fork 137
/
image.go
107 lines (83 loc) · 2.96 KB
/
image.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
package troubleshoot
import (
"context"
"net/http"
dynatracev1beta1 "github.com/Dynatrace/dynatrace-operator/pkg/api/v1beta1/dynakube"
"github.com/Dynatrace/dynatrace-operator/pkg/arch"
"github.com/Dynatrace/dynatrace-operator/pkg/logd"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
)
const (
pullSecretSuffix = "-pull-secret"
)
type Credentials struct {
Username string `json:"username"`
Password string `json:"password"`
Auth string `json:"auth"`
}
type Endpoints map[string]Credentials
type Auths struct {
Auths Endpoints `json:"auths"`
}
type ImagePullFunc func(image string) error
func verifyAllImagesAvailable(ctx context.Context, baseLog logd.Logger, keychain authn.Keychain, transport *http.Transport, dynakube *dynatracev1beta1.DynaKube) error {
log := baseLog.WithName("imagepull")
imagePullFunc := CreateImagePullFunc(ctx, keychain, transport)
if dynakube.NeedsOneAgent() {
verifyImageIsAvailable(log, imagePullFunc, dynakube, componentOneAgent, false)
verifyImageIsAvailable(log, imagePullFunc, dynakube, componentCodeModules, true)
}
if dynakube.NeedsActiveGate() {
verifyImageIsAvailable(log, imagePullFunc, dynakube, componentActiveGate, false)
}
return nil
}
func verifyImageIsAvailable(log logd.Logger, pullImage ImagePullFunc, dynakube *dynatracev1beta1.DynaKube, comp component, proxyWarning bool) {
image, isCustomImage := comp.getImage(dynakube)
if comp.SkipImageCheck(image) {
logErrorf(log, "Unknown %s image", comp.String())
return
}
componentName := comp.Name(isCustomImage)
logNewCheckf(log, "Verifying that %s image %s can be pulled ...", componentName, image)
if image == "" {
logInfof(log, "No %s image configured", componentName)
return
}
if dynakube.HasProxy() && proxyWarning {
logWarningf(log, "Proxy setting in Dynakube is ignored for %s image due to technical limitations.", componentName)
}
if getEnvProxySettings() != nil {
logWarningf(log, "Proxy settings in environment might interfere when pulling %s image in troubleshoot mode.", componentName)
}
err := pullImage(image)
if err != nil {
logErrorf(log, "Pulling %s image %s failed: %v", componentName, image, err)
} else {
logOkf(log, "%s image %s can be successfully pulled", componentName, image)
}
}
func CreateImagePullFunc(ctx context.Context, keychain authn.Keychain, transport *http.Transport) ImagePullFunc {
return func(image string) error {
return tryImagePull(ctx, keychain, transport, image)
}
}
func tryImagePull(ctx context.Context, keychain authn.Keychain, transport *http.Transport, image string) error {
imageReference, err := name.ParseReference(image)
if err != nil {
return err
}
_, err = remote.Get(
imageReference,
remote.WithContext(ctx),
remote.WithAuthFromKeychain(keychain),
remote.WithTransport(transport),
remote.WithPlatform(arch.ImagePlatform),
)
if err != nil {
return err
}
return nil
}