forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
appHandler.go
157 lines (130 loc) · 4.71 KB
/
appHandler.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
package istio
import (
"net/url"
"strings"
"github.com/pkg/errors"
v3 "github.com/rancher/types/apis/project.cattle.io/v3"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/runtime"
mgmtv3 "github.com/rancher/types/apis/management.cattle.io/v3"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
systemCatalogName = "system-library"
istioTemplateName = "rancher-istio"
istioAppName = "cluster-istio"
)
var (
preDefinedIstioMetrics = getPredefinedIstioMetrics()
preDefinedIstioClusterGraph = getPredefinedIstioClusterGraph()
PreDefinedIstioProjectGraph = getPredefinedIstioProjectGraph()
)
type appHandler struct {
istioClusterGraphClient mgmtv3.ClusterMonitorGraphInterface
istioMonitorMetricClient mgmtv3.MonitorMetricInterface
clusterInterface mgmtv3.ClusterInterface
clusterName string
}
func isIstioApp(obj *v3.App) bool {
if obj.Name != istioAppName {
return false
}
values, err := url.Parse(obj.Spec.ExternalID)
if err != nil {
logrus.Errorf("check catalog type failed: %s", err.Error())
return false
}
catalogWithNamespace := values.Query().Get("catalog")
split := strings.SplitN(catalogWithNamespace, "/", 2)
catalog := split[len(split)-1]
template := values.Query().Get("template")
if catalog == systemCatalogName && template == istioTemplateName {
return true
}
return false
}
func isIstioMetricExpressionDeployed(clusterName string, istioClusterGraphClient mgmtv3.ClusterMonitorGraphInterface, istioMetricsClient mgmtv3.MonitorMetricInterface, obj *v3.App) error {
_, err := v3.IstioConditionMetricExpressionDeployed.DoUntilTrue(obj, func() (runtime.Object, error) {
for _, metric := range preDefinedIstioMetrics {
newObj := metric.DeepCopy()
newObj.Namespace = clusterName
if _, err := istioMetricsClient.Create(newObj); err != nil && !apierrors.IsAlreadyExists(err) {
return obj, err
}
}
for _, clusterGraph := range preDefinedIstioClusterGraph {
newObj := clusterGraph.DeepCopy()
newObj.Namespace = clusterName
if _, err := istioClusterGraphClient.Create(newObj); err != nil && !apierrors.IsAlreadyExists(err) {
return obj, err
}
}
for _, projectGraph := range PreDefinedIstioProjectGraph {
newObj := projectGraph.DeepCopy()
newObj.Namespace = clusterName
if _, err := istioClusterGraphClient.Create(newObj); err != nil && !apierrors.IsAlreadyExists(err) {
return obj, err
}
}
return obj, nil
})
if err != nil {
return errors.Wrapf(err, "failed to deploy istio metric expression into Cluster %s", clusterName)
}
logrus.Infof("deploy istio metric expression into CLuster %s successfully", clusterName)
return nil
}
func isIstioMetricExpressionWithdraw(clusterName string, istioClusterGraphClient mgmtv3.ClusterMonitorGraphInterface, istioMetricsClient mgmtv3.MonitorMetricInterface, obj *v3.App) error {
for _, metric := range preDefinedIstioMetrics {
if err := istioMetricsClient.DeleteNamespaced(clusterName, metric.Name, &metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
return err
}
}
for _, clusterGraph := range preDefinedIstioClusterGraph {
if err := istioClusterGraphClient.DeleteNamespaced(clusterName, clusterGraph.Name, &metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
return err
}
}
for _, projectGraph := range PreDefinedIstioProjectGraph {
if err := istioClusterGraphClient.DeleteNamespaced(clusterName, projectGraph.Name, &metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
return err
}
}
logrus.Infof("withdraw istio metric expression from CLuster %s successfully", clusterName)
return nil
}
func (ah *appHandler) sync(key string, obj *v3.App) (runtime.Object, error) {
if obj == nil {
return obj, nil
}
if !isIstioApp(obj) {
return obj, nil
}
if obj == nil || obj.DeletionTimestamp != nil {
err := ah.updateClusterIstioCondition(false)
if err != nil {
return obj, err
}
return obj, isIstioMetricExpressionWithdraw(ah.clusterName, ah.istioClusterGraphClient, ah.istioMonitorMetricClient, obj)
}
if err := ah.updateClusterIstioCondition(true); err != nil {
return obj, err
}
if v3.AppConditionInstalled.IsFalse(obj) {
return obj, errors.Errorf("waiting for the app %s to be installed", obj.Name)
}
return obj, isIstioMetricExpressionDeployed(ah.clusterName, ah.istioClusterGraphClient, ah.istioMonitorMetricClient, obj)
}
func (ah *appHandler) updateClusterIstioCondition(enabled bool) error {
cluster, err := ah.clusterInterface.Get(ah.clusterName, metav1.GetOptions{})
if err != nil {
return err
}
if cluster.Status.IstioEnabled == enabled {
return nil
}
cluster.Status.IstioEnabled = enabled
_, err = ah.clusterInterface.Update(cluster)
return err
}