Skip to content

Commit

Permalink
Update admission to use pflag.
Browse files Browse the repository at this point in the history
Signed-off-by: Klaus Ma <[email protected]>
  • Loading branch information
k82cn committed Nov 27, 2019
1 parent 651867a commit 4885040
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
33 changes: 17 additions & 16 deletions cmd/admission/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ limitations under the License.
package options

import (
"flag"
"fmt"

"github.com/spf13/pflag"

"k8s.io/api/admissionregistration/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -57,27 +58,27 @@ func NewConfig() *Config {
}

// AddFlags add flags
func (c *Config) AddFlags() {
flag.StringVar(&c.Master, "master", c.Master, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
flag.StringVar(&c.Kubeconfig, "kubeconfig", c.Kubeconfig, "Path to kubeconfig file with authorization and master location information.")
flag.StringVar(&c.CertFile, "tls-cert-file", c.CertFile, ""+
func (c *Config) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&c.Master, "master", c.Master, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
fs.StringVar(&c.Kubeconfig, "kubeconfig", c.Kubeconfig, "Path to kubeconfig file with authorization and master location information.")
fs.StringVar(&c.CertFile, "tls-cert-file", c.CertFile, ""+
"File containing the default x509 Certificate for HTTPS. (CA cert, if any, concatenated "+
"after server cert).")
flag.StringVar(&c.KeyFile, "tls-private-key-file", c.KeyFile, "File containing the default x509 private key matching --tls-cert-file.")
flag.StringVar(&c.CaCertFile, "ca-cert-file", c.CaCertFile, "File containing the x509 Certificate for HTTPS.")
flag.IntVar(&c.Port, "port", 443, "the port used by admission-controller-server.")
flag.StringVar(&c.MutateWebhookConfigName, "mutate-webhook-config-name", "",
fs.StringVar(&c.KeyFile, "tls-private-key-file", c.KeyFile, "File containing the default x509 private key matching --tls-cert-file.")
fs.StringVar(&c.CaCertFile, "ca-cert-file", c.CaCertFile, "File containing the x509 Certificate for HTTPS.")
fs.IntVar(&c.Port, "port", 443, "the port used by admission-controller-server.")
fs.StringVar(&c.MutateWebhookConfigName, "mutate-webhook-config-name", "",
"Name of the mutatingwebhookconfiguration resource in Kubernetes [Deprecated]: it will be generated when not specified.")
flag.StringVar(&c.MutateWebhookName, "mutate-webhook-name", "",
fs.StringVar(&c.MutateWebhookName, "mutate-webhook-name", "",
"Name of the webhook entry in the webhook config. [Deprecated]: it will be generated when not specified")
flag.StringVar(&c.ValidateWebhookConfigName, "validate-webhook-config-name", "",
fs.StringVar(&c.ValidateWebhookConfigName, "validate-webhook-config-name", "",
"Name of the mutatingwebhookconfiguration resource in Kubernetes. [Deprecated]: it will be generated when not specified")
flag.StringVar(&c.ValidateWebhookName, "validate-webhook-name", "",
fs.StringVar(&c.ValidateWebhookName, "validate-webhook-name", "",
"Name of the webhook entry in the webhook config. [Deprecated]: it will be generated when not specified")
flag.BoolVar(&c.PrintVersion, "version", false, "Show version and quit")
flag.StringVar(&c.AdmissionServiceNamespace, "webhook-namespace", "default", "The namespace of this webhook")
flag.StringVar(&c.AdmissionServiceName, "webhook-service-name", "admission-service", "The name of this admission service")
flag.StringVar(&c.SchedulerName, "scheduler-name", defaultSchedulerName, "Volcano will handle pods whose .spec.SchedulerName is same as scheduler-name")
fs.BoolVar(&c.PrintVersion, "version", false, "Show version and quit")
fs.StringVar(&c.AdmissionServiceNamespace, "webhook-namespace", "default", "The namespace of this webhook")
fs.StringVar(&c.AdmissionServiceName, "webhook-service-name", "admission-service", "The name of this admission service")
fs.StringVar(&c.SchedulerName, "scheduler-name", defaultSchedulerName, "Volcano will handle pods whose .spec.SchedulerName is same as scheduler-name")
}

const (
Expand Down
20 changes: 17 additions & 3 deletions cmd/admission/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ limitations under the License.
package main

import (
"flag"
"io/ioutil"
"net/http"
"os"
"os/signal"
"runtime"
"strconv"
"syscall"
"time"

"github.com/spf13/pflag"

"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apiserver/pkg/util/flag"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"

Expand All @@ -41,15 +46,24 @@ func serveMutateJobs(w http.ResponseWriter, r *http.Request) {
admission.Serve(w, r, admission.MutateJobs)
}

var logFlushFreq = pflag.Duration("log-flush-frequency", 5*time.Second, "Maximum number of seconds between log flushes")

func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
klog.InitFlags(nil)

config := options.NewConfig()
config.AddFlags()
flag.Parse()
config.AddFlags(pflag.CommandLine)

flag.InitFlags()

if config.PrintVersion {
version.PrintVersionAndExit()
}

go wait.Until(klog.Flush, *logFlushFreq, wait.NeverStop)
defer klog.Flush()

http.HandleFunc(admission.AdmitJobPath, serveJobs)
http.HandleFunc(admission.MutateJobPath, serveMutateJobs)

Expand Down
5 changes: 4 additions & 1 deletion cmd/controllers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main
import (
"fmt"
"os"
"runtime"
"time"

"github.com/spf13/pflag"
Expand All @@ -34,11 +35,13 @@ import (
var logFlushFreq = pflag.Duration("log-flush-frequency", 5*time.Second, "Maximum number of seconds between log flushes")

func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
klog.InitFlags(nil)

s := options.NewServerOption()
s.AddFlags(pflag.CommandLine)

flag.InitFlags()
klog.InitFlags(nil)

if s.PrintVersion {
version.PrintVersionAndExit()
Expand Down

0 comments on commit 4885040

Please sign in to comment.