This repository has been archived by the owner on Mar 31, 2022. It is now read-only.
/
influxdb_controller.go
177 lines (135 loc) · 5.98 KB
/
influxdb_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
/*
Copyright 2020 InfluxData
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package controllers
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"net/http/httptrace"
"time"
"github.com/go-logr/logr"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
influxdbv1 "github.com/influxdata/influxdb-operator/api/v1"
)
// InfluxDBReconciler reconciles a InfluxDB object
type InfluxDBReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
Recorder record.EventRecorder
}
// +kubebuilder:rbac:groups="",resources=events,verbs=create;patch
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get
// +kubebuilder:rbac:groups=influxdb.influxdata.com,resources=influxdbs,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=influxdb.influxdata.com,resources=influxdbs/status,verbs=get;update;patch
func (r *InfluxDBReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
log := r.Log.WithValues("influxdb", req.NamespacedName)
var result = ctrl.Result{RequeueAfter: time.Second * 10}
var influxdb influxdbv1.InfluxDB
if err := r.Get(ctx, req.NamespacedName, &influxdb); err != nil {
log.Error(err, "unable to fetch InfluxDB")
return result, client.IgnoreNotFound(err)
}
// Assume false until we get what we need
influxdb.Status.Available = false
influxdb.Status.Authenticated = false
// Lets load our Secret for our InfluxDB Token
var tokenSecret core.Secret
var tokenSecretName = client.ObjectKey{Name: influxdb.Spec.Token.SecretName, Namespace: req.Namespace}
if err := r.Get(ctx, tokenSecretName, &tokenSecret); err != nil {
r.Recorder.Event(&influxdb, core.EventTypeWarning, "Authentication", err.Error())
err := r.Status().Update(ctx, &influxdb)
if err != nil {
return result, err
}
return result, nil
}
// Lets check if our InfluxDB URL responds to /health
// We'll time this also
var start, connect, dns, tlsHandshake time.Time
var trace = &httptrace.ClientTrace{}
// Refresh stats every 10 seconds
if time.Now().Unix()-influxdb.Status.StatsRefresh > 9 {
influxdb.Status.StatsRefresh = time.Now().Unix()
trace = &httptrace.ClientTrace{
DNSStart: func(dsi httptrace.DNSStartInfo) { dns = time.Now() },
DNSDone: func(ddi httptrace.DNSDoneInfo) {
influxdb.Status.DNSResolution = time.Since(dns).Milliseconds()
},
TLSHandshakeStart: func() { tlsHandshake = time.Now() },
TLSHandshakeDone: func(cs tls.ConnectionState, err error) {
influxdb.Status.TLSHandshake = time.Since(tlsHandshake).Milliseconds()
},
ConnectStart: func(network, addr string) { connect = time.Now() },
ConnectDone: func(network, addr string, err error) {
influxdb.Status.ConnectTime = time.Since(connect).Milliseconds()
},
GotFirstResponseByte: func() {
influxdb.Status.FirstByte = time.Since(start).Milliseconds()
},
}
}
httpReq, _ := http.NewRequest("GET", influxdb.Spec.URL, nil)
httpReq = httpReq.WithContext(httptrace.WithClientTrace(httpReq.Context(), trace))
start = time.Now()
httpResp, httpErr := http.DefaultTransport.RoundTrip(httpReq)
if httpErr != nil {
r.Recorder.Event(&influxdb, core.EventTypeWarning, "Connection", httpErr.Error())
err := r.Status().Update(ctx, &influxdb)
return result, err
}
// Currently this is almost impossible to hit ... but you never know ...
// https://github.com/influxdata/influxdb/blob/master/http/health.go
if httpResp.StatusCode != http.StatusOK {
r.Recorder.Event(&influxdb, core.EventTypeWarning, "Connection", httpErr.Error())
err := r.Status().Update(ctx, &influxdb)
return result, err
}
// OK. We consider InfluxDB Available
influxdb.Status.Available = true
// Test Token with /authorizations
httpReq, httpErr = http.NewRequest("GET", influxdb.Spec.URL, nil)
if httpErr != nil {
r.Recorder.Event(&influxdb, core.EventTypeWarning, "Connection", httpErr.Error())
err := r.Status().Update(ctx, &influxdb)
return result, err
}
token := string(tokenSecret.Data[influxdb.Spec.Token.SecretKey])
httpReq.URL.Path = "/api/v2/authorizations"
httpReq.Header.Add("Authorization", fmt.Sprintf("Token %v", token))
log.Info(fmt.Sprintf("Using token %v", token))
client := &http.Client{}
httpResp, httpErr = client.Do(httpReq)
if httpErr != nil {
r.Recorder.Event(&influxdb, core.EventTypeWarning, "Connection", httpErr.Error())
err := r.Status().Update(ctx, &influxdb)
return result, err
}
if httpResp.StatusCode != http.StatusOK {
r.Recorder.Event(&influxdb, core.EventTypeWarning, "Authorization", "Unauthorized")
err := r.Status().Update(ctx, &influxdb)
return result, err
}
// OK. Now we consider InfluxDB Authenticated
influxdb.Status.Authenticated = true
err := r.Status().Update(ctx, &influxdb)
if err != nil {
return result, err
}
return result, nil
}
func (r *InfluxDBReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&influxdbv1.InfluxDB{}).
Complete(r)
}