diff --git a/dcs/k8s.go b/dcs/k8s.go index 19fecba..cfbb4ae 100644 --- a/dcs/k8s.go +++ b/dcs/k8s.go @@ -77,12 +77,12 @@ func NewKubernetesStore() (*KubernetesStore, error) { clusterName := constant.GetClusterName() if clusterName == "" { - return nil, errors.New(fmt.Sprintf("%s must be set", constant.KBEnvClusterName)) + return nil, errors.New(fmt.Sprintf("%s or %s must be set", constant.EnvClusterName, constant.KBEnvClusterName)) } componentName := constant.GetComponentName() if componentName == "" { - return nil, errors.New(fmt.Sprintf("%s must be set", constant.KBEnvCompName)) + return nil, errors.New(fmt.Sprintf("%s or %s must be set", constant.EnvComponentName, constant.KBEnvCompName)) } clusterCompName := constant.GetClusterCompName() @@ -92,12 +92,12 @@ func NewKubernetesStore() (*KubernetesStore, error) { currentMemberName := constant.GetPodName() if currentMemberName == "" { - return nil, errors.Wrap(err, "get hostname failed") + return nil, errors.New("get hostname failed") } namespace := constant.GetNamespace() if namespace == "" { - return nil, errors.New("KB_NAMESPACE must be set") + return nil, errors.New(fmt.Sprintf("%s or %s must be set", constant.EnvNamespace, constant.KBEnvNamespace)) } isLeaderClusterWide := false diff --git a/engines/base.go b/engines/base.go index 6d521ac..8eb3ade 100644 --- a/engines/base.go +++ b/engines/base.go @@ -25,7 +25,6 @@ import ( "github.com/go-logr/logr" "github.com/pkg/errors" - "github.com/spf13/viper" "github.com/apecloud/dbctl/constant" "github.com/apecloud/dbctl/dcs" @@ -53,8 +52,8 @@ func NewDBManagerBase(logger logr.Logger) (*DBManagerBase, error) { mgr := DBManagerBase{ CurrentMemberName: currentMemberName, CurrentMemberIP: constant.GetPodIP(), - ClusterCompName: viper.GetString(constant.KBEnvClusterCompName), - Namespace: viper.GetString(constant.KBEnvNamespace), + ClusterCompName: constant.GetClusterCompName(), + Namespace: constant.GetNamespace(), Logger: logger, } return &mgr, nil diff --git a/engines/mongodb/config.go b/engines/mongodb/config.go index 3bc212a..3f79880 100644 --- a/engines/mongodb/config.go +++ b/engines/mongodb/config.go @@ -100,8 +100,8 @@ func NewConfig(properties map[string]string) (*Config, error) { config.Password = viper.GetString(RootPasswordEnv) } - if viper.IsSet(constant.KBEnvClusterCompName) { - config.ReplSetName = viper.GetString(constant.KBEnvClusterCompName) + if clusterCompName := constant.GetClusterCompName(); clusterCompName != "" { + config.ReplSetName = clusterCompName } config.DatabaseName = adminDatabase diff --git a/go.mod b/go.mod index b7797d2..e0d3367 100644 --- a/go.mod +++ b/go.mod @@ -9,8 +9,6 @@ require ( github.com/IBM/sarama v1.43.2 github.com/apecloud/kubeblocks v0.9.0 github.com/cenkalti/backoff/v4 v4.2.1 - github.com/deckarep/golang-set/v2 v2.6.0 - github.com/dlclark/regexp2 v1.11.0 github.com/fasthttp/router v1.4.20 github.com/go-logr/logr v1.4.2 github.com/go-logr/zapr v1.3.0 diff --git a/go.sum b/go.sum index 5f8873c..35601fd 100644 --- a/go.sum +++ b/go.sum @@ -40,12 +40,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM= -github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= -github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI= -github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/eapache/go-resiliency v1.6.0 h1:CqGDTLtpwuWKn6Nj3uNUdflaq+/kIPsg0gfNzHton30= diff --git a/util/event.go b/util/event.go index e437afc..34a0358 100644 --- a/util/event.go +++ b/util/event.go @@ -24,7 +24,6 @@ import ( "encoding/json" "errors" "fmt" - "os" "time" "github.com/spf13/viper" @@ -78,7 +77,7 @@ func CreateEvent(reason string, data map[string]any) (*corev1.Event, error) { podName := constant.GetPodName() podUID := constant.GetPodUID() nodeName := viper.GetString(constant.KBEnvNodeName) - namespace := viper.GetString(constant.KBEnvNamespace) + namespace := constant.GetNamespace() msg, err := json.Marshal(data) if err != nil { return nil, err @@ -126,7 +125,7 @@ func SendEvent(ctx context.Context, event *corev1.Event) error { logger.Info("k8s client create failed", "error", err.Error()) return err } - namespace := os.Getenv(constant.KBEnvNamespace) + namespace := constant.GetNamespace() for i := 0; i < 30; i++ { _, err = clientset.CoreV1().Events(namespace).Create(ctx1, event, metav1.CreateOptions{}) if err == nil { diff --git a/util/ping.go b/util/ping.go index fe1b361..a5fb76d 100644 --- a/util/ping.go +++ b/util/ping.go @@ -22,25 +22,8 @@ package util import ( "net" "time" - - "github.com/spf13/viper" - ctlruntime "sigs.k8s.io/controller-runtime" - - "github.com/apecloud/dbctl/constant" ) -var pingerLogger = ctlruntime.Log.WithName("pinger") - -// IsDNSReady checks if dns and ip is ready, it can successfully resolve dns. -// Since the vast majority of container runtimes currently prohibit the NET_RAW capability, -// we can't rely on ICMP protocol to detect DNS resolution. -// Instead, we directly depend on TCP for port detection. -func IsDNSReady(dns string) (bool, error) { - // get the port where the Lorry HTTP service is listening - port := viper.GetString("port") - return IsTCPReady(dns, port) -} - func IsTCPReady(host, port string) (bool, error) { address := net.JoinHostPort(host, port) timeout := 2 * time.Second @@ -54,40 +37,3 @@ func IsTCPReady(host, port string) (bool, error) { return true, nil } - -func CheckDNSReadyWithRetry(dns string, times int) (bool, error) { - var ready bool - var err error - for i := 0; i < times; i++ { - ready, err = IsDNSReady(dns) - if err == nil && ready { - return true, nil - } - } - pingerLogger.Info("dns resolution is ready", "dns", dns) - return ready, err -} - -// WaitForDNSReady checks if dns is ready -func WaitForDNSReady(dns string) { - pingerLogger.Info("Waiting for dns resolution to be ready") - isPodReady, err := IsDNSReady(dns) - for err != nil || !isPodReady { - if err != nil { - pingerLogger.Info("dns check failed", "error", err.Error()) - } - time.Sleep(3 * time.Second) - isPodReady, err = IsDNSReady(dns) - } - pingerLogger.Info("dns resolution is ready", "dns", dns) -} - -// WaitForPodReady checks if pod is ready -func WaitForPodReady(checkPodHeadless bool) { - domain := viper.GetString(constant.KBEnvPodFQDN) - WaitForDNSReady(domain) - if checkPodHeadless { - domain := viper.GetString(constant.KBEnvPodName) - WaitForDNSReady(domain) - } -}