diff --git a/constant/const.go b/constant/const.go index e10d7e6..c7d62ac 100644 --- a/constant/const.go +++ b/constant/const.go @@ -20,257 +20,18 @@ along with this program. If not, see . package constant const ( - KBPrefix = "KB" - KBLowerPrefix = "kb" -) - -const ( - KBServiceAccountName = "KUBEBLOCKS_SERVICEACCOUNT_NAME" - KBToolsImage = "KUBEBLOCKS_TOOLS_IMAGE" - KBImagePullPolicy = "KUBEBLOCKS_IMAGE_PULL_POLICY" - KBDataScriptClientsImage = "KUBEBLOCKS_DATASCRIPT_CLIENTS_IMAGE" -) - -const ( - APIGroup = "kubeblocks.io" - - AppName = "kubeblocks" - - // K8S recommended well-known labels and annotation keys - // AppInstanceLabelKey refer cluster.Name - AppInstanceLabelKey = "app.kubernetes.io/instance" - // AppNameLabelKey refer clusterDefinition.Name before KubeBlocks Version 0.8.0 or refer ComponentDefinition.Name after KubeBlocks Version 0.8.0 (TODO:Pending) - AppNameLabelKey = "app.kubernetes.io/name" - // AppComponentLabelKey refer clusterDefinition.Spec.ComponentDefs[*].Name before KubeBlocks Version 0.8.0 or refer ComponentDefinition.Name after KubeBlocks Version 0.8.0 - AppComponentLabelKey = "app.kubernetes.io/component" - // AppVersionLabelKey refer clusterVersion.Name before KubeBlocks Version 0.8.0 or refer ComponentDefinition.Name after KubeBlocks Version 0.8.0 - AppVersionLabelKey = "app.kubernetes.io/version" - AppManagedByLabelKey = "app.kubernetes.io/managed-by" - RegionLabelKey = "topology.kubernetes.io/region" - ZoneLabelKey = "topology.kubernetes.io/zone" - - // kubeblocks.io labels - BackupProtectionLabelKey = "kubeblocks.io/backup-protection" // BackupProtectionLabelKey Backup delete protection policy label - AddonProviderLabelKey = "kubeblocks.io/provider" // AddonProviderLabelKey marks the addon provider - RoleLabelKey = "kubeblocks.io/role" // RoleLabelKey consensusSet and replicationSet role label key - ReadyWithoutPrimaryKey = "kubeblocks.io/ready-without-primary" - VolumeTypeLabelKey = "kubeblocks.io/volume-type" - ClusterAccountLabelKey = "account.kubeblocks.io/name" - KBAppClusterUIDLabelKey = "apps.kubeblocks.io/cluster-uid" - KBAppComponentLabelKey = "apps.kubeblocks.io/component-name" - KBAppShardingNameLabelKey = "apps.kubeblocks.io/sharding-name" - KBAppComponentDefRefLabelKey = "apps.kubeblocks.io/component-def-ref" // refer clusterDefinition.Spec.ComponentDefs[*].Name before KubeBlocks Version 0.8.0 or refer ComponentDefinition.Name after KubeBlocks Version 0.8.0 - KBAppClusterDefTypeLabelKey = "apps.kubeblocks.io/cluster-type" // refer clusterDefinition.Spec.Type (deprecated) - AppConfigTypeLabelKey = "apps.kubeblocks.io/config-type" - KBManagedByKey = "apps.kubeblocks.io/managed-by" // KBManagedByKey marks resources that auto created - PVCNameLabelKey = "apps.kubeblocks.io/pvc-name" - VolumeClaimTemplateNameLabelKey = "apps.kubeblocks.io/vct-name" - VolumeClaimTemplateNameLabelKeyForLegacy = "vct.kubeblocks.io/name" // Deprecated: only compatible with version 0.5, will be removed in 0.7 - WorkloadTypeLabelKey = "apps.kubeblocks.io/workload-type" - ClassProviderLabelKey = "class.kubeblocks.io/provider" - ClusterDefLabelKey = "clusterdefinition.kubeblocks.io/name" - ClusterVerLabelKey = "clusterversion.kubeblocks.io/name" - ComponentDefinitionLabelKey = "componentdefinition.kubeblocks.io/name" - CMConfigurationSpecProviderLabelKey = "config.kubeblocks.io/config-spec" // CMConfigurationSpecProviderLabelKey is ComponentConfigSpec name - CMConfigurationCMKeysLabelKey = "config.kubeblocks.io/configmap-keys" // CMConfigurationCMKeysLabelKey Specify configmap keys - CMConfigurationTemplateNameLabelKey = "config.kubeblocks.io/config-template-name" - CMTemplateNameLabelKey = "config.kubeblocks.io/template-name" - CMConfigurationTypeLabelKey = "config.kubeblocks.io/config-type" - CMInsConfigurationHashLabelKey = "config.kubeblocks.io/config-hash" - CMInsCurrentConfigurationHashLabelKey = "config.kubeblocks.io/update-config-hash" - CMConfigurationConstraintsNameLabelKey = "config.kubeblocks.io/config-constraints-name" - CMConfigurationTemplateVersion = "config.kubeblocks.io/config-template-version" - ConsensusSetAccessModeLabelKey = "cs.apps.kubeblocks.io/access-mode" - AddonNameLabelKey = "extensions.kubeblocks.io/addon-name" - OpsRequestTypeLabelKey = "ops.kubeblocks.io/ops-type" - OpsRequestNameLabelKey = "ops.kubeblocks.io/ops-name" - OpsRequestNamespaceLabelKey = "ops.kubeblocks.io/ops-namespace" - ServiceDescriptorNameLabelKey = "servicedescriptor.kubeblocks.io/name" - RestoreForHScaleLabelKey = "apps.kubeblocks.io/restore-for-hscale" - ResourceConstraintProviderLabelKey = "resourceconstraint.kubeblocks.io/provider" - - // StatefulSetPodNameLabelKey is used to mark the pod name of the StatefulSet - StatefulSetPodNameLabelKey = "statefulset.kubernetes.io/pod-name" - - // kubeblocks.io annotations - ClusterSnapshotAnnotationKey = "kubeblocks.io/cluster-snapshot" // ClusterSnapshotAnnotationKey saves the snapshot of cluster. - DefaultClusterVersionAnnotationKey = "kubeblocks.io/is-default-cluster-version" // DefaultClusterVersionAnnotationKey specifies the default cluster version. - OpsRequestAnnotationKey = "kubeblocks.io/ops-request" // OpsRequestAnnotationKey OpsRequest annotation key in Cluster - ReconcileAnnotationKey = "kubeblocks.io/reconcile" // ReconcileAnnotationKey Notify k8s object to reconcile - RestartAnnotationKey = "kubeblocks.io/restart" // RestartAnnotationKey the annotation which notices the StatefulSet/DeploySet to restart - RestoreFromBackupAnnotationKey = "kubeblocks.io/restore-from-backup" // RestoreFromBackupAnnotationKey specifies the component to recover from the backup. - SnapShotForStartAnnotationKey = "kubeblocks.io/snapshot-for-start" - ComponentReplicasAnnotationKey = "apps.kubeblocks.io/component-replicas" // ComponentReplicasAnnotationKey specifies the number of pods in replicas - BackupPolicyTemplateAnnotationKey = "apps.kubeblocks.io/backup-policy-template" - LastAppliedClusterAnnotationKey = "apps.kubeblocks.io/last-applied-cluster" - PVLastClaimPolicyAnnotationKey = "apps.kubeblocks.io/pv-last-claim-policy" - HaltRecoveryAllowInconsistentCVAnnotKey = "clusters.apps.kubeblocks.io/allow-inconsistent-cv" - HaltRecoveryAllowInconsistentResAnnotKey = "clusters.apps.kubeblocks.io/allow-inconsistent-resource" - PrimaryAnnotationKey = "rs.apps.kubeblocks.io/primary" - DisableUpgradeInsConfigurationAnnotationKey = "config.kubeblocks.io/disable-reconfigure" - LastAppliedConfigAnnotationKey = "config.kubeblocks.io/last-applied-configuration" - LastAppliedOpsCRAnnotationKey = "config.kubeblocks.io/last-applied-ops-name" - UpgradePolicyAnnotationKey = "config.kubeblocks.io/reconfigure-policy" - KBParameterUpdateSourceAnnotationKey = "config.kubeblocks.io/reconfigure-source" - UpgradeRestartAnnotationKey = "config.kubeblocks.io/restart" - ConfigAppliedVersionAnnotationKey = "config.kubeblocks.io/config-applied-version" - KubeBlocksGenerationKey = "kubeblocks.io/generation" - ExtraEnvAnnotationKey = "kubeblocks.io/extra-env" - LastRoleSnapshotVersionAnnotationKey = "apps.kubeblocks.io/last-role-snapshot-version" - HostPortAnnotationKey = "kubeblocks.io/host-port" - HostPortIncludeAnnotationKey = "network.kubeblocks.io/host-ports-include" - HostPortExcludeAnnotationKey = "network.kubeblocks.io/host-ports-exclude" - DisableHAAnnotationKey = "kubeblocks.io/disable-ha" - // EnabledNodePortSvcAnnotationKey defines the feature gate of NodePort Service defined in ComponentDefinition.Spec.Services. - // Components defined in the annotation value, their all services of type NodePort defined in ComponentDefinition will be created; otherwise, they will be ignored. - // Multiple components are separated by ','. for example: "kubeblocks.io/enabled-node-port-svc: comp1,comp2" - EnabledNodePortSvcAnnotationKey = "kubeblocks.io/enabled-node-port-svc" - // EnabledPodOrdinalSvcAnnotationKey defines the feature gate of PodOrdinal Service defined in ComponentDefinition.Spec.Services. - // Components defined in the annotation value, their all Services defined in the ComponentDefinition with the GeneratePodOrdinalService attribute set to true will be created; otherwise, they will be ignored. - // This can generate a corresponding Service for each Pod, which can be used in certain specific scenarios: for example, creating a dedicated access service for each read-only Pod. - // Multiple components are separated by ','. for example: "kubeblocks.io/enabled-pod-ordinal-svc: comp1,comp2" - EnabledPodOrdinalSvcAnnotationKey = "kubeblocks.io/enabled-pod-ordinal-svc" - // DisabledClusterIPSvcAnnotationKey defines whether the feature gate of ClusterIp Service defined in ComponentDefinition.Spec.Services will be effected. - // Components defined in the annotation value, their all services of type ClusterIp defined in ComponentDefinition will be ignored; otherwise, they will be created. - // Multiple components are separated by ','. for example: "kubeblocks.io/disabled-cluster-ip-svc: comp1,comp2" - DisabledClusterIPSvcAnnotationKey = "kubeblocks.io/disabled-cluster-ip-svc" - // ShardSvcAnnotationKey defines the feature gate of creating service for each shard. - // Sharding name defined in the annotation value, a set of Service defined in Cluster.Spec.Services with the ShardingSelector will be automatically generated for each shard when Cluster.Spec.ShardingSpecs[x].shards is not nil. - // Multiple sharding names are separated by ','. for example: "kubeblocks.io/enabled-shard-svc: proxy-shard,db-shard" - ShardSvcAnnotationKey = "kubeblocks.io/enabled-shard-svc" - - // kubeblocks.io well-known finalizers - DBClusterFinalizerName = "cluster.kubeblocks.io/finalizer" - DBComponentFinalizerName = "component.kubeblocks.io/finalizer" - DBComponentDefinitionFinalizerName = "componentdefinition.kubeblocks.io/finalizer" - ConfigurationTemplateFinalizerName = "config.kubeblocks.io/finalizer" - ServiceDescriptorFinalizerName = "servicedescriptor.kubeblocks.io/finalizer" - OpsRequestFinalizerName = "opsrequest.kubeblocks.io/finalizer" - - // ConfigurationTplLabelPrefixKey clusterVersion or clusterdefinition using tpl - ConfigurationTplLabelPrefixKey = "config.kubeblocks.io/tpl" - ConfigurationConstraintsLabelPrefixKey = "config.kubeblocks.io/constraints" - - // CMInsLastReconfigurePhaseKey defines the current phase - CMInsLastReconfigurePhaseKey = "config.kubeblocks.io/last-applied-reconfigure-phase" - - // ConfigurationRevision defines the current revision - // TODO support multi version - ConfigurationRevision = "config.kubeblocks.io/configuration-revision" - LastConfigurationRevisionPhase = "config.kubeblocks.io/revision-reconcile-phase" - - // Deprecated: only compatible with version 0.6, will be removed in 0.8 - // CMInsEnableRerenderTemplateKey is used to enable rerender template - CMInsEnableRerenderTemplateKey = "config.kubeblocks.io/enable-rerender" - - // IgnoreResourceConstraint is used to specify whether to ignore the resource constraint - IgnoreResourceConstraint = "resource.kubeblocks.io/ignore-constraint" - - RBACRoleName = "kubeblocks-cluster-pod-role" - RBACClusterRoleName = "kubeblocks-volume-protection-pod-role" - - // FeatureReconciliationInCompactModeAnnotationKey indicates that the controller should run in compact mode, - // means to try the best to cutoff useless objects. - FeatureReconciliationInCompactModeAnnotationKey = "kubeblocks.io/compact-mode" -) - -const ( - // ReasonNotFoundCR referenced custom resource not found - ReasonNotFoundCR = "NotFound" - // ReasonRefCRUnavailable referenced custom resource is unavailable - ReasonRefCRUnavailable = "Unavailable" - // ReasonDeletedCR deleted custom resource - ReasonDeletedCR = "DeletedCR" - // ReasonDeletingCR deleting custom resource - ReasonDeletingCR = "DeletingCR" - // ReasonCreatedCR created custom resource - ReasonCreatedCR = "CreatedCR" - // ReasonRunTaskFailed run task failed - ReasonRunTaskFailed = "RunTaskFailed" - // ReasonDeleteFailed delete failed - ReasonDeleteFailed = "DeleteFailed" -) - -const ( - // Container port name - LorryHTTPPortName = "lorry-http-port" - LorryGRPCPortName = "lorry-grpc-port" - LorryContainerName = "lorry" - LorryInitContainerName = "init-lorry" - ProbeInitContainerName = "kb-initprobe" - RoleProbeContainerName = "kb-checkrole" - StatusProbeContainerName = "kb-checkstatus" - RunningProbeContainerName = "kb-checkrunning" - VolumeProtectionProbeContainerName = "kb-volume-protection" - LorryRoleProbePath = "/v1.0/checkrole" - LorryVolumeProtectPath = "/v1.0/volumeprotection" - - // the filedpath name used in event.InvolvedObject.FieldPath - ProbeCheckStatusPath = "spec.containers{" + StatusProbeContainerName + "}" - ProbeCheckRunningPath = "spec.containers{" + RunningProbeContainerName + "}" -) - -const ( - ConfigSidecarName = "config-manager" - ConfigManagerGPRCPortEnv = "CONFIG_MANAGER_GRPC_PORT" - ConfigManagerLogLevel = "CONFIG_MANAGER_LOG_LEVEL" - - PodMinReadySecondsEnv = "POD_MIN_READY_SECONDS" - ConfigTemplateType = "tpl" - ConfigInstanceType = "instance" - - ReconfigureManagerSource = "manager" - ReconfigureUserSource = "ops" - ReconfigureTemplateSource = "external-template" - - ConfigManagerPortName = "config-manager" -) - -const ( - Primary = "primary" - Secondary = "secondary" - - Leader = "leader" - Follower = "follower" - Learner = "learner" -) - -// username and password are keys in created secrets for others to refer to. -const ( - AccountNameForSecret = "username" - AccountPasswdForSecret = "password" + AppInstanceLabelKey = "app.kubernetes.io/instance" + AppManagedByLabelKey = "app.kubernetes.io/managed-by" + RoleLabelKey = "kubeblocks.io/role" // RoleLabelKey consensusSet and replicationSet role label key + KBAppComponentLabelKey = "apps.kubeblocks.io/component-name" ) const ( - FeatureGateReplicatedStateMachine = "REPLICATED_STATE_MACHINE" // enable rsm + LorryHTTPPortName = "lorry-http-port" ) const ( KubernetesClusterDomainEnv = "KUBERNETES_CLUSTER_DOMAIN" DefaultDNSDomain = "cluster.local" ) - -const ( - ServiceDescriptorUsernameKey = "username" - ServiceDescriptorPasswordKey = "password" - ServiceDescriptorEndpointKey = "endpoint" - ServiceDescriptorPortKey = "port" -) - -const ( - BackupNameKeyForRestore = "name" - BackupNamespaceKeyForRestore = "namespace" - VolumeRestorePolicyKeyForRestore = "volumeRestorePolicy" - DoReadyRestoreAfterClusterRunning = "doReadyRestoreAfterClusterRunning" - RestoreTimeKeyForRestore = "restoreTime" - ConnectionPassword = "connectionPassword" -) - -const ( - KBAppMultiClusterPlacementKey = "apps.kubeblocks.io/multi-cluster-placement" -) - -const ( - KBGeneratedVirtualCompDefPrefix = "KB_GENERATED_VIRTUAL_COMP_DEF" -) diff --git a/constant/env.go b/constant/env.go index 3df7238..01d09aa 100644 --- a/constant/env.go +++ b/constant/env.go @@ -19,56 +19,10 @@ along with this program. If not, see . package constant -// Global -const ( - KBEnvNamespace = "KB_NAMESPACE" -) +import ( + "os" -// Cluster -const ( - KBEnvClusterName = "KB_CLUSTER_NAME" - KBEnvClusterUID = "KB_CLUSTER_UID" - KBEnvClusterCompName = "KB_CLUSTER_COMP_NAME" - KBEnvClusterUIDPostfix8Deprecated = "KB_CLUSTER_UID_POSTFIX_8" -) - -// Component -const ( - KBEnvCompName = "KB_COMP_NAME" - KBEnvCompReplicas = "KB_COMP_REPLICAS" - KBEnvCompServiceVersion = "KB_COMP_SERVICE_VERSION" -) - -// Pod -const ( - KBEnvPodName = "KB_POD_NAME" - KBEnvPodUID = "KB_POD_UID" - KBEnvPodIP = "KB_POD_IP" - KBEnvPodIPs = "KB_POD_IPS" - KBEnvPodFQDN = "KB_POD_FQDN" - KBEnvPodOrdinal = "KB_POD_ORDINAL" - KBEnvPodIPDeprecated = "KB_PODIP" - KBEnvPodIPsDeprecated = "KB_PODIPS" -) - -// Host -const ( - KBEnvHostIP = "KB_HOST_IP" - KBEnvNodeName = "KB_NODENAME" - KBEnvHostIPDeprecated = "KB_HOSTIP" -) - -// ServiceAccount -const ( - KBEnvServiceAccountName = "KB_SA_NAME" -) - -// TLS -const ( - KBEnvTLSCertPath = "KB_TLS_CERT_PATH" - KBEnvTLSCertFile = "KB_TLS_CERT_FILE" - KBEnvTLSCAFile = "KB_TLS_CA_FILE" - KBEnvTLSKeyFile = "KB_TLS_KEY_FILE" + "github.com/spf13/viper" ) // Lorry @@ -76,22 +30,15 @@ const ( KBEnvWorkloadType = "KB_WORKLOAD_TYPE" KBEnvBuiltinHandler = "KB_BUILTIN_HANDLER" KBEnvActionCommands = "KB_ACTION_COMMANDS" - KBEnvCronJobs = "KB_CRON_JOBS" KBEnvEngineType = "KB_ENGINE_TYPE" KBEnvServiceUser = "KB_SERVICE_USER" KBEnvServicePassword = "KB_SERVICE_PASSWORD" - KBEnvLorryHTTPPort = "LORRY_HTTP_PORT" - KBEnvLorryGRPCPort = "LORRY_GRPC_PORT" - KBEnvLorryLogLevel = "LORRY_LOG_LEVEL" // KBEnvServiceRoles defines the Roles configured in the cluster definition that are visible to users. KBEnvServiceRoles = "KB_SERVICE_ROLES" // KBEnvServicePort defines the port of the DB service KBEnvServicePort = "KB_SERVICE_PORT" - // KBEnvDataPath defines the data volume path of the DB service. - KBEnvDataPath = "KB_DATA_PATH" - // KBEnvTTL controls the lease expiration time in DCS. If the leader fails to renew its lease within the TTL duration, it will lose the leader role, allowing other replicas to take over. KBEnvTTL = "KB_TTL" @@ -105,6 +52,107 @@ const ( KBEnvRsmRoleUpdateMechanism = "KB_RSM_ROLE_UPDATE_MECHANISM" KBEnvRoleProbeTimeout = "KB_RSM_ROLE_PROBE_TIMEOUT" KBEnvRoleProbePeriod = "KB_RSM_ROLE_PROBE_PERIOD" +) + +// new envs for KB 1.0 +const ( + EnvNamespace = "MY_NAMESPACE" + EnvPodName = "MY_POD_NAME" + EnvPodIP = "MY_POD_IP" + EnvPodUID = "MY_POD_UID" + EnvClusterName = "MY_CLUSTER_NAME" + EnvComponentName = "MY_COMP_NAME" + EnvClusterCompName = "MY_CLUSTER_COMP_NAME" +) - KBEnvVolumeProtectionSpec = "KB_VOLUME_PROTECTION_SPEC" +// old envs for KB 0.9 +const ( + KBEnvNamespace = "KB_NAMESPACE" + KBEnvClusterName = "KB_CLUSTER_NAME" + KBEnvClusterCompName = "KB_CLUSTER_COMP_NAME" + KBEnvCompName = "KB_COMP_NAME" + KBEnvPodName = "KB_POD_NAME" + KBEnvPodUID = "KB_POD_UID" + KBEnvPodIP = "KB_POD_IP" + KBEnvPodFQDN = "KB_POD_FQDN" + KBEnvNodeName = "KB_NODENAME" ) + +func GetPodName() string { + switch { + case viper.IsSet(KBEnvPodName): + return viper.GetString(KBEnvPodName) + case viper.IsSet(EnvPodName): + return viper.GetString(EnvPodName) + default: + // this may be not correct in some cases, like in the case of host network, it will return the node hostname instead of pod name. + podName, _ := os.Hostname() + return podName + } +} + +func GetPodIP() string { + switch { + case viper.IsSet(KBEnvPodIP): + return viper.GetString(KBEnvPodIP) + case viper.IsSet(EnvPodIP): + return viper.GetString(EnvPodIP) + default: + return "" + } +} + +func GetPodUID() string { + switch { + case viper.IsSet(KBEnvPodUID): + return viper.GetString(KBEnvPodUID) + case viper.IsSet(EnvPodUID): + return viper.GetString(EnvPodUID) + default: + return "" + } +} + +func GetNamespace() string { + switch { + case viper.IsSet(KBEnvNamespace): + return viper.GetString(KBEnvNamespace) + case viper.IsSet(EnvNamespace): + return viper.GetString(EnvNamespace) + default: + return "" + } +} + +func GetClusterName() string { + switch { + case viper.IsSet(KBEnvClusterName): + return viper.GetString(KBEnvClusterName) + case viper.IsSet(EnvClusterName): + return viper.GetString(EnvClusterName) + default: + return "" + } +} + +func GetComponentName() string { + switch { + case viper.IsSet(KBEnvCompName): + return viper.GetString(KBEnvCompName) + case viper.IsSet(EnvComponentName): + return viper.GetString(EnvComponentName) + default: + return "" + } +} + +func GetClusterCompName() string { + switch { + case viper.IsSet(KBEnvClusterCompName): + return viper.GetString(KBEnvClusterCompName) + case viper.IsSet(EnvClusterCompName): + return viper.GetString(EnvClusterCompName) + default: + return "" + } +} diff --git a/constant/lorry.go b/constant/lorry.go index 165ad7a..22d33eb 100644 --- a/constant/lorry.go +++ b/constant/lorry.go @@ -20,25 +20,5 @@ along with this program. If not, see . package constant const ( - PostgreSQLCharacterType = "postgresql" - MySQLCharacterType = "mysql" - RedisCharacterType = "redis" - MongoDBCharacterType = "mongodb" - ETCDCharacterType = "etcd" - PolarDBXCharacterType = "polardbx" + RoleProbeAction = "roleProbe" ) - -// action keys -const ( - RoleProbeAction = "roleProbe" - HealthyCheckAction = "healthyCheck" - MemberJoinAction = "memberJoin" - MemberLeaveAction = "memberLeave" - ReadonlyAction = "readonly" - ReadWriteAction = "readwrite" - PostProvisionAction = "postProvision" - PreTerminateAction = "preTerminate" -) - -// action envs -const () diff --git a/dcs/k8s.go b/dcs/k8s.go index 8feaed2..cfbb4ae 100644 --- a/dcs/k8s.go +++ b/dcs/k8s.go @@ -23,7 +23,6 @@ import ( "context" "encoding/json" "fmt" - "os" "strconv" "time" @@ -76,33 +75,29 @@ func NewKubernetesStore() (*KubernetesStore, error) { return nil, err } - clusterName := os.Getenv(constant.KBEnvClusterName) + 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 := os.Getenv(constant.KBEnvCompName) + 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 := os.Getenv(constant.KBEnvClusterCompName) + clusterCompName := constant.GetClusterCompName() if clusterCompName == "" { clusterCompName = clusterName + "-" + componentName } - currentMemberName := os.Getenv(constant.KBEnvPodName) + currentMemberName := constant.GetPodName() if currentMemberName == "" { - var err error - currentMemberName, err = os.Hostname() - if err != nil { - return nil, errors.Wrap(err, "get hostname failed") - } + return nil, errors.New("get hostname failed") } - namespace := os.Getenv(constant.KBEnvNamespace) + 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 b045ae8..8eb3ade 100644 --- a/engines/base.go +++ b/engines/base.go @@ -21,12 +21,10 @@ package engines import ( "context" - "os" "strings" "github.com/go-logr/logr" "github.com/pkg/errors" - "github.com/spf13/viper" "github.com/apecloud/dbctl/constant" "github.com/apecloud/dbctl/dcs" @@ -46,20 +44,16 @@ type DBManagerBase struct { } func NewDBManagerBase(logger logr.Logger) (*DBManagerBase, error) { - currentMemberName := viper.GetString(constant.KBEnvPodName) + currentMemberName := constant.GetPodName() if currentMemberName == "" { - var err error - currentMemberName, err = os.Hostname() - if err != nil { - return nil, errors.Wrap(err, "get hostname failed") - } + return nil, errors.New("pod name is not set") } mgr := DBManagerBase{ CurrentMemberName: currentMemberName, - CurrentMemberIP: viper.GetString(constant.KBEnvPodIP), - ClusterCompName: viper.GetString(constant.KBEnvClusterCompName), - Namespace: viper.GetString(constant.KBEnvNamespace), + CurrentMemberIP: constant.GetPodIP(), + 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 98e8af3..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" @@ -75,10 +74,10 @@ func SentEventForProbe(ctx context.Context, data map[string]any) error { func CreateEvent(reason string, data map[string]any) (*corev1.Event, error) { // get pod object - podName := viper.GetString(constant.KBEnvPodName) - podUID := viper.GetString(constant.KBEnvPodUID) + 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) - } -}