Skip to content

Commit 93dae8f

Browse files
authored
Merge pull request #50 from senthilrch/senthilrch
Senthilrch
2 parents 7763581 + bd50410 commit 93dae8f

File tree

4 files changed

+100
-48
lines changed

4 files changed

+100
-48
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ These instructions will help you build _kube-fledged_ from source and deploy it
2323
### Prerequisites
2424

2525
- A functioning kubernetes cluster (v1.7 or above). It could be a simple development cluster like minikube or a large production cluster.
26-
- All master and worker nodes with docker engine installed, and having the ["kubernetes.io/hostname"](https://kubernetes.io/docs/reference/kubernetes-api/labels-annotations-taints/#kubernetes-io-hostname) label.
26+
- All master and worker nodes having the ["kubernetes.io/hostname"](https://kubernetes.io/docs/reference/kubernetes-api/labels-annotations-taints/#kubernetes-io-hostname) label.
27+
- Supported container runtimes: docker, containerd
2728
- make, go, docker and kubectl installed on a local linux machine. kubectl configured properly to access the cluster.
2829

2930
### Build

cmd/app/controller.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,10 @@ func (c *Controller) enqueueImageCache(workType images.WorkType, old, new interf
273273
newImageCache := new.(*fledgedv1alpha1.ImageCache)
274274

275275
if oldImageCache.Status.Status == fledgedv1alpha1.ImageCacheActionStatusProcessing {
276-
glog.Errorf("Received image cache update/purge/delete for '%s' while it is under processing, so ignoring.", oldImageCache.Name)
277-
return false
276+
if !reflect.DeepEqual(newImageCache.Spec, oldImageCache.Spec) {
277+
glog.Warningf("Received image cache update/purge/delete for '%s' while it is under processing, so ignoring.", oldImageCache.Name)
278+
return false
279+
}
278280
}
279281
if _, exists := newImageCache.Annotations[imageCachePurgeAnnotationKey]; exists {
280282
if _, exists := oldImageCache.Annotations[imageCachePurgeAnnotationKey]; !exists {
@@ -556,10 +558,11 @@ func (c *Controller) syncHandler(wqKey images.WorkQueueKey) error {
556558
for _, n := range nodes {
557559
for m := range i.Images {
558560
ipr := images.ImageWorkRequest{
559-
Image: i.Images[m],
560-
Node: n.Labels["kubernetes.io/hostname"],
561-
WorkType: wqKey.WorkType,
562-
Imagecache: imageCache,
561+
Image: i.Images[m],
562+
Node: n.Labels["kubernetes.io/hostname"],
563+
ContainerRuntimeVersion: n.Status.NodeInfo.ContainerRuntimeVersion,
564+
WorkType: wqKey.WorkType,
565+
Imagecache: imageCache,
563566
}
564567
c.imageworkqueue.AddRateLimited(ipr)
565568
}
@@ -574,10 +577,11 @@ func (c *Controller) syncHandler(wqKey images.WorkQueueKey) error {
574577
}
575578
if !matched {
576579
ipr := images.ImageWorkRequest{
577-
Image: oldimage,
578-
Node: n.Labels["kubernetes.io/hostname"],
579-
WorkType: images.ImageCachePurge,
580-
Imagecache: imageCache,
580+
Image: oldimage,
581+
Node: n.Labels["kubernetes.io/hostname"],
582+
ContainerRuntimeVersion: n.Status.NodeInfo.ContainerRuntimeVersion,
583+
WorkType: images.ImageCachePurge,
584+
Imagecache: imageCache,
581585
}
582586
c.imageworkqueue.AddRateLimited(ipr)
583587
}

pkg/images/image_helpers.go

Lines changed: 73 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func newImagePullJob(imagecache *fledgedv1alpha1.ImageCache, image string, hostn
130130
}
131131

132132
// newImageDeleteJob constructs a job manifest to delete an image from a node
133-
func newImageDeleteJob(imagecache *fledgedv1alpha1.ImageCache, image string, hostname string, dockerclientimage string) (*batchv1.Job, error) {
133+
func newImageDeleteJob(imagecache *fledgedv1alpha1.ImageCache, image string, hostname string, containerRuntimeVersion string, dockerclientimage string) (*batchv1.Job, error) {
134134
if imagecache == nil {
135135
glog.Error("imagecache pointer is nil")
136136
return nil, fmt.Errorf("imagecache pointer is nil")
@@ -146,6 +146,76 @@ func newImageDeleteJob(imagecache *fledgedv1alpha1.ImageCache, image string, hos
146146
backoffLimit := int32(0)
147147
activeDeadlineSeconds := int64((time.Hour).Seconds())
148148

149+
var containerRuntime string
150+
if strings.Contains(containerRuntimeVersion, "docker") {
151+
containerRuntime = "docker"
152+
}
153+
if strings.Contains(containerRuntimeVersion, "containerd") {
154+
containerRuntime = "containerd"
155+
}
156+
157+
containerSpec := map[string]struct {
158+
Containers []corev1.Container
159+
Volumes []corev1.Volume
160+
}{
161+
"docker": {
162+
Containers: []corev1.Container{
163+
{
164+
Name: "docker-client",
165+
Image: dockerclientimage,
166+
Command: []string{"/bin/bash"},
167+
Args: []string{"-c", "exec /usr/bin/docker image rm -f " + image + " > /dev/termination-log 2>&1"},
168+
VolumeMounts: []corev1.VolumeMount{
169+
{
170+
Name: "docker-sock",
171+
MountPath: "/var/run/docker.sock",
172+
},
173+
},
174+
ImagePullPolicy: corev1.PullIfNotPresent,
175+
},
176+
},
177+
Volumes: []corev1.Volume{
178+
{
179+
Name: "docker-sock",
180+
VolumeSource: corev1.VolumeSource{
181+
HostPath: &corev1.HostPathVolumeSource{
182+
Path: "/var/run/docker.sock",
183+
Type: &hostpathtype,
184+
},
185+
},
186+
},
187+
},
188+
},
189+
"containerd": {
190+
Containers: []corev1.Container{
191+
{
192+
Name: "crictl-client",
193+
Image: dockerclientimage,
194+
Command: []string{"/bin/bash"},
195+
Args: []string{"-c", "exec /usr/bin/crictl --runtime-endpoint=unix:///run/containerd/containerd.sock --image-endpoint=unix:///run/containerd/containerd.sock rmi " + image + " > /dev/termination-log 2>&1"},
196+
VolumeMounts: []corev1.VolumeMount{
197+
{
198+
Name: "containerd-sock",
199+
MountPath: "/run/containerd/containerd.sock",
200+
},
201+
},
202+
ImagePullPolicy: corev1.PullIfNotPresent,
203+
},
204+
},
205+
Volumes: []corev1.Volume{
206+
{
207+
Name: "containerd-sock",
208+
VolumeSource: corev1.VolumeSource{
209+
HostPath: &corev1.HostPathVolumeSource{
210+
Path: "/run/containerd/containerd.sock",
211+
Type: &hostpathtype,
212+
},
213+
},
214+
},
215+
},
216+
},
217+
}
218+
149219
job := &batchv1.Job{
150220
ObjectMeta: metav1.ObjectMeta{
151221
GenerateName: imagecache.Name + "-",
@@ -171,32 +241,8 @@ func newImageDeleteJob(imagecache *fledgedv1alpha1.ImageCache, image string, hos
171241
NodeSelector: map[string]string{
172242
"kubernetes.io/hostname": hostname,
173243
},
174-
Containers: []corev1.Container{
175-
{
176-
Name: "docker-client",
177-
Image: dockerclientimage,
178-
Command: []string{"/bin/bash"},
179-
Args: []string{"-c", "exec /usr/bin/docker image rm -f " + image + " > /dev/termination-log 2>&1"},
180-
VolumeMounts: []corev1.VolumeMount{
181-
{
182-
Name: "docker-sock",
183-
MountPath: "/var/run/docker.sock",
184-
},
185-
},
186-
ImagePullPolicy: corev1.PullIfNotPresent,
187-
},
188-
},
189-
Volumes: []corev1.Volume{
190-
{
191-
Name: "docker-sock",
192-
VolumeSource: corev1.VolumeSource{
193-
HostPath: &corev1.HostPathVolumeSource{
194-
Path: "/var/run/docker.sock",
195-
Type: &hostpathtype,
196-
},
197-
},
198-
},
199-
},
244+
Containers: containerSpec[containerRuntime].Containers,
245+
Volumes: containerSpec[containerRuntime].Volumes,
200246
RestartPolicy: corev1.RestartPolicyNever,
201247
ImagePullSecrets: imagecache.Spec.ImagePullSecrets,
202248
Tolerations: []corev1.Toleration{

pkg/images/image_manager.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ type ImageManager struct {
6969

7070
// ImageWorkRequest has image name, node name, work type and imagecache
7171
type ImageWorkRequest struct {
72-
Image string
73-
Node string
74-
WorkType WorkType
75-
Imagecache *fledgedv1alpha1.ImageCache
72+
Image string
73+
Node string
74+
ContainerRuntimeVersion string
75+
WorkType WorkType
76+
Imagecache *fledgedv1alpha1.ImageCache
7677
}
7778

7879
// ImageWorkResult stores the result of pulling and deleting image
@@ -166,9 +167,9 @@ func (m *ImageManager) handlePodStatusChange(pod *corev1.Pod) {
166167
if pod.Status.Phase == corev1.PodSucceeded {
167168
iwres.Status = ImageWorkResultStatusSucceeded
168169
if iwres.ImageWorkRequest.WorkType == ImageCachePurge {
169-
glog.Infof("Job %s succeeded (delete: %s --> %s)", pod.Labels["job-name"], iwres.ImageWorkRequest.Image, iwres.ImageWorkRequest.Node)
170+
glog.Infof("Job %s succeeded (delete:- %s --> %s, runtime: %s)", pod.Labels["job-name"], iwres.ImageWorkRequest.Image, iwres.ImageWorkRequest.Node, iwres.ImageWorkRequest.ContainerRuntimeVersion)
170171
} else {
171-
glog.Infof("Job %s succeeded (pull: %s --> %s)", pod.Labels["job-name"], iwres.ImageWorkRequest.Image, iwres.ImageWorkRequest.Node)
172+
glog.Infof("Job %s succeeded (pull:- %s --> %s, runtime: %s)", pod.Labels["job-name"], iwres.ImageWorkRequest.Image, iwres.ImageWorkRequest.Node, iwres.ImageWorkRequest.ContainerRuntimeVersion)
172173
}
173174
}
174175
if pod.Status.Phase == corev1.PodFailed {
@@ -397,13 +398,13 @@ func (m *ImageManager) processNextWorkItem() bool {
397398
if err != nil {
398399
return fmt.Errorf("error deleting image '%s' from node '%s': %s", iwr.Image, iwr.Node, err.Error())
399400
}
400-
glog.Infof("Job %s created (delete: %s --> %s)", job.Name, iwr.Image, iwr.Node)
401+
glog.Infof("Job %s created (delete:- %s --> %s, runtime: %s)", job.Name, iwr.Image, iwr.Node, iwr.ContainerRuntimeVersion)
401402
} else {
402403
job, err = m.pullImage(iwr)
403404
if err != nil {
404405
return fmt.Errorf("error pulling image '%s' to node '%s': %s", iwr.Image, iwr.Node, err.Error())
405406
}
406-
glog.Infof("Job %s created (pull: %s --> %s)", job.Name, iwr.Image, iwr.Node)
407+
glog.Infof("Job %s created (pull:- %s --> %s, runtime: %s)", job.Name, iwr.Image, iwr.Node, iwr.ContainerRuntimeVersion)
407408
}
408409
// Finally, if no error occurs we Forget this item so it does not
409410
// get queued again until another change happens.
@@ -442,12 +443,12 @@ func (m *ImageManager) pullImage(iwr ImageWorkRequest) (*batchv1.Job, error) {
442443
// deleteImage deletes the image from the node
443444
func (m *ImageManager) deleteImage(iwr ImageWorkRequest) (*batchv1.Job, error) {
444445
// Construct the Job manifest
445-
newjob, err := newImageDeleteJob(iwr.Imagecache, iwr.Image, iwr.Node, m.dockerClientImage)
446+
newjob, err := newImageDeleteJob(iwr.Imagecache, iwr.Image, iwr.Node, iwr.ContainerRuntimeVersion, m.dockerClientImage)
446447
if err != nil {
447448
glog.Errorf("Error when constructing job manifest: %v", err)
448449
return nil, err
449450
}
450-
// Create a Job to pull the image into the node
451+
// Create a Job to delete the image from the node
451452
job, err := m.kubeclientset.BatchV1().Jobs(fledgedNameSpace).Create(newjob)
452453
if err != nil {
453454
glog.Errorf("Error creating job in node %s: %v", iwr.Node, err)

0 commit comments

Comments
 (0)