server.go 3.6 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 31
	"kubesphere.io/kubesphere/pkg/apiserver/runtime"
	"kubesphere.io/kubesphere/pkg/filter"
	"kubesphere.io/kubesphere/pkg/informers"
	"kubesphere.io/kubesphere/pkg/models/iam"
	"kubesphere.io/kubesphere/pkg/signals"
H
hongming 已提交
32
	"kubesphere.io/kubesphere/pkg/utils/jwtutil"
H
hongming 已提交
33 34
	"log"
	"net/http"
Z
zryfish 已提交
35
	"time"
H
hongming 已提交
36 37 38
)

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

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

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

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

H
hongming 已提交
62 63
	var err error

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

	if err != nil {
		return err
	}

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

	if err != nil {
		return err
	}

	waitForResourceSync()

	container := runtime.Container
	container.Filter(filter.Logging)

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

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

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

	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 已提交
114 115 116 117 118 119

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

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