Merge pull request #132177 from pgimalac/apiserver-avoid-template-for… · kubernetes/kubernetes@77bd3f8 · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit 77bd3f8

Browse files
authored
Merge pull request #132177 from pgimalac/apiserver-avoid-template-for-dce
Remove use of html/template in apiserver to avoid disabling dead code elimination
2 parents 3b51e52 + 40c7188 commit 77bd3f8

File tree

6 files changed

+50
-67
lines changed

6 files changed

+50
-67
lines changed

cmd/kubeadm/app/cmd/util/join.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package util
1919
import (
2020
"bytes"
2121
"crypto/x509"
22-
"html/template"
22+
"html/template" //nolint:depguard
2323
"strings"
2424

2525
"k8s.io/client-go/tools/clientcmd"

hack/golangci-hints.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ linters:
254254
deny:
255255
- pkg: "github.com/google/go-cmp/cmp"
256256
desc: "cmp is allowed only in test files"
257+
- pkg: "html/template"
258+
desc: "template is allowed only in test files as it disables dead code elimination"
257259
forbidigo:
258260
analyze-types: true
259261
forbid:

hack/golangci.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ linters:
268268
deny:
269269
- pkg: "github.com/google/go-cmp/cmp"
270270
desc: "cmp is allowed only in test files"
271+
- pkg: "html/template"
272+
desc: "template is allowed only in test files as it disables dead code elimination"
271273
forbidigo:
272274
analyze-types: true
273275
forbid:

hack/golangci.yaml.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ linters:
198198
deny:
199199
- pkg: "github.com/google/go-cmp/cmp"
200200
desc: "cmp is allowed only in test files"
201+
- pkg: "html/template"
202+
desc: "template is allowed only in test files as it disables dead code elimination"
201203
forbidigo:
202204
analyze-types: true
203205
forbid:

staging/src/k8s.io/apiserver/pkg/server/routes/flags.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package routes
1818

1919
import (
2020
"fmt"
21-
"html/template"
21+
"html/template" //nolint:depguard
2222
"io"
2323
"net/http"
2424
"path"
@@ -56,12 +56,15 @@ func (f DebugFlags) Install(c *mux.PathRecorderMux, flag string, handler func(ht
5656
func (f DebugFlags) Index(w http.ResponseWriter, r *http.Request) {
5757
lock.RLock()
5858
defer lock.RUnlock()
59-
if err := indexTmpl.Execute(w, registeredFlags); err != nil {
59+
if err := indexTmpl(w, registeredFlags); err != nil {
6060
klog.Error(err)
6161
}
6262
}
6363

64-
var indexTmpl = template.Must(template.New("index").Parse(`<html>
64+
func indexTmpl(w io.Writer, flags map[string]debugFlag) error {
65+
// avoid using a template to avoid disabling dead code elimination
66+
// https://github.com/golang/go/issues/72895
67+
_, err := w.Write([]byte(`<html>
6568
<head>
6669
<title>/debug/flags/</title>
6770
</head>
@@ -70,15 +73,27 @@ var indexTmpl = template.Must(template.New("index").Parse(`<html>
7073
<br>
7174
flags:<br>
7275
<table>
73-
{{range .}}
74-
<tr>{{.Flag}}<br>
75-
{{end}}
76-
</table>
76+
`))
77+
if err != nil {
78+
return err
79+
}
80+
81+
for _, flag := range flags {
82+
_, err := fmt.Fprintf(w, `<tr>%s<br>
83+
`, template.HTMLEscapeString(flag.Flag))
84+
if err != nil {
85+
return err
86+
}
87+
}
88+
89+
_, err = w.Write([]byte(`</table>
7790
<br>
7891
full flags configurable<br>
7992
</body>
8093
</html>
8194
`))
95+
return err
96+
}
8297

8398
type debugFlag struct {
8499
Flag string

staging/src/k8s.io/component-base/zpages/statusz/statusz.go

Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ limitations under the License.
1717
package statusz
1818

1919
import (
20-
"bytes"
2120
"fmt"
22-
"html/template"
21+
"html"
2322
"math/rand"
2423
"net/http"
2524
"time"
@@ -35,30 +34,11 @@ var (
3534

3635
const DefaultStatuszPath = "/statusz"
3736

38-
const (
39-
headerFmt = `
37+
const headerFmt = `
4038
%s statusz
4139
Warning: This endpoint is not meant to be machine parseable, has no formatting compatibility guarantees and is for debugging purposes only.
4240
`
4341

44-
dataTemplate = `
45-
Started{{.Delim}} {{.StartTime}}
46-
Up{{.Delim}} {{.Uptime}}
47-
Go version{{.Delim}} {{.GoVersion}}
48-
Binary version{{.Delim}} {{.BinaryVersion}}
49-
{{if .EmulationVersion}}Emulation version{{.Delim}} {{.EmulationVersion}}{{end}}
50-
`
51-
)
52-
53-
type contentFields struct {
54-
Delim string
55-
StartTime string
56-
Uptime string
57-
GoVersion string
58-
BinaryVersion string
59-
EmulationVersion string
60-
}
61-
6242
type mux interface {
6343
Handle(path string, handler http.Handler)
6444
}
@@ -68,33 +48,18 @@ func NewRegistry(effectiveVersion compatibility.EffectiveVersion) statuszRegistr
6848
}
6949

7050
func Install(m mux, componentName string, reg statuszRegistry) {
71-
dataTmpl, err := initializeTemplates()
72-
if err != nil {
73-
klog.Errorf("error while parsing gotemplates: %v", err)
74-
return
75-
}
76-
m.Handle(DefaultStatuszPath, handleStatusz(componentName, dataTmpl, reg))
77-
}
78-
79-
func initializeTemplates() (*template.Template, error) {
80-
d := template.New("data")
81-
dataTmpl, err := d.Parse(dataTemplate)
82-
if err != nil {
83-
return nil, err
84-
}
85-
86-
return dataTmpl, nil
51+
m.Handle(DefaultStatuszPath, handleStatusz(componentName, reg))
8752
}
8853

89-
func handleStatusz(componentName string, dataTmpl *template.Template, reg statuszRegistry) http.HandlerFunc {
54+
func handleStatusz(componentName string, reg statuszRegistry) http.HandlerFunc {
9055
return func(w http.ResponseWriter, r *http.Request) {
9156
if !httputil.AcceptableMediaType(r) {
9257
http.Error(w, httputil.ErrUnsupportedMediaType.Error(), http.StatusNotAcceptable)
9358
return
9459
}
9560

9661
fmt.Fprintf(w, headerFmt, componentName)
97-
data, err := populateStatuszData(dataTmpl, reg)
62+
data, err := populateStatuszData(reg)
9863
if err != nil {
9964
klog.Errorf("error while populating statusz data: %v", err)
10065
http.Error(w, "error while populating statusz data", http.StatusInternalServerError)
@@ -106,31 +71,28 @@ func handleStatusz(componentName string, dataTmpl *template.Template, reg status
10671
}
10772
}
10873

109-
func populateStatuszData(tmpl *template.Template, reg statuszRegistry) (string, error) {
110-
if tmpl == nil {
111-
return "", fmt.Errorf("received nil template")
112-
}
113-
74+
func populateStatuszData(reg statuszRegistry) (string, error) {
11475
randomIndex := rand.Intn(len(delimiters))
115-
data := contentFields{
116-
Delim: delimiters[randomIndex],
117-
StartTime: reg.processStartTime().Format(time.UnixDate),
118-
Uptime: uptime(reg.processStartTime()),
119-
GoVersion: reg.goVersion(),
120-
BinaryVersion: reg.binaryVersion().String(),
121-
}
76+
delim := html.EscapeString(delimiters[randomIndex])
77+
startTime := html.EscapeString(reg.processStartTime().Format(time.UnixDate))
78+
uptime := html.EscapeString(uptime(reg.processStartTime()))
79+
goVersion := html.EscapeString(reg.goVersion())
80+
binaryVersion := html.EscapeString(reg.binaryVersion().String())
12281

82+
var emulationVersion string
12383
if reg.emulationVersion() != nil {
124-
data.EmulationVersion = reg.emulationVersion().String()
84+
emulationVersion = fmt.Sprintf(`Emulation version%s %s`, delim, html.EscapeString(reg.emulationVersion().String()))
12585
}
12686

127-
var tpl bytes.Buffer
128-
err := tmpl.Execute(&tpl, data)
129-
if err != nil {
130-
return "", fmt.Errorf("error executing statusz template: %w", err)
131-
}
87+
status := fmt.Sprintf(`
88+
Started%[1]s %[2]s
89+
Up%[1]s %[3]s
90+
Go version%[1]s %[4]s
91+
Binary version%[1]s %[5]s
92+
%[6]s
93+
`, delim, startTime, uptime, goVersion, binaryVersion, emulationVersion)
13294

133-
return tpl.String(), nil
95+
return status, nil
13496
}
13597

13698
func uptime(t time.Time) string {

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.