forked from tektoncd/dashboard
/
cluster.go
208 lines (184 loc) · 8.03 KB
/
cluster.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Copyright 2019 The Tekton Authors
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 endpoints
import (
"errors"
"net/http"
"net/url"
"os"
"strconv"
"strings"
restful "github.com/emicklei/go-restful"
"github.com/tektoncd/dashboard/pkg/logging"
"github.com/tektoncd/dashboard/pkg/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// Properties : properties we want to be able to retrieve via REST
type Properties struct {
InstallNamespace string `json:"InstallNamespace"`
DashboardVersion string `json:"DashboardVersion"`
PipelineVersion string `json:"PipelineVersion"`
TriggersVersion string `json:"TriggersVersion,omitempty"`
IsOpenShift bool `json:"IsOpenShift"`
ReadOnly bool `json:"ReadOnly"`
}
const (
tektonDashboardIngressName string = "tekton-dashboard"
tektonDashboardRouteName string = "tekton-dashboard"
defaultTriggersNamespace string = "tekton-pipelines"
defaultPipelineNamespace string = "tekton-pipelines"
triggersNamespaceEnvVar string = "TRIGGERS_NAMESPACE"
pipelineNamespaceEnvVar string = "PIPELINE_NAMESPACE"
)
// ProxyRequest does as the name suggests: proxies requests and logs what's going on
func (r Resource) ProxyRequest(request *restful.Request, response *restful.Response) {
parsedURL, err := url.Parse(request.Request.URL.String())
if err != nil {
utils.RespondError(response, err, http.StatusNotFound)
return
}
uri := request.PathParameter("subpath") + "?" + parsedURL.RawQuery
forwardRequest := r.K8sClient.CoreV1().RESTClient().Verb(request.Request.Method).RequestURI(uri).Body(request.Request.Body)
forwardRequest.SetHeader("Content-Type", request.HeaderParameter("Content-Type"))
forwardResponse := forwardRequest.Do()
responseBody, requestError := forwardResponse.Raw()
if requestError != nil {
errorInfo := string(responseBody)
errorInfo = strings.Replace(errorInfo, "\"", "", -1)
errorInfo = strings.Replace(errorInfo, "\\", "", -1)
errorInfo = strings.Replace(errorInfo, "\n", "", -1)
// Checks if an error code can be found in the response
if strings.LastIndex(errorInfo, "code:") != -1 {
errorCodeString := strings.LastIndex(errorInfo, "code:")
// Checks if the code is 3-digits long
if len(errorInfo[errorCodeString+5:errorCodeString+8]) == 3 {
errorCode := errorInfo[errorCodeString+5 : errorCodeString+8]
errorCodeFormatted, err := strconv.Atoi(errorCode)
// Checks if the code can be converted to an integer without error
if err != nil {
utils.RespondError(response, requestError, http.StatusInternalServerError)
return
}
utils.RespondError(response, errors.New(errorInfo), errorCodeFormatted)
return
}
utils.RespondError(response, requestError, http.StatusInternalServerError)
return
}
utils.RespondError(response, requestError, http.StatusInternalServerError)
return
}
response.Header().Add("Content-Type", utils.GetContentType(responseBody))
response.Write(responseBody)
}
// GetIngress returns the Ingress endpoint called "tektonDashboardIngressName" in the requested namespace
func (r Resource) GetIngress(request *restful.Request, response *restful.Response) {
requestNamespace := utils.GetNamespace(request)
ingress, err := r.K8sClient.ExtensionsV1beta1().Ingresses(requestNamespace).Get(tektonDashboardIngressName, metav1.GetOptions{})
if err != nil || ingress == nil {
logging.Log.Errorf("Unable to retrieve any ingresses: %s", err)
utils.RespondError(response, err, http.StatusInternalServerError)
return
}
noRuleError := "no Ingress rules found labelled " + tektonDashboardIngressName
// Harden this block to avoid Go panics (array index out of range)
if len(ingress.Spec.Rules) > 0 { // Got more than zero entries?
if ingress.Spec.Rules[0].Host != "" { // For that rule, is there actually a host?
ingressHost := ingress.Spec.Rules[0].Host
response.WriteEntity(ingressHost)
return
}
logging.Log.Errorf("found an empty Ingress rule labelled %s", tektonDashboardIngressName)
} else {
logging.Log.Error(noRuleError)
}
logging.Log.Error("Unable to retrieve any Ingresses")
utils.RespondError(response, err, http.StatusInternalServerError)
return
}
// GetIngress returns the Ingress endpoint called "tektonDashboardIngressName" in the requested namespace
func (r Resource) GetEndpoints(request *restful.Request, response *restful.Response) {
type element struct {
Type string `json:"type"`
Url string `json:"url"`
}
var responses []element
requestNamespace := utils.GetNamespace(request)
route, err := r.RouteClient.RouteV1().Routes(requestNamespace).Get(tektonDashboardIngressName, metav1.GetOptions{})
noRuleError := "no Route found labelled " + tektonDashboardRouteName
if err != nil || route == nil {
logging.Log.Infof("Unable to retrieve any routes: %s", err)
} else {
if route.Spec.Host != "" { // For that rule, is there actually a host?
routeHost := route.Spec.Host
responses = append(responses, element{"Route", routeHost})
} else {
logging.Log.Error(noRuleError)
}
}
ingress, err := r.K8sClient.ExtensionsV1beta1().Ingresses(requestNamespace).Get(tektonDashboardIngressName, metav1.GetOptions{})
noRuleError = "no Ingress rules found labelled " + tektonDashboardIngressName
if err != nil || ingress == nil {
logging.Log.Infof("Unable to retrieve any ingresses: %s", err)
} else {
if len(ingress.Spec.Rules) > 0 { // Got more than zero entries?
if ingress.Spec.Rules[0].Host != "" { // For that rule, is there actually a host?
ingressHost := ingress.Spec.Rules[0].Host
responses = append(responses, element{"Ingress", ingressHost})
}
} else {
logging.Log.Error(noRuleError)
}
}
if len(responses) != 0 {
response.WriteEntity(responses)
} else {
logging.Log.Error("Unable to retrieve any Ingresses or Routes")
utils.RespondError(response, err, http.StatusInternalServerError)
}
}
// getTriggersNamespace returns the namespace where Tekton Tiggers is installed.
func getTriggersNamespace() string {
if e := os.Getenv(triggersNamespaceEnvVar); e != "" {
return e
}
return defaultTriggersNamespace
}
// getPipelineNamespace returns the namespace where Tekton Pipelines is installed.
func getPipelineNamespace() string {
if e := os.Getenv(pipelineNamespaceEnvVar); e != "" {
return e
}
return defaultPipelineNamespace
}
// GetProperties is used to get the installed namespace for the Dashboard,
// the version of the Tekton Dashboard, the version of Tekton Pipelines, whether or not one's
// running on OpenShift, when one's in read-only mode and Tekton Triggers version (if Installed)
func (r Resource) GetProperties(request *restful.Request, response *restful.Response) {
properties := Properties{}
installedNamespace := os.Getenv("INSTALLED_NAMESPACE")
triggersNamespace := getTriggersNamespace()
pipelineNamespace := getPipelineNamespace()
dashboardVersion := GetDashboardVersion(r, installedNamespace)
isOpenShift := IsOpenShift(r, installedNamespace)
isReadOnly := IsReadOnly()
pipelineVersion := GetPipelineVersion(r, pipelineNamespace, isOpenShift)
isTriggersInstalled := IsTriggersInstalled(r, triggersNamespace, isOpenShift)
if isTriggersInstalled == false {
properties = Properties{InstallNamespace: installedNamespace, DashboardVersion: dashboardVersion, PipelineVersion: pipelineVersion, IsOpenShift: isOpenShift, ReadOnly: isReadOnly}
} else {
triggersVersion := GetTriggersVersion(r, triggersNamespace, isOpenShift)
properties = Properties{InstallNamespace: installedNamespace, DashboardVersion: dashboardVersion, PipelineVersion: pipelineVersion, TriggersVersion: triggersVersion, IsOpenShift: isOpenShift, ReadOnly: isReadOnly}
}
response.WriteEntity(properties)
}