server.go 6.3 KB
Newer Older
H
hongming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*

 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.

*/
J
jeff 已提交
18 19 20
package app

import (
H
hongming 已提交
21
	goflag "flag"
J
jeff 已提交
22
	"fmt"
H
hongming 已提交
23
	"github.com/golang/glog"
H
huanggze 已提交
24
	"github.com/json-iterator/go"
J
jeff 已提交
25
	kconfig "github.com/kiali/kiali/config"
J
jeff 已提交
26
	"github.com/spf13/cobra"
J
jeff 已提交
27 28
	"github.com/spf13/pflag"
	"kubesphere.io/kubesphere/cmd/ks-apiserver/app/options"
J
jeff 已提交
29
	"kubesphere.io/kubesphere/pkg/apiserver/runtime"
J
Jeff 已提交
30
	"kubesphere.io/kubesphere/pkg/apiserver/servicemesh/tracing"
J
jeff 已提交
31 32
	"kubesphere.io/kubesphere/pkg/filter"
	"kubesphere.io/kubesphere/pkg/informers"
33
	"kubesphere.io/kubesphere/pkg/models/devops"
H
huanggze 已提交
34
	logging "kubesphere.io/kubesphere/pkg/models/log"
R
update  
runzexia 已提交
35
	"kubesphere.io/kubesphere/pkg/server"
J
jeff 已提交
36
	"kubesphere.io/kubesphere/pkg/signals"
S
soulseen 已提交
37
	"kubesphere.io/kubesphere/pkg/simple/client/admin_jenkins"
R
runzexia 已提交
38
	"kubesphere.io/kubesphere/pkg/simple/client/devops_mysql"
J
jeff 已提交
39 40 41 42
	"log"
	"net/http"
)

H
huanggze 已提交
43 44
var jsonIter = jsoniter.ConfigCompatibleWithStandardLibrary

J
jeff 已提交
45
func NewAPIServerCommand() *cobra.Command {
J
jeff 已提交
46
	s := options.NewServerRunOptions()
J
jeff 已提交
47 48 49 50 51 52 53 54 55 56 57

	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 {
			return Run(s)
		},
	}

J
jeff 已提交
58
	s.AddFlags(cmd.Flags())
H
hongming 已提交
59 60
	cmd.Flags().AddGoFlagSet(goflag.CommandLine)
	glog.CopyStandardLogTo("INFO")
J
jeff 已提交
61

J
jeff 已提交
62 63 64 65 66
	return cmd
}

func Run(s *options.ServerRunOptions) error {

J
jeff 已提交
67 68 69 70
	pflag.VisitAll(func(flag *pflag.Flag) {
		log.Printf("FLAG: --%s=%q", flag.Name, flag.Value)
	})

J
jeff 已提交
71 72
	var err error

H
hongming 已提交
73
	waitForResourceSync()
J
jeff 已提交
74 75

	container := runtime.Container
R
runzexia 已提交
76
	container.DoNotRecover(false)
J
jeff 已提交
77
	container.Filter(filter.Logging)
R
update  
runzexia 已提交
78
	container.RecoverHandler(server.LogStackOnRecover)
J
jeff 已提交
79 80
	for _, webservice := range container.RegisteredWebServices() {
		for _, route := range webservice.Routes() {
H
hongming 已提交
81
			log.Println(route.Method, route.Path)
J
jeff 已提交
82
		}
J
jeff 已提交
83 84
	}

R
runzexia 已提交
85 86
	initializeAdminJenkins()
	initializeDevOpsDatabase()
H
huanggze 已提交
87
	initializeESClientConfig()
J
Jeff 已提交
88
	initializeServicemeshConfig(s)
J
jeff 已提交
89 90

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

	if s.GenericServerRunOptions.SecurePort != 0 && len(s.GenericServerRunOptions.TlsCertFile) > 0 && len(s.GenericServerRunOptions.TlsPrivateKey) > 0 {
H
hongming 已提交
96
		log.Printf("Server listening on %d.", s.GenericServerRunOptions.SecurePort)
J
jeff 已提交
97
		err = http.ListenAndServeTLS(fmt.Sprintf("%s:%d", s.GenericServerRunOptions.BindAddress, s.GenericServerRunOptions.SecurePort), s.GenericServerRunOptions.TlsCertFile, s.GenericServerRunOptions.TlsPrivateKey, container)
J
jeff 已提交
98 99 100 101
	}

	return err
}
H
hongming 已提交
102

R
runzexia 已提交
103
func initializeAdminJenkins() {
S
soulseen 已提交
104 105
	devops.JenkinsInit()
	admin_jenkins.Client()
R
runzexia 已提交
106 107 108 109
}

func initializeDevOpsDatabase() {
	devops_mysql.OpenDatabase()
H
hongming 已提交
110 111
}

J
Jeff 已提交
112
func initializeServicemeshConfig(s *options.ServerRunOptions) {
J
jeff 已提交
113 114 115
	// Initialize kiali config
	config := kconfig.NewConfig()

J
Jeff 已提交
116 117
	tracing.JaegerQueryUrl = s.JaegerQueryServiceUrl

J
jeff 已提交
118 119
	// Exclude system namespaces
	config.API.Namespaces.Exclude = []string{"istio-system", "kubesphere*", "kube*"}
J
Jeff 已提交
120
	config.InCluster = true
J
jeff 已提交
121 122

	// Set default prometheus service url
123
	config.ExternalServices.PrometheusServiceURL = s.ServicemeshPrometheusServiceUrl
J
jeff 已提交
124 125 126 127 128 129 130 131
	config.ExternalServices.PrometheusCustomMetricsURL = config.ExternalServices.PrometheusServiceURL

	// Set istio pilot discovery service url
	config.ExternalServices.Istio.UrlServiceVersion = s.IstioPilotServiceURL

	kconfig.Set(config)
}

H
huanggze 已提交
132 133
func initializeESClientConfig() {

134
	// List all outputs
H
hongming 已提交
135
	outputs, err := logging.GetFluentbitOutputFromConfigMap()
H
huanggze 已提交
136
	if err != nil {
137
		glog.Errorln(err)
H
huanggze 已提交
138 139 140
		return
	}

141
	// Iterate the outputs to get elasticsearch configs
H
huanggze 已提交
142
	for _, output := range outputs {
143 144 145
		if configs := logging.ParseEsOutputParams(output.Parameters); configs != nil {
			configs.WriteESConfigs()
			return
H
huanggze 已提交
146 147 148 149
		}
	}
}

H
hongming 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
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.Storage().V1().StorageClasses().Lister()

	informerFactory.Core().V1().Namespaces().Lister()
	informerFactory.Core().V1().Nodes().Lister()
	informerFactory.Core().V1().ResourceQuotas().Lister()
	informerFactory.Core().V1().Pods().Lister()
	informerFactory.Core().V1().Services().Lister()
	informerFactory.Core().V1().PersistentVolumeClaims().Lister()
	informerFactory.Core().V1().Secrets().Lister()
	informerFactory.Core().V1().ConfigMaps().Lister()

	informerFactory.Apps().V1().ControllerRevisions().Lister()
	informerFactory.Apps().V1().StatefulSets().Lister()
	informerFactory.Apps().V1().Deployments().Lister()
	informerFactory.Apps().V1().DaemonSets().Lister()
H
hongming 已提交
174
	informerFactory.Apps().V1().ReplicaSets().Lister()
H
hongming 已提交
175 176

	informerFactory.Batch().V1().Jobs().Lister()
H
hongming 已提交
177
	informerFactory.Batch().V1beta1().CronJobs().Lister()
H
hongming 已提交
178
	informerFactory.Extensions().V1beta1().Ingresses().Lister()
H
hongming 已提交
179 180 181

	informerFactory.Start(stopChan)
	informerFactory.WaitForCacheSync(stopChan)
R
runzexia 已提交
182

R
runzexia 已提交
183 184 185 186 187 188 189
	s2iInformerFactory := informers.S2iSharedInformerFactory()
	s2iInformerFactory.Devops().V1alpha1().S2iBuilderTemplates().Lister()
	s2iInformerFactory.Devops().V1alpha1().S2iRuns().Lister()
	s2iInformerFactory.Devops().V1alpha1().S2iBuilders().Lister()

	s2iInformerFactory.Start(stopChan)
	s2iInformerFactory.WaitForCacheSync(stopChan)
H
hongming 已提交
190 191 192 193 194 195 196

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

	ksInformerFactory.Start(stopChan)
	ksInformerFactory.WaitForCacheSync(stopChan)

H
hongming 已提交
197 198
	log.Println("resources sync success")
}