forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker.go
178 lines (159 loc) · 5.14 KB
/
docker.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
package util
import (
"fmt"
"strings"
g "github.com/onsi/ginkgo"
gson "encoding/json"
dockerClient "github.com/fsouza/go-dockerclient"
tutil "github.com/openshift/origin/test/util"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/credentialprovider"
)
//TagImage will apply the "tagor" tag string to the image current tagged by "tagee"
func TagImage(tagee, tagor string) error {
client, dcerr := tutil.NewDockerClient()
if dcerr != nil {
return dcerr
}
opts := dockerClient.TagImageOptions{
Repo: tagee,
Tag: "latest",
Force: true,
}
return client.TagImage(tagor, opts)
}
//InspectImage initiates the equivalent of a `docker inspect` for the "name" parameter
func InspectImage(name string) (*dockerClient.Image, error) {
client, err := tutil.NewDockerClient()
if err != nil {
return nil, err
}
return client.InspectImage(name)
}
//PullImage initiates the equivalent of a `docker pull` for the "name" parameter
func PullImage(name string, authCfg dockerClient.AuthConfiguration) error {
client, err := tutil.NewDockerClient()
if err != nil {
return err
}
opts := dockerClient.PullImageOptions{
Repository: name,
Tag: "latest",
}
return client.PullImage(opts, authCfg)
}
//PushImage initiates the equivalent of a `docker push` for the "name" parameter to the local registry
func PushImage(name string, authCfg dockerClient.AuthConfiguration) error {
client, err := tutil.NewDockerClient()
if err != nil {
return err
}
opts := dockerClient.PushImageOptions{
Name: name,
Tag: "latest",
}
return client.PushImage(opts, authCfg)
}
//ListImages initiates the equivalent of a `docker images`
func ListImages() ([]string, error) {
client, err := tutil.NewDockerClient()
if err != nil {
return nil, err
}
imageList, err := client.ListImages(dockerClient.ListImagesOptions{})
if err != nil {
return nil, err
}
returnIds := make([]string, 0)
for _, image := range imageList {
for _, tag := range image.RepoTags {
returnIds = append(returnIds, tag)
}
}
return returnIds, nil
}
//BuildAuthConfiguration constructs a non-standard dockerClient.AuthConfiguration that can be used to communicate with the openshift internal docker registry
func BuildAuthConfiguration(credKey string, oc *CLI) (*dockerClient.AuthConfiguration, error) {
authCfg := &dockerClient.AuthConfiguration{}
secretList, err := oc.AdminKubeREST().Secrets(oc.Namespace()).List(kapi.ListOptions{})
g.By(fmt.Sprintf("get secret list err %v ", err))
if err == nil {
for _, secret := range secretList.Items {
g.By(fmt.Sprintf("secret name %s ", secret.ObjectMeta.Name))
if strings.Contains(secret.ObjectMeta.Name, "builder-dockercfg") {
dockercfgToken := secret.Data[".dockercfg"]
dockercfgTokenJson := string(dockercfgToken)
g.By(fmt.Sprintf("docker cfg token json %s ", dockercfgTokenJson))
creds := credentialprovider.DockerConfig{}
err = gson.Unmarshal(dockercfgToken, &creds)
g.By(fmt.Sprintf("json unmarshal err %v ", err))
if err == nil {
// borrowed from openshift/origin/pkg/build/builder/cmd/dockercfg/cfg.go, but we get the
// secrets and dockercfg data via `oc` vs. internal use of env vars and local file reading,
// so we don't use the public methods present there
keyring := credentialprovider.BasicDockerKeyring{}
keyring.Add(creds)
authConfs, found := keyring.Lookup(credKey)
g.By(fmt.Sprintf("found auth %v with auth cfg len %d ", found, len(authConfs)))
if !found || len(authConfs) == 0 {
return authCfg, err
}
// have seen this does not get set
if len(authConfs[0].ServerAddress) == 0 {
authConfs[0].ServerAddress = credKey
}
g.By(fmt.Sprintf("dockercfg with svrAddr %s user %s pass %s email %s ", authConfs[0].ServerAddress, authConfs[0].Username, authConfs[0].Password, authConfs[0].Email))
c := dockerClient.AuthConfiguration{Username: authConfs[0].Username, ServerAddress: authConfs[0].ServerAddress, Password: authConfs[0].Password}
return &c, err
}
}
}
}
return authCfg, err
}
type MissingTagError struct {
Tags []string
}
func (mte MissingTagError) Error() string {
return fmt.Sprintf("the tag %s passed in was invalid, and not found in the list of images returned from docker", mte.Tags)
}
//GetImageIDForTags will obtain the hexadecimal IDs for the array of human readible image tags IDs provided
func GetImageIDForTags(comps []string) ([]string, error) {
client, dcerr := tutil.NewDockerClient()
if dcerr != nil {
return nil, dcerr
}
imageList, serr := client.ListImages(dockerClient.ListImagesOptions{})
if serr != nil {
return nil, serr
}
returnTags := make([]string, 0)
missingTags := make([]string, 0)
for _, comp := range comps {
var found bool
for _, image := range imageList {
for _, repTag := range image.RepoTags {
if repTag == comp {
found = true
returnTags = append(returnTags, image.ID)
break
}
}
if found {
break
}
}
if !found {
returnTags = append(returnTags, "")
missingTags = append(missingTags, comp)
}
}
if len(missingTags) == 0 {
return returnTags, nil
} else {
mte := MissingTagError{
Tags: missingTags,
}
return returnTags, mte
}
}