mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
Merge pull request #128925 from richabanker/zpages-cleanup
Move zpages common code to an httputil package
This commit is contained in:
commit
79addb569b
@ -23,11 +23,9 @@ import (
|
|||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/munnerz/goautoneg"
|
"k8s.io/component-base/zpages/httputil"
|
||||||
|
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -40,8 +38,7 @@ Warning: This endpoint is not meant to be machine parseable, has no formatting c
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
flagzSeparators = []string{":", ": ", "=", " "}
|
delimiters = []string{":", ": ", "=", " "}
|
||||||
errUnsupportedMediaType = fmt.Errorf("media type not acceptable, must be: text/plain")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type registry struct {
|
type registry struct {
|
||||||
@ -64,8 +61,8 @@ func (reg *registry) installHandler(m mux, componentName string, flagReader Read
|
|||||||
|
|
||||||
func (reg *registry) handleFlags(componentName string, flagReader Reader) http.HandlerFunc {
|
func (reg *registry) handleFlags(componentName string, flagReader Reader) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if !acceptableMediaType(r) {
|
if !httputil.AcceptableMediaType(r) {
|
||||||
http.Error(w, errUnsupportedMediaType.Error(), http.StatusNotAcceptable)
|
http.Error(w, httputil.ErrUnsupportedMediaType.Error(), http.StatusNotAcceptable)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,8 +73,8 @@ func (reg *registry) handleFlags(componentName string, flagReader Reader) http.H
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
randomIndex := rand.Intn(len(flagzSeparators))
|
randomIndex := rand.Intn(len(delimiters))
|
||||||
separator := flagzSeparators[randomIndex]
|
separator := delimiters[randomIndex]
|
||||||
// Randomize the delimiter for printing to prevent scraping of the response.
|
// Randomize the delimiter for printing to prevent scraping of the response.
|
||||||
printSortedFlags(®.response, flagReader.GetFlagz(), separator)
|
printSortedFlags(®.response, flagReader.GetFlagz(), separator)
|
||||||
})
|
})
|
||||||
@ -90,29 +87,6 @@ func (reg *registry) handleFlags(componentName string, flagReader Reader) http.H
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func acceptableMediaType(r *http.Request) bool {
|
|
||||||
accepts := goautoneg.ParseAccept(r.Header.Get("Accept"))
|
|
||||||
for _, accept := range accepts {
|
|
||||||
if !mediaTypeMatches(accept) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if len(accept.Params) == 0 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if len(accept.Params) == 1 {
|
|
||||||
if charset, ok := accept.Params["charset"]; ok && strings.EqualFold(charset, "utf-8") {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func mediaTypeMatches(a goautoneg.Accept) bool {
|
|
||||||
return (a.Type == "text" || a.Type == "*") &&
|
|
||||||
(a.SubType == "plain" || a.SubType == "*")
|
|
||||||
}
|
|
||||||
|
|
||||||
func printSortedFlags(w io.Writer, flags map[string]string, separator string) {
|
func printSortedFlags(w io.Writer, flags map[string]string, separator string) {
|
||||||
var sortedKeys []string
|
var sortedKeys []string
|
||||||
for key := range flags {
|
for key := range flags {
|
||||||
|
@ -35,7 +35,7 @@ Warning: This endpoint is not meant to be machine parseable, has no formatting c
|
|||||||
|
|
||||||
func TestFlagz(t *testing.T) {
|
func TestFlagz(t *testing.T) {
|
||||||
componentName := "test-server"
|
componentName := "test-server"
|
||||||
flagzSeparators = []string{"="}
|
delimiters = []string{"="}
|
||||||
wantHeaderLines := strings.Split(fmt.Sprintf(wantTmpl, componentName), "\n")
|
wantHeaderLines := strings.Split(fmt.Sprintf(wantTmpl, componentName), "\n")
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2024 The Kubernetes 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 httputil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/munnerz/goautoneg"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ErrUnsupportedMediaType is the error returned when the request's
|
||||||
|
// Accept header does not contain "text/plain".
|
||||||
|
var ErrUnsupportedMediaType = fmt.Errorf("media type not acceptable, must be: text/plain")
|
||||||
|
|
||||||
|
// AcceptableMediaType checks if the request's Accept header contains
|
||||||
|
// a supported media type with optional "charset=utf-8" parameter.
|
||||||
|
func AcceptableMediaType(r *http.Request) bool {
|
||||||
|
accepts := goautoneg.ParseAccept(r.Header.Get("Accept"))
|
||||||
|
for _, accept := range accepts {
|
||||||
|
if !mediaTypeMatches(accept) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if len(accept.Params) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if len(accept.Params) == 1 {
|
||||||
|
if charset, ok := accept.Params["charset"]; ok && strings.EqualFold(charset, "utf-8") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func mediaTypeMatches(a goautoneg.Accept) bool {
|
||||||
|
return (a.Type == "text" || a.Type == "*") &&
|
||||||
|
(a.SubType == "plain" || a.SubType == "*")
|
||||||
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2024 The Kubernetes 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 httputil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAcceptableMediaTypes(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
reqHeader string
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "valid text/plain header",
|
||||||
|
reqHeader: "text/plain",
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "valid text/* header",
|
||||||
|
reqHeader: "text/*",
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "valid */plain header",
|
||||||
|
reqHeader: "*/plain",
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "valid accept args",
|
||||||
|
reqHeader: "text/plain; charset=utf-8",
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid text/foo header",
|
||||||
|
reqHeader: "text/foo",
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid text/plain params",
|
||||||
|
reqHeader: "text/plain; foo=bar",
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
req, err := http.NewRequest(http.MethodGet, "http://example.com/statusz", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected error while creating request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Accept", tt.reqHeader)
|
||||||
|
got := AcceptableMediaType(req)
|
||||||
|
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("Unexpected response from AcceptableMediaType(), want %v, got = %v", tt.want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -22,17 +22,14 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/munnerz/goautoneg"
|
"k8s.io/component-base/zpages/httputil"
|
||||||
|
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
delimiters = []string{":", ": ", "=", " "}
|
delimiters = []string{":", ": ", "=", " "}
|
||||||
errUnsupportedMediaType = fmt.Errorf("media type not acceptable, must be: text/plain")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const DefaultStatuszPath = "/statusz"
|
const DefaultStatuszPath = "/statusz"
|
||||||
@ -90,8 +87,8 @@ func initializeTemplates() (*template.Template, error) {
|
|||||||
|
|
||||||
func handleStatusz(componentName string, dataTmpl *template.Template, reg statuszRegistry) http.HandlerFunc {
|
func handleStatusz(componentName string, dataTmpl *template.Template, reg statuszRegistry) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if !acceptableMediaType(r) {
|
if !httputil.AcceptableMediaType(r) {
|
||||||
http.Error(w, errUnsupportedMediaType.Error(), http.StatusNotAcceptable)
|
http.Error(w, httputil.ErrUnsupportedMediaType.Error(), http.StatusNotAcceptable)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,30 +105,6 @@ func handleStatusz(componentName string, dataTmpl *template.Template, reg status
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(richabanker) : Move this to a common place to be reused for all zpages.
|
|
||||||
func acceptableMediaType(r *http.Request) bool {
|
|
||||||
accepts := goautoneg.ParseAccept(r.Header.Get("Accept"))
|
|
||||||
for _, accept := range accepts {
|
|
||||||
if !mediaTypeMatches(accept) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if len(accept.Params) == 0 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if len(accept.Params) == 1 {
|
|
||||||
if charset, ok := accept.Params["charset"]; ok && strings.EqualFold(charset, "utf-8") {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func mediaTypeMatches(a goautoneg.Accept) bool {
|
|
||||||
return (a.Type == "text" || a.Type == "*") &&
|
|
||||||
(a.SubType == "plain" || a.SubType == "*")
|
|
||||||
}
|
|
||||||
|
|
||||||
func populateStatuszData(tmpl *template.Template, reg statuszRegistry) (string, error) {
|
func populateStatuszData(tmpl *template.Template, reg statuszRegistry) (string, error) {
|
||||||
if tmpl == nil {
|
if tmpl == nil {
|
||||||
return "", fmt.Errorf("received nil template")
|
return "", fmt.Errorf("received nil template")
|
||||||
|
@ -152,58 +152,6 @@ func TestStatusz(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAcceptableMediaTypes(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
reqHeader string
|
|
||||||
want bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "valid text/plain header",
|
|
||||||
reqHeader: "text/plain",
|
|
||||||
want: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "valid text/* header",
|
|
||||||
reqHeader: "text/*",
|
|
||||||
want: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "valid */plain header",
|
|
||||||
reqHeader: "*/plain",
|
|
||||||
want: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "valid accept args",
|
|
||||||
reqHeader: "text/plain; charset=utf-8",
|
|
||||||
want: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "invalid text/foo header",
|
|
||||||
reqHeader: "text/foo",
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "invalid text/plain params",
|
|
||||||
reqHeader: "text/plain; foo=bar",
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
req, err := http.NewRequest(http.MethodGet, "http://example.com/statusz", nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Unexpected error while creating request: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Set("Accept", tt.reqHeader)
|
|
||||||
got := acceptableMediaType(req)
|
|
||||||
|
|
||||||
if got != tt.want {
|
|
||||||
t.Errorf("Unexpected response from acceptableMediaType(), want %v, got = %v", tt.want, got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseVersion(t *testing.T, v string) *version.Version {
|
func parseVersion(t *testing.T, v string) *version.Version {
|
||||||
parsed, err := version.ParseMajorMinor(v)
|
parsed, err := version.ParseMajorMinor(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user