forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
172 lines (160 loc) · 4.88 KB
/
state.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
package helm
import (
"context"
"fmt"
"strings"
"github.com/rancher/norman/types/convert"
util "github.com/rancher/rancher/pkg/controllers/user/workload"
corev1 "github.com/rancher/types/apis/core/v1"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
pv3 "github.com/rancher/types/apis/project.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
)
const (
projectLabel = "field.cattle.io/projectId"
)
func StartStateCalculator(ctx context.Context, cluster *config.UserContext) {
apps := cluster.Management.Project.Apps("")
w := &AppStateCalculator{
appLister: apps.Controller().Lister(),
apps: apps,
multiClusterApps: cluster.Management.Management.MultiClusterApps(""),
namespaceLister: cluster.Core.Namespaces("").Controller().Lister(),
projectLister: cluster.Management.Management.Projects("").Controller().Lister(),
clusterName: cluster.ClusterName,
}
w.workloadLister = util.NewWorkloadController(ctx, cluster.UserOnlyContext(), w.sync)
apps.Controller().AddHandler(ctx, "app-state-controller", w.syncAppState)
}
type AppStateCalculator struct {
apps pv3.AppInterface
appLister pv3.AppLister
namespaceLister corev1.NamespaceLister
multiClusterApps v3.MultiClusterAppInterface
workloadLister util.CommonController
projectLister v3.ProjectLister
clusterName string
}
func (s *AppStateCalculator) sync(key string, obj *util.Workload) error {
if obj == nil {
return nil
}
if label, ok := obj.Labels[appLabel]; ok {
ns, err := s.namespaceLister.Get("", obj.Namespace)
if err != nil {
if !errors.IsNotFound(err) {
return err
}
logrus.Errorf("error getting namespace %s for app %s: %v", obj.Namespace, obj.Name, err)
return nil
}
projectNS := ns.Labels[projectLabel]
if projectNS == "" {
return fmt.Errorf("cannot find app namespace in labels of %s", obj.Namespace)
}
app, err := s.appLister.Get(projectNS, label)
if err != nil {
if !errors.IsNotFound(err) {
return err
}
logrus.Errorf("error getting app %s %v", label, err)
return nil
}
if app != nil && app.DeletionTimestamp == nil {
s.apps.Controller().Enqueue(app.Namespace, app.Name)
}
}
return nil
}
func (s *AppStateCalculator) syncAppState(key string, app *pv3.App) (runtime.Object, error) {
if app == nil || app.DeletionTimestamp != nil {
return nil, nil
}
if !pv3.AppConditionInstalled.IsTrue(app) {
return app, nil
}
project, err := s.projectLister.Get(s.clusterName, app.Namespace)
if err != nil && !errors.IsNotFound(err) {
return app, err
}
if err != nil || project == nil || project.DeletionTimestamp != nil {
return app, nil
}
workloads, err := s.getWorkloadsByApp(app.Name)
if err != nil {
return app, fmt.Errorf("error getting workloads %v", err)
}
updatingWorkloads := getUpdating(workloads)
toUpdate := app.DeepCopy()
if len(updatingWorkloads) == 0 {
if pv3.AppConditionDeployed.IsTrue(toUpdate) {
return app, nil
}
pv3.AppConditionDeployed.True(toUpdate)
pv3.AppConditionDeployed.Reason(toUpdate, "")
pv3.AppConditionDeployed.Message(toUpdate, "")
} else {
existing := strings.Split(v3.MultiClusterAppConditionDeployed.GetMessage(toUpdate), ",")
if Equal(existing, updatingWorkloads) {
return app, nil
}
pv3.AppConditionDeployed.Unknown(toUpdate)
pv3.AppConditionDeployed.Message(toUpdate, GetMsg(updatingWorkloads))
pv3.AppConditionDeployed.Reason(toUpdate, "workloads are updating")
}
return s.apps.Update(toUpdate)
}
func (s *AppStateCalculator) getWorkloadsByApp(name string) ([]*util.Workload, error) {
label := map[string]string{
appLabel: name,
}
workloads, err := s.workloadLister.GetWorkloadsMatchingSelector("", label)
if err != nil {
return nil, err
}
return workloads, nil
}
func getUpdating(workloads []*util.Workload) map[string]bool {
updatingWorkloads := map[string]bool{}
for _, workload := range workloads {
if workload.Kind == util.ReplicaSetType {
continue
}
if hasUpdatingCondition(workload.Status.Conditions) {
updatingWorkloads[workload.Name] = true
} else if workload.Status.Replicas != workload.Status.AvailableReplicas {
updatingWorkloads[workload.Name] = true
}
}
return updatingWorkloads
}
func hasUpdatingCondition(conditions []map[string]interface{}) bool {
for _, condition := range conditions {
status := convert.ToString(condition["status"])
if status == "False" || status == "Unknown" {
return true
}
}
return false
}
func GetMsg(data map[string]bool) string {
var keys []string
for key := range data {
keys = append(keys, key)
}
return fmt.Sprintf("%s", strings.Join(keys, ","))
}
func Equal(existing []string, toUpdate map[string]bool) bool {
if len(existing) != len(toUpdate) {
return false
}
for _, name := range existing {
if _, ok := toUpdate[name]; !ok {
return false
}
}
return true
}