forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
137 lines (120 loc) · 3.54 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
package server
import (
"encoding/json"
"fmt"
"os"
"strings"
"time"
"github.com/docker/distribution"
"github.com/docker/distribution/context"
"github.com/docker/distribution/manifest/schema2"
imageapi "github.com/openshift/origin/pkg/image/api"
)
// Context keys
const (
// repositoryKey serves to store/retrieve repository object to/from context.
repositoryKey = "openshift.repository"
)
func WithRepository(parent context.Context, repo *repository) context.Context {
return context.WithValue(parent, repositoryKey, repo)
}
func RepositoryFrom(ctx context.Context) (repo *repository, found bool) {
repo, found = ctx.Value(repositoryKey).(*repository)
return
}
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 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
}
// deserializedManifestFromImage converts an Image to a DeserializedManifest.
func deserializedManifestFromImage(image *imageapi.Image) (*schema2.DeserializedManifest, error) {
var manifest schema2.DeserializedManifest
if err := json.Unmarshal([]byte(image.DockerImageManifest), &manifest); err != nil {
return nil, err
}
return &manifest, nil
}
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
}