forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_registry_service.go
217 lines (180 loc) · 6.58 KB
/
docker_registry_service.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
214
215
216
217
package controllers
import (
"encoding/json"
"fmt"
"net"
"reflect"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/cache"
"github.com/GoogleCloudPlatform/kubernetes/pkg/controller/framework"
"github.com/GoogleCloudPlatform/kubernetes/pkg/credentialprovider"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
)
const DefaultOpenshiftDockerURL = "docker-registry.default.svc.cluster.local:5000"
// DockerRegistryServiceControllerOptions contains options for the DockerRegistryServiceController
type DockerRegistryServiceControllerOptions struct {
// Resync is the time.Duration at which to fully re-list services.
// If zero, re-list will be delayed as long as possible
Resync time.Duration
RegistryNamespace string
RegistryServiceName string
DefaultDockerURL string
DockercfgController *DockercfgController
}
// NewDockerRegistryServiceController returns a new *DockerRegistryServiceController.
func NewDockerRegistryServiceController(cl client.Interface, options DockerRegistryServiceControllerOptions) *DockerRegistryServiceController {
e := &DockerRegistryServiceController{
client: cl,
}
_, e.serviceController = framework.NewInformer(
&cache.ListWatch{
ListFunc: func() (runtime.Object, error) {
return e.client.Services(options.RegistryNamespace).List(labels.Everything())
},
WatchFunc: func(rv string) (watch.Interface, error) {
return e.client.Services(options.RegistryNamespace).Watch(labels.Everything(), fields.Everything(), rv)
},
},
&api.Service{},
options.Resync,
framework.ResourceEventHandlerFuncs{
AddFunc: e.serviceAdded,
UpdateFunc: e.serviceUpdated,
DeleteFunc: e.serviceDeleted,
},
)
e.registryServiceName = options.RegistryServiceName
e.dockercfgController = options.DockercfgController
e.defaultDockerURL = options.DefaultDockerURL
return e
}
// DockerRegistryServiceController manages ServiceToken secrets for Service objects
type DockerRegistryServiceController struct {
stopChan chan struct{}
client client.Interface
registryServiceName string
defaultDockerURL string
dockercfgController *DockercfgController
serviceController *framework.Controller
}
// Runs controller loops and returns immediately
func (e *DockerRegistryServiceController) Run() {
if e.stopChan == nil {
e.stopChan = make(chan struct{})
go e.serviceController.Run(e.stopChan)
}
}
// Stop gracefully shuts down this controller
func (e *DockerRegistryServiceController) Stop() {
if e.stopChan != nil {
close(e.stopChan)
e.stopChan = nil
}
}
func (e *DockerRegistryServiceController) getServiceLocation(service *api.Service) string {
hasPortalIP := (len(service.Spec.ClusterIP) > 0) && (net.ParseIP(service.Spec.ClusterIP) != nil)
if hasPortalIP && len(service.Spec.Ports) > 0 {
return net.JoinHostPort(service.Spec.ClusterIP, fmt.Sprintf("%d", service.Spec.Ports[0].Port))
}
return e.defaultDockerURL
}
// serviceAdded reacts to the creation of a docker-registry service by updating all service account dockercfg secrets and
// changing all interestedURLs
func (e *DockerRegistryServiceController) serviceAdded(obj interface{}) {
service := obj.(*api.Service)
if service.Name != e.registryServiceName {
return
}
if err := e.handleLocationChange(e.getServiceLocation(service)); err != nil {
util.HandleError(err)
}
}
// serviceUpdated reacts to the creation of a docker-registry service by updating all service account dockercfg secrets and
// changing all interestedURLs, if needed
func (e *DockerRegistryServiceController) serviceUpdated(oldObj interface{}, newObj interface{}) {
oldService := oldObj.(*api.Service)
newService := newObj.(*api.Service)
if newService.Name != e.registryServiceName {
return
}
if e.getServiceLocation(oldService) == e.getServiceLocation(newService) {
return
}
if err := e.handleLocationChange(e.getServiceLocation(newService)); err != nil {
util.HandleError(err)
}
}
// serviceDeleted reacts to the docker-registry deletion by updating all the generated dockercfg secrets
func (e *DockerRegistryServiceController) serviceDeleted(obj interface{}) {
service, ok := obj.(*api.Service)
if !ok {
return
}
if service.Name != e.registryServiceName {
return
}
if err := e.handleLocationChange(e.defaultDockerURL); err != nil {
util.HandleError(err)
}
}
// handleLocationChange goes through all service account dockercfg secrets and updates them to point at a new docker-registry location
func (e *DockerRegistryServiceController) handleLocationChange(serviceLocation string) error {
e.dockercfgController.SetDockerURL(serviceLocation)
dockercfgSecrets, err := e.listDockercfgSecrets()
if err != nil {
return err
}
for _, dockercfgSecret := range dockercfgSecrets {
dockercfg := &credentialprovider.DockerConfig{}
if err := json.Unmarshal(dockercfgSecret.Data[api.DockerConfigKey], dockercfg); err != nil {
util.HandleError(err)
continue
}
dockercfgMap := map[string]credentialprovider.DockerConfigEntry(*dockercfg)
keys := util.KeySet(reflect.ValueOf(dockercfgMap))
if len(keys) != 1 {
util.HandleError(err)
continue
}
oldKey := keys.List()[0]
// if there's no change, skip
if oldKey == serviceLocation {
continue
}
dockercfgMap[serviceLocation] = dockercfgMap[oldKey]
delete(dockercfgMap, oldKey)
t := credentialprovider.DockerConfig(dockercfgMap)
dockercfg = &t
dockercfgContent, err := json.Marshal(dockercfg)
if err != nil {
util.HandleError(err)
continue
}
dockercfgSecret.Data[api.DockerConfigKey] = dockercfgContent
if _, err := e.client.Secrets(dockercfgSecret.Namespace).Update(dockercfgSecret); err != nil {
util.HandleError(err)
continue
}
}
return err
}
func (e *DockerRegistryServiceController) listDockercfgSecrets() ([]*api.Secret, error) {
dockercfgSelector := fields.SelectorFromSet(map[string]string{client.SecretType: string(api.SecretTypeDockercfg)})
potentialSecrets, err := e.client.Secrets(api.NamespaceAll).List(labels.Everything(), dockercfgSelector)
if err != nil {
return nil, err
}
dockercfgSecretsForThisSA := []*api.Secret{}
for i, currSecret := range potentialSecrets.Items {
if len(currSecret.Annotations[ServiceAccountTokenSecretNameKey]) > 0 {
dockercfgSecretsForThisSA = append(dockercfgSecretsForThisSA, &potentialSecrets.Items[i])
}
}
return dockercfgSecretsForThisSA, nil
}