forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
134 lines (126 loc) · 4 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
package multiclusterapp
import (
"context"
"fmt"
"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"
"strings"
)
const (
installing = "installing"
deploying = "deploying"
active = "active"
)
//StartMCAppStateController gets all corresponding apps and update condition on multi cluster app sync
func StartMCAppStateController(ctx context.Context, management *config.ManagementContext) {
mcApps := management.Management.MultiClusterApps("")
s := &MCAppStateController{
Apps: management.Project.Apps("").Controller().Lister(),
MultiClusterApps: mcApps,
}
mcApps.AddHandler(ctx, "multi-cluster-app-state-controller", s.sync)
}
type MCAppStateController struct {
Apps pv3.AppLister
MultiClusterApps v3.MultiClusterAppInterface
}
func (m *MCAppStateController) sync(key string, mcapp *v3.MultiClusterApp) (runtime.Object, error) {
if mcapp == nil || mcapp.DeletionTimestamp != nil {
return mcapp, nil
}
mcappState := active
if v3.MultiClusterAppConditionInstalled.IsUnknown(mcapp) && v3.MultiClusterAppConditionInstalled.GetMessage(mcapp) == "upgrading" {
mcappState = ""
}
var toUpdate *v3.MultiClusterApp
for ind, t := range mcapp.Spec.Targets {
if t.AppName == "" {
mcappState = installing
continue
}
split := strings.SplitN(t.ProjectName, ":", 2)
if len(split) != 2 {
return mcapp, fmt.Errorf("error in splitting project ID %v", t.ProjectName)
}
projectNS := split[1]
app, err := m.Apps.Get(projectNS, t.AppName)
if err != nil {
if errors.IsNotFound(err) {
logrus.Debugf("app %s not found for mcapp %s in projectNS %s", t.AppName, mcapp.Name, projectNS)
if mcappState != "" {
mcappState = installing
}
continue
}
return mcapp, err
}
if value, ok := app.Labels[MultiClusterAppIDSelector]; !ok || value != mcapp.Name {
return mcapp, fmt.Errorf("app %s missing label selector for %s", t.AppName, mcapp.Name)
}
if !pv3.AppConditionInstalled.IsTrue(app) {
toUpdate = setAppState(toUpdate, ind, installing, mcapp)
if mcappState != "" {
mcappState = installing
}
} else if !pv3.AppConditionDeployed.IsTrue(app) {
toUpdate = setAppState(toUpdate, ind, deploying, mcapp)
if mcappState == active {
mcappState = deploying
}
} else {
toUpdate = setAppState(toUpdate, ind, active, mcapp)
}
}
if mcappState == installing || mcappState == deploying {
toUpdate = setMcAppStateUnknown(toUpdate, mcapp, mcappState)
} else if mcappState == active {
toUpdate = setMcappStateActive(toUpdate, mcapp)
}
if toUpdate != nil {
return m.MultiClusterApps.Update(toUpdate)
}
return mcapp, nil
}
func setAppState(toUpdate *v3.MultiClusterApp, ind int, state string, mcapp *v3.MultiClusterApp) *v3.MultiClusterApp {
if mcapp.Spec.Targets[ind].State == state {
return toUpdate
}
if toUpdate == nil {
toUpdate = mcapp.DeepCopy()
}
toUpdate.Spec.Targets[ind].State = state
return toUpdate
}
func setMcappStateActive(toUpdate *v3.MultiClusterApp, mcapp *v3.MultiClusterApp) *v3.MultiClusterApp {
if v3.MultiClusterAppConditionInstalled.IsTrue(mcapp) && v3.MultiClusterAppConditionDeployed.IsTrue(mcapp) {
return toUpdate
}
if toUpdate == nil {
toUpdate = mcapp.DeepCopy()
}
v3.MultiClusterAppConditionDeployed.True(toUpdate)
v3.MultiClusterAppConditionInstalled.True(toUpdate)
return toUpdate
}
func setMcAppStateUnknown(toUpdate *v3.MultiClusterApp, mcapp *v3.MultiClusterApp, state string) *v3.MultiClusterApp {
cond := v3.MultiClusterAppConditionInstalled
cleanCond := v3.MultiClusterAppConditionDeployed
if state == deploying {
cond = v3.MultiClusterAppConditionDeployed
cleanCond = v3.MultiClusterAppConditionInstalled
}
if !cond.IsUnknown(mcapp) {
if toUpdate == nil {
toUpdate = mcapp.DeepCopy()
}
if cleanCond.IsUnknown(toUpdate) {
cleanCond.True(toUpdate)
}
cond.Unknown(toUpdate)
}
return toUpdate
}