From 6cdc2da4c20ca3161bceb2da62fa08c29c56c3bb Mon Sep 17 00:00:00 2001 From: Amritansh Amritansh Date: Tue, 9 Jun 2026 11:31:21 +0530 Subject: [PATCH] Add agnhost-h2c-server --- test/images/agnhost/VERSION | 2 +- test/images/agnhost/agnhost.go | 2 + test/images/agnhost/h2c-server/server.go | 74 ++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 test/images/agnhost/h2c-server/server.go diff --git a/test/images/agnhost/VERSION b/test/images/agnhost/VERSION index e604dbd28b0..d1524d4046f 100644 --- a/test/images/agnhost/VERSION +++ b/test/images/agnhost/VERSION @@ -1 +1 @@ -2.63.0 +2.64.0 diff --git a/test/images/agnhost/agnhost.go b/test/images/agnhost/agnhost.go index 088932e4e09..1ca954742d4 100644 --- a/test/images/agnhost/agnhost.go +++ b/test/images/agnhost/agnhost.go @@ -32,6 +32,7 @@ import ( "k8s.io/kubernetes/test/images/agnhost/fakeregistryserver" grpchealthchecking "k8s.io/kubernetes/test/images/agnhost/grpc-health-checking" "k8s.io/kubernetes/test/images/agnhost/guestbook" + h2cserver "k8s.io/kubernetes/test/images/agnhost/h2c-server" "k8s.io/kubernetes/test/images/agnhost/inclusterclient" "k8s.io/kubernetes/test/images/agnhost/liveness" logsgen "k8s.io/kubernetes/test/images/agnhost/logs-generator" @@ -94,6 +95,7 @@ func main() { rootCmd.AddCommand(webhook.CmdWebhook) rootCmd.AddCommand(openidmetadata.CmdTestServiceAccountIssuerDiscovery) rootCmd.AddCommand(grpchealthchecking.CmdGrpcHealthChecking) + rootCmd.AddCommand(h2cserver.CmdH2CServer) rootCmd.AddCommand(vishhstress.CmdStress) rootCmd.AddCommand(podcertificatesigner.CmdPodCertificateSigner) rootCmd.AddCommand(mtlsclient.CmdMtlsClient) diff --git a/test/images/agnhost/h2c-server/server.go b/test/images/agnhost/h2c-server/server.go new file mode 100644 index 00000000000..5ff22b903e5 --- /dev/null +++ b/test/images/agnhost/h2c-server/server.go @@ -0,0 +1,74 @@ +/* +Copyright 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 h2cserver offers a tiny HTTP/2 cleartext (h2c) web server for probe testing. +package h2cserver + +import ( + "fmt" + "log" + "net" + "net/http" + + "github.com/spf13/cobra" + "golang.org/x/net/http2" +) + +// CmdH2CServer is used by agnhost Cobra. +var CmdH2CServer = &cobra.Command{ + Use: "h2c-server", + Short: "Starts a simple HTTP/2 cleartext (h2c) server", + Long: "Starts a simple HTTP/2 cleartext server with prior knowledge on the given --port. Responds with 200 OK to all requests.", + Args: cobra.MaximumNArgs(0), + Run: main, +} + +var ( + port int +) + +func init() { + CmdH2CServer.Flags().IntVar(&port, "port", 80, "Port number.") +} + +func main(cmd *cobra.Command, args []string) { + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain") + w.WriteHeader(http.StatusOK) + _, _ = fmt.Fprintf(w, "ok") + }) + + l, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) + if err != nil { + log.Fatalf("Error from net.Listen(): %s", err) + } + + h2s := &http2.Server{} + srv := &http.Server{Handler: handler} + + log.Printf("Serving h2c on port %d.\n", port) + + for { + c, err := l.Accept() + if err != nil { + log.Fatalf("Error from Accept(): %s", err) + } + go h2s.ServeConn(c, &http2.ServeConnOpts{ + Handler: handler, + BaseConfig: srv, + }) + } +}