github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/cmd/admission/main.go (about)

     1  /*
     2  Copyright 2019 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 main
    18  
    19  import (
    20  	"crypto/tls"
    21  	"errors"
    22  	"flag"
    23  	"fmt"
    24  	"net/http"
    25  	"os"
    26  	"time"
    27  
    28  	"github.com/sirupsen/logrus"
    29  	"sigs.k8s.io/prow/pkg/pjutil/pprof"
    30  
    31  	prowflagutil "sigs.k8s.io/prow/pkg/flagutil"
    32  	"sigs.k8s.io/prow/pkg/interrupts"
    33  	"sigs.k8s.io/prow/pkg/logrusutil"
    34  	"sigs.k8s.io/prow/pkg/pjutil"
    35  )
    36  
    37  type options struct {
    38  	cert                   string
    39  	privateKey             string
    40  	instrumentationOptions prowflagutil.InstrumentationOptions
    41  }
    42  
    43  func parseOptions() options {
    44  	var o options
    45  	if err := o.parse(flag.CommandLine, os.Args[1:]); err != nil {
    46  		logrus.Fatalf("Invalid flags: %v", err)
    47  	}
    48  	return o
    49  }
    50  
    51  func (o *options) parse(flags *flag.FlagSet, args []string) error {
    52  	flags.StringVar(&o.cert, "tls-cert-file", "", "Path to x509 certificate for HTTPS")
    53  	flags.StringVar(&o.privateKey, "tls-private-key-file", "", "Path to matching x509 private key.")
    54  	o.instrumentationOptions.AddFlags(flags)
    55  	if err := flags.Parse(args); err != nil {
    56  		return fmt.Errorf("parse flags: %w", err)
    57  	}
    58  	if len(o.cert) == 0 || len(o.privateKey) == 0 {
    59  		return errors.New("Both --tls-cert-file and --tls-private-key-file are required for HTTPS")
    60  	}
    61  	return nil
    62  }
    63  
    64  func main() {
    65  	logrusutil.ComponentInit()
    66  
    67  	o := parseOptions()
    68  
    69  	defer interrupts.WaitForGracefulShutdown()
    70  
    71  	pprof.Instrument(o.instrumentationOptions)
    72  	health := pjutil.NewHealthOnPort(o.instrumentationOptions.HealthPort)
    73  
    74  	admissionMux := http.NewServeMux()
    75  	admissionMux.HandleFunc("/validate", handle)
    76  	s := http.Server{
    77  		Addr: ":8443",
    78  		TLSConfig: &tls.Config{
    79  			ClientAuth: tls.NoClientCert,
    80  		},
    81  		Handler: admissionMux,
    82  	}
    83  
    84  	health.ServeReady()
    85  	interrupts.ListenAndServeTLS(&s, o.cert, o.privateKey, 5*time.Second)
    86  }