-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
configmap.go
100 lines (84 loc) · 2.74 KB
/
configmap.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
package client
import (
"encoding/json"
"fmt"
api "k8s.io/api/core/v1"
"github.com/kubeedge/beehive/pkg/core/model"
"github.com/kubeedge/kubeedge/edge/pkg/common/message"
"github.com/kubeedge/kubeedge/edge/pkg/common/modules"
)
// ConfigMapsGetter has a method to return a ConfigMapInterface.
// A group's client should implement this interface.
type ConfigMapsGetter interface {
ConfigMaps(namespace string) ConfigMapsInterface
}
// ConfigMapsInterface has methods to work with ConfigMap resources.
type ConfigMapsInterface interface {
Create(*api.ConfigMap) (*api.ConfigMap, error)
Update(*api.ConfigMap) error
Delete(name string) error
Get(name string) (*api.ConfigMap, error)
}
type configMaps struct {
namespace string
send SendInterface
}
func newConfigMaps(namespace string, s SendInterface) *configMaps {
return &configMaps{
send: s,
namespace: namespace,
}
}
func (c *configMaps) Create(cm *api.ConfigMap) (*api.ConfigMap, error) {
return nil, nil
}
func (c *configMaps) Update(cm *api.ConfigMap) error {
return nil
}
func (c *configMaps) Delete(name string) error {
return nil
}
func (c *configMaps) Get(name string) (*api.ConfigMap, error) {
resource := fmt.Sprintf("%s/%s/%s", c.namespace, model.ResourceTypeConfigmap, name)
configMapMsg := message.BuildMsg(modules.MetaGroup, "", modules.EdgedModuleName, resource, model.QueryOperation, nil)
msg, err := c.send.SendSync(configMapMsg)
if err != nil {
return nil, fmt.Errorf("get configmap from metaManager failed, err: %v", err)
}
errContent, ok := msg.GetContent().(error)
if ok {
return nil, errContent
}
content, err := msg.GetContentData()
if err != nil {
return nil, fmt.Errorf("parse message to configmap failed, err: %v", err)
}
if msg.GetOperation() == model.ResponseOperation && msg.GetSource() == modules.MetaManagerModuleName {
return handleConfigMapFromMetaDB(content)
}
return handleConfigMapFromMetaManager(content)
}
func handleConfigMapFromMetaDB(content []byte) (*api.ConfigMap, error) {
var lists []string
err := json.Unmarshal(content, &lists)
if err != nil {
return nil, fmt.Errorf("unmarshal message to ConfigMap list from db failed, err: %v", err)
}
if len(lists) != 1 {
return nil, fmt.Errorf("ConfigMap length from meta db is %d", len(lists))
}
var configMap api.ConfigMap
err = json.Unmarshal([]byte(lists[0]), &configMap)
if err != nil {
return nil, fmt.Errorf("unmarshal message to ConfigMap from db failed, err: %v", err)
}
return &configMap, nil
}
func handleConfigMapFromMetaManager(content []byte) (*api.ConfigMap, error) {
var configMap api.ConfigMap
err := json.Unmarshal(content, &configMap)
if err != nil {
return nil, fmt.Errorf("unmarshal message to ConfigMap failed, err: %v", err)
}
return &configMap, nil
}