/
workflow_manager.go
92 lines (77 loc) · 2.73 KB
/
workflow_manager.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
package klcpermit
import (
"context"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/klog/v2"
)
var workloadInstanceResource = schema.GroupVersionResource{Group: "lifecycle.keptn.sh", Version: "v1alpha1", Resource: "keptnworkloadinstances"}
type Status string
const (
WorkloadInstanceStatusNotSpecified Status = "Workload run status not specified"
WorkloadInstanceNotFound Status = "Workload run not found"
Success Status = "Success"
Failure Status = "Failure"
Wait Status = "Wait"
)
type KeptnState string
const (
StateRunning KeptnState = "Running"
StateSucceeded KeptnState = "Succeeded"
StateFailed KeptnState = "Failed"
StateUnknown KeptnState = "Unknown"
StatePending KeptnState = "Pending"
)
type Manager interface {
Permit(context.Context, *corev1.Pod) Status
}
type WorkloadManager struct {
dynamicClient dynamic.Interface
}
func NewWorkloadManager(d dynamic.Interface) *WorkloadManager {
sMgr := &WorkloadManager{
dynamicClient: d,
}
return sMgr
}
func (sMgr *WorkloadManager) Permit(ctx context.Context, pod *corev1.Pod) Status {
//List workloadInstance run CRDs
name := GetCRDName(pod)
crd, err := sMgr.GetCRD(ctx, pod.Namespace, name)
if err != nil {
klog.Infof("[Keptn Permit Plugin] could not find workloadInstance crd %s, err:%s", name, err.Error())
return WorkloadInstanceNotFound
}
//check CRD status
phase, found, err := unstructured.NestedString(crd.UnstructuredContent(), "status", "preDeploymentStatus")
klog.Infof("[Keptn Permit Plugin] workloadInstance crd %s, found %s with phase %s ", crd, found, phase)
if err == nil && found {
switch KeptnState(phase) {
case StatePending:
return Wait
case StateFailed:
return Failure
case StateSucceeded:
return Success
case StateRunning:
return Wait
case StateUnknown:
return Wait
}
}
return WorkloadInstanceStatusNotSpecified
}
//GetCRD returns unstructured to avoid tight coupling with the CRD resource
func (sMgr *WorkloadManager) GetCRD(ctx context.Context, namespace string, name string) (*unstructured.Unstructured, error) {
// GET /apis/lifecycle.keptn.sh/v1/namespaces/{namespace}/workloadinstance/name
return sMgr.dynamicClient.Resource(workloadInstanceResource).Namespace(namespace).Get(ctx, name, metav1.GetOptions{})
}
func GetCRDName(pod *corev1.Pod) string {
application := pod.Annotations["keptn.sh/app"]
workloadInstance := pod.Annotations["keptn.sh/workload"]
version := pod.Annotations["keptn.sh/version"]
return application + "-" + workloadInstance + "-" + version
}