/
util.go
213 lines (189 loc) · 6.79 KB
/
util.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
package server
import (
"fmt"
"os"
"strings"
"time"
"github.com/docker/distribution"
"github.com/docker/distribution/context"
"github.com/docker/distribution/digest"
"github.com/docker/distribution/registry/api/errcode"
disterrors "github.com/docker/distribution/registry/api/v2"
quotautil "github.com/openshift/origin/pkg/quota/util"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kapi "k8s.io/kubernetes/pkg/api"
osclient "github.com/openshift/origin/pkg/client"
imageapi "github.com/openshift/origin/pkg/image/api"
"github.com/openshift/origin/pkg/image/importer"
)
func getOptionValue(
envVar string,
optionName string,
defval interface{},
options map[string]interface{},
conversionFunc func(v interface{}) (interface{}, error),
) (value interface{}, err error) {
value = defval
if optValue, ok := options[optionName]; ok {
converted, convErr := conversionFunc(optValue)
if convErr != nil {
err = fmt.Errorf("config option %q: invalid value: %v", optionName, convErr)
} else {
value = converted
}
}
if len(envVar) == 0 {
return
}
envValue := os.Getenv(envVar)
if len(envValue) == 0 {
return
}
converted, convErr := conversionFunc(envValue)
if convErr != nil {
err = fmt.Errorf("invalid value of environment variable %s: %v", envVar, convErr)
} else {
value = converted
}
return
}
func getBoolOption(envVar string, optionName string, defval bool, options map[string]interface{}) (bool, error) {
value, err := getOptionValue(envVar, optionName, defval, options, func(value interface{}) (b interface{}, err error) {
switch t := value.(type) {
case bool:
return t, nil
case string:
switch strings.ToLower(t) {
case "true":
return true, nil
case "false":
return false, nil
}
}
return defval, fmt.Errorf("%#+v is not a boolean", value)
})
return value.(bool), err
}
func getStringOption(envVar string, optionName string, defval string, options map[string]interface{}) (string, error) {
value, err := getOptionValue(envVar, optionName, defval, options, func(value interface{}) (b interface{}, err error) {
s, ok := value.(string)
if !ok {
return defval, fmt.Errorf("expected string, not %T", value)
}
return s, err
})
return value.(string), err
}
func getDurationOption(envVar string, optionName string, defval time.Duration, options map[string]interface{}) (time.Duration, error) {
value, err := getOptionValue(envVar, optionName, defval, options, func(value interface{}) (d interface{}, err error) {
s, ok := value.(string)
if !ok {
return defval, fmt.Errorf("expected string, not %T", value)
}
parsed, err := time.ParseDuration(s)
if err != nil {
return defval, fmt.Errorf("parse duration error: %v", err)
}
return parsed, nil
})
return value.(time.Duration), err
}
func getNamespaceName(resourceName string) (string, string, error) {
repoParts := strings.SplitN(resourceName, "/", 2)
if len(repoParts) != 2 {
return "", "", ErrNamespaceRequired
}
ns := repoParts[0]
if len(ns) == 0 {
return "", "", ErrNamespaceRequired
}
name := repoParts[1]
if len(name) == 0 {
return "", "", ErrNamespaceRequired
}
return ns, name, nil
}
// effectiveCreateOptions find out what the blob creation options are going to do by dry-running them.
func effectiveCreateOptions(options []distribution.BlobCreateOption) (*distribution.CreateOptions, error) {
opts := &distribution.CreateOptions{}
for _, createOptions := range options {
err := createOptions.Apply(opts)
if err != nil {
return nil, err
}
}
return opts, nil
}
func isImageManaged(image *imageapi.Image) bool {
managed, ok := image.ObjectMeta.Annotations[imageapi.ManagedByOpenShiftAnnotation]
return ok && managed == "true"
}
// wrapKStatusErrorOnGetImage transforms the given kubernetes status error into a distribution one. Upstream
// handler do not allow us to propagate custom error messages except for ErrManifetUnknownRevision. All the
// other errors will result in an internal server error with details made out of returned error.
func wrapKStatusErrorOnGetImage(repoName string, dgst digest.Digest, err error) error {
switch {
case kerrors.IsNotFound(err):
// This is the only error type we can propagate unchanged to the client.
return distribution.ErrManifestUnknownRevision{
Name: repoName,
Revision: dgst,
}
case err != nil:
// We don't turn this error to distribution error on purpose: Upstream manifest handler wraps any
// error but distribution.ErrManifestUnknownRevision with errcode.ErrorCodeUnknown. If we wrap the
// original error with distribution.ErrorCodeUnknown, the "unknown error" will appear twice in the
// resulting error message.
return err
}
return nil
}
// getImportContext loads secrets for given repository and returns a context for getting distribution clients
// to remote repositories.
func getImportContext(
ctx context.Context,
osClient osclient.ImageStreamSecretsNamespacer,
namespace, name string,
) importer.RepositoryRetriever {
secrets, err := osClient.ImageStreamSecrets(namespace).Secrets(name, metav1.ListOptions{})
if err != nil {
context.GetLogger(ctx).Errorf("error getting secrets for repository %s/%s: %v", namespace, name, err)
secrets = &kapi.SecretList{}
}
credentials := importer.NewCredentialsForSecrets(secrets.Items)
return importer.NewContext(secureTransport, insecureTransport).WithCredentials(credentials)
}
// cachedImageStreamGetter wraps a master API client for getting image streams with a cache.
type cachedImageStreamGetter struct {
ctx context.Context
namespace string
name string
isNamespacer osclient.ImageStreamsNamespacer
cachedImageStream *imageapi.ImageStream
}
func (g *cachedImageStreamGetter) get() (*imageapi.ImageStream, error) {
if g.cachedImageStream != nil {
context.GetLogger(g.ctx).Debugf("(*cachedImageStreamGetter).getImageStream: returning cached copy")
return g.cachedImageStream, nil
}
is, err := g.isNamespacer.ImageStreams(g.namespace).Get(g.name, metav1.GetOptions{})
if err != nil {
context.GetLogger(g.ctx).Errorf("failed to get image stream: %v", err)
switch {
case kerrors.IsNotFound(err):
return nil, disterrors.ErrorCodeNameUnknown.WithDetail(err)
case kerrors.IsForbidden(err), kerrors.IsUnauthorized(err), quotautil.IsErrorQuotaExceeded(err):
return nil, errcode.ErrorCodeDenied.WithDetail(err)
default:
return nil, errcode.ErrorCodeUnknown.WithDetail(err)
}
}
context.GetLogger(g.ctx).Debugf("(*cachedImageStreamGetter).getImageStream: got image stream %s/%s", is.Namespace, is.Name)
g.cachedImageStream = is
return is, nil
}
func (g *cachedImageStreamGetter) cacheImageStream(is *imageapi.ImageStream) {
context.GetLogger(g.ctx).Debugf("(*cachedImageStreamGetter).cacheImageStream: got image stream %s/%s", is.Namespace, is.Name)
g.cachedImageStream = is
}