This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 278
/
maestro.go
215 lines (176 loc) · 7.58 KB
/
maestro.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"sync"
"time"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"github.com/openservicemesh/osm/ci/cmd/maestro"
"github.com/openservicemesh/osm/demo/cmd/common"
"github.com/openservicemesh/osm/pkg/constants"
"github.com/openservicemesh/osm/pkg/logger"
osmStrings "github.com/openservicemesh/osm/pkg/strings"
"github.com/openservicemesh/osm/pkg/utils"
)
var log = logger.NewPretty("ci/maestro")
const (
// Pod labels
bookBuyerLabel = "bookbuyer"
bookThiefLabel = "bookthief"
bookstoreLabel = "bookstore"
bookWarehouseLabel = "bookwarehouse"
mySQLLabel = "mysql"
)
var (
osmControllerPodSelector = fmt.Sprintf("%s=%s", constants.AppLabel, constants.OSMControllerName)
bookThiefSelector = fmt.Sprintf("%s=%s", constants.AppLabel, bookThiefLabel)
bookBuyerSelector = fmt.Sprintf("%s=%s", constants.AppLabel, bookBuyerLabel)
bookstoreV1Selector = fmt.Sprintf("%s=%s,version=v1", constants.AppLabel, bookstoreLabel)
bookstoreV2Selector = fmt.Sprintf("%s=%s,version=v2", constants.AppLabel, bookstoreLabel)
bookWarehouseSelector = fmt.Sprintf("%s=%s", constants.AppLabel, bookWarehouseLabel)
mySQLSelector = fmt.Sprintf("%s=%s", constants.AppLabel, mySQLLabel)
osmNamespace = utils.GetEnv(maestro.OSMNamespaceEnvVar, "osm-system")
bookbuyerNS = utils.GetEnv(maestro.BookbuyerNamespaceEnvVar, "bookbuyer")
bookthiefNS = utils.GetEnv(maestro.BookthiefNamespaceEnvVar, "bookthief")
bookstoreNS = utils.GetEnv(maestro.BookstoreNamespaceEnvVar, "bookstore")
bookWarehouseNS = utils.GetEnv(common.BookwarehouseNamespaceEnvVar, "bookwarehouse")
maxPodWaitString = utils.GetEnv(maestro.WaitForPodTimeSecondsEnvVar, "30")
maxOKWaitString = utils.GetEnv(maestro.WaitForOKSecondsEnvVar, "30")
// Mesh namespaces
namespaces = []string{
bookbuyerNS,
bookthiefNS,
bookstoreNS,
bookWarehouseNS,
}
)
func main() {
log.Debug().Msgf("Looking for: %s/%s, %s/%s, %s/%s, %s/%s %s/%s", bookBuyerLabel, bookbuyerNS, bookThiefLabel, bookthiefNS, bookstoreLabel, bookstoreNS, bookWarehouseLabel, bookWarehouseNS, mySQLLabel, bookWarehouseNS)
kubeClient := maestro.GetKubernetesClient()
bookBuyerPodName, bookThiefPodName, bookWarehousePodName, osmControllerPodName := getPodNames(kubeClient)
// Tail the logs of the pods participating in the service mesh concurrently and watch for success or failure.
didItSucceed := func(ns, podName, label string) chan string {
result := make(chan string)
maestro.SearchLogsForSuccess(kubeClient, ns, podName, label, maxWaitForOK(), result, common.Success, common.Failure)
return result
}
// When both pods return success - easy - we are good to go! CI passed!
allTestsResults := osmStrings.All{
<-didItSucceed(bookbuyerNS, bookBuyerPodName, bookBuyerLabel),
<-didItSucceed(bookthiefNS, bookThiefPodName, bookThiefLabel),
<-didItSucceed(bookWarehouseNS, bookWarehousePodName, bookWarehouseLabel),
}
if allTestsResults.Equal(maestro.TestsPassed) {
log.Debug().Msg("Test succeeded")
maestro.DeleteNamespaces(kubeClient, append(namespaces, osmNamespace)...)
os.Exit(0) // Tests passed! WE ARE DONE !!!
}
if failedTests := osmStrings.Which(allTestsResults).NotEqual(maestro.TestsPassed); len(failedTests) != 0 {
log.Error().Msgf("%s did not pass; Retrieving pod logs", strings.Join(failedTests, ","))
}
// Walk mesh-participant namespaces
for _, ns := range namespaces {
pods, err := kubeClient.CoreV1().Pods(ns).List(context.Background(), metav1.ListOptions{})
if err != nil {
log.Error().Err(err).Msgf("Could not get Pods for Namespace %s", ns)
continue
}
for _, pod := range pods.Items {
printLogsForInitContainers(kubeClient, pod)
printLogsForContainers(kubeClient, pod)
}
}
fmt.Println("-------- OSM-Controller LOGS --------\n",
maestro.GetPodLogs(kubeClient, osmNamespace, osmControllerPodName, "", maestro.FailureLogsFromTimeSince))
os.Exit(1)
}
func cutItAt(logs string, at string) string {
firstOccurrence := strings.Index(logs, at)
if firstOccurrence == -1 {
return logs
}
return logs[:firstOccurrence+len(at)]
}
func cutIt(logs string) string {
firstSuccess := strings.Index(logs, common.Success)
if firstSuccess == -1 {
return cutItAt(logs, common.Failure)
}
return cutItAt(logs, common.Success)
}
func maxWaitForPod() time.Duration {
maxWaitInt, err := strconv.ParseInt(maxPodWaitString, 10, 32)
if err != nil {
log.Fatal().Err(err).Msgf("Could not convert environment variable %s='%s' to int", maestro.WaitForPodTimeSecondsEnvVar, maxPodWaitString)
}
return time.Duration(maxWaitInt) * time.Second
}
func maxWaitForOK() time.Duration {
maxWaitInt, err := strconv.ParseInt(maxOKWaitString, 10, 32)
if err != nil {
log.Fatal().Err(err).Msgf("Could not convert environment variable %s='%s' to int", maestro.WaitForOKSecondsEnvVar, maxOKWaitString)
}
return time.Duration(maxWaitInt) * time.Second
}
func getPodNames(kubeClient kubernetes.Interface) (string, string, string, string) {
var wg sync.WaitGroup
wg.Add(1)
go maestro.WaitForPodToBeReady(kubeClient, maxWaitForPod(), bookthiefNS, bookThiefSelector, &wg)
wg.Add(1)
go maestro.WaitForPodToBeReady(kubeClient, maxWaitForPod(), bookbuyerNS, bookBuyerSelector, &wg)
wg.Add(1)
go maestro.WaitForPodToBeReady(kubeClient, maxWaitForPod(), bookstoreNS, bookstoreV1Selector, &wg)
wg.Add(1)
go maestro.WaitForPodToBeReady(kubeClient, maxWaitForPod(), bookstoreNS, bookstoreV2Selector, &wg)
wg.Add(1)
go maestro.WaitForPodToBeReady(kubeClient, maxWaitForPod(), bookWarehouseNS, bookWarehouseSelector, &wg)
wg.Add(1)
go maestro.WaitForPodToBeReady(kubeClient, maxWaitForPod(), bookWarehouseNS, mySQLSelector, &wg)
wg.Wait()
bookBuyerPodName, err := maestro.GetPodName(kubeClient, bookbuyerNS, bookBuyerSelector)
if err != nil {
fmt.Println("Error getting bookbuyer pod after pod being ready: ", err)
os.Exit(1)
}
bookThiefPodName, err := maestro.GetPodName(kubeClient, bookthiefNS, bookThiefSelector)
if err != nil {
fmt.Println("Error getting bookthief pod after pod being ready: ", err)
os.Exit(1)
}
bookWarehousePodName, err := maestro.GetPodName(kubeClient, bookWarehouseNS, bookWarehouseSelector)
if err != nil {
fmt.Println("Error getting bookWarehouse pod after pod being ready: ", err)
os.Exit(1)
}
osmControllerPodName, err := maestro.GetPodName(kubeClient, osmNamespace, osmControllerPodSelector)
if err != nil {
fmt.Println("Error getting bookWarehouse pod after pod being ready: ", err)
os.Exit(1)
}
return bookBuyerPodName, bookThiefPodName, bookWarehousePodName, osmControllerPodName
}
func printLogsForInitContainers(kubeClient kubernetes.Interface, pod v1.Pod) {
for _, initContainer := range pod.Spec.InitContainers {
initLogs := maestro.GetPodLogs(kubeClient, pod.Namespace, pod.Name, initContainer.Name, maestro.FailureLogsFromTimeSince)
fmt.Println(fmt.Sprintf("---- NS: %s Pod: %s InitContainer: %s --------\n",
pod.Namespace, pod.Name, initContainer.Name), cutIt(initLogs))
}
}
func printLogsForContainers(kubeClient kubernetes.Interface, pod v1.Pod) {
for _, containerObj := range pod.Spec.Containers {
initLogs := maestro.GetPodLogs(kubeClient, pod.Namespace, pod.Name, containerObj.Name, maestro.FailureLogsFromTimeSince)
switch containerObj.Name {
case constants.EnvoyContainerName:
fmt.Println(fmt.Sprintf("---- NS: %s Pod: %s Envoy Logs: --------\n",
pod.Namespace, pod.Name), initLogs)
default:
fmt.Println(fmt.Sprintf("---- NS: %s Pod: %s Container: %s --------\n",
pod.Namespace, pod.Name, containerObj.Name), cutIt(initLogs))
}
}
}