forked from argoproj/gitops-engine
/
health_ingress.go
71 lines (65 loc) · 2.35 KB
/
health_ingress.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
package health
import (
"fmt"
"github.com/argoproj/gitops-engine/pkg/utils/kube"
extv1beta1 "k8s.io/api/extensions/v1beta1"
networkingv1 "k8s.io/api/networking/v1"
networkingv1beta1 "k8s.io/api/networking/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)
func getIngressHealth(obj *unstructured.Unstructured) (*HealthStatus, error) {
gvk := obj.GroupVersionKind()
switch gvk {
case networkingv1.SchemeGroupVersion.WithKind(kube.IngressKind):
var ingress networkingv1.Ingress
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &ingress)
if err != nil {
return nil, fmt.Errorf("failed to convert unstructured Ingress to typed: %v", err)
}
return getNetworkingv1IngressHealth(&ingress)
case networkingv1beta1.SchemeGroupVersion.WithKind(kube.IngressKind):
var ingress networkingv1beta1.Ingress
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &ingress)
if err != nil {
return nil, fmt.Errorf("failed to convert unstructured Ingress to typed: %v", err)
}
return getNetworkingv1beta1IngressHealth(&ingress)
case extv1beta1.SchemeGroupVersion.WithKind(kube.IngressKind):
var ingress extv1beta1.Ingress
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &ingress)
if err != nil {
return nil, fmt.Errorf("failed to convert unstructured Ingress to typed: %v", err)
}
return getExtv1beta1IngressHealth(&ingress)
default:
return nil, fmt.Errorf("unsupported Ingress GVK: %s", gvk)
}
}
func getNetworkingv1IngressHealth(ingress *networkingv1.Ingress) (*HealthStatus, error) {
health := HealthStatus{}
if len(ingress.Status.LoadBalancer.Ingress) > 0 {
health.Status = HealthStatusHealthy
} else {
health.Status = HealthStatusProgressing
}
return &health, nil
}
func getNetworkingv1beta1IngressHealth(ingress *networkingv1beta1.Ingress) (*HealthStatus, error) {
health := HealthStatus{}
if len(ingress.Status.LoadBalancer.Ingress) > 0 {
health.Status = HealthStatusHealthy
} else {
health.Status = HealthStatusProgressing
}
return &health, nil
}
func getExtv1beta1IngressHealth(ingress *extv1beta1.Ingress) (*HealthStatus, error) {
health := HealthStatus{}
if len(ingress.Status.LoadBalancer.Ingress) > 0 {
health.Status = HealthStatusHealthy
} else {
health.Status = HealthStatusProgressing
}
return &health, nil
}