/
util.go
57 lines (49 loc) · 1.37 KB
/
util.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
/*
* CloudLinux Software Inc 2019-2021 All Rights Reserved
*/
package commons
import (
"encoding/gob"
"github.com/hashicorp/go-plugin"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func FromUnstructured(u map[string]interface{}, obj interface{}) error {
return runtime.DefaultUnstructuredConverter.FromUnstructured(u, obj)
}
func ToUnstructured(obj interface{}, gvk schema.GroupVersionKind) (*unstructured.Unstructured, error) {
content, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, err
}
u := &unstructured.Unstructured{Object: content}
u.SetGroupVersionKind(gvk)
return u, nil
}
func ResponseFromObject(object client.Object, gvk schema.GroupVersionKind, service string, protocol protocol) *PluginResponse {
o, err := ToUnstructured(object, gvk)
if err != nil {
return &PluginResponse{
Err: err.Error(),
}
}
return &PluginResponse{
Objects: []*unstructured.Unstructured{
o,
},
Service: service,
Protocol: protocol,
}
}
func ServePlugin(name string, pl PluginService) {
gob.Register(PluginRequest{})
var pluginMap = map[string]plugin.Plugin{
name: &Plugin{Impl: pl},
}
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: HandshakeConfig,
Plugins: pluginMap,
})
}