forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
169 lines (144 loc) · 5.57 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
package images
import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"
g "github.com/onsi/ginkgo"
//o "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
kapiv1 "k8s.io/kubernetes/pkg/api/v1"
kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/clientset/typed/core/v1"
dockerregistryserver "github.com/openshift/origin/pkg/dockerregistry/server"
exutil "github.com/openshift/origin/test/extended/util"
)
const (
readOnlyEnvVar = "REGISTRY_STORAGE_MAINTENANCE_READONLY"
defaultAcceptSchema2 = true
)
// GetDockerRegistryURL returns a cluster URL of internal docker registry if available.
func GetDockerRegistryURL(oc *exutil.CLI) (string, error) {
svc, err := oc.AdminKubeClient().Core().Services("default").Get("docker-registry", metav1.GetOptions{})
if err != nil {
return "", err
}
url := svc.Spec.ClusterIP
for _, p := range svc.Spec.Ports {
url = fmt.Sprintf("%s:%d", url, p.Port)
break
}
return url, nil
}
// GetRegistryStorageSize returns a number of bytes occupied by registry's data on its filesystem.
func GetRegistryStorageSize(oc *exutil.CLI) (int64, error) {
defer func(ns string) { oc.SetNamespace(ns) }(oc.Namespace())
out, err := oc.SetNamespace(metav1.NamespaceDefault).AsAdmin().Run("rsh").Args(
"dc/docker-registry", "du", "--bytes", "--summarize", "/registry/docker/registry").Output()
if err != nil {
return 0, err
}
m := regexp.MustCompile(`^\d+`).FindString(out)
if len(m) == 0 {
return 0, fmt.Errorf("failed to parse du output: %s", out)
}
size, err := strconv.ParseInt(m, 10, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse du output: %s", m)
}
return size, nil
}
// DoesRegistryAcceptSchema2 returns true if the integrated registry is configured to accept manifest V2
// schema 2.
func DoesRegistryAcceptSchema2(oc *exutil.CLI) (bool, error) {
defer func(ns string) { oc.SetNamespace(ns) }(oc.Namespace())
env, err := oc.SetNamespace(metav1.NamespaceDefault).AsAdmin().Run("env").Args("dc/docker-registry", "--list").Output()
if err != nil {
return defaultAcceptSchema2, err
}
if strings.Contains(env, fmt.Sprintf("%s=", dockerregistryserver.AcceptSchema2EnvVar)) {
return strings.Contains(env, fmt.Sprintf("%s=true", dockerregistryserver.AcceptSchema2EnvVar)), nil
}
return defaultAcceptSchema2, nil
}
// RegistriConfiguration holds desired configuration options for the integrated registry. *nil* stands for
// "no change".
type RegistryConfiguration struct {
ReadOnly *bool
AcceptSchema2 *bool
}
type byAgeDesc []kapiv1.Pod
func (ba byAgeDesc) Len() int { return len(ba) }
func (ba byAgeDesc) Swap(i, j int) { ba[i], ba[j] = ba[j], ba[i] }
func (ba byAgeDesc) Less(i, j int) bool {
return ba[j].CreationTimestamp.Before(ba[i].CreationTimestamp)
}
// GetRegistryPod returns the youngest registry pod deployed.
func GetRegistryPod(podsGetter kcoreclient.PodsGetter) (*kapiv1.Pod, error) {
podList, err := podsGetter.Pods(metav1.NamespaceDefault).List(metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(labels.Set{"deploymentconfig": "docker-registry"}).String(),
})
if err != nil {
return nil, err
}
if len(podList.Items) == 0 {
return nil, fmt.Errorf("failed to find any docker-registry pod")
}
sort.Sort(byAgeDesc(podList.Items))
return &podList.Items[0], nil
}
// ConfigureRegistry re-deploys the registry pod if its configuration doesn't match the desiredState. The
// function blocks until the registry is ready.
func ConfigureRegistry(oc *exutil.CLI, desiredState RegistryConfiguration) error {
defer func(ns string) { oc.SetNamespace(ns) }(oc.Namespace())
oc = oc.SetNamespace(metav1.NamespaceDefault).AsAdmin()
env, err := oc.Run("env").Args("dc/docker-registry", "--list").Output()
if err != nil {
return err
}
envOverrides := []string{}
if desiredState.AcceptSchema2 != nil {
current := !strings.Contains(env, fmt.Sprintf("%s=%t", dockerregistryserver.AcceptSchema2EnvVar, false))
if current != *desiredState.AcceptSchema2 {
new := fmt.Sprintf("%s=%t", dockerregistryserver.AcceptSchema2EnvVar, *desiredState.AcceptSchema2)
envOverrides = append(envOverrides, new)
}
}
if desiredState.ReadOnly != nil {
value := fmt.Sprintf("%s=%s", readOnlyEnvVar, makeReadonlyEnvValue(*desiredState.ReadOnly))
if !strings.Contains(env, value) {
envOverrides = append(envOverrides, value)
}
}
if len(envOverrides) == 0 {
g.By("docker-registry is already in the desired state of configuration")
return nil
}
dc, err := oc.AppsClient().Apps().DeploymentConfigs(metav1.NamespaceDefault).Get("docker-registry", metav1.GetOptions{})
if err != nil {
return err
}
waitForVersion := dc.Status.LatestVersion + 1
err = oc.Run("env").Args(append([]string{"dc/docker-registry"}, envOverrides...)...).Execute()
if err != nil {
return fmt.Errorf("failed to update registry's environment with %s: %v", &waitForVersion, err)
}
return exutil.WaitForDeploymentConfig(
oc.AdminKubeClient(),
oc.AdminAppsClient().Apps(),
metav1.NamespaceDefault,
"docker-registry",
waitForVersion,
oc)
}
// EnsureRegistryAcceptsSchema2 checks whether the registry is configured to accept manifests V2 schema 2 or
// not. If the result doesn't match given accept argument, registry's deployment config will be updated
// accordingly and the function will block until the registry have been re-deployed and ready for new
// requests.
func EnsureRegistryAcceptsSchema2(oc *exutil.CLI, accept bool) error {
return ConfigureRegistry(oc, RegistryConfiguration{AcceptSchema2: &accept})
}
func makeReadonlyEnvValue(on bool) string {
return fmt.Sprintf(`{"enabled":%t}`, on)
}