server.go 4.0 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/signals"
R
runzexia 已提交
33 34
	"kubesphere.io/kubesphere/pkg/simple/client/admin_jenkins"
	"kubesphere.io/kubesphere/pkg/simple/client/devops_mysql"
H
hongming 已提交
35
	"kubesphere.io/kubesphere/pkg/utils/jwtutil"
H
hongming 已提交
36 37
	"log"
	"net/http"
Z
zryfish 已提交
38
	"time"
H
hongming 已提交
39 40 41
)

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

	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 已提交
53
	s.AddFlags(cmd.Flags())
H
hongming 已提交
54 55
	cmd.Flags().AddGoFlagSet(goflag.CommandLine)
	glog.CopyStandardLogTo("INFO")
J
jeff 已提交
56

H
hongming 已提交
57 58 59 60
	return cmd
}

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

H
hongming 已提交
65 66
	var err error

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

	if err != nil {
		return err
	}

H
hongming 已提交
73 74
	waitForResourceSync()

R
runzexia 已提交
75 76 77
	initializeAdminJenkins()
	initializeDevOpsDatabase()

Z
zryfish 已提交
78
	err = iam.Init(s.AdminEmail, s.AdminPassword, expireTime)
H
hongming 已提交
79
	jwtutil.Setup(s.JWTSecret)
H
hongming 已提交
80 81 82 83 84 85 86

	if err != nil {
		return err
	}

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

H
hongming 已提交
90 91 92 93 94 95
	for _, webservice := range container.RegisteredWebServices() {
		for _, route := range webservice.Routes() {
			log.Println(route.Method, route.Path)
		}
	}

J
jeff 已提交
96
	if s.GenericServerRunOptions.InsecurePort != 0 {
H
hongming 已提交
97
		log.Printf("Server listening on %d.", s.GenericServerRunOptions.InsecurePort)
J
jeff 已提交
98
		err = http.ListenAndServe(fmt.Sprintf("%s:%d", s.GenericServerRunOptions.BindAddress, s.GenericServerRunOptions.InsecurePort), container)
H
hongming 已提交
99 100
	}

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

	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 已提交
122 123 124 125 126 127

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

	ksInformerFactory.Start(stopChan)
	ksInformerFactory.WaitForCacheSync(stopChan)
H
hongming 已提交
128 129
	log.Println("resources sync success")
}
R
runzexia 已提交
130 131 132 133 134 135 136 137

func initializeAdminJenkins() {
	admin_jenkins.Client()
}

func initializeDevOpsDatabase() {
	devops_mysql.OpenDatabase()
}