-
Notifications
You must be signed in to change notification settings - Fork 677
/
util.go
72 lines (68 loc) · 2.69 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
package events
import (
v1alpha13 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
v1alpha12 "github.com/litmuschaos/chaos-operator/pkg/client/clientset/versioned/typed/litmuschaos/v1alpha1"
"github.com/litmuschaos/litmus/litmus-portal/backend/workflow-agent/pkg/types"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
"strconv"
)
// util function, extracts the chaos data using the litmus go-client
func getChaosData(engineName, engineNS string, chaosClient *v1alpha12.LitmuschaosV1alpha1Client) (*types.ChaosData, error) {
cd := &types.ChaosData{}
cd.EngineName = engineName
cd.Namespace = engineNS
crd, err := chaosClient.ChaosEngines(cd.Namespace).Get(cd.EngineName, v1.GetOptions{})
if err != nil {
return nil, err
}
// considering chaos engine has only 1 experiment
if len(crd.Status.Experiments) == 1 {
expRes, err := chaosClient.ChaosResults(cd.Namespace).Get(crd.Name+"-"+crd.Status.Experiments[0].Name, v1.GetOptions{})
if err != nil {
return nil, err
}
cd.ProbeSuccessPercentage = expRes.Status.ExperimentStatus.ProbeSuccessPercentage
cd.FailStep = expRes.Status.ExperimentStatus.FailStep
cd.ExperimentPod = crd.Status.Experiments[0].ExpPod
cd.RunnerPod = crd.Status.Experiments[0].Runner
cd.EngineUID = string(crd.ObjectMeta.UID)
cd.ExperimentStatus = string(crd.Status.Experiments[0].Status)
cd.ExperimentName = crd.Status.Experiments[0].Name
cd.LastUpdatedAt = strconv.FormatInt(crd.Status.Experiments[0].LastUpdateTime.Unix(), 10)
cd.ExperimentVerdict = crd.Status.Experiments[0].Verdict
} else {
cd = nil
}
return cd, nil
}
// util function, checks if event is a chaos-exp event, if so - extract the chaos data
func CheckChaosData(nodeStatus v1alpha13.NodeStatus, chaosClient *v1alpha12.LitmuschaosV1alpha1Client) (string, *types.ChaosData, error) {
nodeType := string(nodeStatus.Type)
var cd *types.ChaosData = nil
// considering chaos workflow has only 1 artifact with manifest as raw data
data := nodeStatus.Inputs.Artifacts[0].Raw.Data
obj := &unstructured.Unstructured{}
decUnstructured := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
_, _, err := decUnstructured.Decode([]byte(data), nil, obj)
if err == nil && obj.GetKind() == "ChaosEngine" {
nodeType = "ChaosEngine"
if nodeStatus.Phase != "Pending" {
cd, err = getChaosData(obj.GetName(), obj.GetNamespace(), chaosClient)
if err != nil {
return nodeType, nil, err
}
return nodeType, cd, nil
}
}
return nodeType, nil, nil
}
// converts unix timestamp to string
func StrConvTime(time int64) string {
if time < 0 {
return ""
} else {
return strconv.FormatInt(time, 10)
}
}