Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Swagger API support optional, so that consumers can define their own #2945

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/kube-apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func main() {
PortalNet: &n,
EnableLogsSupport: *enableLogsSupport,
EnableUISupport: true,
EnableSwaggerSupport: true,
APIPrefix: *apiPrefix,
CorsAllowedOriginList: corsAllowedOriginList,
ReadOnlyPort: *readOnlyPort,
Expand Down
63 changes: 44 additions & 19 deletions pkg/master/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,15 @@ type Config struct {
PortalNet *net.IPNet
EnableLogsSupport bool
EnableUISupport bool
EnableSwaggerSupport bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't you want to be able to configure the path at which it's exposed, also?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually turned out better to be the same. If necessary we can tweak the swagger config in our own code - I exposed the method for now though.

----- Original Message -----

@@ -69,11 +69,15 @@ type Config struct {
PortalNet *net.IPNet
EnableLogsSupport bool
EnableUISupport bool

  • EnableSwaggerSupport bool

Didn't you want to be able to configure the path at which it's exposed, also?


Reply to this email directly or view it on GitHub:
https://github.com/GoogleCloudPlatform/kubernetes/pull/2945/files#r21854923

APIPrefix string
CorsAllowedOriginList util.StringList
Authenticator authenticator.Request
Authorizer authorizer.Authorizer

// If specified, all web services will be registered into this container
RestfulContainer *restful.Container

// Number of masters running; all masters must be started with the
// same value for this field. (Numbers > 1 currently untested.)
MasterCount int
Expand All @@ -92,21 +96,23 @@ type Config struct {
// Master contains state for a Kubernetes cluster master/api server.
type Master struct {
// "Inputs", Copied from Config
podRegistry pod.Registry
controllerRegistry controller.Registry
serviceRegistry service.Registry
endpointRegistry endpoint.Registry
minionRegistry minion.Registry
bindingRegistry binding.Registry
eventRegistry generic.Registry
storage map[string]apiserver.RESTStorage
client *client.Client
portalNet *net.IPNet
podRegistry pod.Registry
controllerRegistry controller.Registry
serviceRegistry service.Registry
endpointRegistry endpoint.Registry
minionRegistry minion.Registry
bindingRegistry binding.Registry
eventRegistry generic.Registry
storage map[string]apiserver.RESTStorage
client *client.Client
portalNet *net.IPNet

mux apiserver.Mux
handlerContainer *restful.Container
rootWebService *restful.WebService
enableLogsSupport bool
enableUISupport bool
enableSwaggerSupport bool
apiPrefix string
corsAllowedOriginList util.StringList
authenticator authenticator.Request
Expand Down Expand Up @@ -222,7 +228,7 @@ func New(c *Config) *Master {
if c.KubeletClient == nil {
glog.Fatalf("master.New() called with config.KubeletClient == nil")
}
mx := http.NewServeMux()

m := &Master{
podRegistry: etcd.NewRegistry(c.EtcdHelper, boundPodFactory),
controllerRegistry: etcd.NewRegistry(c.EtcdHelper, nil),
Expand All @@ -233,11 +239,10 @@ func New(c *Config) *Master {
minionRegistry: minionRegistry,
client: c.Client,
portalNet: c.PortalNet,
mux: mx,
handlerContainer: NewHandlerContainer(mx),
rootWebService: new(restful.WebService),
enableLogsSupport: c.EnableLogsSupport,
enableUISupport: c.EnableUISupport,
enableSwaggerSupport: c.EnableSwaggerSupport,
apiPrefix: c.APIPrefix,
corsAllowedOriginList: c.CorsAllowedOriginList,
authenticator: c.Authenticator,
Expand All @@ -247,6 +252,16 @@ func New(c *Config) *Master {
readOnlyServer: net.JoinHostPort(c.PublicAddress, strconv.Itoa(int(c.ReadOnlyPort))),
readWriteServer: net.JoinHostPort(c.PublicAddress, strconv.Itoa(int(c.ReadWritePort))),
}

if c.RestfulContainer != nil {
m.mux = c.RestfulContainer.ServeMux
m.handlerContainer = c.RestfulContainer
} else {
mux := http.NewServeMux()
m.mux = mux
m.handlerContainer = NewHandlerContainer(mux)
}

m.masterServices = util.NewRunner(m.serviceWriterLoop, m.roServiceWriterLoop)
m.init(c)
return m
Expand Down Expand Up @@ -377,7 +392,22 @@ func (m *Master) init(c *Config) {
// Install root web services
m.handlerContainer.Add(m.rootWebService)

// TODO: Make this optional?
// TODO: Make this optional? Consumers of master depend on this currently.
m.Handler = handler

if m.enableSwaggerSupport {
m.InstallSwaggerAPI()
}

// TODO: Attempt clean shutdown?
m.masterServices.Start()
}

// InstallSwaggerAPI installs the /swaggerapi/ endpoint to allow schema discovery
// and traversal. It is optional to allow consumers of the Kubernetes master to
// register their own web services into the Kubernetes mux prior to initialization
// of swagger, so that other resource types show up in the documentation.
func (m *Master) InstallSwaggerAPI() {
// Enable swagger UI and discovery API
swaggerConfig := swagger.Config{
WebServices: m.handlerContainer.RegisteredWebServices(),
Expand All @@ -388,11 +418,6 @@ func (m *Master) init(c *Config) {
//SwaggerFilePath: "/srv/apiserver/swagger/dist"
}
swagger.RegisterSwaggerService(swaggerConfig, m.handlerContainer)

m.Handler = handler

// TODO: Attempt clean shutdown?
m.masterServices.Start()
}

func (m *Master) getServersToValidate(c *Config) map[string]apiserver.Server {
Expand Down
8 changes: 4 additions & 4 deletions pkg/standalone/standalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ func RunApiServer(cl *client.Client, etcdClient tools.EtcdClient, addr string, p
Client: http.DefaultClient,
Port: 10250,
},
EnableLogsSupport: false,
APIPrefix: "/api",
Authorizer: apiserver.NewAlwaysAllowAuthorizer(),
EnableLogsSupport: false,
EnableSwaggerSupport: true,
APIPrefix: "/api",
Authorizer: apiserver.NewAlwaysAllowAuthorizer(),

ReadWritePort: port,
ReadOnlyPort: port,
PublicAddress: addr,
})

handler.delegate = m.InsecureHandler

go http.ListenAndServe(fmt.Sprintf("%s:%d", addr, port), &handler)
Expand Down