/
cache.go
183 lines (164 loc) · 5.04 KB
/
cache.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2017 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kubernetesenv
import (
"errors"
"fmt"
"strings"
"time"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"istio.io/istio/mixer/pkg/adapter"
)
type (
// internal interface used to support testing
cacheController interface {
Run(<-chan struct{})
Pod(string) (*v1.Pod, bool)
Workload(*v1.Pod) workload
HasSynced() bool
StopControlChannel()
}
controllerImpl struct {
env adapter.Env
stopChan chan struct{}
pods cache.SharedIndexInformer
rs cache.SharedIndexInformer
rc cache.SharedIndexInformer
}
workload struct {
uid, name, namespace string
selfLinkURL string
}
)
func podIP(obj interface{}) ([]string, error) {
pod, ok := obj.(*v1.Pod)
if !ok {
return nil, errors.New("object is not a pod")
}
ip := pod.Status.PodIP
if ip == "" {
return nil, nil
}
return []string{ip}, nil
}
// Responsible for setting up the cacheController, based on the supplied client.
// It configures the index informer to list/watch k8sCache and send update events
// to a mutations channel for processing (in this case, logging).
func newCacheController(clientset kubernetes.Interface, refreshDuration time.Duration, env adapter.Env, stopChan chan struct{}) cacheController {
sharedInformers := informers.NewSharedInformerFactory(clientset, refreshDuration)
podInformer := sharedInformers.Core().V1().Pods().Informer()
podInformer.AddIndexers(cache.Indexers{
"ip": podIP,
})
return &controllerImpl{
env: env,
stopChan: stopChan,
pods: podInformer,
rs: sharedInformers.Apps().V1().ReplicaSets().Informer(),
rc: sharedInformers.Core().V1().ReplicationControllers().Informer(),
}
}
func (c *controllerImpl) StopControlChannel() {
close(c.stopChan)
}
func (c *controllerImpl) HasSynced() bool {
if c.pods.HasSynced() && c.rs.HasSynced() && c.rc.HasSynced() {
return true
}
return false
}
func (c *controllerImpl) Run(stop <-chan struct{}) {
// TODO: scheduledaemon
c.env.ScheduleDaemon(func() { c.pods.Run(stop) })
c.env.ScheduleDaemon(func() { c.rs.Run(stop) })
c.env.ScheduleDaemon(func() { c.rc.Run(stop) })
<-stop
// TODO: logging?
}
// Pod returns a k8s Pod object that corresponds to the supplied key, if one
// exists (and is known to the store). Keys are expected in the form of:
// namespace/name or IP address (example: "default/curl-2421989462-b2g2d.default").
func (c *controllerImpl) Pod(podKey string) (*v1.Pod, bool) {
indexer := c.pods.GetIndexer()
objs, err := indexer.ByIndex("ip", podKey)
if err != nil {
return nil, false
}
if len(objs) > 0 {
pod, ok := objs[0].(*v1.Pod)
if !ok {
return nil, false
}
return pod, true
}
item, exists, err := indexer.GetByKey(podKey)
if !exists || err != nil {
return nil, false
}
return item.(*v1.Pod), true
}
func key(namespace, name string) string {
return namespace + "/" + name
}
func (c *controllerImpl) Workload(pod *v1.Pod) workload {
wl := workload{name: pod.Name, namespace: pod.Namespace, selfLinkURL: pod.SelfLink}
if owner, found := c.rootController(&pod.ObjectMeta); found {
wl.name = owner.Name
wl.selfLinkURL = fmt.Sprintf("kubernetes://apis/%s/namespaces/%s/%ss/%s", owner.APIVersion, pod.Namespace, strings.ToLower(owner.Kind), owner.Name)
}
wl.uid = "istio://" + wl.namespace + "/workloads/" + wl.name
return wl
}
func (c *controllerImpl) rootController(obj *metav1.ObjectMeta) (metav1.OwnerReference, bool) {
for _, ref := range obj.OwnerReferences {
if *ref.Controller {
switch ref.Kind {
case "ReplicaSet":
indexer := c.rs.GetIndexer()
if rs, found := c.objectMeta(indexer, key(obj.Namespace, ref.Name)); found {
if rootRef, ok := c.rootController(rs); ok {
return rootRef, true
}
}
case "ReplicationController":
indexer := c.rc.GetIndexer()
if rc, found := c.objectMeta(indexer, key(obj.Namespace, ref.Name)); found {
if rootRef, ok := c.rootController(rc); ok {
return rootRef, true
}
}
}
return ref, true
}
}
return metav1.OwnerReference{}, false
}
func (c *controllerImpl) objectMeta(keyGetter cache.KeyGetter, key string) (*metav1.ObjectMeta, bool) {
item, exists, err := keyGetter.GetByKey(key)
if !exists || err != nil {
return nil, false
}
switch v := item.(type) {
case *v1.ReplicationController:
return &v.ObjectMeta, true
case *appsv1.ReplicaSet:
return &v.ObjectMeta, true
}
return nil, false
}