-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.go
197 lines (165 loc) · 5.19 KB
/
history.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
package history
import (
"context"
"fmt"
"sort"
"strconv"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
appsv1client "k8s.io/client-go/kubernetes/typed/apps/v1"
"k8s.io/klog/v2"
)
const (
DeploymentRevisionAnnotation = "deployment.kubernetes.io/revision"
)
type DeploymentViewer struct {
c kubernetes.Interface
}
func (h *DeploymentViewer) ViewHistory(namespace, name string) ([]*RevisionHistory, []*RevisionHistory, error) {
appsClient := h.c.AppsV1()
deployment, err := appsClient.Deployments(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
return nil, nil, err
}
rsList, err := listReplicaSets(appsClient, deployment)
if err != nil {
return nil, nil, err
}
newRS := findNewReplicaSet(deployment, rsList)
oldRSs := findOldReplicaSets(rsList, newRS)
newRH, err := singleReplicaSetToRevisionHistory(newRS)
if err != nil {
return nil, nil, err
}
oldRHs := replicaSetsToRevisionHistory(oldRSs)
return []*RevisionHistory{&newRH}, oldRHs, nil
}
func replicaSetsToRevisionHistory(rsList []*appsv1.ReplicaSet) []*RevisionHistory {
rhs := make([]*RevisionHistory, 0, len(rsList))
for _, rs := range rsList {
rh, err := singleReplicaSetToRevisionHistory(rs)
if err != nil {
klog.Warningf("ReplicaSet %s to revision history fail, %v", rs.Name, err)
continue
}
rhs = append(rhs, &rh)
}
return rhs
}
func singleReplicaSetToRevisionHistory(rs *appsv1.ReplicaSet) (RevisionHistory, error) {
revision, err := Revision(rs)
if err != nil {
return RevisionHistory{}, err
}
status := HistoryStatus{
DesiredReplicas: rs.Status.Replicas,
FullyLabeledReplicas: rs.Status.FullyLabeledReplicas,
ReadyReplicas: rs.Status.ReadyReplicas,
AvailableReplicas: rs.Status.AvailableReplicas,
}
images := make([]string, 0)
for _, c := range rs.Spec.Template.Spec.InitContainers {
images = append(images, c.Image)
}
for _, c := range rs.Spec.Template.Spec.Containers {
images = append(images, c.Image)
}
return RevisionHistory{
Revision: revision,
Name: rs.Name,
Labels: rs.Labels,
Annotations: rs.Annotations,
CreateTimestamp: rs.CreationTimestamp,
Status: status,
Images: images,
}, nil
}
func Revision(obj runtime.Object) (int64, error) {
acc, err := meta.Accessor(obj)
if err != nil {
return 0, err
}
v, ok := acc.GetAnnotations()[DeploymentRevisionAnnotation]
if !ok {
return 0, nil
}
return strconv.ParseInt(v, 10, 64)
}
// equalIgnoreHash 如果deployment和replicasSet的podTemplateSpec相同,返回true。
// 对比的过程中忽略pod-template-hash,因为deployment没有该字段。
func equalIgnoreHash(template1, template2 *corev1.PodTemplateSpec) bool {
t1 := template1.DeepCopy()
t2 := template2.DeepCopy()
delete(t1.Labels, appsv1.DefaultDeploymentUniqueLabelKey)
delete(t2.Labels, appsv1.DefaultDeploymentUniqueLabelKey)
return apiequality.Semantic.DeepEqual(t1, t2)
}
func findNewReplicaSet(deployment *appsv1.Deployment, rsList []*appsv1.ReplicaSet) *appsv1.ReplicaSet {
sort.Sort(replicaSetsByCreationTimestamp(rsList))
for _, r := range rsList {
if equalIgnoreHash(&deployment.Spec.Template, &r.Spec.Template) {
return r
}
}
return nil
}
func findOldReplicaSets(rsList []*appsv1.ReplicaSet, newRs *appsv1.ReplicaSet) []*appsv1.ReplicaSet {
var oldRSs []*appsv1.ReplicaSet
for _, rs := range rsList {
if newRs != nil && newRs.UID == rs.UID {
continue
}
oldRSs = append(oldRSs, rs)
}
return oldRSs
}
type replicaSetsByCreationTimestamp []*appsv1.ReplicaSet
func (r replicaSetsByCreationTimestamp) Len() int {
return len(r)
}
func (r replicaSetsByCreationTimestamp) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
func (r replicaSetsByCreationTimestamp) Less(i, j int) bool {
if r[i].CreationTimestamp.Equal(&r[j].CreationTimestamp) {
return r[i].Name < r[j].Name
}
return r[i].CreationTimestamp.Before(&r[j].CreationTimestamp)
}
func listReplicaSets(appsClient appsv1client.AppsV1Interface, deployment *appsv1.Deployment) ([]*appsv1.ReplicaSet, error) {
labelSelector, err := metav1.LabelSelectorAsSelector(deployment.Spec.Selector)
if err != nil {
return nil, err
}
options := metav1.ListOptions{LabelSelector: labelSelector.String()}
rsList, err := appsClient.ReplicaSets(deployment.Namespace).List(context.TODO(), options)
if err != nil {
return nil, err
}
list := rsList.Items
// 获取属于该deployment的ReplicaSet
owned := make([]*appsv1.ReplicaSet, 0, len(list))
for i := range list {
if metav1.IsControlledBy(&list[i], deployment) {
owned = append(owned, &list[i])
}
}
return owned, nil
}
type StatefulSetViewer struct {
c kubernetes.Interface
}
func (h *StatefulSetViewer) ViewHistory(namespace, name string) ([]*RevisionHistory, []*RevisionHistory, error) {
return nil, nil, fmt.Errorf("not implement")
}
type DaemonsetViewer struct {
c kubernetes.Interface
}
func (h *DaemonsetViewer) ViewHistory(namespace, name string) ([]*RevisionHistory, []*RevisionHistory, error) {
return nil, nil, fmt.Errorf("not implement")
}