/
apisix_tls.go
254 lines (234 loc) · 7.61 KB
/
apisix_tls.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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 controller
import (
"fmt"
"sync"
"time"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"github.com/apache/apisix-ingress-controller/pkg/ingress/apisix"
configv1 "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v1"
clientset "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/client/clientset/versioned"
apisixscheme "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/client/clientset/versioned/scheme"
informersv1 "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/client/informers/externalversions/config/v1"
listersv1 "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/client/listers/config/v1"
"github.com/apache/apisix-ingress-controller/pkg/log"
"github.com/apache/apisix-ingress-controller/pkg/seven/state"
v1 "github.com/apache/apisix-ingress-controller/pkg/types/apisix/v1"
)
var (
// the struct of secretSSLMap is a map[secretKey string]map[sslKey string]bool
// the xxxKey is format as namespace + "/" + name
secretSSLMap = sync.Map{}
)
type ApisixTLSController struct {
controller *Controller
kubeclientset kubernetes.Interface
apisixClientset clientset.Interface
apisixTLSList listersv1.ApisixTlsLister
apisixTLSSynced cache.InformerSynced
workqueue workqueue.RateLimitingInterface
}
type TlsQueueObj struct {
Key string `json:"key"`
OldObj *configv1.ApisixTls `json:"old_obj"`
Ope string `json:"ope"` // add / update / delete
}
func BuildApisixTlsController(
kubeclientset kubernetes.Interface,
apisixTLSClientset clientset.Interface,
apisixTLSInformer informersv1.ApisixTlsInformer,
root *Controller) *ApisixTLSController {
runtime.Must(apisixscheme.AddToScheme(scheme.Scheme))
controller := &ApisixTLSController{
controller: root,
kubeclientset: kubeclientset,
apisixClientset: apisixTLSClientset,
apisixTLSList: apisixTLSInformer.Lister(),
apisixTLSSynced: apisixTLSInformer.Informer().HasSynced,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.NewItemFastSlowRateLimiter(1*time.Second, 60*time.Second, 5), "ApisixTlses"),
}
apisixTLSInformer.Informer().AddEventHandler(
cache.ResourceEventHandlerFuncs{
AddFunc: controller.addFunc,
UpdateFunc: controller.updateFunc,
DeleteFunc: controller.deleteFunc,
})
return controller
}
func (c *ApisixTLSController) Run(stop <-chan struct{}) error {
if ok := cache.WaitForCacheSync(stop); !ok {
log.Errorf("sync ApisixTLS cache failed")
return fmt.Errorf("failed to wait for caches to sync")
}
go wait.Until(c.runWorker, time.Second, stop)
return nil
}
func (c *ApisixTLSController) runWorker() {
for c.processNextWorkItem() {
}
}
func (c *ApisixTLSController) processNextWorkItem() bool {
defer recoverException()
obj, shutdown := c.workqueue.Get()
if shutdown {
return false
}
err := func(obj interface{}) error {
defer c.workqueue.Done(obj)
var key string
var ok bool
var tqo *TlsQueueObj
if tqo, ok = obj.(*TlsQueueObj); !ok {
c.workqueue.Forget(obj)
return fmt.Errorf("expected TlsQueueObj in workqueue but got %#v", obj)
}
if err := c.syncHandler(tqo); err != nil {
c.workqueue.AddRateLimited(tqo)
log.Errorf("sync tls %s failed", tqo.Key)
return fmt.Errorf("error syncing '%s': %s", key, err.Error())
}
c.workqueue.Forget(obj)
return nil
}(obj)
if err != nil {
runtime.HandleError(err)
}
return true
}
func (c *ApisixTLSController) syncHandler(tqo *TlsQueueObj) error {
namespace, name, err := cache.SplitMetaNamespaceKey(tqo.Key)
if err != nil {
log.Errorf("invalid resource key: %s", tqo.Key)
return fmt.Errorf("invalid resource key: %s", tqo.Key)
}
apisixTlsYaml := tqo.OldObj
if tqo.Ope == state.Delete {
apisixIngressTls, _ := c.apisixTLSList.ApisixTlses(namespace).Get(name)
if apisixIngressTls != nil && apisixIngressTls.ResourceVersion > tqo.OldObj.ResourceVersion {
log.Warnf("TLS %s has been covered when retry", tqo.Key)
return nil
}
} else {
apisixTlsYaml, err = c.apisixTLSList.ApisixTlses(namespace).Get(name)
if err != nil {
if errors.IsNotFound(err) {
log.Infof("apisixTls %s is removed", tqo.Key)
return nil
}
runtime.HandleError(fmt.Errorf("failed to list apisixTls %s/%s", tqo.Key, err.Error()))
return err
}
}
apisixTls := apisix.ApisixTLSCRD(*apisixTlsYaml)
sc := &apisix.SecretClient{}
if tls, err := apisixTls.Convert(sc); err != nil {
return err
} else {
// sync to apisix
log.Debug(tls)
log.Debug(tqo)
err = state.SyncSsl(tls, tqo.Ope)
// sync SyncSecretSSL
secretKey := fmt.Sprintf("%s_%s", apisixTls.Spec.Secret.Namespace, apisixTls.Spec.Secret.Name)
SyncSecretSSL(secretKey, tls, tqo.Ope)
return err
}
}
// SyncSecretSSL sync the secretSSLMap
// the struct of secretSSLMap is a map[secretKey string]map[sslKey string]bool
// the xxxKey is format as namespace + "_" + name
func SyncSecretSSL(key string, ssl *v1.Ssl, operator string) {
ssls, ok := secretSSLMap.Load(key)
if ok {
sslMap := ssls.(sync.Map)
switch operator {
case state.Delete:
sslMap.Delete(ssl.ID)
secretSSLMap.Store(key, sslMap)
default:
sslMap.Store(ssl.ID, ssl)
secretSSLMap.Store(key, sslMap)
}
} else {
if operator != state.Delete {
sslMap := sync.Map{}
sslMap.Store(ssl.ID, ssl)
secretSSLMap.Store(key, sslMap)
}
}
}
func (c *ApisixTLSController) addFunc(obj interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
runtime.HandleError(err)
return
}
if !c.controller.namespaceWatching(key) {
return
}
rqo := &TlsQueueObj{Key: key, OldObj: nil, Ope: state.Create}
c.workqueue.AddRateLimited(rqo)
}
func (c *ApisixTLSController) updateFunc(oldObj, newObj interface{}) {
oldTls := oldObj.(*configv1.ApisixTls)
newTls := newObj.(*configv1.ApisixTls)
if oldTls.ResourceVersion == newTls.ResourceVersion {
return
}
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(newObj); err != nil {
runtime.HandleError(err)
return
}
if !c.controller.namespaceWatching(key) {
return
}
rqo := &TlsQueueObj{Key: key, OldObj: oldTls, Ope: state.Update}
c.workqueue.AddRateLimited(rqo)
}
func (c *ApisixTLSController) deleteFunc(obj interface{}) {
oldTls, ok := obj.(*configv1.ApisixTls)
if !ok {
oldState, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
return
}
oldTls, ok = oldState.Obj.(*configv1.ApisixTls)
if !ok {
return
}
}
var key string
var err error
key, err = cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
runtime.HandleError(err)
return
}
if !c.controller.namespaceWatching(key) {
return
}
rqo := &TlsQueueObj{Key: key, OldObj: oldTls, Ope: state.Delete}
c.workqueue.AddRateLimited(rqo)
}