-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_controller.go
More file actions
218 lines (192 loc) · 5.99 KB
/
plugin_controller.go
File metadata and controls
218 lines (192 loc) · 5.99 KB
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
/*
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 controllers
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/go-logr/logr"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
pluginsv1alpha1 "github.com/danielfbm/plugin-example/controller/api/v1alpha1"
ext "github.com/danielfbm/plugin-example/controller/extension"
)
// PluginReconciler reconciles a Plugin object
type PluginReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
PluginManager ext.Manager
}
// +kubebuilder:rbac:groups=plugins.danielfbm.github.com,resources=plugins,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=plugins.danielfbm.github.com,resources=plugins/status,verbs=get;update;patch
func (r *PluginReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
logger := r.Log.WithValues("plugin", req.NamespacedName)
// Get plugin
plgin := &pluginsv1alpha1.Plugin{}
if err := r.Client.Get(ctx, req.NamespacedName, plgin); err != nil {
err = client.IgnoreNotFound(err)
logger.Error(err, "get error")
return ctrl.Result{}, err
}
// Verifying if it is a local plugin and tried to delete
// we should recover the local plugin, update and return
if plgin.DeletionTimestamp != nil && plgin.Spec.Local != nil && len(plgin.Finalizers) > 0 {
logger.Info("was deleted and is a local plugin... will recover")
plgin.DeletionTimestamp = nil
err := r.Client.Update(ctx, plgin)
return ctrl.Result{}, err
}
// Check if plugin with the same name is loaded
// and if is the same plugin
pluginClient, err := r.PluginManager.Get(plgin.Name)
logger.Info("get", "err", err, "client", pluginClient)
if err != nil {
if errors.IsNotFound(err) {
netAddr, localPath := "", ""
if plgin.Spec.Local != nil && plgin.Spec.Local.Path != "" {
localPath = plgin.Spec.Local.Path
} else if plgin.Spec.Network != nil && plgin.Spec.Network.Address != "" {
netAddr = plgin.Spec.Network.Address
}
logger.Info("will load", "net", netAddr, "path", localPath)
err = r.PluginManager.Load(plgin.Name, ext.PluginLoadOptions{
Config: &plugin.ClientConfig{Logger: hclog.Default()},
LocalPluginPath: localPath,
NetworkPluginAddress: netAddr,
})
logger.Info("load", "err", err)
if err != nil {
setCondition(plgin, pluginsv1alpha1.Condition{
Type: "Load",
Ready: strconv.FormatBool(false),
Message: err.Error(),
Reason: "LoadError",
})
err = r.Client.Update(ctx, plgin)
logger.Info("update", "err", err)
}
}
return ctrl.Result{Requeue: err == nil}, err
}
// Validate each plugin and return values
setCondition(plgin, fooCheck(pluginClient))
setCondition(plgin, barCheck(pluginClient))
setCondition(plgin, barGRPCCheck(pluginClient))
err = r.Client.Status().Update(ctx, plgin)
logger.Info("update", "err", err)
return ctrl.Result{}, err
}
// func (r *PluginReconciler) Update(ctx context.Context, plgin *pluginsv1alpha1.Plugin) {
// }
func (r *PluginReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&pluginsv1alpha1.Plugin{}).
Complete(r)
}
func setCondition(plgin *pluginsv1alpha1.Plugin, cond pluginsv1alpha1.Condition) {
if plgin.Status.Conditions == nil {
plgin.Status.Conditions = make([]pluginsv1alpha1.Condition, 0, 2)
}
found := false
for i, current := range plgin.Status.Conditions {
if current.Type == cond.Type {
found = true
plgin.Status.Conditions[i] = cond
break
}
}
if !found {
plgin.Status.Conditions = append(plgin.Status.Conditions, cond)
}
}
func fooCheck(pluginClient plugin.ClientProtocol) (cond pluginsv1alpha1.Condition) {
cond = pluginsv1alpha1.Condition{Type: "FooPlugin"}
var raw interface{}
var err error
defer func() {
if err != nil {
cond.Ready = "false"
cond.Message = err.Error()
}
}()
if raw, err = pluginClient.Dispense("foo"); err != nil {
cond.Reason = "DispenseFailed"
return
}
fooClient, ok := raw.(ext.Foo)
if !ok {
err = fmt.Errorf("Not a Foo interface")
cond.Reason = "WrongInterface"
return
}
if cond.Message, err = fooClient.Foos(); err != nil {
cond.Reason = "FooResult"
return
}
cond.Ready = "true"
return
}
func barCheck(pluginClient plugin.ClientProtocol) (cond pluginsv1alpha1.Condition) {
cond = pluginsv1alpha1.Condition{Type: "BarPlugin"}
var raw interface{}
var err error
defer func() {
if err != nil {
cond.Ready = "false"
cond.Message = err.Error()
}
}()
if raw, err = pluginClient.Dispense("bar"); err != nil {
cond.Reason = "DispenseFailed"
return
}
barClient, ok := raw.(ext.Bar)
if !ok {
err = fmt.Errorf("Not a Bar interface")
cond.Reason = "WrongInterface"
return
}
cond.Message = strings.Join(barClient.Bars(), ",")
cond.Ready = "true"
return
}
func barGRPCCheck(pluginClient plugin.ClientProtocol) (cond pluginsv1alpha1.Condition) {
cond = pluginsv1alpha1.Condition{Type: "BarGRPCPlugin"}
var raw interface{}
var err error
defer func() {
if err != nil {
cond.Ready = "false"
cond.Message = err.Error()
}
}()
if raw, err = pluginClient.Dispense("bar_grpc"); err != nil {
cond.Reason = "DispenseFailed"
return
}
barClient, ok := raw.(ext.Bar)
if !ok {
err = fmt.Errorf("Not a Bar interface")
cond.Reason = "WrongInterface"
return
}
cond.Message = strings.Join(barClient.Bars(), ",")
cond.Ready = "true"
return
}