/
testsuite.go
64 lines (49 loc) · 1.73 KB
/
testsuite.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
package example
import (
"context"
"encoding/json"
"fmt"
"github.com/thatchd/thatchd/pkg/thatchd/strategy"
"github.com/thatchd/thatchd/pkg/thatchd/testsuite"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type PodStatus string
var PodReady PodStatus = "Ready"
var PodNotReady PodStatus = "NotReady"
var PodTested PodStatus = "Tested"
var PodAnnotated PodStatus = "Annotated"
// PodSuiteState represent the current testing state as an association between
// pod names -> whether they're ready or not
type PodSuiteState map[string]PodStatus
// PodsSuiteReconciler reconciles the testing state with the pods in the
// namespace
type PodsSuiteReconciler struct{}
var _ testsuite.Reconciler = &PodsSuiteReconciler{}
func (r *PodsSuiteReconciler) ParseState(state string) (interface{}, error) {
result := PodSuiteState{}
err := json.Unmarshal([]byte(state), &result)
return result, err
}
func (r *PodsSuiteReconciler) Reconcile(c client.Client, namespace string, s interface{}) (interface{}, error) {
currentState := s.(PodSuiteState)
podList := &corev1.PodList{}
if err := c.List(context.TODO(), podList, client.InNamespace(namespace)); err != nil {
return nil, fmt.Errorf("failed to list pods in namespace %s: %v", namespace, err)
}
for _, pod := range podList.Items {
podState, ok := currentState[pod.Name]
if ok && (podState == PodTested || podState == PodAnnotated) {
continue
}
podState = PodNotReady
if pod.Status.Phase == corev1.PodSucceeded || pod.Status.Phase == corev1.PodRunning {
podState = PodReady
}
currentState[pod.Name] = podState
}
return currentState, nil
}
func NewPodsSuiteProvider() strategy.StrategyProvider {
return strategy.NewProviderForType(&PodsSuiteReconciler{})
}