-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
306 lines (268 loc) · 7.68 KB
/
main.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package main
import (
"context"
"embed"
"flag"
"fmt"
"net"
"net/http"
"time"
"github.com/cezarguimaraes/tkn-dash/internal/components"
"github.com/cezarguimaraes/tkn-dash/internal/handlers"
"github.com/cezarguimaraes/tkn-dash/internal/loader"
"github.com/cezarguimaraes/tkn-dash/internal/model"
"github.com/cezarguimaraes/tkn-dash/internal/tekton"
"github.com/cezarguimaraes/tkn-dash/internal/tools"
"github.com/cezarguimaraes/tkn-dash/pkg/cache"
"github.com/go-logr/logr"
"github.com/pkg/browser"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
pipelinev1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
tektoncs "github.com/tektoncd/pipeline/pkg/client/clientset/versioned"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
)
//go:embed static/*
var static embed.FS
var (
kubeconfig = flag.String("kubeconfig", "", "(optional) path to kubeconfig")
chromaStyle = flag.String("syntax-style", "github-dark", "a valid style name from https://xyproto.github.io/splash/docs/")
addr = flag.String("addr", ":", "[address]:port to listen on")
openBrowser = flag.Bool("browser", false, "whether to try and open a browser to the dashboard")
)
func main() {
klog.InitFlags(nil)
flag.Parse()
log := klog.NewKlogr()
var trs, prs cache.Store
var kubeclientset *clientset.Clientset
if args := flag.Args(); len(args) > 0 {
log.Info("loading tekton resources from files", "files", args)
stores, err := loader.LoadLocalLists(args...)
if err != nil {
log.Error(err, "error loading tekton resources from files")
klog.FlushAndExit(10*time.Second, 1)
}
trs = stores["taskrun"]
prs = stores["pipelinerun"]
} else {
kubecfg, err := loadKubeConfig()
if err != nil {
log.Error(err, "error loading kubeconfig")
klog.FlushAndExit(10*time.Second, 1)
}
kubeclientset, err = clientset.NewForConfig(kubecfg)
if err != nil {
log.Error(err, "error initializing kubernetes clientset")
klog.FlushAndExit(10*time.Second, 1)
}
log.Info("loading tekton resources from cluster")
tcs, err := tektoncs.NewForConfig(kubecfg)
if err != nil {
log.Error(err, "error loading tekton resources from cluster")
klog.FlushAndExit(10*time.Second, 1)
}
var stopFn func()
trs, prs, stopFn = initializeStores(log, tcs)
defer stopFn()
}
nsLister := tools.NamespaceListerFromStore(trs, prs)
namespaces, err := nsLister.List(context.Background())
if err != nil {
log.Error(err, "error listing namespaces")
klog.FlushAndExit(10*time.Second, 1)
}
tknMiddleware := tekton.NewMiddleware(
prs, trs,
tekton.WithNamespaces(namespaces),
tekton.WithLogger(log),
)
e := echo.New()
e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
LogURI: true,
LogError: true,
LogRemoteIP: true,
LogHost: true,
LogMethod: true,
LogLatency: true,
LogResponseSize: true,
LogContentLength: true,
LogStatus: true,
LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
keysAndValues := []interface{}{
"method", v.Method,
"URI", v.URI,
"status", v.Status,
"remote_ip", v.RemoteIP,
"host", v.Host,
"latency", v.Latency,
"request_content_length", v.ContentLength,
"response_size", v.ResponseSize,
}
if v.Error != nil {
log.Error(v.Error, "error handling request", keysAndValues...)
} else {
log.V(2).Info("request responded", keysAndValues...)
}
return nil
},
}))
// workaround to add middleware to .StaticFS
staticGrp := e.Group("/_static",
func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Response().Header().Set(
echo.HeaderCacheControl,
"public, max-age=31536000",
)
return next(c)
}
},
)
staticGrp.StaticFS("/", echo.MustSubFS(static, "static"))
e.Use(tknMiddleware)
e.Use(middleware.Gzip())
e.GET("/*", func(c echo.Context) error {
return c.Redirect(
http.StatusFound,
c.Echo().Reverse("list", namespaces[0], "taskruns"),
)
})
e.GET("/favicon.ico", func(c echo.Context) error {
return c.String(http.StatusNotFound, "not found")
})
componentRoutes := []struct {
name, route string
component model.TektonComponent
}{
{
route: "/:namespace/:resource",
name: "list",
component: components.Shell(components.Explorer),
},
{
route: "/:namespace/:resource/:name",
name: "list-w-details",
component: components.Shell(components.Explorer),
},
{
route: "/:namespace/:resource/:taskRun/step/:step",
name: "list-w-task-details",
component: components.Shell(components.Explorer),
},
{
route: "/:namespace/:resource/:taskRun/step/:step/:tab",
name: "list-w-task-details-tab",
component: components.Shell(components.Explorer),
},
{
route: "/:namespace/:resource/:pipelineRun/taskruns/:taskRun/step/:step",
name: "list-w-pipe-details",
component: components.Shell(components.Explorer),
},
{
route: "/:namespace/:resource/:pipelineRun/taskruns/:taskRun/step/:step/:tab",
name: "list-w-pipe-details-tab",
component: components.Shell(components.Explorer),
},
{
route: "/:namespace/:resource/:name/details",
name: "details",
component: components.TaskRuns,
},
{
route: "/:namespace/details/:taskRun/step/:step",
name: "details-w-step",
component: components.TaskRunDetails(true),
},
}
for _, ct := range componentRoutes {
e.GET(
ct.route,
handlers.Component(ct.component),
).Name = ct.name
}
e.GET("/log/:namespace/:taskRun/step/:step",
handlers.StepLog(kubeclientset),
).Name = "log"
e.GET("/script/:namespace/:taskRun/step/:step",
handlers.StepScript(*chromaStyle),
).Name = "script"
e.GET("/manifest/:namespace/:taskRun/step/:step",
handlers.Manifest(*chromaStyle),
).Name = "manifest"
e.GET("/:resource/items",
handlers.Search(
components.ExplorerListItems,
),
).Name = "items"
lc := net.ListenConfig{
KeepAlive: 3 * time.Minute,
}
l, err := lc.Listen(context.Background(), "tcp4", *addr)
if err != nil {
log.Error(err, "failed to listen on specified address", *addr)
klog.FlushAndExit(10*time.Second, 1)
}
tcpAddr := l.Addr().(*net.TCPAddr)
localURL := fmt.Sprintf("http://127.0.0.1:%d", tcpAddr.Port)
log.Info("server is listening", "address", l.Addr(), "URL", localURL)
if *openBrowser {
err := browser.OpenURL(localURL)
if err != nil {
log.Error(err, "failed to open browser", "URL", localURL)
}
}
e.HideBanner = true
e.HidePort = true
e.Listener = l
if err := e.Start(*addr); err != nil {
log.Error(err, "error starting server")
}
}
func loadKubeConfig() (*rest.Config, error) {
lr := clientcmd.NewDefaultClientConfigLoadingRules()
if *kubeconfig != "" {
lr.ExplicitPath = *kubeconfig
}
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
lr,
&clientcmd.ConfigOverrides{},
)
return clientConfig.ClientConfig()
}
func initializeStores(
log logr.Logger,
tcs *tektoncs.Clientset,
) (trs cache.Store, prs cache.Store, stopFn func()) {
trInformer, trStopFn := cache.NewSharedInformerCache(
tcs.TektonV1beta1().RESTClient(),
"taskruns",
&pipelinev1beta1.TaskRun{},
)
prInformer, prStopFn := cache.NewSharedInformerCache(
tcs.TektonV1beta1().RESTClient(),
"pipelineruns",
&pipelinev1beta1.PipelineRun{},
)
stopFn = func() {
prStopFn()
trStopFn()
}
log.Info("waiting until shared informers have synced")
for {
time.Sleep(1 * time.Second)
if !prInformer.HasSynced() {
continue
}
if !trInformer.HasSynced() {
continue
}
break
}
log.Info("shared informers have synced")
return trInformer, prInformer, stopFn
}