Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
29a063e6
Commit
29a063e6
authored
Feb 26, 2017
by
Random-Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Check infra container image existence before pulling.
parent
7265908e
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
64 additions
and
14 deletions
+64
-14
docker_sandbox.go
pkg/kubelet/dockershim/docker_sandbox.go
+3
-2
helpers.go
pkg/kubelet/dockershim/helpers.go
+16
-0
helpers_test.go
pkg/kubelet/dockershim/helpers_test.go
+31
-0
docker.go
pkg/kubelet/dockertools/docker.go
+1
-1
docker_manager.go
pkg/kubelet/dockertools/docker_manager.go
+5
-4
kube_docker_client.go
pkg/kubelet/dockertools/kube_docker_client.go
+8
-7
No files found.
pkg/kubelet/dockershim/docker_sandbox.go
View file @
29a063e6
...
@@ -61,8 +61,9 @@ func (ds *dockerService) RunPodSandbox(config *runtimeapi.PodSandboxConfig) (str
...
@@ -61,8 +61,9 @@ func (ds *dockerService) RunPodSandbox(config *runtimeapi.PodSandboxConfig) (str
// NOTE: To use a custom sandbox image in a private repository, users need to configure the nodes with credentials properly.
// NOTE: To use a custom sandbox image in a private repository, users need to configure the nodes with credentials properly.
// see: http://kubernetes.io/docs/user-guide/images/#configuring-nodes-to-authenticate-to-a-private-repository
// see: http://kubernetes.io/docs/user-guide/images/#configuring-nodes-to-authenticate-to-a-private-repository
if
err
:=
ds
.
client
.
PullImage
(
image
,
dockertypes
.
AuthConfig
{},
dockertypes
.
ImagePullOptions
{});
err
!=
nil
{
// Only pull sandbox image when it's not present - v1.PullIfNotPresent.
return
""
,
fmt
.
Errorf
(
"unable to pull image for the sandbox container: %v"
,
err
)
if
err
:=
ensureSandboxImageExists
(
ds
.
client
,
image
);
err
!=
nil
{
return
""
,
err
}
}
// Step 2: Create the sandbox container.
// Step 2: Create the sandbox container.
...
...
pkg/kubelet/dockershim/helpers.go
View file @
29a063e6
...
@@ -320,3 +320,19 @@ func getSecurityOptSeparator(v *semver.Version) rune {
...
@@ -320,3 +320,19 @@ func getSecurityOptSeparator(v *semver.Version) rune {
return
dockertools
.
SecurityOptSeparatorNew
return
dockertools
.
SecurityOptSeparatorNew
}
}
}
}
// ensureSandboxImageExists pulls the sandbox image when it's not present.
func
ensureSandboxImageExists
(
client
dockertools
.
DockerInterface
,
image
string
)
error
{
_
,
err
:=
client
.
InspectImageByRef
(
image
)
if
err
==
nil
{
return
nil
}
if
!
dockertools
.
IsImageNotFoundError
(
err
)
{
return
fmt
.
Errorf
(
"failed to inspect sandbox image %q: %v"
,
image
,
err
)
}
err
=
client
.
PullImage
(
image
,
dockertypes
.
AuthConfig
{},
dockertypes
.
ImagePullOptions
{})
if
err
!=
nil
{
return
fmt
.
Errorf
(
"unable to pull sandbox image %q: %v"
,
image
,
err
)
}
return
nil
}
pkg/kubelet/dockershim/helpers_test.go
View file @
29a063e6
...
@@ -17,6 +17,7 @@ limitations under the License.
...
@@ -17,6 +17,7 @@ limitations under the License.
package
dockershim
package
dockershim
import
(
import
(
"fmt"
"testing"
"testing"
"github.com/blang/semver"
"github.com/blang/semver"
...
@@ -25,6 +26,7 @@ import (
...
@@ -25,6 +26,7 @@ import (
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/api/v1"
runtimeapi
"k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
runtimeapi
"k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
"k8s.io/kubernetes/pkg/kubelet/dockertools"
"k8s.io/kubernetes/pkg/security/apparmor"
"k8s.io/kubernetes/pkg/security/apparmor"
)
)
...
@@ -261,3 +263,32 @@ func TestGetSecurityOptSeparator(t *testing.T) {
...
@@ -261,3 +263,32 @@ func TestGetSecurityOptSeparator(t *testing.T) {
assert
.
Equal
(
t
,
test
.
expected
,
actual
,
c
)
assert
.
Equal
(
t
,
test
.
expected
,
actual
,
c
)
}
}
}
}
func
TestEnsureSandboxImageExists
(
t
*
testing
.
T
)
{
for
desc
,
test
:=
range
map
[
string
]
struct
{
inject
error
calls
[]
string
err
bool
}{
"should not pull image when it already exists"
:
{
inject
:
nil
,
calls
:
[]
string
{
"inspect_image"
},
},
"should pull image when it doesn't exist"
:
{
inject
:
dockertools
.
ImageNotFoundError
{
ID
:
"image_id"
},
calls
:
[]
string
{
"inspect_image"
,
"pull"
},
},
"should return error when inspect image fails"
:
{
inject
:
fmt
.
Errorf
(
"arbitrary error"
),
calls
:
[]
string
{
"inspect_image"
},
err
:
true
,
},
}
{
t
.
Logf
(
"TestCase: %q"
,
desc
)
_
,
fakeDocker
,
_
:=
newTestDockerService
()
fakeDocker
.
InjectError
(
"inspect_image"
,
test
.
inject
)
err
:=
ensureSandboxImageExists
(
fakeDocker
,
"gcr.io/test/image"
)
assert
.
NoError
(
t
,
fakeDocker
.
AssertCalls
(
test
.
calls
))
assert
.
Equal
(
t
,
test
.
err
,
err
!=
nil
)
}
}
pkg/kubelet/dockertools/docker.go
View file @
29a063e6
...
@@ -290,7 +290,7 @@ func (p dockerPuller) GetImageRef(image string) (string, error) {
...
@@ -290,7 +290,7 @@ func (p dockerPuller) GetImageRef(image string) (string, error) {
}
}
return
imageRef
,
nil
return
imageRef
,
nil
}
}
if
_
,
ok
:=
err
.
(
imageNotFoundError
);
ok
{
if
IsImageNotFoundError
(
err
)
{
return
""
,
nil
return
""
,
nil
}
}
return
""
,
err
return
""
,
err
...
...
pkg/kubelet/dockertools/docker_manager.go
View file @
29a063e6
...
@@ -405,6 +405,7 @@ func (dm *DockerManager) inspectContainer(id string, podName, podNamespace strin
...
@@ -405,6 +405,7 @@ func (dm *DockerManager) inspectContainer(id string, podName, podNamespace strin
// default to the image ID, but try and inspect for the RepoDigests
// default to the image ID, but try and inspect for the RepoDigests
imageID
:=
DockerPrefix
+
iResult
.
Image
imageID
:=
DockerPrefix
+
iResult
.
Image
imageName
:=
iResult
.
Config
.
Image
imgInspectResult
,
err
:=
dm
.
client
.
InspectImageByID
(
iResult
.
Image
)
imgInspectResult
,
err
:=
dm
.
client
.
InspectImageByID
(
iResult
.
Image
)
if
err
!=
nil
{
if
err
!=
nil
{
utilruntime
.
HandleError
(
fmt
.
Errorf
(
"unable to inspect docker image %q while inspecting docker container %q: %v"
,
iResult
.
Image
,
containerName
,
err
))
utilruntime
.
HandleError
(
fmt
.
Errorf
(
"unable to inspect docker image %q while inspecting docker container %q: %v"
,
iResult
.
Image
,
containerName
,
err
))
...
@@ -416,12 +417,12 @@ func (dm *DockerManager) inspectContainer(id string, podName, podNamespace strin
...
@@ -416,12 +417,12 @@ func (dm *DockerManager) inspectContainer(id string, podName, podNamespace strin
if
len
(
imgInspectResult
.
RepoDigests
)
>
0
{
if
len
(
imgInspectResult
.
RepoDigests
)
>
0
{
imageID
=
DockerPullablePrefix
+
imgInspectResult
.
RepoDigests
[
0
]
imageID
=
DockerPullablePrefix
+
imgInspectResult
.
RepoDigests
[
0
]
}
}
}
imageName
:=
iResult
.
Config
.
Image
if
len
(
imgInspectResult
.
RepoTags
)
>
0
{
if
len
(
imgInspectResult
.
RepoTags
)
>
0
{
imageName
=
imgInspectResult
.
RepoTags
[
0
]
imageName
=
imgInspectResult
.
RepoTags
[
0
]
}
}
}
status
:=
kubecontainer
.
ContainerStatus
{
status
:=
kubecontainer
.
ContainerStatus
{
Name
:
containerName
,
Name
:
containerName
,
RestartCount
:
containerInfo
.
RestartCount
,
RestartCount
:
containerInfo
.
RestartCount
,
...
...
pkg/kubelet/dockertools/kube_docker_client.go
View file @
29a063e6
...
@@ -187,7 +187,7 @@ func (d *kubeDockerClient) inspectImageRaw(ref string) (*dockertypes.ImageInspec
...
@@ -187,7 +187,7 @@ func (d *kubeDockerClient) inspectImageRaw(ref string) (*dockertypes.ImageInspec
}
}
if
err
!=
nil
{
if
err
!=
nil
{
if
dockerapi
.
IsErrImageNotFound
(
err
)
{
if
dockerapi
.
IsErrImageNotFound
(
err
)
{
err
=
i
mageNotFoundError
{
ID
:
ref
}
err
=
I
mageNotFoundError
{
ID
:
ref
}
}
}
return
nil
,
err
return
nil
,
err
}
}
...
@@ -202,7 +202,7 @@ func (d *kubeDockerClient) InspectImageByID(imageID string) (*dockertypes.ImageI
...
@@ -202,7 +202,7 @@ func (d *kubeDockerClient) InspectImageByID(imageID string) (*dockertypes.ImageI
}
}
if
!
matchImageIDOnly
(
*
resp
,
imageID
)
{
if
!
matchImageIDOnly
(
*
resp
,
imageID
)
{
return
nil
,
i
mageNotFoundError
{
ID
:
imageID
}
return
nil
,
I
mageNotFoundError
{
ID
:
imageID
}
}
}
return
resp
,
nil
return
resp
,
nil
}
}
...
@@ -214,7 +214,7 @@ func (d *kubeDockerClient) InspectImageByRef(imageRef string) (*dockertypes.Imag
...
@@ -214,7 +214,7 @@ func (d *kubeDockerClient) InspectImageByRef(imageRef string) (*dockertypes.Imag
}
}
if
!
matchImageTagOrSHA
(
*
resp
,
imageRef
)
{
if
!
matchImageTagOrSHA
(
*
resp
,
imageRef
)
{
return
nil
,
i
mageNotFoundError
{
ID
:
imageRef
}
return
nil
,
I
mageNotFoundError
{
ID
:
imageRef
}
}
}
return
resp
,
nil
return
resp
,
nil
}
}
...
@@ -613,18 +613,19 @@ func IsContainerNotFoundError(err error) bool {
...
@@ -613,18 +613,19 @@ func IsContainerNotFoundError(err error) bool {
return
containerNotFoundErrorRegx
.
MatchString
(
err
.
Error
())
return
containerNotFoundErrorRegx
.
MatchString
(
err
.
Error
())
}
}
// imageNotFoundError is the error returned by InspectImage when image not found.
// ImageNotFoundError is the error returned by InspectImage when image not found.
type
imageNotFoundError
struct
{
// Expose this to inject error in dockershim for testing.
type
ImageNotFoundError
struct
{
ID
string
ID
string
}
}
func
(
e
i
mageNotFoundError
)
Error
()
string
{
func
(
e
I
mageNotFoundError
)
Error
()
string
{
return
fmt
.
Sprintf
(
"no such image: %q"
,
e
.
ID
)
return
fmt
.
Sprintf
(
"no such image: %q"
,
e
.
ID
)
}
}
// IsImageNotFoundError checks whether the error is image not found error. This is exposed
// IsImageNotFoundError checks whether the error is image not found error. This is exposed
// to share with dockershim.
// to share with dockershim.
func
IsImageNotFoundError
(
err
error
)
bool
{
func
IsImageNotFoundError
(
err
error
)
bool
{
_
,
ok
:=
err
.
(
i
mageNotFoundError
)
_
,
ok
:=
err
.
(
I
mageNotFoundError
)
return
ok
return
ok
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment