/
handler_deployment.go
107 lines (96 loc) · 3.5 KB
/
handler_deployment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package handlers
import (
"encoding/json"
"errors"
"strings"
log "github.com/Sirupsen/logrus"
v1beta2 "k8s.io/api/apps/v1beta2"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
cache "k8s.io/client-go/tools/cache"
//metav1 "k8s.io/apimachinery/pkg/resources/meta/v1"
)
// DeploymentHandler is a sample implementation of Handler
type DeploymentHandler struct{}
// GetDeploymentInformer get index Informer to watch Deployment
func GetDeploymentInformer(client kubernetes.Interface) cache.SharedIndexInformer {
informer := cache.NewSharedIndexInformer(
// the ListWatch contains two different functions that our
// informer requires: ListFunc to take care of listing and watching
// the resources we want to handle
&cache.ListWatch{
ListFunc: func(options v1.ListOptions) (runtime.Object, error) {
// list all of the deployments (AppsV1beta2 resource) in the deafult namespace
return client.AppsV1beta2().Deployments(AppNamespace).List(options)
},
WatchFunc: func(options v1.ListOptions) (watch.Interface, error) {
// watch all of the deployments (AppsV1beta2 resource) in the default namespace
return client.AppsV1beta2().Deployments(AppNamespace).Watch(options)
},
},
&v1beta2.Deployment{}, // the target type (Pod)
0, // no resync (period of 0)
cache.Indexers{},
)
return informer
}
// Init handles any handler initialization
// a method of DeploymentHandler returns type error
// func (<object>) <name>(<params>) <return>
func (t *DeploymentHandler) Init() error {
log.Info("DeploymentHandler.Init")
return nil
}
// ValidateDeployment to check deployment required attributes
func ValidateDeployment(deployment *v1beta2.Deployment) bool {
if deployment.ObjectMeta.Name == "" {
return false
}
if deployment.ObjectMeta.Namespace == "" {
return false
}
if deployment.ResourceVersion == "" {
return false
}
return true
}
// ObjectCreated is called when an object is created
func (t *DeploymentHandler) ObjectCreated(obj interface{}) error {
log.Info("DeploymentHandler.ObjectCreated")
// assert the type to a Pod object to pull out relevant data
deployment := obj.(*v1beta2.Deployment)
defer func() {
if r := recover(); r != nil {
t.ObjectUpdated(obj, obj)
return
}
}()
if !ValidateDeployment(deployment) {
return errors.New("Could not validate deployment object " + deployment.ObjectMeta.Name)
}
j, err := json.MarshalIndent(deployment, "", " ")
if err != nil {
log.Error(err)
}
log.Debugf(" Deployment: %s, \n", j)
SendJSONQueryWithRetries(deployment, RestSvcEndpoint+"v1.1/entity?objtype=deployment")
return nil
}
// ObjectDeleted is called when an object is deleted
func (t *DeploymentHandler) ObjectDeleted(obj interface{}, key string) error {
log.Info("DeploymentHandler.ObjectDeleted")
SendDeleteRequest(RestSvcEndpoint + "v1/entity/deployment/deployment:" + ClusterName + ":" + strings.Replace(key, "/", ":", -1))
return nil
}
// ObjectUpdated is called when an object is updated
func (t *DeploymentHandler) ObjectUpdated(objOld, objNew interface{}) error {
log.Info("DeploymentHandler.ObjectUpdated")
return nil
}
// DeploymentSynchronize sync all Deployments periodically in case missing events
func DeploymentSynchronize(client kubernetes.Interface) {
clusterdeploymentslist, _ := client.AppsV1beta2().Deployments(AppNamespace).List(v1.ListOptions{})
SendJSONQueryWithRetries(clusterdeploymentslist.Items, RestSvcEndpoint+"v1/sync/deployment")
}