forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
health.go
99 lines (81 loc) · 3.51 KB
/
health.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
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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 kubernetes
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
kubermaticv1 "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1"
kubermaticv1helper "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1/helper"
"github.com/kubermatic/kubermatic/api/pkg/provider/kubernetes"
"github.com/kubermatic/kubermatic/api/pkg/resources"
)
func (r *Reconciler) clusterHealth(ctx context.Context, cluster *kubermaticv1.Cluster) (*kubermaticv1.ExtendedClusterHealth, error) {
ns := kubernetes.NamespaceName(cluster.Name)
extendedHealth := cluster.Status.ExtendedHealth.DeepCopy()
type depInfo struct {
healthStatus *kubermaticv1.HealthStatus
minReady int32
}
healthMapping := map[string]*depInfo{
resources.ApiserverDeploymentName: {healthStatus: &extendedHealth.Apiserver, minReady: 1},
resources.ControllerManagerDeploymentName: {healthStatus: &extendedHealth.Controller, minReady: 1},
resources.SchedulerDeploymentName: {healthStatus: &extendedHealth.Scheduler, minReady: 1},
resources.MachineControllerDeploymentName: {healthStatus: &extendedHealth.MachineController, minReady: 1},
resources.OpenVPNServerDeploymentName: {healthStatus: &extendedHealth.OpenVPN, minReady: 1},
resources.UserClusterControllerDeploymentName: {healthStatus: &extendedHealth.UserClusterControllerManager, minReady: 1},
}
for name := range healthMapping {
key := types.NamespacedName{Namespace: ns, Name: name}
status, err := resources.HealthyDeployment(ctx, r, key, healthMapping[name].minReady)
if err != nil {
return nil, fmt.Errorf("failed to get dep health %q: %v", name, err)
}
*healthMapping[name].healthStatus = kubermaticv1helper.GetHealthStatus(status, cluster)
}
var err error
key := types.NamespacedName{Namespace: ns, Name: resources.EtcdStatefulSetName}
etcdHealthStatus, err := resources.HealthyStatefulSet(ctx, r, key, 2)
if err != nil {
return nil, fmt.Errorf("failed to get etcd health: %v", err)
}
extendedHealth.Etcd = kubermaticv1helper.GetHealthStatus(etcdHealthStatus, cluster)
return extendedHealth, nil
}
func (r *Reconciler) syncHealth(ctx context.Context, cluster *kubermaticv1.Cluster) error {
extendedHealth, err := r.clusterHealth(ctx, cluster)
if err != nil {
return err
}
if cluster.Status.ExtendedHealth != *extendedHealth {
err = r.updateCluster(ctx, cluster, func(c *kubermaticv1.Cluster) {
c.Status.ExtendedHealth = *extendedHealth
})
}
if err != nil {
return err
}
if !cluster.Status.HasConditionValue(kubermaticv1.ClusterConditionClusterInitialized, corev1.ConditionTrue) && kubermaticv1helper.IsClusterInitialized(cluster) {
err = r.updateCluster(ctx, cluster, func(c *kubermaticv1.Cluster) {
kubermaticv1helper.SetClusterCondition(
c,
kubermaticv1.ClusterConditionClusterInitialized,
corev1.ConditionTrue,
"",
"Cluster has been initialized successfully",
)
})
}
return err
}