Files
kubevela/cmd/apiserver/main.go
qiaozp 9317bb1cab Refactor: addon cache mechanism and code architecture (#2956)
* Refactor: fix addon cache and code

Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com>

* basic trim

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* Fix list OSS bucket addon's meta

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* rename listAddonMeta func

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* fix enable

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* rename and trim cache func call

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* remove same source code, use Registry to implement Source interface. Keep the compatibility of DeployTo fields.

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* complete github reader

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* fix read from github, fix test

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* reviewable

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* header

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* rename function, restore test

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* try CI

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* sort out functions name. add detail test

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* fix test

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* fix test

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* filter directory without metadata.yaml in oss

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* add GitHub reader unit test

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* clean up

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* reviewable

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* header

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* add cache arg

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* fix test

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* change field name

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* build swagger

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

* some json tag, revert cache logic

Signed-off-by: qiaozp <chivalry.pp@gmail.com>

Co-authored-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com>
2021-12-21 09:31:37 +08:00

125 lines
3.8 KiB
Go

/*
Copyright 2021 The KubeVela 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 main
import (
"context"
"encoding/json"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
restfulspec "github.com/emicklei/go-restful-openapi/v2"
"github.com/go-openapi/spec"
"github.com/google/uuid"
"github.com/oam-dev/kubevela/pkg/apiserver/log"
"github.com/oam-dev/kubevela/pkg/apiserver/rest"
"github.com/oam-dev/kubevela/version"
)
func main() {
s := &Server{}
flag.StringVar(&s.restCfg.BindAddr, "bind-addr", "0.0.0.0:8000", "The bind address used to serve the http APIs.")
flag.StringVar(&s.restCfg.MetricPath, "metrics-path", "/metrics", "The path to expose the metrics.")
flag.StringVar(&s.restCfg.Datastore.Type, "datastore-type", "kubeapi", "Metadata storage driver type, support kubeapi and mongodb")
flag.StringVar(&s.restCfg.Datastore.Database, "datastore-database", "kubevela", "Metadata storage database name, takes effect when the storage driver is mongodb.")
flag.StringVar(&s.restCfg.Datastore.URL, "datastore-url", "", "Metadata storage database url,takes effect when the storage driver is mongodb.")
flag.StringVar(&s.restCfg.LeaderConfig.ID, "id", uuid.New().String(), "the holder identity name")
flag.StringVar(&s.restCfg.LeaderConfig.LockName, "lock-name", "apiserver-lock", "the lease lock resource name")
flag.DurationVar(&s.restCfg.LeaderConfig.Duration, "duration", time.Second*5, "the lease lock resource name")
flag.DurationVar(&s.restCfg.AddonCacheTime, "addon-cache-duration", time.Minute*10, "how long between two addon cache operation")
flag.Parse()
if len(os.Args) > 2 && os.Args[1] == "build-swagger" {
func() {
swagger, err := s.buildSwagger()
if err != nil {
log.Logger.Fatal(err.Error())
}
outData, err := json.MarshalIndent(swagger, "", "\t")
if err != nil {
log.Logger.Fatal(err.Error())
}
swaggerFile, err := os.OpenFile(os.Args[2], os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
if err != nil {
log.Logger.Fatal(err.Error())
}
defer func() {
if err := swaggerFile.Close(); err != nil {
log.Logger.Errorf("close swagger file failure %s", err.Error())
}
}()
_, err = swaggerFile.Write(outData)
if err != nil {
log.Logger.Fatal(err.Error())
}
fmt.Println("build swagger config file success")
}()
return
}
srvc := make(chan struct{})
ctx, cancel := context.WithCancel(context.Background())
go func() {
if err := s.run(ctx); err != nil {
log.Logger.Errorf("failed to run apiserver: %v", err)
}
close(srvc)
}()
var term = make(chan os.Signal, 1)
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
select {
case <-term:
log.Logger.Infof("Received SIGTERM, exiting gracefully...")
cancel()
case <-srvc:
cancel()
os.Exit(1)
}
log.Logger.Infof("See you next time!")
}
// Server apiserver
type Server struct {
restCfg rest.Config
}
func (s *Server) run(ctx context.Context) error {
log.Logger.Infof("KubeVela information: version: %v, gitRevision: %v", version.VelaVersion, version.GitRevision)
server, err := rest.New(s.restCfg)
if err != nil {
return fmt.Errorf("create apiserver failed : %w ", err)
}
return server.Run(ctx)
}
func (s *Server) buildSwagger() (*spec.Swagger, error) {
server, err := rest.New(s.restCfg)
if err != nil {
return nil, fmt.Errorf("create apiserver failed : %w ", err)
}
return restfulspec.BuildSwagger(server.RegisterServices()), nil
}