Watching for deployments

This commit is contained in:
danielgormly
2019-08-23 17:40:46 +10:00
parent 1d00fee0c3
commit abbde42209
7 changed files with 47 additions and 12 deletions
+1
View File
@@ -41,3 +41,4 @@ See [example/Role.yaml](example/Role.yaml), [example/ServiceAccount.yaml](exampl
- [Installing kubernetes/client-go](https://github.com/kubernetes/client-go/blob/master/INSTALL.md)
- [Creating a Drone plugin in Go](https://docs.drone.io/plugins/examples/golang/)
- Testing with minikube (OSX: `brew cask install minikube`)
- [Client-go API Docs @ godoc.org](https://godoc.org/k8s.io/client-go/kubernetes)
+28 -1
View File
@@ -1,6 +1,10 @@
package main
import (
"fmt"
"log"
"strings"
appv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -11,10 +15,11 @@ import (
func CreateOrUpdateDeployment(clientset *kubernetes.Clientset, namespace string, deployment *appv1.Deployment) error {
deploymentExists, err := deploymentExists(clientset, namespace, deployment.Name)
if deploymentExists {
// log.Printf("📦 Found existing deployment. Updating.\n%s\n", depYaml)
log.Printf("📦 Found existing deployment. Updating.")
_, err = clientset.AppsV1().Deployments(namespace).Update(deployment)
return err
}
log.Printf("📦 Creating new deployment.")
_, err = clientset.AppsV1().Deployments(namespace).Create(deployment)
return err
}
@@ -32,3 +37,25 @@ func deploymentExists(clientset *kubernetes.Clientset, namespace string, name st
}
return true, nil
}
// waitUntilDeploymentSettled -- Waits until ready, failure or timeout
func waitUntilDeploymentSettled(clientset *kubernetes.Clientset, namespace string, name string, timeout int64) (state string, err error) {
fieldSelector := strings.Join([]string{"metadata.name", name}, "=")
watchOptions := meta.ListOptions{
FieldSelector: fieldSelector,
Watch: true,
}
watcher, error := clientset.AppsV1().Deployments(namespace).Watch(watchOptions)
liveDeployment, error := clientset.AppsV1().Deployments(namespace).Get(name, meta.GetOptions{})
log.Printf("📦 Unavailable replicas: %d", liveDeployment.Status.UnavailableReplicas)
if liveDeployment.Status.UnavailableReplicas == 0 {
return "ready", error
}
i := 0
for {
event := <-watcher.ResultChan()
fmt.Printf("%s\n\n", event.Object)
i++
}
return "failed", error
}
View File
Generated
+7 -7
View File
@@ -1,5 +1,5 @@
hash: 0a6476e347f6e80ed358c4dadb03f9a46c1acac36f211864199ff09a4325ec94
updated: 2019-05-29T12:43:58.994174+10:00
updated: 2019-08-23T12:18:51.611093+10:00
imports:
- name: github.com/aymerick/raymond
version: b565731e1464263de0bda75f2e45d97b54b60110
@@ -25,7 +25,7 @@ imports:
- ptypes/duration
- ptypes/timestamp
- name: github.com/google/gofuzz
version: 44d81051d367757e1c7c6a5a86423ece9afcf63c
version: 24818f796faf91cd76ec7bddd72458fbced7a6c1
- name: github.com/googleapis/gnostic
version: 0c5108395e2debce0d731cf0287ddf7242066aba
subpackages:
@@ -47,13 +47,13 @@ imports:
subpackages:
- ssh/terminal
- name: golang.org/x/net
version: 0ed95abb35c445290478a5348a7b38bb154135fd
version: 65e2d4e15006aab9813ff8769e768bbf4bb667a0
subpackages:
- context
- http/httpguts
- http2
- http2/hpack
- idna
- lex/httplex
- name: golang.org/x/oauth2
version: a6bd8cefa1811bd24b86f8902872e4e8225f74c4
subpackages:
@@ -78,7 +78,7 @@ imports:
subpackages:
- rate
- name: google.golang.org/appengine
version: 311d3c5cf9373249645db030e53c37c209a8b378
version: fb139bde60fa77cede04f226b4d5a3cf68dcce27
subpackages:
- internal
- internal/base
@@ -131,7 +131,7 @@ imports:
- storage/v1alpha1
- storage/v1beta1
- name: k8s.io/apimachinery
version: 2b1284ed4c93a43499e781493253e2ac5959c4fd
version: d7deff9243b165ee192f5551710ea4285dcfd615
subpackages:
- pkg/api/apitesting
- pkg/api/apitesting/fuzzer
@@ -237,7 +237,7 @@ imports:
- util/homedir
- util/keyutil
- name: k8s.io/klog
version: 8139d8cb77af419532b33dfa7dd09fbc5f1d344f
version: 8e90cee79f823779174776412c13478955131846
- name: k8s.io/utils
version: c2654d5206da6b7b6ace12841e8f359bb89b443c
subpackages:
+8
View File
@@ -85,6 +85,14 @@ func (p Plugin) Exec() error {
case *appv1.Deployment:
log.Print("📦 Resource type: Deployment")
err = CreateOrUpdateDeployment(clientset, p.KubeConfig.Namespace, o)
if err != nil {
return err
}
// Watch for successful update
log.Print("📦 Waiting for succesful update")
state, watchErr := waitUntilDeploymentSettled(clientset, p.KubeConfig.Namespace, o.ObjectMeta.Name, 120)
log.Printf("%s", state)
return watchErr
case *corev1.ConfigMap:
log.Print("📦 Resource type: ConfigMap")
err = ApplyConfigMapFromFile(clientset, p.KubeConfig.Namespace, o, p.ConfigMapFile)
+2 -3
View File
@@ -1,8 +1,7 @@
#!/bin/bash
PLUGIN_TEMPLATE=test/deployment.template.yaml
PLUGIN_NAME=drone-kube-test
PLUGIN_COMMIT=a5b81d0f
export PLUGIN_TEMPLATE=test/deployment.template.yaml
export PLUGIN_NAME=drone-kube-test
go build -o build/kubano
export $(cat .env | xargs) && ./build/kubano
+1 -1
View File
@@ -14,7 +14,7 @@ spec:
spec:
containers:
- name: nginx
image: 10.0.0.24:443/test:example.{{commit}}
image: nginx:alpine
ports:
- containerPort: 80
imagePullSecrets: