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 main
18
+
19
+ import (
20
+ "net/http"
21
+ "os"
22
+ "time"
23
+
24
+ "github.com/spf13/pflag"
25
+ "k8s.io/apiserver/pkg/server"
26
+ "k8s.io/apiserver/pkg/server/options"
27
+
28
+ "github.com/programming-kubernetes/pizza-crd/pkg/webhook/conversion"
29
+ )
30
+
31
+ func NewDefaultOptions () * Options {
32
+ o := & Options {
33
+ * options .NewSecureServingOptions (),
34
+ }
35
+ o .SecureServing .ServerCert .PairName = "pizza-crd-webhook"
36
+ return o
37
+ }
38
+
39
+ type Options struct {
40
+ SecureServing options.SecureServingOptions
41
+ }
42
+
43
+ type Config struct {
44
+ SecureServing * server.SecureServingInfo
45
+ }
46
+
47
+ func (o * Options ) AddFlags (fs * pflag.FlagSet ) {
48
+ o .SecureServing .AddFlags (fs )
49
+ }
50
+
51
+ func (o * Options ) Config () (* Config , error ) {
52
+ if err := o .SecureServing .MaybeDefaultWithSelfSignedCerts ("0.0.0.0" , nil , nil ); err != nil {
53
+ return nil , err
54
+ }
55
+
56
+ c := & Config {}
57
+
58
+ if err := o .SecureServing .ApplyTo (& c .SecureServing ); err != nil {
59
+ return nil , err
60
+ }
61
+
62
+ return c , nil
63
+ }
64
+
65
+ func main () {
66
+ // parse flags
67
+ opt := NewDefaultOptions ()
68
+ fs := pflag .NewFlagSet ("pizza-crd-webhook" , pflag .ExitOnError )
69
+ opt .AddFlags (fs )
70
+ if err := fs .Parse (os .Args ); err != nil {
71
+ panic (err )
72
+ }
73
+
74
+ // create runtime config
75
+ cfg , err := opt .Config ()
76
+ if err != nil {
77
+ panic (err )
78
+ }
79
+
80
+ // run server
81
+ mux := http .NewServeMux ()
82
+ mux .Handle ("/convert/pizza" , http .HandlerFunc (conversion .Serve ))
83
+ if doneCh , err := cfg .SecureServing .Serve (mux , time .Second * 30 , server .SetupSignalHandler ()); err != nil {
84
+ panic (err )
85
+ } else {
86
+ <- doneCh
87
+ }
88
+ }
0 commit comments