Skip to content

Commit

Permalink
Copy generic-admission-server into pkg (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamalsaha authored Feb 5, 2018
1 parent 5a5093f commit fb8c1e4
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 6 deletions.
113 changes: 113 additions & 0 deletions apiserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package cmds

import (
"fmt"
"io"
"net"

"github.com/appscode/stash/pkg/admission/plugin"
"github.com/appscode/stash/pkg/apiserver"
"github.com/spf13/cobra"
admissionv1beta1 "k8s.io/api/admission/v1beta1"
genericapiserver "k8s.io/apiserver/pkg/server"
genericoptions "k8s.io/apiserver/pkg/server/options"
)

const defaultEtcdPathPrefix = "/registry/stash.appscode.com"

type AdmissionServerOptions struct {
RecommendedOptions *genericoptions.RecommendedOptions

AdmissionHooks []apiserver.AdmissionHook

StdOut io.Writer
StdErr io.Writer
}

func NewAdmissionServerOptions(out, errOut io.Writer, admissionHooks ...apiserver.AdmissionHook) *AdmissionServerOptions {
o := &AdmissionServerOptions{
// TODO we will nil out the etcd storage options. This requires a later level of k8s.io/apiserver
RecommendedOptions: genericoptions.NewRecommendedOptions(defaultEtcdPathPrefix, apiserver.Codecs.LegacyCodec(admissionv1beta1.SchemeGroupVersion)),

AdmissionHooks: admissionHooks,

StdOut: out,
StdErr: errOut,
}
o.RecommendedOptions.Etcd = nil

return o
}

// NewCommandStartMaster provides a CLI handler for 'start master' command
func NewCommandStartAPIServer(out, errOut io.Writer, stopCh <-chan struct{}) *cobra.Command {
o := NewAdmissionServerOptions(out, errOut)

cmd := &cobra.Command{
Use: "apiserver",
Short: "Launch Stash API server",
Long: "Launch Stash API server",
RunE: func(c *cobra.Command, args []string) error {
o.AdmissionHooks = []apiserver.AdmissionHook{
&plugin.AdmissionHook{},
}

if err := o.Complete(); err != nil {
return err
}
if err := o.Validate(args); err != nil {
return err
}
if err := o.RunServer(stopCh); err != nil {
return err
}
return nil
},
}

flags := cmd.Flags()
o.RecommendedOptions.AddFlags(flags)

return cmd
}

func (o AdmissionServerOptions) Validate(args []string) error {
return nil
}

func (o *AdmissionServerOptions) Complete() error {
return nil
}

func (o AdmissionServerOptions) Config() (*apiserver.Config, error) {
// TODO have a "real" external address
if err := o.RecommendedOptions.SecureServing.MaybeDefaultWithSelfSignedCerts("localhost", nil, []net.IP{net.ParseIP("127.0.0.1")}); err != nil {
return nil, fmt.Errorf("error creating self-signed certificates: %v", err)
}

serverConfig := genericapiserver.NewRecommendedConfig(apiserver.Codecs)
if err := o.RecommendedOptions.ApplyTo(serverConfig); err != nil {
return nil, err
}

config := &apiserver.Config{
GenericConfig: serverConfig,
ExtraConfig: apiserver.ExtraConfig{
AdmissionHooks: o.AdmissionHooks,
},
}
return config, nil
}

func (o AdmissionServerOptions) RunServer(stopCh <-chan struct{}) error {
config, err := o.Config()
if err != nil {
return err
}

server, err := config.Complete().New()
if err != nil {
return err
}
return server.GenericAPIServer.PrepareRun().Run(stopCh)
}
7 changes: 1 addition & 6 deletions root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import (
v "github.com/appscode/go/version"
"github.com/appscode/kutil/tools/analytics"
"github.com/appscode/stash/client/scheme"
"github.com/appscode/stash/pkg/admission/plugin"
"github.com/appscode/stash/pkg/util"
"github.com/jpillora/go-ogle-analytics"
"github.com/openshift/generic-admission-server/pkg/cmd/server"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
genericapiserver "k8s.io/apiserver/pkg/server"
Expand Down Expand Up @@ -58,10 +56,7 @@ func NewRootCmd() *cobra.Command {
rootCmd.AddCommand(NewCmdCheck())

stopCh := genericapiserver.SetupSignalHandler()
cmd := server.NewCommandStartAdmissionServer(os.Stdout, os.Stderr, stopCh, &plugin.AdmissionHook{})
cmd.Use = "admission-webhook"
cmd.Long = "Launch Stash admission webhook server"
cmd.Short = cmd.Long
cmd := NewCommandStartAPIServer(os.Stdout, os.Stderr, stopCh)
rootCmd.AddCommand(cmd)

return rootCmd
Expand Down

0 comments on commit fb8c1e4

Please sign in to comment.