forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_handler.go
162 lines (148 loc) · 4.92 KB
/
list_handler.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
package logging
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
corev1 "github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
loggingconfig "github.com/rancher/rancher/pkg/controllers/user/logging/config"
)
const (
failed = "Failed"
running = "Running"
pending = "Pending"
)
type ClusterLoggingHandler struct {
ClusterLoggingLister v3.ClusterLoggingLister
PodLister corev1.PodLister
ServiceLister corev1.ServiceLister
}
func (h *ClusterLoggingHandler) ListHandler(request *types.APIContext, _ types.RequestHandler) error {
if request.ID != "" {
strArray := strings.Split(request.ID, ":")
if len(strArray) != 2 {
return nil
}
clusterName := strArray[0]
id := strArray[1]
cl, err := h.ClusterLoggingLister.Get(clusterName, id)
if err != nil {
return httperror.NewAPIError(httperror.ServerError, fmt.Sprintf("%v", err))
}
if cl.Spec.EmbeddedConfig != nil {
if err = h.setEmbeddedEndpoint(cl); err != nil {
cl.Spec.EmbeddedConfig.ElasticsearchEndpoint = failed
cl.Spec.EmbeddedConfig.KibanaEndpoint = failed
logrus.Warnf("set embedded endpoint failed, %v", err)
}
}
clusterLoggingData, err := convertToClusterResource(request.Schema, cl)
if err != nil {
return httperror.NewAPIError(httperror.ServerError, fmt.Sprintf("%v", err))
}
request.WriteResponse(http.StatusOK, clusterLoggingData)
return nil
}
cls, err := h.ClusterLoggingLister.List("", labels.NewSelector())
if err != nil {
return httperror.NewAPIError(httperror.ServerError, fmt.Sprintf("%v", err))
}
clusterLoggingDatas := make([]map[string]interface{}, len(cls))
for _, cl := range cls {
if cl.Spec.EmbeddedConfig != nil {
if err = h.setEmbeddedEndpoint(cl); err != nil {
cl.Spec.EmbeddedConfig.ElasticsearchEndpoint = failed
cl.Spec.EmbeddedConfig.KibanaEndpoint = failed
logrus.Warnf("set embedded endpoint failed, %v", err)
}
}
clData, err := convertToClusterResource(request.Schema, cl)
if err != nil {
return httperror.NewAPIError(httperror.ServerError, fmt.Sprintf("%v", err))
}
clusterLoggingDatas = append(clusterLoggingDatas, clData)
}
request.WriteResponse(http.StatusOK, clusterLoggingDatas)
return nil
}
func convertToClusterResource(schema *types.Schema, clusterLogging *v3.ClusterLogging) (map[string]interface{}, error) {
clusterLoggingData, err := convert.EncodeToMap(clusterLogging)
if err != nil {
return nil, err
}
mapper := schema.Mapper
if mapper == nil {
return nil, errors.New("no schema mapper available")
}
mapper.FromInternal(clusterLoggingData)
return clusterLoggingData, nil
}
func (h *ClusterLoggingHandler) setEmbeddedEndpoint(cl *v3.ClusterLogging) error {
selector := labels.NewSelector()
requirement, err := labels.NewRequirement(loggingconfig.LabelK8sApp, selection.Equals, []string{loggingconfig.EmbeddedESName})
if err != nil {
return err
}
espods, err := h.PodLister.List(loggingconfig.LoggingNamespace, selector.Add(*requirement))
if err != nil {
return err
}
esservice, err := h.ServiceLister.Get(loggingconfig.LoggingNamespace, loggingconfig.EmbeddedESName)
if err != nil {
return err
}
if len(esservice.Spec.Ports) == 0 {
return fmt.Errorf("get service %s node port failed", loggingconfig.EmbeddedESName)
}
var esPort int32
for _, v := range esservice.Spec.Ports {
if v.Name == "http" {
esPort = v.NodePort
break
}
}
if len(espods) == 0 {
cl.Spec.EmbeddedConfig.ElasticsearchEndpoint = pending
} else {
espod := espods[0]
if espod.Status.Phase == running {
cl.Spec.EmbeddedConfig.ElasticsearchEndpoint = fmt.Sprintf("http://%s:%v", espod.Status.HostIP, esPort)
} else {
cl.Spec.EmbeddedConfig.ElasticsearchEndpoint = fmt.Sprintf("%s", espod.Status.Phase)
}
}
selector = labels.NewSelector()
requirement, err = labels.NewRequirement(loggingconfig.LabelK8sApp, selection.Equals, []string{loggingconfig.EmbeddedKibanaName})
if err != nil {
return err
}
kibanapods, err := h.PodLister.List(loggingconfig.LoggingNamespace, selector.Add(*requirement))
if err != nil {
return err
}
kibanaservice, err := h.ServiceLister.Get(loggingconfig.LoggingNamespace, loggingconfig.EmbeddedKibanaName)
if err != nil {
return err
}
if len(kibanaservice.Spec.Ports) == 0 {
return fmt.Errorf("get service %s node port failed", loggingconfig.EmbeddedKibanaName)
}
if len(kibanapods) == 0 {
cl.Spec.EmbeddedConfig.KibanaEndpoint = pending
} else {
kibanapod := kibanapods[0]
if kibanapod.Status.Phase == running {
cl.Spec.EmbeddedConfig.KibanaEndpoint = fmt.Sprintf("http://%s:%v", kibanapod.Status.HostIP, kibanaservice.Spec.Ports[0].NodePort)
} else {
cl.Spec.EmbeddedConfig.KibanaEndpoint = fmt.Sprintf("%s", kibanapod.Status.Phase)
}
}
return nil
}