Skip to content

Commit

Permalink
Merge pull request #51 from senthilrch/senthilrch
Browse files Browse the repository at this point in the history
PR for v0.5.0 release
  • Loading branch information
senthilrch authored Jan 31, 2020
2 parents 93dae8f + 6bb886b commit 22f0a75
Show file tree
Hide file tree
Showing 5,797 changed files with 798 additions and 2,369,389 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
549 changes: 0 additions & 549 deletions Gopkg.lock

This file was deleted.

37 changes: 0 additions & 37 deletions Gopkg.toml

This file was deleted.

9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

.PHONY: clean clean-fledged clean-client fledged-image client-image push-images test deploy update
.PHONY: clean clean-fledged clean-client fledged-image client-image push-images test deploy update remove
# Default tag and architecture. Can be overridden
TAG?=$(shell git describe --tags --dirty)
ARCH?=amd64
Expand Down Expand Up @@ -108,3 +108,10 @@ update:
kubectl scale deployment fledged --replicas=0 -n kube-fledged && sleep 5 && \
kubectl scale deployment fledged --replicas=1 -n kube-fledged && sleep 5 && \
kubectl get pods -l app=fledged -n kube-fledged

remove:
kubectl delete -f deploy/fledged-namespace.yaml && \
kubectl delete -f deploy/fledged-clusterrolebinding.yaml && \
kubectl delete -f deploy/fledged-clusterrole.yaml && \
kubectl delete -f deploy/fledged-crd.yaml

155 changes: 92 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
[![Build Status](https://travis-ci.org/senthilrch/kube-fledged.svg?branch=master)](https://travis-ci.org/senthilrch/kube-fledged)
[![Coverage Status](https://coveralls.io/repos/github/senthilrch/kube-fledged/badge.svg?branch=master)](https://coveralls.io/github/senthilrch/kube-fledged?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/senthilrch/kube-fledged)](https://goreportcard.com/report/github.com/senthilrch/kube-fledged)
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/senthilrch/kube-fledged)](https://img.shields.io/github/v/release/senthilrch/kube-fledged)
[![License](https://img.shields.io/github/license/senthilrch/kube-fledged)](https://img.shields.io/github/license/senthilrch/kube-fledged)

**_kube-fledged_** is a kubernetes add-on for creating and managing a cache of container images directly on the worker nodes of a kubernetes cluster. It allows a user to define a list
of images and onto which worker nodes those images should be cached (i.e. pre-pulled). As a result, application pods start almost instantly, since the images need not be pulled from the registry.
Expand All @@ -16,92 +18,103 @@ _kube-fledged_ provides CRUD APIs to manage the lifecycle of the image cache, an
- IoT applications that run on Edge devices when the network connectivity between the edge and image registry is intermittent.
- If a cluster administrator or operator needs to roll-out upgrades to an application and wants to verify before-hand if the new images can be pulled successfully.

## Build and Deploy
## Prerequisites

These instructions will help you build _kube-fledged_ from source and deploy it on a kubernetes cluster.
- A functioning kubernetes cluster (v1.9 or above). It could be a simple development cluster like minikube or a large production cluster.
- All master and worker nodes having the ["kubernetes.io/hostname"](https://kubernetes.io/docs/reference/kubernetes-api/labels-annotations-taints/#kubernetes-io-hostname) label.
- Supported container runtimes: docker, containerd, cri-o
- git, make, go, docker and kubectl installed on a local linux machine. kubectl configured properly to access the cluster.

### Prerequisites
## Quick Install

- A functioning kubernetes cluster (v1.7 or above). It could be a simple development cluster like minikube or a large production cluster.
- All master and worker nodes having the ["kubernetes.io/hostname"](https://kubernetes.io/docs/reference/kubernetes-api/labels-annotations-taints/#kubernetes-io-hostname) label.
- Supported container runtimes: docker, containerd
- make, go, docker and kubectl installed on a local linux machine. kubectl configured properly to access the cluster.
These instructions install _kube-fledged_ using pre-built images of the latest stable release in [Docker Hub.](https://hub.docker.com/u/senthilrch)

### Build
- Clone the source code repository

Create the source code directories on local linux machine and setup $GOPATH
```
$ mkdir -p $HOME/src/github.com/senthilrch
$ git clone https://github.com/senthilrch/kube-fledged.git $HOME/src/github.com/senthilrch/kube-fledged
$ cd $HOME/src/github.com/senthilrch/kube-fledged
```

```
$ mkdir -p $HOME/src/github.com/senthilrch
$ export GOPATH=$HOME
```
- Deploy _kube-fledged_ to the cluster

Clone the repository
```
$ make deploy
```

```
$ git clone https://github.com/senthilrch/kube-fledged.git $HOME/src/github.com/senthilrch/kube-fledged
$ cd $HOME/src/github.com/senthilrch/kube-fledged
```
- Verify if _kube-fledged_ deployed successfully

If you are behind a proxy, export the following ENV variables (UPPER case)
```
$ kubectl get pods -n kube-fledged -l app=fledged
$ kubectl logs -f <pod_name_obtained_from_above_command> -n kube-fledged
$ kubectl get imagecaches -n kube-fledged (Output should be: 'No resources found')
```

```
export HTTP_PROXY=http://proxy_ip_or_hostname:port
export HTTPS_PROXY=https://proxy_ip_or_hostname:port
```
## Build and Deploy

Build and push the docker images to registry (e.g. Docker hub)
These instructions will help you build _kube-fledged_ from source and deploy it on a kubernetes cluster.

```
$ export FLEDGED_IMAGE_NAME=<your_docker_hub_username>/fledged:<your_tag>
$ export FLEDGED_DOCKER_CLIENT_IMAGE_NAME=<your_docker_hub_username>/fledged-docker-client:<your_tag>
$ export DOCKER_VERSION=<docker_version_used_for_building_docker_client_image>
$ docker login -u <username> -p <password>
$ make fledged-image && make client-image && make push-image
```
### Build

- Clone the source code repository

```
$ mkdir -p $HOME/src/github.com/senthilrch
$ git clone https://github.com/senthilrch/kube-fledged.git $HOME/src/github.com/senthilrch/kube-fledged
$ cd $HOME/src/github.com/senthilrch/kube-fledged
```

- If you are behind a proxy, export the following ENV variables (UPPER case)

```
export HTTP_PROXY=http://proxy_ip_or_hostname:port
export HTTPS_PROXY=https://proxy_ip_or_hostname:port
```

- Build and push the docker images to registry (e.g. Docker hub)

```
$ export FLEDGED_IMAGE_NAME=<your_docker_hub_username>/fledged:<your_tag>
$ export FLEDGED_DOCKER_CLIENT_IMAGE_NAME=<your_docker_hub_username>/fledged-docker-client:<your_tag>
$ export DOCKER_VERSION=<docker_version_used_for_building_docker_client_image>
$ docker login -u <username> -p <password>
$ make fledged-image && make client-image && make push-image
```

### Deploy

_Note:- You need to have 'cluster-admin' privileges to deploy_

All manifests required for deploying _kube-fledged_ are present inside 'kube-fledged/deploy'. These steps deploy _kube-fledged_ into a separate namespace called "kube-fledged" with default configuration flags.
- All manifests required for deploying _kube-fledged_ are present inside 'kube-fledged/deploy'. These steps deploy _kube-fledged_ into a separate namespace called "kube-fledged" with default configuration flags. Edit "fledged-deployment.yaml".

Edit "fledged-deployment.yaml":-
Set "image" to "<your_docker_hub_username>/fledged:<your_tag>"

- Set the value of KUBERNETES_SERVICE_HOST to the IP/hostname of api server of the cluster
- Set KUBERNETES_SERVICE_PORT to port number of api server
- Set "image" to "<your_docker_hub_username>/fledged:<your_tag>"
```
image: <your_docker_hub_username>/fledged:<your_tag>
```

```
- env:
- name: KUBERNETES_SERVICE_HOST
value: "<IP address of api server>"
- name: KUBERNETES_SERVICE_PORT
value: "<port number of api server>"
image: <your_docker_hub_username>/fledged:<your_tag>
```
- If you pushed the image to a private repository, add 'imagePullSecrets' to the end of "fledged-deployment.yaml". Refer to kubernetes documentation on [Specifying ImagePullSecrets on a Pod](https://kubernetes.io/docs/concepts/containers/images/#specifying-imagepullsecrets-on-a-pod). The secret <your_registry_key> should be created in "kube-fledged" namespace.

If you pushed the image to a private repository, add 'imagePullSecrets' to the end of "fledged-deployment.yaml". Refer to kubernetes documentation on [Specifying ImagePullSecrets on a Pod](https://kubernetes.io/docs/concepts/containers/images/#specifying-imagepullsecrets-on-a-pod). The secret <your_registry_key> should be created in "kube-fledged" namespace.
```
serviceAccountName: fledged
imagePullSecrets:
- name: <your_registry_key>
```

```
serviceAccountName: fledged
imagePullSecrets:
- name: <your_registry_key>
```
- Deploy _kube-fledged_ to the cluster

Deploy _kube-fledged_ to the cluster
```
$ make deploy
```

```
$ make deploy
```
- Verify if _kube-fledged_ deployed successfully

Verify if _kube-fledged_ deployed successfully

```
$ kubectl get pods -n kube-fledged -l app=fledged
$ kubectl logs -f <pod_name_obtained_from_above_command> -n kube-fledged
$ kubectl get imagecaches -n kube-fledged (Output should be: 'No resources found')
```
```
$ kubectl get pods -n kube-fledged -l app=fledged
$ kubectl logs -f <pod_name_obtained_from_above_command> -n kube-fledged
$ kubectl get imagecaches -n kube-fledged (Output should be: 'No resources found')
```

## How to use

Expand Down Expand Up @@ -140,6 +153,14 @@ $ kubectl edit imagecaches imagecache1 -n kube-fledged
$ kubectl get imagecaches imagecache1 -n kube-fledged -o json
```

### Refresh image cache

_kube-fledged_ supports both automatic and on-demand refresh of image cache. Auto refresh is enabled using the flag `--image-cache-refresh-frequency:`. To request for an on-demand refresh, run the following command:-

```
$ kubectl annotate imagecaches imagecache1 -n kube-fledged fledged.k8s.io/refresh-imagecache=
```

### Delete image cache

Before you could delete the image cache, you need to purge the images in the cache using the following command. This will remove all cached images from the worker nodes.
Expand All @@ -151,7 +172,7 @@ $ kubectl annotate imagecaches imagecache1 -n kube-fledged fledged.k8s.io/purge-
View the status of purging the image cache. If any failures, such images should be removed manually or you could decide to leave the images in the worker nodes.

```
kubectl get imagecaches imagecache1 -n kube-fledged -o json
$ kubectl get imagecaches imagecache1 -n kube-fledged -o json
```

Finally delete the image cache using following command.
Expand All @@ -160,6 +181,14 @@ Finally delete the image cache using following command.
$ kubectl delete imagecaches imagecache1 -n kube-fledged
```

### Remove kube-fledged

Run the following command to remove _kube-fledged_ from the cluster.

```
$ make remove
```

## How it works

Kubernetes allows developers to extend the kubernetes api via [Custom Resources](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/). _kube-fledged_ defines a custom resource of kind “ImageCache” and implements a custom controller (named _fledged_). _fledged_ does the heavy-lifting for managing image cache. Users can use kubectl commands for creation and deletion of ImageCache resources.
Expand Down Expand Up @@ -189,7 +218,7 @@ For more detailed description, go through _kube-fledged's_ [design proposal](doc
## Built With

* [kubernetes/sample-controller](https://github.com/kubernetes/sample-controller) - Building our own kubernetes-style controller using CRD.
* [Dep](https://github.com/golang/dep) - Go dependency management tool
* [Go Modules](https://golang.org/doc/go1.11#modules) - Go Modules for Dependency Management
* [Make](https://www.gnu.org/software/make/) - GNU Make


Expand Down
35 changes: 23 additions & 12 deletions cmd/app/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ import (

const controllerAgentName = "fledged"
const fledgedNameSpace = "kube-fledged"
const fledgedFinalizer = "fledged"
const fledgedCacheSpecValidationKey = "fledged.k8s.io/cachespecvalidation"
const imageCachePurgeAnnotationKey = "fledged.k8s.io/purge-imagecache"
const imageCacheRefreshAnnotationKey = "fledged.k8s.io/refresh-imagecache"

const (
// SuccessSynced is used as part of the Event 'reason' when a ImageCache is synced
Expand Down Expand Up @@ -185,7 +185,7 @@ func (c *Controller) danglingImageCaches() error {
return nil
}
status := &fledgedv1alpha1.ImageCacheStatus{
Failures: map[string][]fledgedv1alpha1.NodeReasonMessage{},
Failures: map[string]fledgedv1alpha1.NodeReasonMessageList{},
Status: fledgedv1alpha1.ImageCacheActionStatusAborted,
Reason: fledgedv1alpha1.ImageCacheReasonImagePullAborted,
Message: fledgedv1alpha1.ImageCacheMessageImagePullAborted,
Expand Down Expand Up @@ -284,6 +284,12 @@ func (c *Controller) enqueueImageCache(workType images.WorkType, old, new interf
break
}
}
if _, exists := newImageCache.Annotations[imageCacheRefreshAnnotationKey]; exists {
if _, exists := oldImageCache.Annotations[imageCacheRefreshAnnotationKey]; !exists {
workType = images.ImageCacheRefresh
break
}
}
if !reflect.DeepEqual(newImageCache.Spec, oldImageCache.Spec) {
if validation, ok := newImageCache.Annotations[fledgedCacheSpecValidationKey]; ok {
if validation == "failed" {
Expand Down Expand Up @@ -404,8 +410,8 @@ func (c *Controller) runRefreshWorker() {
imageCaches[i].Status.Reason == fledgedv1alpha1.ImageCacheReasonCacheSpecValidationFailed {
continue
}
// Do not refresh image cache marked for deletion
if imageCaches[i].DeletionTimestamp != nil {
// Do not refresh if image cache has been purged
if imageCaches[i].Status.Reason == fledgedv1alpha1.ImageCacheReasonImageCachePurge {
continue
}
c.enqueueImageCache(images.ImageCacheRefresh, imageCaches[i], nil)
Expand All @@ -417,7 +423,7 @@ func (c *Controller) runRefreshWorker() {
// with the current status of the resource.
func (c *Controller) syncHandler(wqKey images.WorkQueueKey) error {
status := &fledgedv1alpha1.ImageCacheStatus{
Failures: map[string][]fledgedv1alpha1.NodeReasonMessage{},
Failures: map[string]fledgedv1alpha1.NodeReasonMessageList{},
}

// Convert the namespace/name string into a distinct namespace and name
Expand Down Expand Up @@ -646,15 +652,23 @@ func (c *Controller) syncHandler(wqKey images.WorkQueueKey) error {
return err
}

if imageCache.Status.Reason == fledgedv1alpha1.ImageCacheReasonImageCachePurge {
if imageCache.Status.Reason == fledgedv1alpha1.ImageCacheReasonImageCachePurge || imageCache.Status.Reason == fledgedv1alpha1.ImageCacheReasonImageCacheRefresh {
imageCache, err := c.fledgedclientset.FledgedV1alpha1().ImageCaches(namespace).Get(name, metav1.GetOptions{})
if err != nil {
glog.Errorf("Error getting image cache %s: %v", name, err)
return err
}
if err := c.removeAnnotation(imageCache, imageCachePurgeAnnotationKey); err != nil {
glog.Errorf("Error removing Annotation %s from imagecache(%s): %v", imageCachePurgeAnnotationKey, imageCache.Name, err)
return err
if imageCache.Status.Reason == fledgedv1alpha1.ImageCacheReasonImageCachePurge {
if err := c.removeAnnotation(imageCache, imageCachePurgeAnnotationKey); err != nil {
glog.Errorf("Error removing Annotation %s from imagecache(%s): %v", imageCachePurgeAnnotationKey, imageCache.Name, err)
return err
}
}
if imageCache.Status.Reason == fledgedv1alpha1.ImageCacheReasonImageCacheRefresh {
if err := c.removeAnnotation(imageCache, imageCacheRefreshAnnotationKey); err != nil {
glog.Errorf("Error removing Annotation %s from imagecache(%s): %v", imageCacheRefreshAnnotationKey, imageCache.Name, err)
return err
}
}
}

Expand All @@ -665,9 +679,6 @@ func (c *Controller) syncHandler(wqKey images.WorkQueueKey) error {
if status.Status == fledgedv1alpha1.ImageCacheActionStatusFailed {
c.recorder.Event(imageCache, corev1.EventTypeWarning, status.Reason, status.Message)
}

case images.ImageCacheDelete:
break
}
glog.Infof("Completed sync actions for image cache %s(%s)", name, wqKey.WorkType)
return nil
Expand Down
Loading

0 comments on commit 22f0a75

Please sign in to comment.