github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/cmd/lorry/main.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package main
    21  
    22  import (
    23  	"flag"
    24  	"fmt"
    25  	"os"
    26  	"os/signal"
    27  	"strings"
    28  	"syscall"
    29  
    30  	"github.com/pkg/errors"
    31  	"github.com/spf13/pflag"
    32  	"go.uber.org/automaxprocs/maxprocs"
    33  	"go.uber.org/zap"
    34  	ctrl "sigs.k8s.io/controller-runtime"
    35  	kzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
    36  
    37  	"github.com/1aal/kubeblocks/pkg/constant"
    38  	"github.com/1aal/kubeblocks/pkg/lorry/dcs"
    39  	"github.com/1aal/kubeblocks/pkg/lorry/engines/register"
    40  	"github.com/1aal/kubeblocks/pkg/lorry/grpcserver"
    41  	"github.com/1aal/kubeblocks/pkg/lorry/highavailability"
    42  	"github.com/1aal/kubeblocks/pkg/lorry/httpserver"
    43  	opsregister "github.com/1aal/kubeblocks/pkg/lorry/operations/register"
    44  	viper "github.com/1aal/kubeblocks/pkg/viperx"
    45  )
    46  
    47  var configDir string
    48  
    49  func init() {
    50  	viper.AutomaticEnv()
    51  	pflag.StringVar(&configDir, "config-path", "/config/lorry/components/", "Lorry default config directory for builtin type")
    52  }
    53  
    54  func main() {
    55  	// Set GOMAXPROCS
    56  	_, _ = maxprocs.Set()
    57  
    58  	// Initialize flags
    59  	opts := kzap.Options{
    60  		Development: true,
    61  	}
    62  	opts.BindFlags(flag.CommandLine)
    63  	pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
    64  	pflag.Parse()
    65  	err := viper.BindPFlags(pflag.CommandLine)
    66  	if err != nil {
    67  		panic(errors.Wrap(err, "fatal error viper bindPFlags"))
    68  	}
    69  
    70  	// Initialize logger
    71  	kopts := []kzap.Opts{kzap.UseFlagOptions(&opts)}
    72  	if strings.EqualFold("debug", viper.GetString("zap-log-level")) {
    73  		kopts = append(kopts, kzap.RawZapOpts(zap.AddCaller()))
    74  	}
    75  	ctrl.SetLogger(kzap.New(kopts...))
    76  
    77  	// Initialize DB Manager
    78  	err = register.InitDBManager(configDir)
    79  	if err != nil {
    80  		panic(errors.Wrap(err, "DB manager initialize failed"))
    81  	}
    82  
    83  	// Initialize DCS (Distributed Control System)
    84  	err = dcs.InitStore()
    85  	if err != nil {
    86  		panic(errors.Wrap(err, "DCS initialize failed"))
    87  	}
    88  
    89  	// Start HA
    90  	characterType := viper.GetString(constant.KBEnvCharacterType)
    91  	if viper.IsSet(constant.KBEnvBuiltinHandler) {
    92  		characterType = viper.GetString(constant.KBEnvBuiltinHandler)
    93  	}
    94  	workloadType := viper.GetString(constant.KBEnvWorkloadType)
    95  	if highavailability.IsHAAvailable(characterType, workloadType) {
    96  		ha := highavailability.NewHa()
    97  		if ha != nil {
    98  			defer ha.ShutdownWithWait()
    99  			go ha.Start()
   100  		}
   101  	}
   102  
   103  	// start grpc server for role probe
   104  	grpcServer, err := grpcserver.NewGRPCServer()
   105  	if err != nil {
   106  		panic(fmt.Errorf("fatal error grpcserver create failed: %v", err))
   107  	}
   108  	err = grpcServer.StartNonBlocking()
   109  	if err != nil {
   110  		panic(fmt.Errorf("fatal error grpcserver serve failed: %v", err))
   111  	}
   112  
   113  	// Start HTTP Server
   114  	ops := opsregister.Operations()
   115  	httpServer := httpserver.NewServer(ops)
   116  	err = httpServer.StartNonBlocking()
   117  	if err != nil {
   118  		panic(errors.Wrap(err, "HTTP server initialize failed"))
   119  	}
   120  
   121  	stop := make(chan os.Signal, 1)
   122  	signal.Notify(stop, syscall.SIGTERM, os.Interrupt)
   123  	<-stop
   124  }