Skip to content

Commit 1b42932

Browse files
author
k8s-merge-robot
committed
Merge pull request kubernetes#19183 from yifan-gu/rkt_refactor
Auto commit by PR queue bot
2 parents cf35905 + 9b81b67 commit 1b42932

File tree

2 files changed

+218
-183
lines changed

2 files changed

+218
-183
lines changed

pkg/kubelet/rkt/image.go

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
/*
2+
Copyright 2015 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// This file contains all image related functions for rkt runtime.
18+
package rkt
19+
20+
import (
21+
"encoding/json"
22+
"fmt"
23+
"io/ioutil"
24+
"os"
25+
"path"
26+
"sort"
27+
"strings"
28+
29+
appcschema "github.com/appc/spec/schema"
30+
rktapi "github.com/coreos/rkt/api/v1alpha"
31+
"github.com/fsouza/go-dockerclient"
32+
"github.com/golang/glog"
33+
"golang.org/x/net/context"
34+
"k8s.io/kubernetes/pkg/api"
35+
"k8s.io/kubernetes/pkg/credentialprovider"
36+
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
37+
"k8s.io/kubernetes/pkg/util/parsers"
38+
)
39+
40+
// PullImage invokes 'rkt fetch' to download an aci.
41+
// TODO(yifan): Now we only support docker images, this should be changed
42+
// once the format of image is landed, see:
43+
//
44+
// http://issue.k8s.io/7203
45+
//
46+
func (r *Runtime) PullImage(image kubecontainer.ImageSpec, pullSecrets []api.Secret) error {
47+
img := image.Image
48+
// TODO(yifan): The credential operation is a copy from dockertools package,
49+
// Need to resolve the code duplication.
50+
repoToPull, _ := parsers.ParseImageName(img)
51+
keyring, err := credentialprovider.MakeDockerKeyring(pullSecrets, r.dockerKeyring)
52+
if err != nil {
53+
return err
54+
}
55+
56+
creds, ok := keyring.Lookup(repoToPull)
57+
if !ok {
58+
glog.V(1).Infof("Pulling image %s without credentials", img)
59+
}
60+
61+
// Let's update a json.
62+
// TODO(yifan): Find a way to feed this to rkt.
63+
if err := r.writeDockerAuthConfig(img, creds); err != nil {
64+
return err
65+
}
66+
67+
if _, err := r.runCommand("fetch", dockerPrefix+img); err != nil {
68+
glog.Errorf("Failed to fetch: %v", err)
69+
return err
70+
}
71+
return nil
72+
}
73+
74+
func (r *Runtime) IsImagePresent(image kubecontainer.ImageSpec) (bool, error) {
75+
images, err := r.listImages(image.Image, false)
76+
return len(images) > 0, err
77+
}
78+
79+
// ListImages lists all the available appc images on the machine by invoking 'rkt image list'.
80+
func (r *Runtime) ListImages() ([]kubecontainer.Image, error) {
81+
listResp, err := r.apisvc.ListImages(context.Background(), &rktapi.ListImagesRequest{})
82+
if err != nil {
83+
return nil, fmt.Errorf("couldn't list images: %v", err)
84+
}
85+
86+
images := make([]kubecontainer.Image, len(listResp.Images))
87+
for i, image := range listResp.Images {
88+
images[i] = kubecontainer.Image{
89+
ID: image.Id,
90+
Tags: []string{buildImageName(image)},
91+
//TODO: fill in the size of the image
92+
}
93+
}
94+
return images, nil
95+
}
96+
97+
// RemoveImage removes an on-disk image using 'rkt image rm'.
98+
func (r *Runtime) RemoveImage(image kubecontainer.ImageSpec) error {
99+
imageID, err := r.getImageID(image.Image)
100+
if err != nil {
101+
return err
102+
}
103+
if _, err := r.runCommand("image", "rm", imageID); err != nil {
104+
return err
105+
}
106+
return nil
107+
}
108+
109+
// buildImageName constructs the image name for kubecontainer.Image.
110+
func buildImageName(img *rktapi.Image) string {
111+
return fmt.Sprintf("%s:%s", img.Name, img.Version)
112+
}
113+
114+
// getImageID tries to find the image ID for the given image name.
115+
// imageName should be in the form of 'name[:version]', e.g., 'example.com/app:latest'.
116+
// The name should matches the result of 'rkt image list'. If the version is empty,
117+
// then 'latest' is assumed.
118+
func (r *Runtime) getImageID(imageName string) (string, error) {
119+
images, err := r.listImages(imageName, false)
120+
if err != nil {
121+
return "", err
122+
}
123+
if len(images) == 0 {
124+
return "", fmt.Errorf("cannot find the image %q", imageName)
125+
}
126+
return images[0].Id, nil
127+
}
128+
129+
type sortByImportTime []*rktapi.Image
130+
131+
func (s sortByImportTime) Len() int { return len(s) }
132+
func (s sortByImportTime) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
133+
func (s sortByImportTime) Less(i, j int) bool { return s[i].ImportTimestamp < s[j].ImportTimestamp }
134+
135+
// listImages lists the images that have the given name. If detail is true,
136+
// then image manifest is also included in the result.
137+
// Note that there could be more than one images that have the given name, we
138+
// will return the result reversely sorted by the import time, so that the latest
139+
// image comes first.
140+
func (r *Runtime) listImages(image string, detail bool) ([]*rktapi.Image, error) {
141+
repoToPull, tag := parsers.ParseImageName(image)
142+
listResp, err := r.apisvc.ListImages(context.Background(), &rktapi.ListImagesRequest{
143+
Detail: detail,
144+
Filters: []*rktapi.ImageFilter{
145+
{
146+
// TODO(yifan): Add a field in the ImageFilter to match the whole name,
147+
// not just keywords.
148+
// https://github.com/coreos/rkt/issues/1872#issuecomment-166456938
149+
Keywords: []string{repoToPull},
150+
Labels: []*rktapi.KeyValue{{Key: "version", Value: tag}},
151+
},
152+
},
153+
})
154+
if err != nil {
155+
return nil, fmt.Errorf("couldn't list images: %v", err)
156+
}
157+
158+
// TODO(yifan): Let the API service to sort the result:
159+
// See https://github.com/coreos/rkt/issues/1911.
160+
sort.Sort(sort.Reverse(sortByImportTime(listResp.Images)))
161+
return listResp.Images, nil
162+
}
163+
164+
// getImageManifest retrieves the image manifest for the given image.
165+
func (r *Runtime) getImageManifest(image string) (*appcschema.ImageManifest, error) {
166+
var manifest appcschema.ImageManifest
167+
168+
images, err := r.listImages(image, true)
169+
if err != nil {
170+
return nil, err
171+
}
172+
if len(images) == 0 {
173+
return nil, fmt.Errorf("cannot find the image %q", image)
174+
}
175+
176+
return &manifest, json.Unmarshal(images[0].Manifest, &manifest)
177+
}
178+
179+
// TODO(yifan): This is very racy, unefficient, and unsafe, we need to provide
180+
// different namespaces. See: https://github.com/coreos/rkt/issues/836.
181+
func (r *Runtime) writeDockerAuthConfig(image string, credsSlice []docker.AuthConfiguration) error {
182+
if len(credsSlice) == 0 {
183+
return nil
184+
}
185+
186+
creds := docker.AuthConfiguration{}
187+
// TODO handle multiple creds
188+
if len(credsSlice) >= 1 {
189+
creds = credsSlice[0]
190+
}
191+
192+
registry := "index.docker.io"
193+
// Image spec: [<registry>/]<repository>/<image>[:<version]
194+
explicitRegistry := (strings.Count(image, "/") == 2)
195+
if explicitRegistry {
196+
registry = strings.Split(image, "/")[0]
197+
}
198+
199+
localConfigDir := rktLocalConfigDir
200+
if r.config.LocalConfigDir != "" {
201+
localConfigDir = r.config.LocalConfigDir
202+
}
203+
authDir := path.Join(localConfigDir, "auth.d")
204+
if _, err := os.Stat(authDir); os.IsNotExist(err) {
205+
if err := os.Mkdir(authDir, 0600); err != nil {
206+
glog.Errorf("rkt: Cannot create auth dir: %v", err)
207+
return err
208+
}
209+
}
210+
211+
config := fmt.Sprintf(dockerAuthTemplate, registry, creds.Username, creds.Password)
212+
if err := ioutil.WriteFile(path.Join(authDir, registry+".json"), []byte(config), 0600); err != nil {
213+
glog.Errorf("rkt: Cannot write docker auth config file: %v", err)
214+
return err
215+
}
216+
return nil
217+
}

0 commit comments

Comments
 (0)