server.go 3.7 KB
Newer Older
H
hongming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*

 Copyright 2019 The KubeSphere 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 app

import (
	goflag "flag"
	"fmt"
	"github.com/golang/glog"
	"github.com/spf13/cobra"
J
jeff 已提交
25 26
	"github.com/spf13/pflag"
	"kubesphere.io/kubesphere/cmd/ks-iam/app/options"
H
hongming 已提交
27 28 29 30
	"kubesphere.io/kubesphere/pkg/apiserver/runtime"
	"kubesphere.io/kubesphere/pkg/filter"
	"kubesphere.io/kubesphere/pkg/informers"
	"kubesphere.io/kubesphere/pkg/models/iam"
R
update  
runzexia 已提交
31
	"kubesphere.io/kubesphere/pkg/server"
H
hongming 已提交
32
	"kubesphere.io/kubesphere/pkg/utils/jwtutil"
33
	"kubesphere.io/kubesphere/pkg/utils/signals"
H
hongming 已提交
34 35
	"log"
	"net/http"
Z
zryfish 已提交
36
	"time"
H
hongming 已提交
37 38 39
)

func NewAPIServerCommand() *cobra.Command {
J
jeff 已提交
40
	s := options.NewServerRunOptions()
H
hongming 已提交
41 42 43 44 45 46 47 48 49 50

	cmd := &cobra.Command{
		Use: "ks-iam",
		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 {
			return Run(s)
		},
	}
J
jeff 已提交
51
	s.AddFlags(cmd.Flags())
H
hongming 已提交
52 53
	cmd.Flags().AddGoFlagSet(goflag.CommandLine)
	glog.CopyStandardLogTo("INFO")
J
jeff 已提交
54

H
hongming 已提交
55 56 57 58
	return cmd
}

func Run(s *options.ServerRunOptions) error {
J
jeff 已提交
59 60 61 62
	pflag.VisitAll(func(flag *pflag.Flag) {
		log.Printf("FLAG: --%s=%q", flag.Name, flag.Value)
	})

H
hongming 已提交
63 64
	var err error

Z
zryfish 已提交
65 66 67 68 69 70
	expireTime, err := time.ParseDuration(s.TokenExpireTime)

	if err != nil {
		return err
	}

H
hongming 已提交
71 72
	waitForResourceSync()

73
	err = iam.Init(s.AdminEmail, s.AdminPassword, expireTime, s.AuthRateLimit)
H
hongming 已提交
74
	jwtutil.Setup(s.JWTSecret)
H
hongming 已提交
75 76 77 78 79 80 81

	if err != nil {
		return err
	}

	container := runtime.Container
	container.Filter(filter.Logging)
R
runzexia 已提交
82
	container.DoNotRecover(false)
R
update  
runzexia 已提交
83
	container.RecoverHandler(server.LogStackOnRecover)
H
hongming 已提交
84

H
hongming 已提交
85 86 87 88 89 90
	for _, webservice := range container.RegisteredWebServices() {
		for _, route := range webservice.Routes() {
			log.Println(route.Method, route.Path)
		}
	}

J
jeff 已提交
91
	if s.GenericServerRunOptions.InsecurePort != 0 {
H
hongming 已提交
92
		log.Printf("Server listening on %d.", s.GenericServerRunOptions.InsecurePort)
J
jeff 已提交
93
		err = http.ListenAndServe(fmt.Sprintf("%s:%d", s.GenericServerRunOptions.BindAddress, s.GenericServerRunOptions.InsecurePort), container)
H
hongming 已提交
94 95
	}

J
jeff 已提交
96
	if s.GenericServerRunOptions.SecurePort != 0 && len(s.GenericServerRunOptions.TlsCertFile) > 0 && len(s.GenericServerRunOptions.TlsPrivateKey) > 0 {
H
hongming 已提交
97
		log.Printf("Server listening on %d.", s.GenericServerRunOptions.SecurePort)
J
jeff 已提交
98
		err = http.ListenAndServeTLS(fmt.Sprintf("%s:%d", s.GenericServerRunOptions.BindAddress, s.GenericServerRunOptions.SecurePort), s.GenericServerRunOptions.TlsCertFile, s.GenericServerRunOptions.TlsPrivateKey, container)
H
hongming 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	}

	return err
}

func waitForResourceSync() {
	stopChan := signals.SetupSignalHandler()

	informerFactory := informers.SharedInformerFactory()
	informerFactory.Rbac().V1().Roles().Lister()
	informerFactory.Rbac().V1().RoleBindings().Lister()
	informerFactory.Rbac().V1().ClusterRoles().Lister()
	informerFactory.Rbac().V1().ClusterRoleBindings().Lister()

	informerFactory.Core().V1().Namespaces().Lister()

	informerFactory.Start(stopChan)
	informerFactory.WaitForCacheSync(stopChan)
H
hongming 已提交
117 118 119 120 121 122

	ksInformerFactory := informers.KsSharedInformerFactory()
	ksInformerFactory.Tenant().V1alpha1().Workspaces().Lister()

	ksInformerFactory.Start(stopChan)
	ksInformerFactory.WaitForCacheSync(stopChan)
H
hongming 已提交
123 124
	log.Println("resources sync success")
}