forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
configz.go
90 lines (74 loc) · 2.59 KB
/
configz.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
// Copyright 2018 Istio 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 configz
import (
"html/template"
"net/http"
"istio.io/istio/pkg/ctrlz"
"istio.io/istio/pkg/ctrlz/fw"
"istio.io/istio/pkg/mcp/client"
)
// configzTopic topic is a Topic fw.implementation that exposes the state info about an MCP client.
type configzTopic struct {
tmpl *template.Template
cl *client.Client
}
var _ fw.Topic = &configzTopic{}
// Register the Configz topic for the given client.
// TODO: Multi-client registration is currently not supported. We should update the topic, so that we can
// show output from multiple clients.
func Register(c *client.Client) {
ctrlz.RegisterTopic(CreateTopic(c))
}
// CreateTopic creates and returns a configz topic from the given MCP client. It does not do any registration.
func CreateTopic(c *client.Client) fw.Topic {
return &configzTopic{
cl: c,
}
}
// Title is implementation of Topic.Title.
func (c *configzTopic) Title() string {
return "Config"
}
// Prefix is implementation of Topic.Prefix.
func (c *configzTopic) Prefix() string {
return "config"
}
type data struct {
ID string
Metadata map[string]string
SupportedTypeURLs []string
LatestRequests []client.RecentRequestInfo
}
// Activate is implementation of Topic.Activate.
func (c *configzTopic) Activate(context fw.TopicContext) {
l := template.Must(context.Layout().Clone())
c.tmpl = template.Must(l.Parse(string(MustAsset("assets/templates/config.html"))))
_ = context.HTMLRouter().StrictSlash(true).NewRoute().Path("/").HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
d := c.collectData()
fw.RenderHTML(w, c.tmpl, d)
})
_ = context.JSONRouter().StrictSlash(true).NewRoute().Methods("GET").Path("/").HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
d := c.collectData()
fw.RenderJSON(w, http.StatusOK, d)
})
}
func (c *configzTopic) collectData() *data {
return &data{
ID: c.cl.ID(),
Metadata: c.cl.Metadata(),
SupportedTypeURLs: c.cl.SupportedTypeURLs(),
LatestRequests: c.cl.SnapshotRequestInfo(),
}
}