-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployment.go
106 lines (92 loc) · 3.27 KB
/
deployment.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
package sources
import (
"regexp"
"github.com/overmindtech/discovery"
"github.com/overmindtech/sdp-go"
v1 "k8s.io/api/apps/v1"
"k8s.io/client-go/kubernetes"
)
//go:generate docgen ../docs-data
// +overmind:type Deployment
// +overmind:descriptiveType Deployment
// +overmind:get Get a deployment by name
// +overmind:list List all deployments
// +overmind:search Search for a deployment using the ListOptions JSON format: https://github.com/overmindtech/k8s-source#search
// +overmind:group Kubernetes
// +overmind:terraform:queryMap kubernetes_deployment.metadata[0].name
// +overmind:terraform:queryMap kubernetes_deployment_v1.metadata[0].name
// +overmind:terraform:scope ${provider_mapping.cluster_name}.${values.metadata[0].namespace}
// +overmind:link ReplicaSet
var replicaSetProgressedRegex = regexp.MustCompile(`ReplicaSet "([^"]+)" has successfully progressed`)
func newDeploymentSource(cs *kubernetes.Clientset, cluster string, namespaces []string) discovery.Source {
return &KubeTypeSource[*v1.Deployment, *v1.DeploymentList]{
ClusterName: cluster,
Namespaces: namespaces,
TypeName: "Deployment",
NamespacedInterfaceBuilder: func(namespace string) ItemInterface[*v1.Deployment, *v1.DeploymentList] {
return cs.AppsV1().Deployments(namespace)
},
ListExtractor: func(list *v1.DeploymentList) ([]*v1.Deployment, error) {
extracted := make([]*v1.Deployment, len(list.Items))
for i := range list.Items {
extracted[i] = &list.Items[i]
}
return extracted, nil
},
LinkedItemQueryExtractor: func(deployment *v1.Deployment, scope string) ([]*sdp.LinkedItemQuery, error) {
queries := make([]*sdp.LinkedItemQuery, 0)
for _, condition := range deployment.Status.Conditions {
// Parse out conditions that mention replica sets e.g.
//
// - lastTransitionTime: "2023-06-16T14:23:33Z"
// lastUpdateTime: "2023-09-15T13:07:07Z"
// message: ReplicaSet "gateway-5cf5578d94" has successfully progressed.
// reason: NewReplicaSetAvailable
// status: "True"
// type: Progressing
if condition.Type == v1.DeploymentProgressing && condition.Reason == "NewReplicaSetAvailable" {
matches := replicaSetProgressedRegex.FindStringSubmatch(condition.Message)
if len(matches) > 1 {
queries = append(queries, &sdp.LinkedItemQuery{
Query: &sdp.Query{
Type: "ReplicaSet",
Method: sdp.QueryMethod_GET,
Query: matches[1],
Scope: scope,
},
BlastPropagation: &sdp.BlastPropagation{
// These are tightly bound
In: true,
Out: true,
},
})
}
}
}
return queries, nil
},
HealthExtractor: func(deployment *v1.Deployment) *sdp.Health {
var available bool
var progressing bool
for _, condition := range deployment.Status.Conditions {
// Extract available and progressing conditions
switch condition.Type {
case v1.DeploymentAvailable:
available = condition.Status == "True"
case v1.DeploymentProgressing:
progressing = condition.Status == "True"
}
}
if available {
return sdp.Health_HEALTH_OK.Enum()
} else if progressing {
return sdp.Health_HEALTH_PENDING.Enum()
} else {
return sdp.Health_HEALTH_ERROR.Enum()
}
},
}
}
func init() {
registerSourceLoader(newDeploymentSource)
}