github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/cmd/reloader/container_killer/killer.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 "context" 24 "fmt" 25 "os" 26 "time" 27 28 "github.com/go-logr/logr" 29 "github.com/go-logr/zapr" 30 "github.com/spf13/pflag" 31 zaplogfmt "github.com/sykesm/zap-logfmt" 32 "go.uber.org/zap" 33 "go.uber.org/zap/zapcore" 34 35 cfgutil "github.com/1aal/kubeblocks/pkg/configuration/container" 36 viper "github.com/1aal/kubeblocks/pkg/viperx" 37 ) 38 39 const ( 40 rfc3339Mills = "2006-01-02T15:04:05.000" 41 ) 42 43 var logger logr.Logger 44 45 func main() { 46 var containerRuntime cfgutil.CRIType 47 var runtimeEndpoint string 48 var containerID []string 49 50 pflag.StringVar((*string)(&containerRuntime), 51 "container-runtime", "auto", "the config sets cri runtime type.") 52 pflag.StringVar(&runtimeEndpoint, 53 "runtime-endpoint", runtimeEndpoint, "the config sets cri runtime endpoint.") 54 pflag.StringArrayVar(&containerID, 55 "container-id", containerID, "the container-id to be killed.") 56 pflag.Parse() 57 58 if len(containerID) == 0 { 59 fmt.Fprintf(os.Stderr, " container-id required!\n\n") 60 pflag.Usage() 61 os.Exit(-1) 62 } 63 64 logCfg := zap.NewProductionEncoderConfig() 65 logCfg.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) { 66 encoder.AppendString(ts.UTC().Format(rfc3339Mills)) 67 } 68 69 zapLogger := zap.New(zapcore.NewCore(zaplogfmt.NewEncoder(logCfg), os.Stdout, zap.DebugLevel)) 70 logger = zapr.NewLogger(zapLogger) 71 72 killer, err := cfgutil.NewContainerKiller(containerRuntime, runtimeEndpoint, zapLogger.Sugar()) 73 if err != nil { 74 logger.Error(err, "failed to create container killing process") 75 os.Exit(-1) 76 } 77 78 if err := killer.Init(context.Background()); err != nil { 79 logger.Error(err, "failed to init killer") 80 } 81 82 if err := killer.Kill(context.Background(), containerID, viper.GetString(cfgutil.KillContainerSignalEnvName), nil); err != nil { 83 logger.Error(err, fmt.Sprintf("failed to kill container[%s]", containerID)) 84 } 85 }