-
Notifications
You must be signed in to change notification settings - Fork 2
/
info.go
195 lines (172 loc) · 5.92 KB
/
info.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
/**
* Copyright 2024 The KusionStack 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
*
* https://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 workload
import (
"context"
"fmt"
"reflect"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/conversion"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"kusionstack.io/kube-utils/multicluster/clusterinfo"
"sigs.k8s.io/controller-runtime/pkg/client"
rolloutapi "kusionstack.io/rollout/apis/rollout"
rolloutv1alpha1 "kusionstack.io/rollout/apis/rollout/v1alpha1"
"kusionstack.io/rollout/pkg/utils"
)
// workload info
type Info struct {
metav1.ObjectMeta
// GVK is the GroupVersionKind of the workload.
schema.GroupVersionKind
// Status is the status of the workload.
Status InfoStatus
// Object is the object representing the workload.
Object client.Object
}
// workload status
type InfoStatus struct {
// ObservedGeneration is the most recent generation observed for this workload.
ObservedGeneration int64
// StableRevision is the old stable revision used to generate pods.
StableRevision string
// UpdatedRevision is the updated template revision used to generate pods.
UpdatedRevision string
// Replicas is the desired number of pods targeted by workload
Replicas int32
// UpdatedReplicas is the number of pods targeted by workload that have the updated template spec.
UpdatedReplicas int32
// UpdatedReadyReplicas is the number of ready pods targeted by workload that have the updated template spec.
UpdatedReadyReplicas int32
// UpdatedAvailableReplicas is the number of service available pods targeted by workload that have the updated template spec.
UpdatedAvailableReplicas int32
}
func NewInfo(cluster string, gvk schema.GroupVersionKind, obj client.Object, status InfoStatus) *Info {
return &Info{
ObjectMeta: metav1.ObjectMeta{
Namespace: obj.GetNamespace(),
Name: obj.GetName(),
Labels: obj.GetLabels(),
ClusterName: cluster,
Generation: obj.GetGeneration(),
},
GroupVersionKind: gvk,
Status: status,
Object: obj,
}
}
func (o *Info) NamespacedName() types.NamespacedName {
return types.NamespacedName{
Namespace: o.Namespace,
Name: o.Name,
}
}
func (o *Info) String() string {
return rolloutv1alpha1.CrossClusterObjectNameReference{Cluster: o.ClusterName, Name: o.Name}.String()
}
func (o *Info) CheckUpdatedReady(replicas int32) bool {
if o.Generation != o.Status.ObservedGeneration {
return false
}
return o.Status.UpdatedAvailableReplicas >= replicas
}
func (o *Info) APIStatus() rolloutv1alpha1.RolloutWorkloadStatus {
return rolloutv1alpha1.RolloutWorkloadStatus{
RolloutReplicasSummary: rolloutv1alpha1.RolloutReplicasSummary{
Replicas: o.Status.Replicas,
UpdatedReplicas: o.Status.UpdatedReplicas,
UpdatedReadyReplicas: o.Status.UpdatedReadyReplicas,
UpdatedAvailableReplicas: o.Status.UpdatedAvailableReplicas,
},
Generation: o.Generation,
ObservedGeneration: o.Status.ObservedGeneration,
StableRevision: o.Status.StableRevision,
UpdatedRevision: o.Status.UpdatedRevision,
Cluster: o.ClusterName,
Name: o.Name,
}
}
func (o *Info) UpdateOnConflict(ctx context.Context, client client.Client, mutateFn func(client.Object) error) (bool, error) {
ctx = clusterinfo.WithCluster(ctx, o.ClusterName)
obj := o.Object
updated, err := utils.UpdateOnConflict(ctx, client, client, obj, func() error {
return mutateFn(obj)
})
if err != nil {
return false, err
}
if updated {
o.Object = obj
}
return updated, nil
}
func IsWaitingRollout(info Info) bool {
if len(info.Status.StableRevision) != 0 &&
info.Status.StableRevision != info.Status.UpdatedRevision &&
info.Status.UpdatedReplicas == 0 {
return true
}
return false
}
func Get(ctx context.Context, c client.Client, inter Accessor, cluster, namespace, name string) (*Info, error) {
obj := inter.NewObject()
ctx = clusterinfo.WithCluster(ctx, cluster)
if err := c.Get(ctx, client.ObjectKey{Namespace: namespace, Name: name}, obj); err != nil {
return nil, err
}
return inter.GetInfo(cluster, obj)
}
func List(ctx context.Context, c client.Client, inter Accessor, namespace string, match rolloutv1alpha1.ResourceMatch) ([]*Info, error) {
listObj := inter.NewObjectList()
if err := c.List(clusterinfo.WithCluster(ctx, clusterinfo.Clusters), listObj, &client.ListOptions{Namespace: namespace}); err != nil {
return nil, err
}
matcher := MatchAsMatcher(match)
listPtr, err := meta.GetItemsPtr(listObj)
if err != nil {
return nil, err
}
v, err := conversion.EnforcePtr(listPtr)
if err != nil || v.Kind() != reflect.Slice {
return nil, fmt.Errorf("neet ptr to slice: %v", err)
}
length := v.Len()
workloads := make([]*Info, 0)
for i := 0; i < length; i++ {
elemPtr := v.Index(i).Addr().Interface()
obj, ok := elemPtr.(client.Object)
if !ok {
return nil, fmt.Errorf("can not convert element to client.Object")
}
canary := utils.GetMapValueByDefault(obj.GetLabels(), rolloutapi.LabelCanary, "false")
if canary == "true" {
// ignore canary workload here, you should get canary worload from release control interface
continue
}
cluster := GetClusterFromLabel(obj.GetLabels())
if !matcher.Matches(cluster, obj.GetName(), obj.GetLabels()) {
continue
}
info, err := inter.GetInfo(cluster, obj)
if err != nil {
return nil, err
}
workloads = append(workloads, info)
}
return workloads, nil
}