k8s.io/kubernetes@v1.29.3/test/images/agnhost/crd-conversion-webhook/main.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package crdconvwebhook
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  
    23  	"github.com/spf13/cobra"
    24  
    25  	"k8s.io/kubernetes/test/images/agnhost/crd-conversion-webhook/converter"
    26  )
    27  
    28  var (
    29  	certFile string
    30  	keyFile  string
    31  	port     int
    32  )
    33  
    34  // CmdCrdConversionWebhook is used by agnhost Cobra.
    35  var CmdCrdConversionWebhook = &cobra.Command{
    36  	Use:   "crd-conversion-webhook",
    37  	Short: "Starts HTTP server on port 443 for testing CustomResourceConversionWebhook",
    38  	Long: `The subcommand tests "CustomResourceConversionWebhook".
    39  
    40  After deploying it to Kubernetes cluster, the administrator needs to create a "CustomResourceConversion.Webhook" in Kubernetes cluster to use remote webhook for conversions.
    41  
    42  The subcommand starts a HTTP server, listening on port 443, and creating the "/crdconvert" endpoint.`,
    43  	Args: cobra.MaximumNArgs(0),
    44  	Run:  main,
    45  }
    46  
    47  func init() {
    48  	CmdCrdConversionWebhook.Flags().StringVar(&certFile, "tls-cert-file", "",
    49  		"File containing the default x509 Certificate for HTTPS. (CA cert, if any, concatenated "+
    50  			"after server cert.")
    51  	CmdCrdConversionWebhook.Flags().StringVar(&keyFile, "tls-private-key-file", "",
    52  		"File containing the default x509 private key matching --tls-cert-file.")
    53  	CmdCrdConversionWebhook.Flags().IntVar(&port, "port", 443,
    54  		"Secure port that the webhook listens on")
    55  }
    56  
    57  // Config contains the server (the webhook) cert and key.
    58  type Config struct {
    59  	CertFile string
    60  	KeyFile  string
    61  }
    62  
    63  func main(cmd *cobra.Command, args []string) {
    64  	config := Config{CertFile: certFile, KeyFile: keyFile}
    65  
    66  	http.HandleFunc("/crdconvert", converter.ServeExampleConvert)
    67  	http.HandleFunc("/readyz", func(w http.ResponseWriter, req *http.Request) { w.Write([]byte("ok")) })
    68  	clientset := getClient()
    69  	server := &http.Server{
    70  		Addr:      fmt.Sprintf(":%d", port),
    71  		TLSConfig: configTLS(config, clientset),
    72  	}
    73  	err := server.ListenAndServeTLS("", "")
    74  	if err != nil {
    75  		panic(err)
    76  	}
    77  }