forked from chaos-mesh/chaos-mesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
153 lines (136 loc) · 3.68 KB
/
common.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
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
// Copyright 2021 Chaos Mesh Authors.
//
// 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 common
import (
"encoding/json"
"fmt"
"regexp"
"github.com/fatih/color"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
)
type Color string
const (
Blue Color = "Blue"
Red Color = "Red"
Green Color = "Green"
Cyan Color = "Cyan"
NoColor Color = ""
)
var (
colorFunc = map[Color]func(string, ...interface{}){
Blue: color.Blue,
Red: color.Red,
Green: color.Green,
Cyan: color.Cyan,
}
scheme = runtime.NewScheme()
)
// ClientSet contains two different clients
type ClientSet struct {
CtrlCli client.Client
KubeCli *kubernetes.Clientset
}
type ChaosResult struct {
Name string
Pods []PodResult
}
type PodResult struct {
Name string
Items []ItemResult
}
const (
ItemSuccess = iota + 1
ItemFailure
)
type ItemResult struct {
Name string
Value string
Status int `json:",omitempty"`
SucInfo string `json:",omitempty"`
ErrInfo string `json:",omitempty"`
}
func init() {
_ = v1alpha1.AddToScheme(scheme)
_ = clientgoscheme.AddToScheme(scheme)
}
// PrettyPrint print with tab number and color
func PrettyPrint(s string, indentLevel int, color Color) {
var tabStr string
for i := 0; i < indentLevel; i++ {
tabStr += "\t"
}
str := fmt.Sprintf("%s%s\n\n", tabStr, regexp.MustCompile("\n").ReplaceAllString(s, "\n"+tabStr))
if color != NoColor {
if cfunc, ok := colorFunc[color]; !ok {
fmt.Print("COLOR NOT SUPPORTED")
} else {
cfunc(str)
}
} else {
fmt.Print(str)
}
}
// PrintResult prints result to users in prettier format
func PrintResult(result []*ChaosResult) {
for _, chaos := range result {
PrettyPrint("[Chaos]: "+chaos.Name, 0, Blue)
for _, pod := range chaos.Pods {
PrettyPrint("[Pod]: "+pod.Name, 0, Blue)
for i, item := range pod.Items {
PrettyPrint(fmt.Sprintf("%d. [%s]", i+1, item.Name), 1, Cyan)
PrettyPrint(item.Value, 1, NoColor)
if item.Status == ItemSuccess {
if item.SucInfo != "" {
PrettyPrint(item.SucInfo, 1, Green)
} else {
PrettyPrint("Execute as expected", 1, Green)
}
} else if item.Status == ItemFailure {
PrettyPrint(fmt.Sprintf("Failed: %s ", item.ErrInfo), 1, Red)
}
}
}
}
}
// MarshalChaos returns json in readable format
func MarshalChaos(s interface{}) (string, error) {
b, err := json.MarshalIndent(s, "", " ")
if err != nil {
return "", errors.Wrap(err, "failed to marshal indent")
}
return string(b), nil
}
// InitClientSet inits two different clients that would be used
func InitClientSet() (*ClientSet, error) {
restconfig, err := config.GetConfig()
if err != nil {
return nil, err
}
ctrlClient, err := client.New(restconfig, client.Options{Scheme: scheme})
if err != nil {
return nil, errors.New("failed to create client")
}
kubeClient, err := kubernetes.NewForConfig(restconfig)
if err != nil {
return nil, errors.Wrap(err, "error in getting acess to k8s")
}
return &ClientSet{ctrlClient, kubeClient}, nil
}