Check in generated openapi for the kops apiserver.

- Note the openapi will not be regenerated on each change.  This needs to done in a follow up.
This commit is contained in:
Phillip Wittrock 2017-07-19 10:43:40 -07:00
parent b94dd9e7d4
commit 6a020bcd59
6 changed files with 6273 additions and 1 deletions

View File

@ -71,6 +71,7 @@ k8s.io/kops/pkg/model/gcemodel
k8s.io/kops/pkg/model/iam
k8s.io/kops/pkg/model/resources
k8s.io/kops/pkg/model/vspheremodel
k8s.io/kops/pkg/openapi
k8s.io/kops/pkg/pretty
k8s.io/kops/pkg/resources
k8s.io/kops/pkg/resources/digitalocean

View File

@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
// +k8s:openapi-gen=true
// +k8s:conversion-gen=k8s.io/kops/pkg/apis/kops
// +k8s:defaulter-gen=TypeMeta

View File

@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
// +k8s:openapi-gen=true
// +k8s:conversion-gen=k8s.io/kops/pkg/apis/kops
// +k8s:defaulter-gen=TypeMeta

View File

@ -17,9 +17,12 @@ limitations under the License.
package server
import (
"bytes"
"fmt"
"io"
"net"
"net/http"
"os"
"github.com/golang/glog"
"github.com/spf13/cobra"
@ -32,6 +35,7 @@ import (
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/apis/kops/v1alpha2"
"k8s.io/kops/pkg/apiserver"
"k8s.io/kops/pkg/openapi"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
)
@ -46,6 +50,8 @@ type KopsServerOptions struct {
StdOut io.Writer
StdErr io.Writer
PrintOpenapi bool
}
// NewCommandStartKopsServer provides a CLI handler for 'start master' command
@ -85,6 +91,9 @@ func NewCommandStartKopsServer(out, err io.Writer) *cobra.Command {
o.Authentication.AddFlags(flags)
o.Authorization.AddFlags(flags)
flags.BoolVar(&o.PrintOpenapi, "print-openapi", false,
"Print the openapi json and exit")
return cmd
}
@ -134,13 +143,48 @@ func (o KopsServerOptions) RunKopsServer() error {
RESTOptionsGetter: &restOptionsFactory{storageConfig: &o.Etcd.StorageConfig},
}
// Configure the openapi spec provided on /swagger.json
// TODO: Come up with a better titlie and a meaningful version
config.GenericConfig.OpenAPIConfig = genericapiserver.DefaultOpenAPIConfig(
openapi.GetOpenAPIDefinitions, kops.Scheme)
config.GenericConfig.OpenAPIConfig.Info.Title = "Kops API"
config.GenericConfig.OpenAPIConfig.Info.Version = "0.1"
server, err := config.Complete().New()
if err != nil {
return err
}
return server.GenericAPIServer.PrepareRun().Run(wait.NeverStop)
srv := server.GenericAPIServer.PrepareRun()
// Just print the openapi spec and exit. This is useful for
// updating the published openapi and generating documentation.
if o.PrintOpenapi {
fmt.Printf("%s", readOpenapi(server.GenericAPIServer.Handler))
os.Exit(0)
}
return srv.Run(wait.NeverStop)
}
// Read the openapi spec from the http request handler.
func readOpenapi(handler *genericapiserver.APIServerHandler) string {
req, err := http.NewRequest("GET", "/swagger.json", nil)
if err != nil {
panic(fmt.Errorf("Could not create openapi request %v", err))
}
resp := &BufferedResponse{}
handler.ServeHTTP(resp, req)
return resp.String()
}
type BufferedResponse struct {
bytes.Buffer
}
func (BufferedResponse) Header() http.Header { return http.Header{} }
func (BufferedResponse) WriteHeader(int) {}
type restOptionsFactory struct {
storageConfig *storagebackend.Config
}

17
pkg/openapi/doc.go Normal file
View File

@ -0,0 +1,17 @@
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package openapi

File diff suppressed because one or more lines are too long