Initial commit

This commit is contained in:
jeff
2019-03-07 17:08:54 +08:00
commit 47bf8820f4
2817 changed files with 960937 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
package options
import (
"fmt"
"github.com/spf13/pflag"
"os"
)
type ServerRunOptions struct {
// server bind address
BindAddress string
// insecure port number
InsecurePort int
// secure port number
SecurePort int
// OpenPitrix api gateway service url
OpenPitrixAddress string
// database connection string in MySQL like
// user:password@tcp(host)/dbname?charset=utf8&parseTime=True
DatabaseConnectionString string
// tls cert file
TlsCertFile string
// tls private key file
TlsPrivateKey string
// host openapi doc
ApiDoc bool
// kubeconfig file path
KubeConfig string
}
func NewServerRunOptions() *ServerRunOptions {
// create default server run options
s := ServerRunOptions{
BindAddress: "0.0.0.0",
InsecurePort: 9090,
SecurePort: 0,
OpenPitrixAddress: "openpitrix-api-gateway.openpitrix-system.svc",
DatabaseConnectionString: "",
TlsCertFile: "",
TlsPrivateKey: "",
ApiDoc: true,
}
return &s
}
func (s *ServerRunOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&s.BindAddress, "bind-address", "0.0.0.0", "server bind address")
fs.IntVar(&s.InsecurePort, "insecure-port", 9090, "insecure port number")
fs.IntVar(&s.SecurePort, "secure-port", 0, "secure port number")
fs.StringVar(&s.OpenPitrixAddress, "openpitrix", "openpitrix-api-gateway.openpitrix-system.svc", "openpitrix api gateway address")
fs.StringVar(&s.DatabaseConnectionString, "database-connection", "", "database connection string")
fs.StringVar(&s.TlsCertFile, "tls-cert-file", "", "tls cert file")
fs.StringVar(&s.TlsPrivateKey, "tls-private-key", "", "tls private key")
fs.BoolVar(&s.ApiDoc, "api-doc", true, "host OpenAPI doc")
fs.StringVar(&s.KubeConfig, "kubeconfig", fmt.Sprintf("%s/.kube/config", os.Getenv("HOME")), "path to kubeconfig file")
}

View File

@@ -0,0 +1,73 @@
package app
import (
"fmt"
"github.com/emicklei/go-restful-openapi"
"github.com/spf13/cobra"
"kubesphere.io/kubesphere/cmd/ks-apiserver/app/options"
"kubesphere.io/kubesphere/pkg/apiserver/runtime"
"kubesphere.io/kubesphere/pkg/client"
"kubesphere.io/kubesphere/pkg/filter"
"kubesphere.io/kubesphere/pkg/informers"
"kubesphere.io/kubesphere/pkg/signals"
"log"
"net/http"
)
func NewAPIServerCommand() *cobra.Command {
s := options.NewServerRunOptions()
cmd := &cobra.Command{
Use: "ks-apiserver",
Long: `The KubeSphere API server validates and configures data
for the api objects. The API Server services REST operations and provides the frontend to the
cluster's shared state through which all other components interact.`,
RunE: func(cmd *cobra.Command, args []string) error {
//s.AddFlags(cmd.Flags())
return Run(s)
},
}
s.AddFlags(cmd.Flags())
return cmd
}
func Run(s *options.ServerRunOptions) error {
var err error
stopChan := signals.SetupSignalHandler()
informers.SharedInformerFactory().Start(stopChan)
informers.SharedInformerFactory().WaitForCacheSync(stopChan)
log.Println("resources sync success")
container := runtime.Container
container.Filter(filter.Logging)
if len(s.KubeConfig) > 0 {
client.KubeConfigFile = s.KubeConfig
}
if s.ApiDoc {
config := restfulspec.Config{
WebServices: container.RegisteredWebServices(),
APIPath: "/apidoc.json",
}
container.Add(restfulspec.NewOpenAPIService(config))
}
log.Printf("Server listening on %d.", s.InsecurePort)
if s.InsecurePort != 0 {
err = http.ListenAndServe(fmt.Sprintf("%s:%d", s.BindAddress, s.InsecurePort), container)
}
if s.SecurePort != 0 && len(s.TlsCertFile) > 0 && len(s.TlsPrivateKey) > 0 {
err = http.ListenAndServeTLS(fmt.Sprintf("%s:%d", s.BindAddress, s.SecurePort), s.TlsCertFile, s.TlsPrivateKey, container)
}
return err
}