forked from kubeedge/kubeedge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
83 lines (72 loc) · 2.94 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
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
package messagelayer
import (
"fmt"
"strings"
"k8s.io/klog/v2"
"github.com/kubeedge/beehive/pkg/core/model"
controller "github.com/kubeedge/kubeedge/cloud/pkg/edgecontroller/constants"
"github.com/kubeedge/kubeedge/common/constants"
)
// BuildResource return a string as "beehive/pkg/core/model".Message.Router.Resource
func BuildResource(nodeID, namespace, resourceType, resourceID string) (resource string, err error) {
if namespace == "" || resourceType == "" || nodeID == "" {
err = fmt.Errorf("required parameter are not set (node id, namespace or resource type)")
return
}
resource = fmt.Sprintf("%s%s%s%s%s%s%s", controller.ResourceNode, constants.ResourceSep, nodeID, constants.ResourceSep, namespace, constants.ResourceSep, resourceType)
if resourceID != "" {
resource += fmt.Sprintf("%s%s", constants.ResourceSep, resourceID)
}
return
}
// BuildResourceForRouter return a string as "beehive/pkg/core/model".Message.Router.Resource
func BuildResourceForRouter(resourceType, resourceID string) (string, error) {
if resourceID == "" || resourceType == "" {
return "", fmt.Errorf("required parameter are not set (resourceID or resource type)")
}
return fmt.Sprintf("%s%s%s", resourceType, constants.ResourceSep, resourceID), nil
}
// getElementByIndex returns a string from "beehive/pkg/core/model".Message.Router.Resource by index
func getElementByIndex(msg model.Message, index int) string {
sli := strings.Split(msg.GetResource(), constants.ResourceSep)
if len(sli) <= index {
return ""
}
return sli[index]
}
// GetNodeID from "beehive/pkg/core/model".Message.Router.Resource
func GetNodeID(msg model.Message) (string, error) {
res := getElementByIndex(msg, controller.ResourceNodeIDIndex)
if res == "" {
return "", fmt.Errorf("node id not found")
}
klog.V(4).Infof("The node id %s, %d", res, controller.ResourceNodeIDIndex)
return res, nil
}
// GetNamespace from "beehive/pkg/core/model".Model.Router.Resource
func GetNamespace(msg model.Message) (string, error) {
res := getElementByIndex(msg, controller.ResourceNamespaceIndex)
if res == "" {
return "", fmt.Errorf("namespace not found")
}
klog.V(4).Infof("The namespace %s, %d", res, controller.ResourceNamespaceIndex)
return res, nil
}
// GetResourceType from "beehive/pkg/core/model".Model.Router.Resource
func GetResourceType(msg model.Message) (string, error) {
res := getElementByIndex(msg, controller.ResourceResourceTypeIndex)
if res == "" {
return "", fmt.Errorf("resource type not found")
}
klog.V(4).Infof("The resource type is %s, %d", res, controller.ResourceResourceTypeIndex)
return res, nil
}
// GetResourceName from "beehive/pkg/core/model".Model.Router.Resource
func GetResourceName(msg model.Message) (string, error) {
res := getElementByIndex(msg, controller.ResourceResourceNameIndex)
if res == "" {
return "", fmt.Errorf("resource name not found")
}
klog.V(4).Infof("The resource name is %s, %d", res, controller.ResourceResourceNameIndex)
return res, nil
}