github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/cmd/reloader/app/flags.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 app
    21  
    22  import (
    23  	"os"
    24  
    25  	"github.com/spf13/pflag"
    26  
    27  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    28  	cfgutil "github.com/1aal/kubeblocks/pkg/configuration/container"
    29  	cfgcore "github.com/1aal/kubeblocks/pkg/configuration/core"
    30  	viper "github.com/1aal/kubeblocks/pkg/viperx"
    31  )
    32  
    33  type NotifyEventType int
    34  
    35  const (
    36  	UnixSignal NotifyEventType = iota // "signal"
    37  	WebHook                           // "http"
    38  	ShellTool                         // "exec"
    39  	SQL                               // "sql"
    40  	TPLScript                         // "tpl"
    41  	Comb                              // "comb"
    42  )
    43  
    44  const (
    45  	configManagerDefaultPort = 9901
    46  	configPodIPEnvName       = "CONFIG_MANAGER_POD_IP"
    47  	localhostAddress         = "127.0.0.1"
    48  )
    49  
    50  var allNotifyType = map[NotifyEventType]appsv1alpha1.CfgReloadType{
    51  	UnixSignal: appsv1alpha1.UnixSignalType,
    52  	ShellTool:  appsv1alpha1.ShellType,
    53  	TPLScript:  appsv1alpha1.TPLScriptType,
    54  }
    55  
    56  func init() {
    57  	if err := viper.BindEnv(configPodIPEnvName); err != nil {
    58  		os.Exit(-2)
    59  	}
    60  	// viper.AutomaticEnv()
    61  	viper.SetDefault(configPodIPEnvName, localhostAddress)
    62  }
    63  
    64  func (f *NotifyEventType) Type() string {
    65  	return "notifyType"
    66  }
    67  
    68  func (f *NotifyEventType) Set(val string) error {
    69  	for key, value := range allNotifyType {
    70  		if val == string(value) {
    71  			*f = key
    72  			return nil
    73  		}
    74  	}
    75  	return cfgcore.MakeError("not supported type[%s], required list: [%v]", val, allNotifyType)
    76  }
    77  
    78  func (f *NotifyEventType) String() string {
    79  	reloadType, ok := allNotifyType[*f]
    80  	if !ok {
    81  		return ""
    82  	}
    83  	return string(reloadType)
    84  }
    85  
    86  type ReconfigureServiceOptions struct {
    87  	GrpcPort int
    88  	PodIP    string
    89  
    90  	// EnableRemoteOnlineUpdate enables remote online update
    91  	RemoteOnlineUpdateEnable bool
    92  	// EnableContainerRuntime enables container runtime
    93  	ContainerRuntimeEnable bool
    94  
    95  	DebugMode        bool
    96  	ContainerRuntime cfgutil.CRIType
    97  	RuntimeEndpoint  string
    98  }
    99  
   100  type VolumeWatcherOpts struct {
   101  	VolumeDirs []string
   102  
   103  	// Exec command for reload
   104  	BackupPath string
   105  
   106  	LogLevel   string
   107  	CombConfig string
   108  
   109  	ServiceOpt ReconfigureServiceOptions
   110  }
   111  
   112  func NewVolumeWatcherOpts() *VolumeWatcherOpts {
   113  	return &VolumeWatcherOpts{
   114  		// for reconfigure options
   115  		ServiceOpt: ReconfigureServiceOptions{
   116  			GrpcPort:                 configManagerDefaultPort,
   117  			PodIP:                    viper.GetString(configPodIPEnvName),
   118  			ContainerRuntime:         cfgutil.AutoType,
   119  			DebugMode:                false,
   120  			ContainerRuntimeEnable:   false,
   121  			RemoteOnlineUpdateEnable: false,
   122  		},
   123  		LogLevel: "info",
   124  	}
   125  }
   126  
   127  func InstallFlags(flags *pflag.FlagSet, opt *VolumeWatcherOpts) {
   128  	flags.StringArrayVar(&opt.VolumeDirs,
   129  		"volume-dir",
   130  		opt.VolumeDirs,
   131  		"the config map volume directory to be watched for updates; may be used multiple times.")
   132  
   133  	// flags.Var(&opt.NotifyHandType,
   134  	//	"notify-type",
   135  	//	"the config describes how to process notification messages.",
   136  	// )
   137  	//
   138  	// for signal handle
   139  	// flags.StringVar(&opt.ProcessName,
   140  	//	"process",
   141  	//	opt.ProcessName,
   142  	//	"the config describes what db program is.")
   143  	// flags.StringVar((*string)(&opt.Signal),
   144  	//	"signal",
   145  	//	string(opt.Signal),
   146  	//	"the config describes the reload unix signal.")
   147  	//
   148  	// for exec handle
   149  	// flags.StringVar(&opt.Command,
   150  	//	"command",
   151  	//	opt.Command,
   152  	//	"the config describes reload command. ")
   153  	//
   154  	// for exec tpl scripts
   155  	// flags.StringVar(&opt.TPLConfig,
   156  	//	"tpl-config",
   157  	//	opt.TPLConfig,
   158  	//	"the config describes reload behaviors by tpl script.")
   159  	// flags.StringVar(&opt.BackupPath,
   160  	//	"backup-path",
   161  	//	opt.BackupPath,
   162  	//	"the config describes backup path.")
   163  	// flags.StringVar(&opt.FileRegex,
   164  	//	"regex",
   165  	//	opt.FileRegex,
   166  	//	"the config sets filter config file.")
   167  
   168  	flags.StringVar(&opt.LogLevel,
   169  		"log-level",
   170  		opt.LogLevel,
   171  		"the config sets log level. enum: [error, info, debug]")
   172  	flags.StringVar(&opt.ServiceOpt.PodIP,
   173  		"pod-ip",
   174  		opt.ServiceOpt.PodIP,
   175  		"the config sets pod ip address.")
   176  	flags.IntVar(&opt.ServiceOpt.GrpcPort,
   177  		"tcp",
   178  		opt.ServiceOpt.GrpcPort,
   179  		"the config sets service port.")
   180  	flags.BoolVar(&opt.ServiceOpt.DebugMode,
   181  		"debug",
   182  		opt.ServiceOpt.DebugMode,
   183  		"the config sets debug mode.")
   184  	flags.StringVar((*string)(&opt.ServiceOpt.ContainerRuntime),
   185  		"container-runtime",
   186  		string(opt.ServiceOpt.ContainerRuntime),
   187  		"the config sets cri runtime type.")
   188  	flags.StringVar(&opt.ServiceOpt.RuntimeEndpoint,
   189  		"runtime-endpoint",
   190  		opt.ServiceOpt.RuntimeEndpoint,
   191  		"the config sets cri runtime endpoint.")
   192  
   193  	flags.BoolVar(&opt.ServiceOpt.ContainerRuntimeEnable,
   194  		"cri-enable",
   195  		opt.ServiceOpt.ContainerRuntimeEnable,
   196  		"the config sets enable cri.")
   197  
   198  	flags.BoolVar(&opt.ServiceOpt.RemoteOnlineUpdateEnable,
   199  		"operator-update-enable",
   200  		opt.ServiceOpt.ContainerRuntimeEnable,
   201  		"the config sets enable operator update parameter.")
   202  
   203  	// for multi handler
   204  	flags.StringVar(&opt.CombConfig,
   205  		"config",
   206  		"",
   207  		"the reload config.")
   208  }