-
Notifications
You must be signed in to change notification settings - Fork 39.6k
/
rancher_registry_credentials.go
145 lines (121 loc) · 3.69 KB
/
rancher_registry_credentials.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
/*
Copyright 2014 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package rancher_credentials
import (
"os"
"time"
"github.com/rancher/go-rancher/client"
"k8s.io/klog"
"k8s.io/kubernetes/pkg/credentialprovider"
)
// rancher provider
type rancherProvider struct {
credGetter credentialsGetter
}
// credentials getter from Rancher private registry
type rancherCredentialsGetter struct {
client *client.RancherClient
}
type rConfig struct {
Global configGlobal
}
// An interface for testing purposes.
type credentialsGetter interface {
getCredentials() []registryCredential
}
type configGlobal struct {
CattleURL string `gcfg:"cattle-url"`
CattleAccessKey string `gcfg:"cattle-access-key"`
CattleSecretKey string `gcfg:"cattle-secret-key"`
}
type registryCredential struct {
credential *client.RegistryCredential
serverIP string
}
var rancherGetter = &rancherCredentialsGetter{}
func init() {
credentialprovider.RegisterCredentialProvider("rancher-registry-creds",
&credentialprovider.CachingDockerConfigProvider{
Provider: &rancherProvider{rancherGetter},
Lifetime: 30 * time.Second,
})
}
func (p *rancherProvider) Enabled() bool {
client, err := getRancherClient()
if err != nil {
return false
}
if client == nil {
return false
}
rancherGetter.client = client
return true
}
// LazyProvide implements DockerConfigProvider. Should never be called.
func (p *rancherProvider) LazyProvide() *credentialprovider.DockerConfigEntry {
return nil
}
// Provide implements DockerConfigProvider.Provide, refreshing Rancher tokens on demand
func (p *rancherProvider) Provide() credentialprovider.DockerConfig {
cfg := credentialprovider.DockerConfig{}
for _, cred := range p.credGetter.getCredentials() {
entry := credentialprovider.DockerConfigEntry{
Username: cred.credential.PublicValue,
Password: cred.credential.SecretValue,
Email: cred.credential.Email,
}
cfg[cred.serverIP] = entry
}
return cfg
}
func (g *rancherCredentialsGetter) getCredentials() []registryCredential {
var registryCreds []registryCredential
credColl, err := g.client.RegistryCredential.List(client.NewListOpts())
if err != nil {
klog.Errorf("Failed to pull registry credentials from rancher %v", err)
return registryCreds
}
for _, cred := range credColl.Data {
registry := &client.Registry{}
if err = g.client.GetLink(cred.Resource, "registry", registry); err != nil {
klog.Errorf("Failed to pull registry from rancher %v", err)
return registryCreds
}
registryCred := registryCredential{
credential: &cred,
serverIP: registry.ServerAddress,
}
registryCreds = append(registryCreds, registryCred)
}
return registryCreds
}
func getRancherClient() (*client.RancherClient, error) {
url := os.Getenv("CATTLE_URL")
accessKey := os.Getenv("CATTLE_ACCESS_KEY")
secretKey := os.Getenv("CATTLE_SECRET_KEY")
if url == "" || accessKey == "" || secretKey == "" {
return nil, nil
}
conf := rConfig{
Global: configGlobal{
CattleURL: url,
CattleAccessKey: accessKey,
CattleSecretKey: secretKey,
},
}
return client.NewRancherClient(&client.ClientOpts{
Url: conf.Global.CattleURL,
AccessKey: conf.Global.CattleAccessKey,
SecretKey: conf.Global.CattleSecretKey,
})
}