/
pods_finder.go
171 lines (160 loc) · 5.54 KB
/
pods_finder.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
/*
Copyright 2021 The Kruise Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controllerfinder
import (
"context"
"github.com/openkruise/kruise/pkg/util"
utilclient "github.com/openkruise/kruise/pkg/util/client"
"github.com/openkruise/kruise/pkg/util/fieldindex"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
kubecontroller "k8s.io/kubernetes/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// GetPodsForRef return target workload's podList and spec.replicas.
func (r *ControllerFinder) GetPodsForRef(apiVersion, kind, ns, name string, active bool) ([]*corev1.Pod, int32, error) {
var workloadUIDs []types.UID
var workloadReplicas int32
var labelSelector *metav1.LabelSelector
switch kind {
// ReplicaSet
case ControllerKindRS.Kind:
rs, err := r.getReplicaSet(ControllerReference{APIVersion: apiVersion, Kind: kind, Name: name}, ns)
if err != nil {
return nil, -1, err
} else if rs == nil || !rs.DeletionTimestamp.IsZero() {
return nil, 0, nil
}
workloadReplicas = *rs.Spec.Replicas
workloadUIDs = append(workloadUIDs, rs.UID)
// statefulset, rc, cloneSet
case ControllerKindSS.Kind, ControllerKindRC.Kind, ControllerKruiseKindCS.Kind, ControllerKruiseKindSS.Kind:
obj, err := r.GetScaleAndSelectorForRef(apiVersion, kind, ns, name, "")
if err != nil {
return nil, -1, err
} else if obj == nil || !obj.Metadata.DeletionTimestamp.IsZero() {
return nil, 0, nil
}
workloadReplicas = obj.Scale
workloadUIDs = append(workloadUIDs, obj.UID)
// Deployment
case ControllerKindDep.Kind:
obj, err := r.GetScaleAndSelectorForRef(apiVersion, kind, ns, name, "")
if err != nil {
return nil, -1, err
} else if obj == nil || !obj.Metadata.DeletionTimestamp.IsZero() {
return nil, 0, nil
}
workloadReplicas = obj.Scale
// try to get replicaSets
rss, err := r.getReplicaSetsForObject(obj)
if err != nil {
return nil, -1, err
}
if len(rss) == 0 {
workloadUIDs = append(workloadUIDs, obj.UID)
} else {
for _, rs := range rss {
workloadUIDs = append(workloadUIDs, rs.UID)
}
}
// The Other custom workload(support scale sub-resources)
default:
obj, err := r.GetScaleAndSelectorForRef(apiVersion, kind, ns, name, "")
if err != nil {
return nil, -1, err
} else if obj == nil || !obj.Metadata.DeletionTimestamp.IsZero() {
return nil, 0, nil
}
workloadReplicas = obj.Scale
labelSelector = obj.Selector
workloadUIDs = append(workloadUIDs, obj.UID)
}
if workloadReplicas == 0 {
return nil, workloadReplicas, nil
}
listPods := func(listOption *client.ListOptions) ([]*corev1.Pod, error) {
matchedPods := make([]*corev1.Pod, 0)
podList := &corev1.PodList{}
if err := r.List(context.TODO(), podList, listOption, utilclient.DisableDeepCopy); err != nil {
return nil, err
}
for i := range podList.Items {
pod := &podList.Items[i]
// filter not active Pod if active is true.
if active && !kubecontroller.IsPodActive(pod) {
continue
}
matchedPods = append(matchedPods, pod)
}
return matchedPods, nil
}
var err error
var matchedPods []*corev1.Pod
for _, uid := range workloadUIDs {
listOption := client.ListOptions{
Namespace: ns,
FieldSelector: fields.SelectorFromSet(fields.Set{fieldindex.IndexNameForOwnerRefUID: string(uid)}),
}
pods, err := listPods(&listOption)
if err != nil {
return nil, -1, err
}
matchedPods = append(matchedPods, pods...)
}
// For such workloads like Deployment that do not manage the Pods directly,
// Pods' ownerReferences do not contain the workload, so we have to retry
// to use the label selector to list the Pods.
if labelSelector != nil && len(matchedPods) == 0 {
selector, _ := metav1.LabelSelectorAsSelector(labelSelector)
listOption := client.ListOptions{
Namespace: ns,
LabelSelector: selector,
}
matchedPods, err = listPods(&listOption)
if err != nil {
return nil, -1, err
}
}
return matchedPods, workloadReplicas, nil
}
func (r *ControllerFinder) getReplicaSetsForObject(scale *ScaleAndSelector) ([]appsv1.ReplicaSet, error) {
// List ReplicaSets owned by this Deployment
rsList := &appsv1.ReplicaSetList{}
selector, err := util.ValidatedLabelSelectorAsSelector(scale.Selector)
if err != nil {
klog.Warningf("Object (%s/%s) get labelSelector failed: %s", scale.Metadata.Namespace, scale.Metadata.Name, err.Error())
return nil, nil
}
err = r.List(context.TODO(), rsList, &client.ListOptions{Namespace: scale.Metadata.Namespace, LabelSelector: selector}, utilclient.DisableDeepCopy)
if err != nil {
return nil, err
}
rss := make([]appsv1.ReplicaSet, 0)
for i := range rsList.Items {
rs := rsList.Items[i]
// This method is used to list the pods of the deployment, so the rs of spec.replicas == 0 cannot be ignored,
// because even if rs.spec.replicas == 0, rs may still contain pods.
if !rs.DeletionTimestamp.IsZero() {
continue
}
if ref := metav1.GetControllerOf(&rs); ref != nil && ref.UID == scale.UID {
rss = append(rss, rs)
}
}
return rss, nil
}