forked from linkerd/linkerd2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
178 lines (150 loc) · 4.66 KB
/
controller.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
package ca
import (
"fmt"
"strings"
"time"
"github.com/linkerd/linkerd2/controller/k8s"
pkgK8s "github.com/linkerd/linkerd2/pkg/k8s"
log "github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
)
type CertificateController struct {
namespace string
k8sAPI *k8s.API
ca *CA
syncHandler func(key string) error
// The queue is keyed on a string. If the string doesn't contain any dots
// then it is a namespace name and the task is to create the CA bundle
// configmap in that namespace. Otherwise the string must be of the form
// "$podOwner.$podKind.$podNamespace" and the task is to create the secret
// for that pod owner.
queue workqueue.RateLimitingInterface
}
func NewCertificateController(controllerNamespace string, k8sAPI *k8s.API) (*CertificateController, error) {
ca, err := NewCA()
if err != nil {
return nil, err
}
c := &CertificateController{
namespace: controllerNamespace,
k8sAPI: k8sAPI,
ca: ca,
queue: workqueue.NewNamedRateLimitingQueue(
workqueue.DefaultControllerRateLimiter(), "certificates"),
}
k8sAPI.Pod().Informer().AddEventHandler(
cache.ResourceEventHandlerFuncs{
AddFunc: c.handlePodAdd,
UpdateFunc: c.handlePodUpdate,
},
)
c.syncHandler = c.syncObject
return c, nil
}
func (c *CertificateController) Run(readyCh <-chan struct{}, stopCh <-chan struct{}) {
defer runtime.HandleCrash()
defer c.queue.ShutDown()
<-readyCh
log.Info("starting certificate controller")
defer log.Info("shutting down certificate controller")
go wait.Until(c.worker, time.Second, stopCh)
<-stopCh
}
func (c *CertificateController) worker() {
for c.processNextWorkItem() {
}
}
func (c *CertificateController) processNextWorkItem() bool {
key, quit := c.queue.Get()
if quit {
return false
}
defer c.queue.Done(key)
err := c.syncHandler(key.(string))
if err != nil {
log.Errorf("error syncing object: %s", err)
c.queue.AddRateLimited(key)
return true
}
c.queue.Forget(key)
return true
}
func (c *CertificateController) syncObject(key string) error {
log.Debugf("syncObject(%s)", key)
if !strings.Contains(key, ".") {
return c.syncNamespace(key)
}
return c.syncSecret(key)
}
func (c *CertificateController) syncNamespace(ns string) error {
log.Debugf("syncNamespace(%s)", ns)
configMap := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: pkgK8s.TLSTrustAnchorConfigMapName},
Data: map[string]string{
pkgK8s.TLSTrustAnchorFileName: c.ca.TrustAnchorPEM(),
},
}
log.Debugf("adding configmap [%s] to namespace [%s]",
pkgK8s.TLSTrustAnchorConfigMapName, ns)
_, err := c.k8sAPI.Client.CoreV1().ConfigMaps(ns).Create(configMap)
if apierrors.IsAlreadyExists(err) {
_, err = c.k8sAPI.Client.CoreV1().ConfigMaps(ns).Update(configMap)
}
return err
}
func (c *CertificateController) syncSecret(key string) error {
log.Debugf("syncSecret(%s)", key)
parts := strings.Split(key, ".")
if len(parts) != 3 {
log.Errorf("Failed to parse secret sync request %s", key)
return nil // TODO
}
identity := pkgK8s.TLSIdentity{
Name: parts[0],
Kind: parts[1],
Namespace: parts[2],
ControllerNamespace: c.namespace,
}
dnsName := identity.ToDNSName()
secretName := identity.ToSecretName()
certAndPrivateKey, err := c.ca.IssueEndEntityCertificate(dnsName)
if err != nil {
log.Errorf("Failed to issue certificate for %s", dnsName)
return err
}
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: secretName},
Data: map[string][]byte{
pkgK8s.TLSCertFileName: certAndPrivateKey.Certificate,
pkgK8s.TLSPrivateKeyFileName: certAndPrivateKey.PrivateKey,
},
}
_, err = c.k8sAPI.Client.CoreV1().Secrets(identity.Namespace).Create(secret)
if apierrors.IsAlreadyExists(err) {
_, err = c.k8sAPI.Client.CoreV1().Secrets(identity.Namespace).Update(secret)
}
return err
}
func (c *CertificateController) handlePodAdd(obj interface{}) {
pod := obj.(*v1.Pod)
if c.isInjectedPod(pod) {
log.Debugf("enqueuing update of CA bundle configmap in %s", pod.Namespace)
c.queue.Add(pod.Namespace)
ownerKind, ownerName := c.k8sAPI.GetOwnerKindAndName(pod)
item := fmt.Sprintf("%s.%s.%s", ownerName, ownerKind, pod.Namespace)
log.Debugf("enqueuing secret write for %s", item)
c.queue.Add(item)
}
}
func (c *CertificateController) handlePodUpdate(oldObj, newObj interface{}) {
c.handlePodAdd(newObj)
}
func (c *CertificateController) isInjectedPod(pod *v1.Pod) bool {
return pkgK8s.GetControllerNs(pod) == c.namespace
}