From 1081feeff3b8b6ca7796c58334d65bbcd028edce Mon Sep 17 00:00:00 2001 From: Sooyoung Kim Date: Thu, 25 Jul 2024 17:06:32 +0900 Subject: [PATCH 01/11] Deduplicate code for checking k8scluster enablement --- src/core/mcis/k8scluster.go | 184 ++++++++++++++++-------------------- 1 file changed, 80 insertions(+), 104 deletions(-) diff --git a/src/core/mcis/k8scluster.go b/src/core/mcis/k8scluster.go index df3ee5033..e7470a43f 100644 --- a/src/core/mcis/k8scluster.go +++ b/src/core/mcis/k8scluster.go @@ -484,38 +484,8 @@ func CreateK8sCluster(nsId string, u *TbK8sClusterReq, option string) (TbK8sClus /* * Check for K8sCluster Enablement from K8sClusterSetting */ - - connConfig, err := common.GetConnConfig(u.ConnectionName) + err = checkK8sClusterEnablement(u.ConnectionName) if err != nil { - err := fmt.Errorf("Failed to get the connConfig " + u.ConnectionName + ": " + err.Error()) - log.Err(err).Msg("Failed to Create a K8sCluster") - return emptyObj, err - } - - cloudType := connConfig.ProviderName - - // Convert cloud type to field name (e.g., AWS to Aws, OPENSTACK to Openstack) - lowercase := strings.ToLower(cloudType) - fnCloudType := strings.ToUpper(string(lowercase[0])) + lowercase[1:] - - // Get cloud setting with field name - cloudSetting := common.CloudSetting{} - - getCloudSetting := func() { - defer func() { - if err := recover(); err != nil { - log.Error().Msgf("%v", err) - cloudSetting = reflect.ValueOf(&common.RuntimeConf.Cloud).Elem().FieldByName("Common").Interface().(common.CloudSetting) - } - }() - - cloudSetting = reflect.ValueOf(&common.RuntimeConf.Cloud).Elem().FieldByName(fnCloudType).Interface().(common.CloudSetting) - } - - getCloudSetting() - - if cloudSetting.K8sCluster.Enable != "y" { - err := fmt.Errorf("The K8sCluster Management function is not enabled for Cloud(" + fnCloudType + ")") log.Err(err).Msg("Failed to Create a K8sCluster") return emptyObj, err } @@ -526,8 +496,8 @@ func CreateK8sCluster(nsId string, u *TbK8sClusterReq, option string) (TbK8sClus spName := fmt.Sprintf("%s-%s", nsId, u.Id) - // Validate K8sCluster Version - err = validateK8sClusterVersion(connConfig.ProviderName, connConfig.RegionDetail.RegionName, u.Version) + // Validate + err = validateAtCreateK8sCluster(u) if err != nil { log.Err(err).Msgf("Failed to Create a K8sCluster: Requested K8sVersion(%s)", u.Version) return emptyObj, err @@ -795,37 +765,8 @@ func AddK8sNodeGroup(nsId string, k8sClusterId string, u *TbK8sNodeGroupReq) (Tb * Check for K8sCluster Enablement from ClusterSetting */ - connConfig, err := common.GetConnConfig(oldTbK8sCInfo.ConnectionName) + err = checkK8sClusterEnablement(oldTbK8sCInfo.ConnectionName) if err != nil { - err := fmt.Errorf("failed to get the connConfig " + oldTbK8sCInfo.ConnectionName + ": " + err.Error()) - log.Err(err).Msg("Failed to Add K8sNodeGroup") - return emptyObj, err - } - - cloudType := connConfig.ProviderName - - // Convert cloud type to field name (e.g., AWS to Aws, OPENSTACK to Openstack) - lowercase := strings.ToLower(cloudType) - fnCloudType := strings.ToUpper(string(lowercase[0])) + lowercase[1:] - - // Get cloud setting with field name - cloudSetting := common.CloudSetting{} - - getCloudSetting := func() { - defer func() { - if err := recover(); err != nil { - log.Error().Msgf("%v", err) - cloudSetting = reflect.ValueOf(&common.RuntimeConf.Cloud).Elem().FieldByName("Common").Interface().(common.CloudSetting) - } - }() - - cloudSetting = reflect.ValueOf(&common.RuntimeConf.Cloud).Elem().FieldByName(fnCloudType).Interface().(common.CloudSetting) - } - - getCloudSetting() - - if cloudSetting.K8sCluster.Enable != "y" { - err := fmt.Errorf("The K8sCluster Management function is not enabled for Cloud(" + fnCloudType + ")") log.Err(err).Msg("Failed to Add K8sNodeGroup") return emptyObj, err } @@ -1628,7 +1569,7 @@ func UpgradeK8sCluster(nsId string, k8sClusterId string, u *TbUpgradeK8sClusterR err := validate.Struct(u) if err != nil { if _, ok := err.(*validator.InvalidValidationError); ok { - log.Err(err).Msg("Failed to Upgrade K8sCluster") + log.Err(err).Msg("Failed to Upgrade a K8sCluster") return emptyObj, err } @@ -1637,13 +1578,13 @@ func UpgradeK8sCluster(nsId string, k8sClusterId string, u *TbUpgradeK8sClusterR check, err := CheckK8sCluster(nsId, k8sClusterId) if err != nil { - log.Err(err).Msg("Failed to Upgrade K8sCluster") + log.Err(err).Msg("Failed to Upgrade a K8sCluster") return emptyObj, err } if !check { err := fmt.Errorf("The K8sCluster " + k8sClusterId + " does not exist.") - log.Err(err).Msg("Failed to Upgrade K8sCluster") + log.Err(err).Msg("Failed to Upgrade a K8sCluster") return emptyObj, err } @@ -1655,7 +1596,7 @@ func UpgradeK8sCluster(nsId string, k8sClusterId string, u *TbUpgradeK8sClusterR kv, err := common.CBStore.Get(k) if err != nil { err = fmt.Errorf("In UpgradeK8sCluster(); CBStore.Get() returned an error: " + err.Error()) - log.Err(err).Msg("Failed to Upgrade K8sCluster") + log.Err(err).Msg("Failed to Upgrade a K8sCluster") return emptyObj, err } @@ -1663,7 +1604,7 @@ func UpgradeK8sCluster(nsId string, k8sClusterId string, u *TbUpgradeK8sClusterR err = json.Unmarshal([]byte(kv.Value), &oldTbK8sCInfo) if err != nil { - log.Err(err).Msg("Failed to Upgrade K8sCluster") + log.Err(err).Msg("Failed to Upgrade a K8sCluster") return emptyObj, err } @@ -1671,38 +1612,9 @@ func UpgradeK8sCluster(nsId string, k8sClusterId string, u *TbUpgradeK8sClusterR * Check for K8sCluster Enablement from K8sClusterSetting */ - connConfig, err := common.GetConnConfig(oldTbK8sCInfo.ConnectionName) + err = checkK8sClusterEnablement(oldTbK8sCInfo.ConnectionName) if err != nil { - err := fmt.Errorf("failed to get the connConfig " + oldTbK8sCInfo.ConnectionName + ": " + err.Error()) - log.Err(err).Msg("Failed to Upgrade K8sCluster") - return emptyObj, err - } - - cloudType := connConfig.ProviderName - - // Convert cloud type to field name (e.g., AWS to Aws, OPENSTACK to Openstack) - lowercase := strings.ToLower(cloudType) - fnCloudType := strings.ToUpper(string(lowercase[0])) + lowercase[1:] - - // Get cloud setting with field name - cloudSetting := common.CloudSetting{} - - getCloudSetting := func() { - defer func() { - if err := recover(); err != nil { - log.Error().Msgf("%v", err) - cloudSetting = reflect.ValueOf(&common.RuntimeConf.Cloud).Elem().FieldByName("Common").Interface().(common.CloudSetting) - } - }() - - cloudSetting = reflect.ValueOf(&common.RuntimeConf.Cloud).Elem().FieldByName(fnCloudType).Interface().(common.CloudSetting) - } - - getCloudSetting() - - if cloudSetting.K8sCluster.Enable != "y" { - err := fmt.Errorf("The K8sCluster Management function is not enabled for Cloud(" + fnCloudType + ")") - log.Err(err).Msg("Failed to Upgrade K8sCluster") + log.Err(err).Msg("Failed to Upgrade a K8sCluster") return emptyObj, err } @@ -1710,10 +1622,10 @@ func UpgradeK8sCluster(nsId string, k8sClusterId string, u *TbUpgradeK8sClusterR * Build RequestBody for SpiderUpgradeClusterReq{} */ - // Validate K8sCluster Version - err = validateK8sClusterVersion(connConfig.ProviderName, connConfig.RegionDetail.RegionName, u.Version) + // Validate + err = validateAtUpgradeK8sCluster(oldTbK8sCInfo.ConnectionName, u) if err != nil { - log.Err(err).Msg("Failed to Upgrade K8sCluster") + log.Err(err).Msg("Failed to Upgrade a K8sCluster") return emptyObj, err } spVersion := u.Version @@ -1743,7 +1655,7 @@ func UpgradeK8sCluster(nsId string, k8sClusterId string, u *TbUpgradeK8sClusterR ) if err != nil { - log.Err(err).Msg("Failed to Upgrade K8sCluster") + log.Err(err).Msg("Failed to Upgrade a K8sCluster") return emptyObj, err } @@ -1761,7 +1673,7 @@ func UpgradeK8sCluster(nsId string, k8sClusterId string, u *TbUpgradeK8sClusterR err = common.CBStore.Put(k, string(Val)) if err != nil { - log.Err(err).Msg("Failed to Upgrade K8sCluster") + log.Err(err).Msg("Failed to Upgrade a K8sCluster") return emptyObj, err } @@ -1932,6 +1844,70 @@ func convertSpiderNodeGroupStatusToTbK8sNodeGroupStatus(spNodeGroupStatus Spider return TbK8sNodeGroupInactive } +// checkK8sClusterEnablement returns the enablement status(nil or error) for K8sCluster related to Connection. +func checkK8sClusterEnablement(connectionName string) error { + connConfig, err := common.GetConnConfig(connectionName) + if err != nil { + err := fmt.Errorf("failed to get the connConfig " + connectionName + ": " + err.Error()) + return err + } + + cloudType := connConfig.ProviderName + + // Convert cloud type to field name (e.g., AWS to Aws, OPENSTACK to Openstack) + lowercase := strings.ToLower(cloudType) + fnCloudType := strings.ToUpper(string(lowercase[0])) + lowercase[1:] + + // Get cloud setting with field name + cloudSetting := common.CloudSetting{} + + getCloudSetting := func() { + defer func() { + if err := recover(); err != nil { + log.Error().Msgf("%v", err) + cloudSetting = reflect.ValueOf(&common.RuntimeConf.Cloud).Elem().FieldByName("Common").Interface().(common.CloudSetting) + } + }() + + cloudSetting = reflect.ValueOf(&common.RuntimeConf.Cloud).Elem().FieldByName(fnCloudType).Interface().(common.CloudSetting) + } + + getCloudSetting() + + if cloudSetting.K8sCluster.Enable != "y" { + err := fmt.Errorf("k8scluster management function is not enabled for cloud(" + fnCloudType + ")") + return err + } + + return nil +} + +func validateAtCreateK8sCluster(tbK8sClusterReq *TbK8sClusterReq) error { + connConfig, err := common.GetConnConfig(tbK8sClusterReq.ConnectionName) + + // Validate K8sCluster Version + err = validateK8sClusterVersion(connConfig.ProviderName, connConfig.RegionDetail.RegionName, tbK8sClusterReq.Version) + if err != nil { + log.Err(err).Msgf("Failed to Create a K8sCluster: Requested K8sVersion(%s)", tbK8sClusterReq.Version) + return err + } + + return nil +} + +func validateAtUpgradeK8sCluster(connectionName string, tbUpgradeK8sClusterReq *TbUpgradeK8sClusterReq) error { + connConfig, err := common.GetConnConfig(connectionName) + + // Validate K8sCluster Version + err = validateK8sClusterVersion(connConfig.ProviderName, connConfig.RegionDetail.RegionName, tbUpgradeK8sClusterReq.Version) + if err != nil { + log.Err(err).Msgf("Failed to Create a K8sCluster: Requested K8sVersion(%s)", tbUpgradeK8sClusterReq.Version) + return err + } + + return nil +} + func validateK8sClusterVersion(providerName, regionName, version string) error { availableVersion, err := common.GetAvailableK8sClusterVersion(providerName, regionName) if err != nil { From cde07008a6658f5455b7d03693a022d6cc39b712 Mon Sep 17 00:00:00 2001 From: Sooyoung Kim Date: Thu, 25 Jul 2024 16:04:19 +0900 Subject: [PATCH 02/11] Update k8sclusterinfo.yaml and change a keyword 'all' to 'common' --- assets/k8sclusterinfo.yaml | 67 ++++++++++++++++++++++++++++++++++---- src/core/common/common.go | 2 ++ src/core/common/utility.go | 58 ++++++++++++++++++++++++++++----- 3 files changed, 113 insertions(+), 14 deletions(-) diff --git a/assets/k8sclusterinfo.yaml b/assets/k8sclusterinfo.yaml index b161cc390..932ed0ad7 100644 --- a/assets/k8sclusterinfo.yaml +++ b/assets/k8sclusterinfo.yaml @@ -6,13 +6,66 @@ # : Name of the CSP # nodegroupsWithCluster: # version: -# - region: [region1, region2, all(special keyword)] +# - region: [region1, region2, common(special keyword: most of regions)] +# k8scluster: + azure: + nodegroupsWithCluster: true + nodeImageDesignation: false + version: + - region: [westeurope,westus] + available: + - name: 1.29 + id: 1.29.4 + - name: 1.28 + id: 1.28.9 + - name: 1.27 + id: 1.27.13 + - region: [common] + available: + - name: 1.29 + id: 1.29.5 + - name: 1.28 + id: 1.28.10 + - name: 1.27 + id: 1.27.14 + rootDisk: + - region: [common] + type: + - name: default + id: default + size: + min: 10 + max: 40 + gcp: + nodegroupsWithCluster: true + nodeImageDesignation: true + version: + - region: [common] + available: + - name: 1.29 + id: 1.29.6-gke.1038001 + - name: 1.28 + id: 1.28.11-gke.1019001 + - name: 1.26 + id: 1.27.14-gke.1059002 + rootDisk: + - region: [common] + type: + - name: default + id: default + size: + min: 10 + max: 40 alibaba: nodegroupsWithCluster: false + nodeImageDesignation: true version: - - region: [ap-northeast-1,ap-northeast-2,ap-southeast-1,ap-southeast-3,ap-southeast-5,us-west-1,us-east-1,eu-central-1,eu-west-1,cn-beijing,cn-hongkong,cn-shanghai,cn-huhehaote,cn-heyuan,cn-wulanchabu,cn-guangzhou] + # ServiceUnavailable or NotSupportedSLB + - region: [me-east-1, cn-zhangjiakou, cn-hangzhou, cn-shenzhen, cn-chengdu, ap-south-1, ap-sourtheast-2] + # ap-northeast-1,ap-northeast-2,ap-southeast-1,ap-southeast-3,ap-southeast-5,us-west-1,us-east-1,eu-central-1,eu-west-1,cn-beijing,cn-hongkong,cn-shanghai,cn-huhehaote,cn-heyuan,cn-wulanchabu,cn-guangzhou + - region: [common] available: - name: 1.30 id: 1.30.1-aliyun.1 @@ -21,7 +74,7 @@ k8scluster: - name: 1.26 id: 1.26.15-aliyun.1 rootDisk: - - region: [all] + - region: [common] type: - name: cloud_essd id: cloud_essd @@ -30,6 +83,7 @@ k8scluster: max: 40 nhncloud: nodegroupsWithCluster: true + nodeImageDesignation: true version: - region: [kr1, kr2] available: @@ -42,7 +96,7 @@ k8scluster: - name: 1.26 id: v1.26.3 rootDisk: - - region: [all] + - region: [common] type: - name: default id: default @@ -51,8 +105,9 @@ k8scluster: max: 40 tencent: nodegroupsWithCluster: false + nodeImageDesignation: true version: - - region: [all] + - region: [common] available: - name: 1.28 id: 1.28.3 @@ -61,7 +116,7 @@ k8scluster: - name: 1.24 id: 1.24.4 rootDisk: - - region: [all] + - region: [common] type: - name: default id: default diff --git a/src/core/common/common.go b/src/core/common/common.go index b2c630e66..5a0f1e498 100644 --- a/src/core/common/common.go +++ b/src/core/common/common.go @@ -93,6 +93,8 @@ const ( StrK8s string = "k8s" StrKubernetes string = "kubernetes" StrContainer string = "container" + StrCommon string = "common" + StrEmpty string = "empty" StrDefaultResourceName string = "-systemdefault-" // StrFirewallRule string = "firewallRule" diff --git a/src/core/common/utility.go b/src/core/common/utility.go index 966783151..4736a0f49 100644 --- a/src/core/common/utility.go +++ b/src/core/common/utility.go @@ -1305,10 +1305,11 @@ func getK8sClusterDetail(providerName string) *K8sClusterDetail { // GetAvailableK8sClusterVersion is func to get available kubernetes cluster versions for provider and region from K8sClusterInfo func GetAvailableK8sClusterVersion(providerName string, regionName string) (*[]K8sClusterVersionDetailAvailable, error) { // - // Check available K8sCluster version and node image in k8sclusterinfo.yaml + // Check available K8sCluster version in k8sclusterinfo.yaml // providerName = strings.ToLower(providerName) + regionName = strings.ToLower(regionName) // Get K8sClusterDetail for providerName k8sClusterDetail := getK8sClusterDetail(providerName) @@ -1316,19 +1317,38 @@ func GetAvailableK8sClusterVersion(providerName string, regionName string) (*[]K return nil, fmt.Errorf("unsupported provider(%s) for kubernetes cluster", providerName) } - // Get Available Versions for regionName + // Check if 'regionName' exists var availableVersion *[]K8sClusterVersionDetailAvailable = nil for _, versionDetail := range k8sClusterDetail.Version { for _, region := range versionDetail.Region { region = strings.ToLower(region) - if region == "all" || region == regionName { - availableVersion = &versionDetail.Available + if strings.EqualFold(region, regionName) { + if len(versionDetail.Available) == 0 { + availableVersion = &[]K8sClusterVersionDetailAvailable{{StrEmpty, StrEmpty}} + } else { + availableVersion = &versionDetail.Available + } + return availableVersion, nil + } + } + } + + // Check if 'common' exists + for _, versionDetail := range k8sClusterDetail.Version { + for _, region := range versionDetail.Region { + region = strings.ToLower(region) + if strings.EqualFold(region, StrCommon) { + if len(versionDetail.Available) == 0 { + availableVersion = &[]K8sClusterVersionDetailAvailable{{StrEmpty, StrEmpty}} + } else { + availableVersion = &versionDetail.Available + } return availableVersion, nil } } } - return nil, fmt.Errorf("no available kubernetes cluster version for provider(%s):region(%s)", providerName, regionName) + return nil, fmt.Errorf("no entry for provider(%s):region(%s)", providerName, regionName) } // GetAvailableK8sClusterNodeImage is func to get available kubernetes cluster node images for provider and region from K8sClusterInfo @@ -1338,6 +1358,7 @@ func GetAvailableK8sClusterNodeImage(providerName string, regionName string) (*[ // providerName = strings.ToLower(providerName) + regionName = strings.ToLower(regionName) // Get K8sClusterDetail for providerName k8sClusterDetail := getK8sClusterDetail(providerName) @@ -1345,13 +1366,34 @@ func GetAvailableK8sClusterNodeImage(providerName string, regionName string) (*[ return nil, fmt.Errorf("unsupported provider(%s) for kubernetes cluster", providerName) } - // Get Available Node Image for regionName + // Check if 'regionName' exists var availableNodeImage *[]K8sClusterNodeImageDetailAvailable = nil for _, nodeImageDetail := range k8sClusterDetail.NodeImage { for _, region := range nodeImageDetail.Region { region = strings.ToLower(region) - if region == "all" || region == regionName { - availableNodeImage = &nodeImageDetail.Available + if strings.EqualFold(region, regionName) { + if len(nodeImageDetail.Available) == 0 { + availableNodeImage = &[]K8sClusterNodeImageDetailAvailable{{StrEmpty, StrEmpty}} + break + } else { + availableNodeImage = &nodeImageDetail.Available + } + return availableNodeImage, nil + } + } + } + + // Check if 'common' exists + for _, nodeImageDetail := range k8sClusterDetail.NodeImage { + for _, region := range nodeImageDetail.Region { + region = strings.ToLower(region) + if strings.EqualFold(region, StrCommon) { + if len(nodeImageDetail.Available) == 0 { + availableNodeImage = &[]K8sClusterNodeImageDetailAvailable{{StrEmpty, StrEmpty}} + break + } else { + availableNodeImage = &nodeImageDetail.Available + } return availableNodeImage, nil } } From 1c20f63443a2da45f6b18b7a2b668a6a435e5b0f Mon Sep 17 00:00:00 2001 From: Yunkon Kim Date: Thu, 25 Jul 2024 22:42:11 +0900 Subject: [PATCH 03/11] Add `etcd` auth * Add `setup-auth.sh` and `.env`, which is used by Docker Compose * Set up or apply auth on etcd server through `etcd-setup-auth` (an ephemeral container) * Improve configuration for connecting to `etcd` and `kvstore` - Add environment variables of `etcd` - Read envs and set to the configs (including auth info) - Retry several times until you connect to the `etcd` server * Update examples for `etcd` test --- conf/setup.env | 6 + docker-compose.yaml | 27 +++- scripts/etcd/.env | 4 + scripts/etcd/sample-disable-auth.sh | 196 ++++++++++++++++++++++++++ scripts/etcd/sample-enable-auth.sh | 211 ++++++++++++++++++++++++++++ scripts/etcd/setup-auth.sh | 147 +++++++++++++++++++ src/core/common/common.go | 2 + src/core/common/config.go | 3 + src/examples/kvstore/main.go | 28 ++-- src/kvstore/etcd/etcd.go | 4 + src/main.go | 51 +++++++ 11 files changed, 669 insertions(+), 10 deletions(-) create mode 100644 scripts/etcd/.env create mode 100755 scripts/etcd/sample-disable-auth.sh create mode 100755 scripts/etcd/sample-enable-auth.sh create mode 100755 scripts/etcd/setup-auth.sh diff --git a/conf/setup.env b/conf/setup.env index d80519e2c..6c3ac3fd4 100644 --- a/conf/setup.env +++ b/conf/setup.env @@ -36,6 +36,12 @@ export DB_DATABASE=cb_tumblebug export DB_USER=cb_tumblebug export DB_PASSWORD=cb_tumblebug +## Set etcd cluster +export ETCD_CLUSTER_ENDPOINTS=localhost:2379 +export ETCD_AUTH_ENABLED=true +export ETCD_USERNAME=default +export ETCD_PASSWORD=default + ## Set period for auto control goroutine invocation export AUTOCONTROL_DURATION_MS=10000 diff --git a/docker-compose.yaml b/docker-compose.yaml index 6866806b9..c8a16a587 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -3,7 +3,7 @@ services: # etcd etcd: image: gcr.io/etcd-development/etcd:v3.5.14 - container_name: etcd-gcr-v3.5.14 + container_name: etcd ports: - "2379:2379" - "2380:2380" @@ -35,12 +35,33 @@ services: - zap - --log-outputs - stderr - healthcheck: + - --auth-token + - simple + healthcheck: # for etcd test: [ "CMD", "/usr/local/bin/etcd", "--version"] interval: 1m timeout: 5s retries: 3 start_period: 10s + + # etcd-setup-auth + etcd-setup-auth: + image: alpine:latest + container_name: etcd-setup-auth + depends_on: + - etcd + volumes: + - ./scripts/etcd/:/scripts/etcd/ + env_file: + - ./scripts/etcd/.env + command: sh -c "sh /scripts/etcd/setup-auth.sh" + healthcheck: # for etcd-setup-auth + test: ["CMD", "test", "-f", "/tmp/healthcheck"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 10s + # CB-Spider cb-spider: image: cloudbaristaorg/cb-spider:0.9.0 @@ -70,6 +91,7 @@ services: timeout: 5s retries: 3 start_period: 10s + # CB-Tumblebug cb-tumblebug: image: cloudbaristaorg/cb-tumblebug:0.9.0 @@ -84,6 +106,7 @@ services: protocol: tcp depends_on: - cb-spider + - etcd-setup-auth volumes: - ./conf/:/app/conf/ - ./container-volume/cb-tumblebug-container/meta_db/:/app/meta_db/ diff --git a/scripts/etcd/.env b/scripts/etcd/.env new file mode 100644 index 000000000..38dc90387 --- /dev/null +++ b/scripts/etcd/.env @@ -0,0 +1,4 @@ +ETCD_AUTH_ENABLED=true +ETCD_ROOT_PASSWORD=default +ETCD_ADMIN_USERNAME=default +ETCD_ADMIN_PASSWORD=default \ No newline at end of file diff --git a/scripts/etcd/sample-disable-auth.sh b/scripts/etcd/sample-disable-auth.sh new file mode 100755 index 000000000..777f7167d --- /dev/null +++ b/scripts/etcd/sample-disable-auth.sh @@ -0,0 +1,196 @@ +#!/bin/bash + +ETCD_CONTAINER_NAME="etcd" +ETCD_CTL="docker exec ${ETCD_CONTAINER_NAME} etcdctl" + +ROOT_USERNAME="root" +ROOT_PASSWORD="default" +ADMIN_USERNAME="default" +ADMIN_PASSWORD="default" + +# Check if the etcd container is running +check_etcd() { + if ! docker ps | grep -q ${ETCD_CONTAINER_NAME}; then + echo "The etcd container is not running." + exit 1 + fi + + while ! docker exec ${ETCD_CONTAINER_NAME} etcdctl endpoint health --cluster; do + echo "The etcd service is not ready yet. Waiting..." + sleep 5 + done + echo "The etcd service is running normally." +} + +get_root_username_and_password() { + # Get root username from the user + while true; do + echo -e "Enter root username (press Enter for default username 'root'): \c" + read -sp "" USERNAME + echo + + if [ -z "$USERNAME" ]; then + ROOT_USERNAME="root" + echo "Using default username 'default'." + break + elif [[ "$USERNAME" =~ ^[a-zA-Z0-9_]+$ ]]; then + ROOT_USERNAME=$USERNAME + break + else + echo "Invalid root username. Use only letters, numbers, and underscores. Please try again." + fi + done + + # Get root password from the user + while true; do + echo -e "Enter root password (press Enter for default password 'default'): \c" + read -sp "" PASSWORD + echo + + if [ -z "$PASSWORD" ]; then + ROOT_PASSWORD="default" + echo "Using default password." + break + fi + + if [ -n "$PASSWORD" ]; then + read -sp "Confirm the password: " PASSWORD_CONFIRM + echo + + if [ "$PASSWORD" = "$PASSWORD_CONFIRM" ]; then + ROOT_PASSWORD=$PASSWORD + break + fi + fi + echo "Passwords do not match. Please try again." + done +} + +get_admin_username_and_password() { + # Get admin username from the user + while true; do + echo -e "Enter admin username (press Enter for default username 'default'): \c" + read -sp "" USERNAME + echo + + if [ -z "$USERNAME" ]; then + ADMIN_USERNAME="default" + echo "Using default username 'default'." + break + elif [[ "$USERNAME" =~ ^[a-zA-Z0-9_]+$ ]]; then + ADMIN_USERNAME=$USERNAME + break + else + echo "Invalid admin username. Use only letters, numbers, and underscores. Please try again." + fi + done + + # Get admin password from the user + while true; do + echo -e "Enter admin password (press Enter for default password 'default'): \c" + read -sp "" PASSWORD + echo + + if [ -z "$PASSWORD" ]; then + ADMIN_PASSWORD="default" + echo "Using default password." + break + fi + + if [ -n "$PASSWORD" ]; then + read -sp "Confirm the password: " PASSWORD_CONFIRM + echo + + if [ "$PASSWORD" = "$PASSWORD_CONFIRM" ]; then + ADMIN_PASSWORD=$PASSWORD + break + fi + fi + echo "Passwords do not match. Please try again." + done +} + +# Check if authentication is already disabled +check_auth_disabled() { + local RET_CHECK_AUTH + RET_CHECK_AUTH=$($ETCD_CTL --user ${ROOT_USERNAME}:${ROOT_PASSWORD} auth status) + + if echo "$RET_CHECK_AUTH" | grep -q "Authentication Status: false"; then + echo "Authentication is already disabled. Exit the script" + exit 1 + fi +} + +# Disable authentication +disable_auth() { + local RET_AUTH_DISABLE + + RET_AUTH_DISABLE=$($ETCD_CTL --user ${ROOT_USERNAME}:${ROOT_PASSWORD} auth disable) + if ! echo "$RET_AUTH_DISABLE" | grep -q "Authentication Disabled"; then + echo "Failed to disable authentication." + exit 1 + fi + + echo "Successfully disabled the authentication" +} + +# Remove the root and admin users and their roles +remove_users_and_roles() { + local ROOT_USERNAME="root" + local ROOT_ROLE="root" + local ADMIN_ROLE="admin" + local RET_REVOKE_ROLE + local RET_DELETE_USER + local RET_DELETE_ROLE + + # Revoke 'admin' role from 'admin' user + RET_REVOKE_ROLE=$($ETCD_CTL user revoke-role ${ADMIN_USERNAME} ${ADMIN_ROLE}) + if ! echo "$RET_REVOKE_ROLE" | grep -q "Role ${ADMIN_ROLE} is revoked from user ${ADMIN_USERNAME}"; then + echo $RET_REVOKE_ROLE + fi + + # Delete 'admin' user + RET_DELETE_USER=$($ETCD_CTL user delete ${ADMIN_USERNAME}) + if ! echo "$RET_DELETE_USER" | grep -q "User ${ADMIN_USERNAME} deleted"; then + echo $RET_DELETE_USER + fi + + # Delete 'admin' role + RET_DELETE_ROLE=$($ETCD_CTL role delete ${ADMIN_ROLE}) + if ! echo "$RET_DELETE_ROLE" | grep -q "Role ${ADMIN_ROLE} deleted"; then + echo $RET_DELETE_ROLE + fi + + # Revoke 'root' role from 'root' user + RET_REVOKE_ROLE=$($ETCD_CTL user revoke-role ${ROOT_USERNAME} ${ROOT_ROLE}) + if ! echo "$RET_REVOKE_ROLE" | grep -q "Role ${ROOT_ROLE} is revoked from user ${ROOT_USERNAME}"; then + echo $RET_REVOKE_ROLE + fi + + # Delete 'root' user + RET_DELETE_USER=$($ETCD_CTL user delete ${ROOT_USERNAME}) + if ! echo "$RET_DELETE_USER" | grep -q "User ${ROOT_USERNAME} deleted"; then + echo $RET_DELETE_USER + fi + + # Delete 'root' role + RET_DELETE_ROLE=$($ETCD_CTL role delete ${ROOT_ROLE}) + if ! echo "$RET_DELETE_ROLE" | grep -q "Role ${ROOT_ROLE} deleted"; then + echo $RET_DELETE_ROLE + fi + + echo "Successfully removed the root and admin users and their roles" +} + +# Main execution flow +main() { + check_etcd + check_auth_disabled + get_root_username_and_password + get_admin_username_and_password + disable_auth + remove_users_and_roles + echo "Successfully disabled authentication and removed users/roles" +} + +main \ No newline at end of file diff --git a/scripts/etcd/sample-enable-auth.sh b/scripts/etcd/sample-enable-auth.sh new file mode 100755 index 000000000..37f95627c --- /dev/null +++ b/scripts/etcd/sample-enable-auth.sh @@ -0,0 +1,211 @@ +#!/bin/bash + +ETCD_CONTAINER_NAME="etcd" +ETCD_CTL="docker exec ${ETCD_CONTAINER_NAME} etcdctl" + +ROOT_USERNAME="root" +ROOT_PASSWORD="default" +ADMIN_USERNAME="default" +ADMIN_PASSWORD="default" + +# Check if the etcd container is running +check_etcd() { + if ! docker ps | grep -q ${ETCD_CONTAINER_NAME}; then + echo "The etcd container is not running." + exit 1 + fi + + while ! docker exec ${ETCD_CONTAINER_NAME} etcdctl endpoint health --cluster; do + echo "The etcd service is not ready yet. Waiting..." + sleep 5 + done + echo "The etcd service is running normally." +} + +# Check if authentication is already enabled +check_auth_enabled() { + local RET_CHECK_AUTH + RET_CHECK_AUTH=$($ETCD_CTL auth status) + + if echo "$RET_CHECK_AUTH" | grep -q "Authentication Status: true"; then + echo "Authentication is already enabled. Exit the script" + exit 1 + fi +} + +# Set root password and enable authentication +set_root_user() { + + local RET_ADD_ROLE + local RET_ADD_USER + local RET_GRANT_ROLE + local ROOT_ROLE="root" + local ROOT_USERNAME="root" # Require 'root' to enable authentication + + # Get root password from the user + while true; do + echo -e "Enter root user password (press Enter for default password 'default'): \c" + read -sp "" PASSWORD + echo + + if [ -z "$PASSWORD" ]; then + echo "Using default password." + ROOT_PASSWORD="default" + break + fi + + if [ -n "$PASSWORD" ]; then + read -sp "Confirm the password: " PASSWORD_CONFIRM + echo + + if [ "$PASSWORD" = "$PASSWORD_CONFIRM" ]; then + ROOT_PASSWORD=$PASSWORD + break + fi + fi + echo "Passwords do not match. Please try again." + done + + # Add 'root' role + RET_ADD_ROLE=$($ETCD_CTL role add ${ROOT_ROLE}) + if ! echo "$RET_ADD_ROLE" | grep -q "Role ${ROOT_ROLE} created"; then + echo $RET_ADD_ROLE + echo "Exit the script" + exit 1 + fi + echo "Successfully added role (root)" + + # Add user 'root' with the password + RET_ADD_USER=$($ETCD_CTL user add ${ROOT_USERNAME}:${ROOT_PASSWORD}) + if ! echo "$RET_ADD_USER" | grep -q "User ${ROOT_USERNAME} created"; then + echo $RET_ADD_USER + echo "Exit the script" + exit 1 + fi + + echo "Successfully added, the user (root) with the password" + + # Grant 'root' role to 'root' user + RET_GRANT_ROLE=$($ETCD_CTL user grant-role ${ROOT_USERNAME} ${ROOT_ROLE}) + if ! echo "$RET_GRANT_ROLE" | grep -q "Role ${ROOT_ROLE} is granted to user ${ROOT_USERNAME}"; then + echo $RET_GRANT_ROLE + echo "Exit the script" + exit 1 + fi + + echo "Successfully granted, the role to user" +} + +# Add additional admin user +add_additional_admin_user() { + + local RET_ADD_ROLE + local RET_GRANT_PERMISSION + local RET_ADD_USER + local RET_GRANT_ROLE + local ADMIN_ROLE="admin" + + # Get admin username from the user + while true; do + echo -e "Enter admin username (press Enter for default username 'default'): \c" + read -sp "" USERNAME + echo + + if [ -z "$USERNAME" ]; then + ADMIN_USERNAME="default" + echo "Using default username 'default'." + break + elif [[ "$USERNAME" =~ ^[a-zA-Z0-9_]+$ ]]; then + ADMIN_USERNAME=$USERNAME + break + else + echo "Invalid admin username. Use only letters, numbers, and underscores. Please try again." + fi + done + + # Get admin password from the user + while true; do + echo -e "Enter user password (press Enter for default password 'default'): \c" + read -sp "" PASSWORD + echo + + if [ -z "$PASSWORD" ]; then + ADMIN_PASSWORD="default" + echo "Using default password." + break + fi + + if [ -n "$PASSWORD" ]; then + read -sp "Confirm the password: " PASSWORD_CONFIRM + echo + + if [ "$PASSWORD" = "$PASSWORD_CONFIRM" ]; then + ADMIN_PASSWORD=$PASSWORD + break + fi + fi + echo "Passwords do not match. Please try again." + done + + # Add 'admin' role + RET_ADD_ROLE=$($ETCD_CTL role add ${ADMIN_ROLE}) + if ! echo "$RET_ADD_ROLE" | grep -q "Role ${ADMIN_ROLE} created"; then + echo $RET_ADD_ROLE + echo "Exit the script" + exit 1 + fi + echo "Successfully added the role" + + # Give full access permission to 'admin' role + RET_GRANT_PERMISSION=$($ETCD_CTL role grant-permission ${ADMIN_ROLE} --prefix=true readwrite / ) + if ! echo "$RET_GRANT_PERMISSION" | grep -q "Role ${ADMIN_ROLE} updated"; then + echo $RET_GRANT_PERMISSION + echo "Exit the script" + exit 1 + fi + + # Add a user with the password + RET_ADD_USER=$($ETCD_CTL user add ${ADMIN_USERNAME}:${ADMIN_PASSWORD}) + if ! echo "$RET_ADD_USER" | grep -q "User ${ADMIN_USERNAME} created"; then + echo $RET_ADD_USER + echo "Exit the script" + exit 1 + fi + + echo "Successfully added, the user with the password" + + # Grant 'admin' role to the user + RET_GRANT_ROLE=$($ETCD_CTL user grant-role ${ADMIN_USERNAME} ${ADMIN_ROLE}) + if ! echo "$RET_GRANT_ROLE" | grep -q "Role ${ADMIN_ROLE} is granted to user ${ADMIN_USERNAME}"; then + echo $RET_GRANT_ROLE + echo "Exit the script" + exit 1 + fi + + echo "Successfully added, the additional admin user with full access permission" +} + +enable_auth() { + + local RET_AUTH_ENABLE + + RET_AUTH_ENABLE=$($ETCD_CTL auth enable) + if ! echo "$RET_AUTH_ENABLE" | grep -q "Authentication Enabled"; then + echo "Failed to enable authentication." + exit 1 + fi + + echo "Successfully enabled, the authentication" +} + +# Main execution flow +main() { + check_etcd + check_auth_enabled + set_root_user + add_additional_admin_user + enable_auth + echo "Successfully completed authentication setup" +} + +main diff --git a/scripts/etcd/setup-auth.sh b/scripts/etcd/setup-auth.sh new file mode 100755 index 000000000..4ca7224d7 --- /dev/null +++ b/scripts/etcd/setup-auth.sh @@ -0,0 +1,147 @@ +#!/bin/bash + +ETCD_VER=v3.5.14 + +ENDPOINTS="http://etcd:2379" +ETCD_PATH="/tmp/etcd-download-test" +ETCD_CTL="${ETCD_PATH}/etcdctl --endpoints=${ENDPOINTS}" + +ROOT_USERNAME="root" # Require 'root' to enable authentication +ROOT_PASSWORD=${ETCD_ROOT_PASSWORD:-default} +ADMIN_USERNAME=${ETCD_ADMIN_USERNAME:-default} +ADMIN_PASSWORD=${ETCD_ADMIN_PASSWORD:-default} + +# Choose either URL +GOOGLE_URL=https://storage.googleapis.com/etcd +GITHUB_URL=https://github.com/etcd-io/etcd/releases/download +DOWNLOAD_URL=${GOOGLE_URL} + + +# Update the package list quietly, but show errors +apk update > /dev/null +# Install curl and tar quietly, but show errors +apk add --no-cache curl tar > /dev/null + +# Clean up any previous downloads and create the target directory +rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz +rm -rf ${ETCD_PATH} && mkdir -p ${ETCD_PATH} + +# Download the etcd tarball quietly, but show errors +curl -sSL ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz +# Extract the tarball to the target directory quietly, but show errors +tar xzf /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz -C ${ETCD_PATH} --strip-components=1 2>&1 +# Clean up the downloaded tarball +rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz + +# Enable auth if ETCD_AUTH_ENABLED is true +if [ "$ETCD_AUTH_ENABLED" = "true" ]; then + + echo "ETCD_AUTH_ENABLED is true" + + # Try to check auth status without authentication + RET_CHECK_AUTH=$($ETCD_CTL auth status 2>/dev/null) + if [ -z "$RET_CHECK_AUTH" ]; then + # Check again auth status with authentication + RET_CHECK_AUTH=$($ETCD_CTL --user ${ROOT_USERNAME}:${ROOT_PASSWORD} auth status) + fi + + if ! echo "$RET_CHECK_AUTH" | grep -q "Authentication Status: true"; then + + echo "Authentication is disabled. Enable it now." + + ROOT_ROLE="root" + ADMIN_ROLE="admin" + + ## Set 'root' + # Add 'root' role + $ETCD_CTL role add ${ROOT_ROLE} + + # Add user 'root' with the password + $ETCD_CTL user add ${ROOT_USERNAME}:${ROOT_PASSWORD} + + # Grant 'root' role to 'root' user + $ETCD_CTL user grant-role ${ROOT_USERNAME} ${ROOT_ROLE} + + ## Set 'admin' + # Add 'admin' user with the password + $ETCD_CTL user add ${ADMIN_USERNAME}:${ADMIN_PASSWORD} + + # Add 'admin' role + $ETCD_CTL role add ${ADMIN_ROLE} + + # Give full access permission to 'admin' role + $ETCD_CTL role grant-permission ${ADMIN_ROLE} --prefix=true readwrite / + + + # Grant 'admin' role to the 'admin' user + $ETCD_CTL user grant-role ${ADMIN_USERNAME} ${ADMIN_ROLE} + + ## Enable authentication + # Enable auth + $ETCD_CTL auth enable + + else + + echo "Authentication is already enabled. Nothing to do." + + fi + +# Disable auth if ETCD_AUTH_ENABLED is false +elif [ "$ETCD_AUTH_ENABLED" = "false" ]; then + + echo "ETCD_AUTH_ENABLED is false" + + # Try to check auth status without authentication + RET_CHECK_AUTH=$($ETCD_CTL auth status 2>/dev/null) + if [ -z "$RET_CHECK_AUTH" ]; then + # Check again auth status with authentication + RET_CHECK_AUTH=$($ETCD_CTL --user ${ROOT_USERNAME}:${ROOT_PASSWORD} auth status) + fi + + if ! echo "$RET_CHECK_AUTH" | grep -q "Authentication Status: false"; then + + echo "Authentication is enabled. Disable it now." + + ROOT_ROLE="root" + ADMIN_ROLE="admin" + + ## Disable authentication + # Disable auth + $ETCD_CTL --user ${ROOT_USERNAME}:${ROOT_PASSWORD} auth disable + + ## Reset 'admin' + # Revoke 'admin' role from 'admin' user + $ETCD_CTL user revoke-role ${ADMIN_USERNAME} ${ADMIN_ROLE} + + # Delete 'admin' user + $ETCD_CTL user delete ${ADMIN_USERNAME} + + # Delete 'admin' role + $ETCD_CTL role delete ${ADMIN_ROLE} + + ## Reset 'root' + # Revoke root role from root user + $ETCD_CTL user revoke-role ${ROOT_USERNAME} ${ROOT_ROLE} + + # Delete 'root' user + $ETCD_CTL user delete ${ROOT_USERNAME} + + # Delete 'root' role + $ETCD_CTL role delete ${ROOT_ROLE} + + else + + echo "Authentication is already disabled. Nothing to do." + + fi + +else + echo "Invalid value for ETCD_AUTH_ENABLED. Please set it to 'true' or 'false'." + exit 1 +fi + +# Indicate successful completion +touch /tmp/healthcheck + +# Add this line at the end of the script to handle termination signals +trap 'exit 0' SIGTERM SIGINT \ No newline at end of file diff --git a/src/core/common/common.go b/src/core/common/common.go index 5a0f1e498..ce7f4e5a1 100644 --- a/src/core/common/common.go +++ b/src/core/common/common.go @@ -63,6 +63,7 @@ var DBPassword string var AutocontrolDurationMs string var DefaultNamespace string var DefaultCredentialHolder string +var EtcdClusterEndpoints string var MyDB *sql.DB var err error var ORM *xorm.Engine @@ -76,6 +77,7 @@ const ( StrDBUser string = "DB_USER" StrDBPassword string = "DB_PASSWORD" StrAutocontrolDurationMs string = "AUTOCONTROL_DURATION_MS" + StrEtcdClusterEndpoints string = "ETCD_CLUSTER_ENDPOINTS" CbStoreKeyNotFoundErrorString string = "key not found" StrAdd string = "add" StrDelete string = "delete" diff --git a/src/core/common/config.go b/src/core/common/config.go index af56e4db2..a27db5e9f 100644 --- a/src/core/common/config.go +++ b/src/core/common/config.go @@ -351,6 +351,9 @@ func UpdateGlobalVariable(id string) error { case StrAutocontrolDurationMs: AutocontrolDurationMs = configInfo.Value log.Debug().Msg(" " + AutocontrolDurationMs) + case StrEtcdClusterEndpoints: + EtcdClusterEndpoints = configInfo.Value + log.Debug().Msg(" " + EtcdClusterEndpoints) default: } diff --git a/src/examples/kvstore/main.go b/src/examples/kvstore/main.go index 16425a054..e82a65396 100644 --- a/src/examples/kvstore/main.go +++ b/src/examples/kvstore/main.go @@ -18,6 +18,8 @@ func main() { config := etcd.Config{ Endpoints: []string{"localhost:2379"}, // Replace with your etcd server endpoints DialTimeout: 5 * time.Second, + Username: "default", + Password: "default", } // Create EtcdStore instance (singleton) @@ -86,10 +88,20 @@ func main() { wg.Wait() fmt.Println("\nAll operations completed successfully!") + + fmt.Println("\nAfter 10 seconds, delete some example keys and values") + time.Sleep(10 * time.Second) + + kvstore.Delete("/mykey") + for i := 0; i < 10; i++ { + kvstore.Delete("/myprefixkey/key" + strconv.Itoa(i)) + } + + time.Sleep(5 * time.Second) } func ExampleBasicCRUDTest(ctx context.Context) { - key := "test_key" + key := "/test_key" value := "Hello, Etcd!" // Put (Store) a key-value pair @@ -140,7 +152,7 @@ func ExampleBasicCRUDTest(ctx context.Context) { func ExampleRaceConditionTest(ctx context.Context) { fmt.Println("Starting race condition test...") - key := "race_test_key" + key := "/race_test_key" iterations := 100 goroutines := 5 @@ -347,7 +359,7 @@ func ExampleContainsIDs() { func watchSingleKey(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() - watchChan := kvstore.WatchKeyWith(ctx, "mykey") + watchChan := kvstore.WatchKeyWith(ctx, "/mykey") for { select { case resp, ok := <-watchChan: @@ -367,7 +379,7 @@ func watchSingleKey(ctx context.Context, wg *sync.WaitGroup) { func watchMultipleKeys(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() - watchChan := kvstore.WatchKeysWith(ctx, "myprefix") + watchChan := kvstore.WatchKeysWith(ctx, "/myprefixkey") for { select { case resp, ok := <-watchChan: @@ -394,15 +406,15 @@ func changeValues(ctx context.Context, wg *sync.WaitGroup) { return default: // Update value with a single key - err := kvstore.PutWith(ctx, "mykey", fmt.Sprintf("value%d", i)) + err := kvstore.PutWith(ctx, "/mykey", fmt.Sprintf("value%d", i)) if err != nil { - log.Printf("Error putting mykey: %v", err) + log.Printf("Error putting /mykey: %v", err) } // Update values with multiple keys - err = kvstore.PutWith(ctx, fmt.Sprintf("myprefix/key%d", i), fmt.Sprintf("prefixvalue%d", i)) + err = kvstore.PutWith(ctx, fmt.Sprintf("/myprefixkey/key%d", i), fmt.Sprintf("prefixvalue%d", i)) if err != nil { - log.Printf("Error putting myprefix/key%d: %v", i, err) + log.Printf("Error putting /myprefixkey/key%d: %v", i, err) } time.Sleep(1 * time.Second) diff --git a/src/kvstore/etcd/etcd.go b/src/kvstore/etcd/etcd.go index 356e0547d..1f409de14 100644 --- a/src/kvstore/etcd/etcd.go +++ b/src/kvstore/etcd/etcd.go @@ -21,6 +21,8 @@ type EtcdStore struct { type Config struct { Endpoints []string DialTimeout time.Duration + Username string + Password string } // NewEtcdStore creates a new instance of EtcdStore (singleton). @@ -29,6 +31,8 @@ func NewEtcdStore(ctx context.Context, config Config) (kvstore.Store, error) { cli, err := clientv3.New(clientv3.Config{ Endpoints: config.Endpoints, DialTimeout: config.DialTimeout, + Username: config.Username, + Password: config.Password, }) if err != nil { return nil, err diff --git a/src/main.go b/src/main.go index 8de8f6ea7..6aca717f8 100644 --- a/src/main.go +++ b/src/main.go @@ -16,17 +16,21 @@ package main import ( "bufio" + "context" "encoding/csv" "flag" "fmt" "os" "os/user" "strconv" + "strings" "sync" "time" // Black import (_) is for running a package's init() function without using its other contents. _ "github.com/cloud-barista/cb-tumblebug/src/core/common/logger" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/etcd" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" "github.com/rs/zerolog/log" //_ "github.com/go-sql-driver/mysql" @@ -58,6 +62,8 @@ func init() { common.AutocontrolDurationMs = common.NVL(os.Getenv("AUTOCONTROL_DURATION_MS"), "10000") common.DefaultNamespace = common.NVL(os.Getenv("DEFAULT_NAMESPACE"), "ns01") common.DefaultCredentialHolder = common.NVL(os.Getenv("DEFAULT_CREDENTIALHOLDER"), "admin") + // Etcd + common.EtcdClusterEndpoints = common.NVL(os.Getenv("ETCD_CLUSTER_ENDPOINTS"), "localhost:2379") // load the latest configuration from DB (if exist) @@ -361,6 +367,51 @@ func main() { }) }() + // Setup etcd and kvstore + var etcdAuthEnabled bool + var etcdUsername string + var etcdPassword string + etcdAuthEnabled = os.Getenv("ETCD_AUTH_ENABLED") == "true" + if etcdAuthEnabled { + etcdUsername = common.NVL(os.Getenv("ETCD_USERNAME"), "default") + etcdPassword = common.NVL(os.Getenv("ETCD_PASSWORD"), "default") + } + + etcdEndpoints := strings.Split(common.EtcdClusterEndpoints, ",") + + ctx := context.Background() + config := etcd.Config{ + Endpoints: etcdEndpoints, + DialTimeout: 5 * time.Second, + Username: etcdUsername, + Password: etcdPassword, + } + + // Wait until etcd is ready + var etcdStore kvstore.Store + var err error + maxAttempts := 10 // (50 sec) + attempt := 1 + for ; attempt <= maxAttempts; attempt++ { + etcdStore, err = etcd.NewEtcdStore(ctx, config) + if err == nil { + log.Info().Msg("etcd is now available.") + break + } + log.Warn().Err(err).Msgf("etcd at %s is not ready. Attempt %d/%d", common.EtcdClusterEndpoints, attempt, maxAttempts) + time.Sleep(5 * time.Second) + } + + if err != nil { + log.Fatal().Err(err).Msg("failed to initialize etcd") + } + + err = kvstore.InitializeStore(etcdStore) + if err != nil { + log.Fatal().Err(err).Msg("") + } + log.Info().Msg("kvstore is initialized successfully. Initializing CB-Tumblebug...") + // Launch API servers (REST) wg := new(sync.WaitGroup) wg.Add(1) From ce10210b32bb66a85115045e91e242656a5b4582 Mon Sep 17 00:00:00 2001 From: Yunkon Kim Date: Fri, 26 Jul 2024 11:31:12 +0900 Subject: [PATCH 04/11] Exclude `key not found` from errors --- src/examples/kvstore/main.go | 93 ++++++++++++++++++++++++++++++---- src/kvstore/etcd/etcd.go | 25 ++++++--- src/kvstore/kvstore/kvstore.go | 4 +- 3 files changed, 103 insertions(+), 19 deletions(-) diff --git a/src/examples/kvstore/main.go b/src/examples/kvstore/main.go index e82a65396..72ff96fd1 100644 --- a/src/examples/kvstore/main.go +++ b/src/examples/kvstore/main.go @@ -46,9 +46,13 @@ func main() { fmt.Println("\n## ExampleRaceConditionTest") ExampleRaceConditionTest(ctx2) - // FilterKVsBy example - fmt.Println("\n## FilterKVsBy example") - ExampleFilterKVsBy() + // FilterKvMapBy example + fmt.Println("\n## FilterKvListBy example") + ExampleFilterKvListBy() + + // FilterKvMapBy example + fmt.Println("\n## FilterKvMapBy example") + ExampleFilterKvMapBy() // ExtractIDsFromKey example fmt.Println("\n## ExtractIDsFromKey example") @@ -250,9 +254,80 @@ func ExampleRaceConditionTest(ctx context.Context) { kvstore.DeleteWith(ctx, key) } -// ExampleFilterKVsBy demonstrates the usage of the FilterKVsBy function +func ExampleFilterKvListBy() { + kvs := []kvstore.KeyValue{ + {Key: "/ns/ns01/mcis/mcis02", Value: "value1"}, + {Key: "/ns/ns01/mcis/mcis02/", Value: "value1"}, + {Key: "/ns/ns01/mcis/mcis03", Value: "value2"}, + {Key: "/ns/ns01/mcis/mcis03/", Value: "value2"}, + {Key: "/ns/ns04/mcis/mcis02", Value: "value3"}, + {Key: "/ns/ns04/mcis/mcis02/", Value: "value3"}, + {Key: "/ns/ns01", Value: "value4"}, + {Key: "/ns/ns01/", Value: "value4"}, + {Key: "/ns/ns04/mcis/mcis05/vpc/vpc01", Value: "value5"}, + {Key: "/ns/ns04/mcis/mcis05/vpc/vpc01/", Value: "value5"}, + {Key: "/ns/ns01/mcis/mcis07", Value: "value6"}, + {Key: "/ns/ns01/mcis/mcis07/", Value: "value6"}, + } + + // Print all key-value pairs + fmt.Println("\nAll key-value pairs:") + for _, kv := range kvs { + fmt.Println(kv.Key, kv.Value) + } + + // Case 1-1: Filter by ns=ns01 and mcis=id2 + prefixkey11 := "/ns/ns01/mcis" + filteredKVs11 := kvutil.FilterKvListBy(kvs, prefixkey11, 1) + fmt.Println("\nFiltered by '/ns/ns01/mcis', Output 'ns/ns01/mcis/{mcisId}': ") + for _, kv := range filteredKVs11 { + fmt.Println(kv.Key, kv.Value) + } + + // Case 1-2: Filter by ns=ns01 and mcis=id2 + prefixkey12 := "/ns/ns01/mcis/" + filteredKVs12 := kvutil.FilterKvListBy(kvs, prefixkey12, 1) + fmt.Println("\nFiltered by '/ns/ns01/mcis/', Output 'ns/ns01/mcis/{mcisId}': ") + for _, kv := range filteredKVs12 { + fmt.Println(kv.Key, kv.Value) + } + + // Case 2-1: Filter by ns=ns01 + prefixkey21 := "/ns" + filteredKVs21 := kvutil.FilterKvListBy(kvs, prefixkey21, 1) + fmt.Println("\nFiltered by '/ns', Output 'ns/{nsId}'") + for _, kv := range filteredKVs21 { + fmt.Println(kv.Key, kv.Value) + } + + // Case 2-2: Filter by ns=ns01 + prefixkey22 := "/ns/" + filteredKVs22 := kvutil.FilterKvListBy(kvs, prefixkey22, 1) + fmt.Println("\nFiltered by '/ns/', Output 'ns/{nsId}'") + for _, kv := range filteredKVs22 { + fmt.Println(kv.Key, kv.Value) + } + + // Case 3-1: Filter by ns=ns04, mcis=mcis05, and vpc=vpc01 + prefixkey31 := "/ns/ns04/mcis/mcis05/vpc" + filteredKVs31 := kvutil.FilterKvListBy(kvs, prefixkey31, 1) + fmt.Println("\nFiltered by '/ns/ns04/mcis/mcis05/vpc', Output '/ns/ns04/mcis/mcis05/vpc/{vpcId}'") + for _, kv := range filteredKVs31 { + fmt.Println(kv.Key, kv.Value) + } + + // Case 3-2: Filter by ns=ns04, mcis=mcis05, and vpc=vpc01 + prefixkey32 := "/ns/ns04/mcis/mcis05/vpc/" + filteredKVs32 := kvutil.FilterKvListBy(kvs, prefixkey32, 1) + fmt.Println("\nFiltered by '/ns/ns04/mcis/mcis05/vpc', Output '/ns/ns04/mcis/mcis05/vpc/{vpcId}'") + for _, kv := range filteredKVs32 { + fmt.Println(kv.Key, kv.Value) + } +} + +// ExampleFilterKvMapBy demonstrates the usage of the FilterKVsBy function // with various key values and different levels of depth. -func ExampleFilterKVsBy() { +func ExampleFilterKvMapBy() { kvs := kvstore.KeyValueMap{ "/ns/ns01/mcis/mcis02": "value1", "/ns/ns01/mcis/mcis03": "value2", @@ -263,7 +338,7 @@ func ExampleFilterKVsBy() { } // Print all key-value pairs - fmt.Println("All key-value pairs:") + fmt.Println("\nAll key-value pairs:") for key, value := range kvs { fmt.Println(key, value) } @@ -271,7 +346,7 @@ func ExampleFilterKVsBy() { // Case 1: Filter by ns=ns01 and mcis=id2 prefixkey1 := "/ns/ns01/mcis" filteredKVs1 := kvutil.FilterKvMapBy(kvs, prefixkey1, 1) - fmt.Println("Filtered by '/ns/ns01/mcis', Output 'ns/ns01/mcis/{mcisId}': ") + fmt.Println("\nFiltered by '/ns/ns01/mcis', Output 'ns/ns01/mcis/{mcisId}': ") for key, value := range filteredKVs1 { fmt.Println(key, value) } @@ -282,7 +357,7 @@ func ExampleFilterKVsBy() { // Case 2: Filter by ns=ns01 prefixkey2 := "/ns" filteredKVs2 := kvutil.FilterKvMapBy(kvs, prefixkey2, 1) - fmt.Println("Filtered by '/ns', Output 'ns/{nsId}'") + fmt.Println("\nFiltered by '/ns', Output 'ns/{nsId}'") for key, value := range filteredKVs2 { fmt.Println(key, value) } @@ -291,7 +366,7 @@ func ExampleFilterKVsBy() { // Case 3: Filter by ns=ns04, mcis=mcis05, and vpc=vpc01 prefixkey3 := "/ns/ns04/mcis/mcis05/vpc" filteredKVs3 := kvutil.FilterKvMapBy(kvs, prefixkey3, 1) - fmt.Println("Filtered by '/ns/ns04/mcis/mcis05/vpc', Output '/ns/ns04/mcis/mcis05/vpc/{vpcId}'") + fmt.Println("\nFiltered by '/ns/ns04/mcis/mcis05/vpc', Output '/ns/ns04/mcis/mcis05/vpc/{vpcId}'") for key, value := range filteredKVs3 { fmt.Println(key, value) } diff --git a/src/kvstore/etcd/etcd.go b/src/kvstore/etcd/etcd.go index 1f409de14..5fbde5382 100644 --- a/src/kvstore/etcd/etcd.go +++ b/src/kvstore/etcd/etcd.go @@ -83,10 +83,16 @@ func (s *EtcdStore) GetWith(ctx context.Context, key string) (string, error) { if err != nil { return "", fmt.Errorf("failed to get key: %w", err) } - if len(resp.Kvs) == 0 { - return "", fmt.Errorf("key not found: %s", key) + + // Return the value of the first key-value pair found + value := "" + for _, kv := range resp.Kvs { + value = string(kv.Value) + return value, nil } - return string(resp.Kvs[0].Value), nil + + // Return an empty string if no key-value pair is found + return value, nil } // GetListWith retrieves multiple values for keys with the given keyPrefix from etcd. @@ -123,13 +129,16 @@ func (s *EtcdStore) GetKvWith(ctx context.Context, key string) (kvstore.KeyValue if err != nil { return kvstore.KeyValue{}, fmt.Errorf("failed to get key: %w", err) } - if len(resp.Kvs) == 0 { - return kvstore.KeyValue{}, fmt.Errorf("key not found: %s", key) - } - kv := kvstore.KeyValue{Key: string(resp.Kvs[0].Key), Value: string(resp.Kvs[0].Value)} + // Return the first key-value pair found + keyValue := kvstore.KeyValue{} + for _, kv := range resp.Kvs { + keyValue = kvstore.KeyValue{Key: string(kv.Key), Value: string(kv.Value)} + return keyValue, nil + } - return kv, nil + // Return an empty key-value pair if no key-value pair is found + return keyValue, nil } // GetKvList retrieves multiple key-value pairs with the given keyPrefix from etcd. diff --git a/src/kvstore/kvstore/kvstore.go b/src/kvstore/kvstore/kvstore.go index 785d01417..01cc04373 100644 --- a/src/kvstore/kvstore/kvstore.go +++ b/src/kvstore/kvstore/kvstore.go @@ -44,8 +44,8 @@ type Store interface { } type KeyValue struct { - Key string - Value string + Key string `json:"key"` + Value string `json:"value"` } // KeyValueMap represents a key-value pair. From 471844f2a6d5a22251cd0cf998feb3f276c08a30 Mon Sep 17 00:00:00 2001 From: Yunkon Kim Date: Fri, 26 Jul 2024 11:33:47 +0900 Subject: [PATCH 05/11] Replace `cb-store` with `kvstore` --- src/core/common/common.go | 63 ++++++++++++------------- src/core/common/config.go | 26 ++++++----- src/core/common/namespace.go | 31 ++++++------ src/core/common/utility.go | 40 ++++++++-------- src/core/mcir/common.go | 67 +++++++++++++------------- src/core/mcir/customimage.go | 5 +- src/core/mcir/datadisk.go | 7 ++- src/core/mcir/firewallrule.go | 15 +++--- src/core/mcir/image.go | 8 ++-- src/core/mcir/securitygroup.go | 4 +- src/core/mcir/spec.go | 10 ++-- src/core/mcir/sshkey.go | 13 +++--- src/core/mcir/subnet.go | 8 ++-- src/core/mcir/vnet.go | 8 ++-- src/core/mcis/control.go | 7 +-- src/core/mcis/k8scluster.go | 75 ++++++++++++++--------------- src/core/mcis/manageInfo.go | 83 +++++++++++++++++---------------- src/core/mcis/nlb.go | 51 ++++++++++---------- src/core/mcis/orchestration.go | 37 ++++++++------- src/core/mcis/provisioning.go | 31 ++++++------ src/core/mcis/recommendation.go | 10 ++-- src/core/mcis/remoteCommand.go | 15 +++--- src/core/mcis/snapshot.go | 5 +- src/core/mcis/utility.go | 19 ++++---- 24 files changed, 323 insertions(+), 315 deletions(-) diff --git a/src/core/common/common.go b/src/core/common/common.go index ce7f4e5a1..0ebff622a 100644 --- a/src/core/common/common.go +++ b/src/core/common/common.go @@ -19,8 +19,6 @@ import ( "sync" "time" - cbstore "github.com/cloud-barista/cb-store" - icbs "github.com/cloud-barista/cb-store/interfaces" "xorm.io/xorm" ) @@ -51,8 +49,6 @@ type OptionalParameter struct { // SystemReady is global variable for checking SystemReady status var SystemReady bool -// CB-Store -var CBStore icbs.Store var SpiderRestUrl string var DragonflyRestUrl string var TerrariumRestUrl string @@ -69,35 +65,35 @@ var err error var ORM *xorm.Engine const ( - StrSpiderRestUrl string = "SPIDER_REST_URL" - StrDragonflyRestUrl string = "DRAGONFLY_REST_URL" - StrTerrariumRestUrl string = "TERRARIUM_REST_URL" - StrDBUrl string = "DB_URL" - StrDBDatabase string = "DB_DATABASE" - StrDBUser string = "DB_USER" - StrDBPassword string = "DB_PASSWORD" - StrAutocontrolDurationMs string = "AUTOCONTROL_DURATION_MS" - StrEtcdClusterEndpoints string = "ETCD_CLUSTER_ENDPOINTS" - CbStoreKeyNotFoundErrorString string = "key not found" - StrAdd string = "add" - StrDelete string = "delete" - StrSSHKey string = "sshKey" - StrImage string = "image" - StrCustomImage string = "customImage" - StrSecurityGroup string = "securityGroup" - StrSpec string = "spec" - StrVNet string = "vNet" - StrSubnet string = "subnet" - StrDataDisk string = "dataDisk" - StrNLB string = "nlb" - StrVM string = "vm" - StrMCIS string = "mcis" - StrK8s string = "k8s" - StrKubernetes string = "kubernetes" - StrContainer string = "container" - StrCommon string = "common" - StrEmpty string = "empty" - StrDefaultResourceName string = "-systemdefault-" + StrSpiderRestUrl string = "SPIDER_REST_URL" + StrDragonflyRestUrl string = "DRAGONFLY_REST_URL" + StrTerrariumRestUrl string = "TERRARIUM_REST_URL" + StrDBUrl string = "DB_URL" + StrDBDatabase string = "DB_DATABASE" + StrDBUser string = "DB_USER" + StrDBPassword string = "DB_PASSWORD" + StrAutocontrolDurationMs string = "AUTOCONTROL_DURATION_MS" + StrEtcdClusterEndpoints string = "ETCD_CLUSTER_ENDPOINTS" + ErrStrKeyNotFound string = "key not found" + StrAdd string = "add" + StrDelete string = "delete" + StrSSHKey string = "sshKey" + StrImage string = "image" + StrCustomImage string = "customImage" + StrSecurityGroup string = "securityGroup" + StrSpec string = "spec" + StrVNet string = "vNet" + StrSubnet string = "subnet" + StrDataDisk string = "dataDisk" + StrNLB string = "nlb" + StrVM string = "vm" + StrMCIS string = "mcis" + StrK8s string = "k8s" + StrKubernetes string = "kubernetes" + StrContainer string = "container" + StrCommon string = "common" + StrEmpty string = "empty" + StrDefaultResourceName string = "-systemdefault-" // StrFirewallRule string = "firewallRule" // SystemCommonNs is const for SystemCommon NameSpace ID @@ -107,7 +103,6 @@ const ( var StartTime string func init() { - CBStore = cbstore.GetStore() StartTime = time.Now().Format("2006.01.02 15:04:05 Mon") } diff --git a/src/core/common/config.go b/src/core/common/config.go index a27db5e9f..c65fe0c9b 100644 --- a/src/core/common/config.go +++ b/src/core/common/config.go @@ -22,7 +22,8 @@ import ( "github.com/jedib0t/go-pretty/v6/table" - cbstore_utils "github.com/cloud-barista/cb-store/utils" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvutil" "github.com/rs/zerolog/log" ) @@ -303,12 +304,13 @@ func UpdateConfig(u *ConfigReq) (ConfigInfo, error) { key := "/config/" + content.Id //mapA := map[string]string{"name": content.Name, "description": content.Description} val, _ := json.Marshal(content) - err := CBStore.Put(key, string(val)) + err := kvstore.Put(key, string(val)) if err != nil { log.Error().Err(err).Msg("") return content, err } - keyValue, _ := CBStore.Get(key) + keyValue, _ := kvstore.GetKv(key) + fmt.Println("UpdateConfig(); ===========================") fmt.Println("UpdateConfig(); Key: " + keyValue.Key + "\nValue: " + keyValue.Value) fmt.Println("UpdateConfig(); ===========================") @@ -398,7 +400,7 @@ func InitConfig(id string) error { log.Debug().Msg("[Init config] " + id) key := "/config/" + id - CBStore.Delete(key) + kvstore.Delete(key) // if err != nil { // log.Error().Err(err).Msg("") // return err @@ -427,7 +429,7 @@ func GetConfig(id string) (ConfigInfo, error) { key := "/config/" + id - keyValue, err := CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { err := fmt.Errorf(errString) return res, err @@ -448,8 +450,8 @@ func ListConfig() ([]ConfigInfo, error) { key := "/config" log.Debug().Msg(key) - keyValue, err := CBStore.GetList(key, true) - keyValue = cbstore_utils.GetChildList(keyValue, key) + keyValue, err := kvstore.GetKvList(key) + keyValue = kvutil.FilterKvListBy(keyValue, key, 1) if err != nil { log.Error().Err(err).Msg("") @@ -478,7 +480,7 @@ func ListConfigId() []string { key := "/config" log.Debug().Msg(key) - keyValue, _ := CBStore.GetList(key, true) + keyValue, _ := kvstore.GetKvList(key) var configList []string for _, v := range keyValue { @@ -498,14 +500,14 @@ func DelAllConfig() error { key := "/config" log.Debug().Msg(key) - keyValue, _ := CBStore.GetList(key, true) + keyValue, _ := kvstore.GetKvList(key) if len(keyValue) == 0 { return nil } for _, v := range keyValue { - err = CBStore.Delete(v.Key) + err = kvstore.Delete(v.Key) if err != nil { return err } @@ -531,8 +533,8 @@ func CheckConfig(id string) (bool, error) { key := "/config/" + id - keyValue, _ := CBStore.Get(key) - if keyValue != nil { + keyValue, _ := kvstore.GetKv(key) + if keyValue != (kvstore.KeyValue{}) { return true, nil } return false, nil diff --git a/src/core/common/namespace.go b/src/core/common/namespace.go index aed7fe29d..c297612e6 100644 --- a/src/core/common/namespace.go +++ b/src/core/common/namespace.go @@ -26,7 +26,8 @@ import ( "github.com/labstack/echo/v4" "github.com/rs/zerolog/log" - cbstore_utils "github.com/cloud-barista/cb-store/utils" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvutil" ) type NsReq struct { @@ -94,12 +95,12 @@ func CreateNs(u *NsReq) (NsInfo, error) { Key := "/ns/" + content.Id Val, _ := json.Marshal(content) - err = CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("") return content, err } - keyValue, _ := CBStore.Get(Key) + keyValue, _ := kvstore.GetKv(Key) fmt.Println("CreateNs: Key: " + keyValue.Key + "\nValue: " + keyValue.Value) return content, nil } @@ -129,7 +130,7 @@ func UpdateNs(id string, u *NsReq) (NsInfo, error) { } key := "/ns/" + id - keyValue, err := CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") return emptyInfo, err @@ -152,12 +153,12 @@ func UpdateNs(id string, u *NsReq) (NsInfo, error) { log.Error().Err(err).Msg("") return emptyInfo, err } - err = CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("") return emptyInfo, err } - keyValue, err = CBStore.Get(Key) + keyValue, err = kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") return emptyInfo, err @@ -200,7 +201,7 @@ func GetNs(id string) (NsInfo, error) { key := "/ns/" + id log.Debug().Msg(key) - keyValue, err := CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") return res, err @@ -219,8 +220,8 @@ func ListNs() ([]NsInfo, error) { key := "/ns" log.Debug().Msg(key) - keyValue, err := CBStore.GetList(key, true) - keyValue = cbstore_utils.GetChildList(keyValue, key) + keyValue, err := kvstore.GetKvList(key) + keyValue = kvutil.FilterKvListBy(keyValue, key, 1) if err != nil { log.Error().Err(err).Msg("") @@ -259,7 +260,7 @@ func ListNsId() ([]string, error) { var nsList []string // Implementation Option 1 - // keyValue, _ := CBStore.GetList(key, true) + // keyValue, _ := kvstore.GetKvList(key) // r, _ := regexp.Compile("/ns/[a-z]([-a-z0-9]*[a-z0-9])?$") @@ -281,8 +282,8 @@ func ListNsId() ([]string, error) { // EOF of Implementation Option 1 // Implementation Option 2 - keyValue, err := CBStore.GetList(key, true) - keyValue = cbstore_utils.GetChildList(keyValue, key) + keyValue, err := kvstore.GetKvList(key) + keyValue = kvutil.FilterKvListBy(keyValue, key, 1) if err != nil { log.Error().Err(err).Msg("") @@ -359,7 +360,7 @@ func DelNs(id string) error { } // delete ns info - err = CBStore.Delete(key) + err = kvstore.Delete(key) if err != nil { log.Error().Err(err).Msg("") return err @@ -403,8 +404,8 @@ func CheckNs(id string) (bool, error) { key := "/ns/" + id - keyValue, _ := CBStore.Get(key) - if keyValue != nil { + keyValue, _ := kvstore.GetKv(key) + if keyValue != (kvstore.KeyValue{}) { return true, nil } return false, nil diff --git a/src/core/common/utility.go b/src/core/common/utility.go index 4736a0f49..6045cdfc5 100644 --- a/src/core/common/utility.go +++ b/src/core/common/utility.go @@ -23,7 +23,8 @@ import ( "sync" "time" - cbstore_utils "github.com/cloud-barista/cb-store/utils" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvutil" uid "github.com/rs/xid" "github.com/rs/zerolog/log" @@ -259,12 +260,13 @@ func GetCspResourceId(nsId string, resourceType string, resourceId string) (stri if key == "/invalidKey" { return "", fmt.Errorf("invalid nsId or resourceType or resourceId") } - keyValue, err := CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") return "", err } - if keyValue == nil { + + if keyValue == (kvstore.KeyValue{}) { //log.Error().Err(err).Msg("") // if there is no matched value for the key, return empty string. Error will be handled in a parent function return "", fmt.Errorf("cannot find the key " + key) @@ -374,12 +376,12 @@ func GetConnConfig(ConnConfigName string) (ConnConfig, error) { connConfig := ConnConfig{} key := GenConnectionKey(ConnConfigName) - keyValue, err := CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") return ConnConfig{}, err } - if keyValue == nil { + if keyValue == (kvstore.KeyValue{}) { return ConnConfig{}, fmt.Errorf("Cannot find the ConnConfig " + key) } err = json.Unmarshal([]byte(keyValue.Value), &connConfig) @@ -459,8 +461,8 @@ func GetConnConfigList(filterCredentialHolder string, filterVerified bool, filte var tmpConnections ConnConfigList key := "/connection" - keyValue, err := CBStore.GetList(key, true) - keyValue = cbstore_utils.GetChildList(keyValue, key) + keyValue, err := kvstore.GetKvList(key) + keyValue = kvutil.FilterKvListBy(keyValue, key, 1) if err != nil { log.Error().Err(err).Msg("") @@ -806,7 +808,7 @@ func RegisterCredential(req CredentialReq) (CredentialInfo, error) { if err != nil { return CredentialInfo{}, err } - err = CBStore.Put(string(key), string(val)) + err = kvstore.Put(string(key), string(val)) if err != nil { return callResult, err } @@ -845,7 +847,7 @@ func RegisterCredential(req CredentialReq) (CredentialInfo, error) { if err != nil { return callResult, err } - err = CBStore.Put(string(key), string(val)) + err = kvstore.Put(string(key), string(val)) if err != nil { return callResult, err } @@ -883,7 +885,7 @@ func RegisterCredential(req CredentialReq) (CredentialInfo, error) { if err != nil { return callResult, err } - err = CBStore.Put(string(key), string(val)) + err = kvstore.Put(string(key), string(val)) if err != nil { return callResult, err } @@ -995,7 +997,7 @@ func RegisterConnectionConfig(connConfig ConnConfig) (ConnConfig, error) { if err != nil { return ConnConfig{}, err } - err = CBStore.Put(string(key), string(val)) + err = kvstore.Put(string(key), string(val)) if err != nil { log.Error().Err(err).Msg("") return ConnConfig{}, err @@ -1180,8 +1182,8 @@ func NVL(str string, def string) string { // GetChildIdList is func to get child id list from given key func GetChildIdList(key string) []string { - keyValue, _ := CBStore.GetList(key, true) - keyValue = cbstore_utils.GetChildList(keyValue, key) + keyValue, _ := kvstore.GetKvList(key) + keyValue = kvutil.FilterKvListBy(keyValue, key, 1) var childIdList []string for _, v := range keyValue { @@ -1199,7 +1201,7 @@ func GetChildIdList(key string) []string { // GetObjectList is func to return IDs of each child objects that has the same key func GetObjectList(key string) []string { - keyValue, _ := CBStore.GetList(key, true) + keyValue, _ := kvstore.GetKvList(key) var childIdList []string for _, v := range keyValue { @@ -1213,12 +1215,12 @@ func GetObjectList(key string) []string { // GetObjectValue is func to return the object value func GetObjectValue(key string) (string, error) { - keyValue, err := CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") return "", err } - if keyValue == nil { + if keyValue == (kvstore.KeyValue{}) { return "", nil } return keyValue.Value, nil @@ -1227,7 +1229,7 @@ func GetObjectValue(key string) (string, error) { // DeleteObject is func to delete the object func DeleteObject(key string) error { - err := CBStore.Delete(key) + err := kvstore.Delete(key) if err != nil { log.Error().Err(err).Msg("") return err @@ -1237,9 +1239,9 @@ func DeleteObject(key string) error { // DeleteObjects is func to delete objects func DeleteObjects(key string) error { - keyValue, _ := CBStore.GetList(key, true) + keyValue, _ := kvstore.GetKvList(key) for _, v := range keyValue { - err := CBStore.Delete(v.Key) + err := kvstore.Delete(v.Key) if err != nil { log.Error().Err(err).Msg("") return err diff --git a/src/core/mcir/common.go b/src/core/mcir/common.go index 3647afe7f..60a48ebff 100644 --- a/src/core/mcir/common.go +++ b/src/core/mcir/common.go @@ -29,10 +29,10 @@ import ( //uuid "github.com/google/uuid" "github.com/cloud-barista/cb-tumblebug/src/core/common" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvutil" "github.com/go-resty/resty/v2" - // CB-Store - cbstore_utils "github.com/cloud-barista/cb-store/utils" "github.com/tidwall/gjson" "github.com/tidwall/sjson" @@ -141,8 +141,8 @@ func DelResource(nsId string, resourceType string, resourceId string, forceFlag } key := common.GenResourceKey(nsId, resourceType, resourceId) - keyValue, _ := common.CBStore.Get(key) - // In CheckResource() above, calling 'CBStore.Get()' and checking err parts exist. + keyValue, _ := kvstore.GetKv(key) + // In CheckResource() above, calling 'kvstore.GetKv()' and checking err parts exist. // So, in here, we don't need to check whether keyValue == nil or err != nil. /* Disabled the deletion protection feature @@ -172,7 +172,7 @@ func DelResource(nsId string, resourceType string, resourceId string, forceFlag switch resourceType { case common.StrImage: // delete image info - err := common.CBStore.Delete(key) + err := kvstore.Delete(key) if err != nil { log.Error().Err(err).Msg("") return err @@ -199,7 +199,7 @@ func DelResource(nsId string, resourceType string, resourceId string, forceFlag /* // delete image info - err := common.CBStore.Delete(key) + err := kvstore.Delete(key) if err != nil { log.Error().Err(err).Msg("") return err @@ -219,7 +219,7 @@ func DelResource(nsId string, resourceType string, resourceId string, forceFlag // delete spec info //get related recommend spec - //keyValue, err := common.CBStore.Get(key) + //keyValue, err := kvstore.GetKv(key) content := TbSpecInfo{} err := json.Unmarshal([]byte(keyValue.Value), &content) if err != nil { @@ -227,7 +227,7 @@ func DelResource(nsId string, resourceType string, resourceId string, forceFlag return err } - err = common.CBStore.Delete(key) + err = kvstore.Delete(key) if err != nil { log.Error().Err(err).Msg("") return err @@ -330,7 +330,7 @@ func DelResource(nsId string, resourceType string, resourceId string, forceFlag for _, v := range subnets { subnetKey := common.GenChildResourceKey(nsId, common.StrSubnet, resourceId, v.Id) // subnetKeys = append(subnetKeys, subnetKey) - err = common.CBStore.Delete(subnetKey) + err = kvstore.Delete(subnetKey) if err != nil { log.Error().Err(err).Msg("") // return err @@ -346,7 +346,7 @@ func DelResource(nsId string, resourceType string, resourceId string, forceFlag } } - err = common.CBStore.Delete(key) + err = kvstore.Delete(key) if err != nil { log.Error().Err(err).Msg("") return err @@ -416,7 +416,7 @@ func DelChildResource(nsId string, resourceType string, parentResourceId string, childResourceKey := common.GenChildResourceKey(nsId, resourceType, parentResourceId, resourceId) log.Debug().Msg("childResourceKey: " + childResourceKey) - parentKeyValue, _ := common.CBStore.Get(parentResourceKey) + parentKeyValue, _ := kvstore.GetKv(parentResourceKey) //cspType := common.GetResourcesCspType(nsId, resourceType, resourceId) @@ -467,7 +467,7 @@ func DelChildResource(nsId string, resourceType string, parentResourceId string, return err } - err = common.CBStore.Delete(childResourceKey) + err = kvstore.Delete(childResourceKey) if err != nil { log.Error().Err(err).Msg("") return err @@ -503,7 +503,7 @@ func DelChildResource(nsId string, resourceType string, parentResourceId string, } Val, _ := json.Marshal(newVNet) - err = common.CBStore.Put(parentResourceKey, string(Val)) + err = kvstore.Put(parentResourceKey, string(Val)) if err != nil { log.Error().Err(err).Msg("") return err @@ -554,7 +554,7 @@ func ListResourceId(nsId string, resourceType string) ([]string, error) { } key := "/ns/" + nsId + "/resources/" - keyValue, err := common.CBStore.GetList(key, true) + keyValue, err := kvstore.GetKvList(key) if err != nil { log.Error().Err(err).Msg("") @@ -612,8 +612,8 @@ func ListResource(nsId string, resourceType string, filterKey string, filterVal key := "/ns/" + nsId + "/resources/" + resourceType log.Debug().Msg(key) - keyValue, err := common.CBStore.GetList(key, true) - keyValue = cbstore_utils.GetChildList(keyValue, key) + keyValue, err := kvstore.GetKvList(key) + keyValue = kvutil.FilterKvListBy(keyValue, key, 1) if err != nil { log.Error().Err(err).Msg("") @@ -845,12 +845,12 @@ func GetAssociatedObjectCount(nsId string, resourceType string, resourceId strin key := common.GenResourceKey(nsId, resourceType, resourceId) - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") return -1, err } - if keyValue != nil { + if keyValue != (kvstore.KeyValue{}) { inUseCount := int(gjson.Get(keyValue.Value, "associatedObjectList.#").Int()) return inUseCount, nil } @@ -890,12 +890,12 @@ func GetAssociatedObjectList(nsId string, resourceType string, resourceId string key := common.GenResourceKey(nsId, resourceType, resourceId) - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") return nil, err } - if keyValue != nil { + if keyValue != (kvstore.KeyValue{}) { type stringList struct { AssociatedObjectList []string `json:"associatedObjectList"` @@ -947,13 +947,13 @@ func UpdateAssociatedObjectList(nsId string, resourceType string, resourceId str key := common.GenResourceKey(nsId, resourceType, resourceId) - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") return nil, err } - if keyValue != nil { + if keyValue != (kvstore.KeyValue{}) { objList, _ := GetAssociatedObjectList(nsId, resourceType, resourceId) switch cmd { case common.StrAdd: @@ -1007,7 +1007,7 @@ func UpdateAssociatedObjectList(nsId string, resourceType string, resourceId str log.Error().Err(err).Msg("") return nil, err } - err = common.CBStore.Put(key, keyValue.Value) + err = kvstore.Put(key, keyValue.Value) if err != nil { log.Error().Err(err).Msg("") return nil, err @@ -1051,12 +1051,13 @@ func GetResource(nsId string, resourceType string, resourceId string) (interface key := common.GenResourceKey(nsId, resourceType, resourceId) - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) + if err != nil { log.Error().Err(err).Msg("") return nil, err } - if keyValue != nil { + if keyValue != (kvstore.KeyValue{}) { switch resourceType { case common.StrImage: res := TbImageInfo{} @@ -1263,12 +1264,12 @@ func CheckResource(nsId string, resourceType string, resourceId string) (bool, e key := common.GenResourceKey(nsId, resourceType, resourceId) - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") return false, err } - if keyValue != nil { + if keyValue != (kvstore.KeyValue{}) { return true, nil } return false, nil @@ -1326,12 +1327,12 @@ func CheckChildResource(nsId string, resourceType string, parentResourceId strin key := common.GenResourceKey(nsId, parentResourceType, parentResourceId) key += "/" + resourceType + "/" + resourceId - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") return false, err } - if keyValue != nil { + if keyValue != (kvstore.KeyValue{}) { return true, nil } return false, nil @@ -2004,8 +2005,8 @@ func UpdateResourceObject(nsId string, resourceType string, resourceObject inter key := common.GenResourceKey(nsId, resourceType, resourceId) // Check existence of the key. If no key, no update. - keyValue, err := common.CBStore.Get(key) - if keyValue == nil || err != nil { + keyValue, err := kvstore.GetKv(key) + if keyValue == (kvstore.KeyValue{}) || err != nil { return } @@ -2023,7 +2024,7 @@ func UpdateResourceObject(nsId string, resourceType string, resourceObject inter } if !isEqualJSON { - err = common.CBStore.Put(key, string(newJSON)) + err = kvstore.Put(key, string(newJSON)) if err != nil { log.Error().Err(err).Msg("") } @@ -2039,7 +2040,7 @@ func UpdateResourceObject(nsId string, resourceType string, resourceObject inter if !reflect.DeepEqual(oldObject, resourceObject) { val, _ := json.Marshal(resourceObject) - err = common.CBStore.Put(key, string(val)) + err = kvstore.Put(key, string(val)) if err != nil { log.Error().Err(err).Msg("") } diff --git a/src/core/mcir/customimage.go b/src/core/mcir/customimage.go index bf88e4185..db22be0ba 100644 --- a/src/core/mcir/customimage.go +++ b/src/core/mcir/customimage.go @@ -21,6 +21,7 @@ import ( "time" "github.com/cloud-barista/cb-tumblebug/src/core/common" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" validator "github.com/go-playground/validator/v10" "github.com/go-resty/resty/v2" "github.com/rs/zerolog/log" @@ -137,7 +138,7 @@ func RegisterCustomImageWithInfo(nsId string, content TbCustomImageInfo) (TbCust log.Info().Msg("POST registerCustomImage") Key := common.GenResourceKey(nsId, resourceType, content.Id) Val, _ := json.Marshal(content) - err = common.CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("") return TbCustomImageInfo{}, err @@ -323,7 +324,7 @@ func RegisterCustomImageWithId(nsId string, u *TbCustomImageReq) (TbCustomImageI Key := common.GenResourceKey(nsId, resourceType, content.Id) Val, _ := json.Marshal(content) - err = common.CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("") return content, err diff --git a/src/core/mcir/datadisk.go b/src/core/mcir/datadisk.go index f135b1feb..297f1ae83 100644 --- a/src/core/mcir/datadisk.go +++ b/src/core/mcir/datadisk.go @@ -24,6 +24,7 @@ import ( "github.com/rs/zerolog/log" "github.com/cloud-barista/cb-tumblebug/src/core/common" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" validator "github.com/go-playground/validator/v10" ) @@ -260,11 +261,10 @@ func CreateDataDisk(nsId string, u *TbDataDiskReq, option string) (TbDataDiskInf } } - // cb-store log.Info().Msg("PUT CreateDataDisk") Key := common.GenResourceKey(nsId, resourceType, content.Id) Val, _ := json.Marshal(content) - err = common.CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("") return content, err @@ -380,11 +380,10 @@ func UpsizeDataDisk(nsId string, resourceId string, u *TbDataDiskUpsizeReq) (TbD content.DiskSize = u.DiskSize content.Description = u.Description - // cb-store log.Info().Msg("PUT UpsizeDataDisk") Key := common.GenResourceKey(nsId, resourceType, content.Id) Val, _ := json.Marshal(content) - err = common.CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("") return content, err diff --git a/src/core/mcir/firewallrule.go b/src/core/mcir/firewallrule.go index 9d786b649..504ef986f 100644 --- a/src/core/mcir/firewallrule.go +++ b/src/core/mcir/firewallrule.go @@ -22,6 +22,7 @@ import ( "strings" "github.com/cloud-barista/cb-tumblebug/src/core/common" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" validator "github.com/go-playground/validator/v10" "github.com/go-resty/resty/v2" "github.com/rs/zerolog/log" @@ -81,7 +82,7 @@ func CreateFirewallRules(nsId string, securityGroupId string, req []TbFirewallRu } securityGroupKey := common.GenResourceKey(nsId, common.StrSecurityGroup, securityGroupId) - securityGroupKeyValue, _ := common.CBStore.Get(securityGroupKey) + securityGroupKeyValue, _ := kvstore.GetKv(securityGroupKey) oldSecurityGroup := TbSecurityGroupInfo{} err = json.Unmarshal([]byte(securityGroupKeyValue.Value), &oldSecurityGroup) if err != nil { @@ -142,7 +143,6 @@ func CreateFirewallRules(nsId string, securityGroupId string, req []TbFirewallRu } - // cb-store log.Info().Msg("POST CreateFirewallRule") newSecurityGroup := TbSecurityGroupInfo{} @@ -154,14 +154,14 @@ func CreateFirewallRules(nsId string, securityGroupId string, req []TbFirewallRu } Val, _ := json.Marshal(newSecurityGroup) - err = common.CBStore.Put(securityGroupKey, string(Val)) + err = kvstore.Put(securityGroupKey, string(Val)) if err != nil { log.Error().Err(err).Msg("") return oldSecurityGroup, err } // securityGroupKey := common.GenResourceKey(nsId, common.StrSecurityGroup, securityGroupId) - // keyValue, _ := common.CBStore.Get(securityGroupKey) + // keyValue, _ := kvstore.GetKv(securityGroupKey) // // // content := TbSecurityGroupInfo{} @@ -226,7 +226,7 @@ func DeleteFirewallRules(nsId string, securityGroupId string, req []TbFirewallRu } securityGroupKey := common.GenResourceKey(nsId, common.StrSecurityGroup, securityGroupId) - securityGroupKeyValue, _ := common.CBStore.Get(securityGroupKey) + securityGroupKeyValue, _ := kvstore.GetKv(securityGroupKey) oldSecurityGroup := TbSecurityGroupInfo{} err = json.Unmarshal([]byte(securityGroupKeyValue.Value), &oldSecurityGroup) if err != nil { @@ -341,7 +341,6 @@ func DeleteFirewallRules(nsId string, securityGroupId string, req []TbFirewallRu tempSpiderSecurityInfo = resp.Result().(*SpiderSecurityInfo) - // cb-store log.Info().Msg("DELETE FirewallRule") newSecurityGroup := TbSecurityGroupInfo{} @@ -352,14 +351,14 @@ func DeleteFirewallRules(nsId string, securityGroupId string, req []TbFirewallRu } Val, _ := json.Marshal(newSecurityGroup) - err = common.CBStore.Put(securityGroupKey, string(Val)) + err = kvstore.Put(securityGroupKey, string(Val)) if err != nil { log.Error().Err(err).Msg("") return oldSecurityGroup, err } // securityGroupKey := common.GenResourceKey(nsId, common.StrSecurityGroup, securityGroupId) - // keyValue, _ := common.CBStore.Get(securityGroupKey) + // keyValue, _ := kvstore.GetKv(securityGroupKey) // // // content := TbSecurityGroupInfo{} diff --git a/src/core/mcir/image.go b/src/core/mcir/image.go index f8adaa383..7e52ba9bb 100644 --- a/src/core/mcir/image.go +++ b/src/core/mcir/image.go @@ -25,6 +25,7 @@ import ( "github.com/rs/zerolog/log" "github.com/cloud-barista/cb-tumblebug/src/core/common" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" validator "github.com/go-playground/validator/v10" ) @@ -177,11 +178,10 @@ func RegisterImageWithId(nsId string, u *TbImageReq, update bool) (TbImageInfo, content.Name = u.Name content.AssociatedObjectList = []string{} - // cb-store //log.Info().Msg("PUT registerImage") Key := common.GenResourceKey(nsId, resourceType, content.Id) Val, _ := json.Marshal(content) - err = common.CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("") return content, err @@ -235,7 +235,7 @@ func RegisterImageWithInfo(nsId string, content *TbImageInfo, update bool) (TbIm log.Info().Msg("PUT registerImage") Key := common.GenResourceKey(nsId, resourceType, content.Id) Val, _ := json.Marshal(content) - err = common.CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("") return TbImageInfo{}, err @@ -495,7 +495,7 @@ func UpdateImage(nsId string, imageId string, fieldsToUpdate TbImageInfo) (TbIma Key := common.GenResourceKey(nsId, resourceType, toBeImage.Id) Val, _ := json.Marshal(toBeImage) - err = common.CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("") return temp, err diff --git a/src/core/mcir/securitygroup.go b/src/core/mcir/securitygroup.go index fc1a75f50..109e30327 100644 --- a/src/core/mcir/securitygroup.go +++ b/src/core/mcir/securitygroup.go @@ -20,6 +20,7 @@ import ( "strconv" "github.com/cloud-barista/cb-tumblebug/src/core/common" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" validator "github.com/go-playground/validator/v10" "github.com/go-resty/resty/v2" "github.com/rs/zerolog/log" @@ -325,11 +326,10 @@ func CreateSecurityGroup(nsId string, u *TbSecurityGroupReq, option string) (TbS content.SystemLabel = "Registered from CSP resource" } - // cb-store log.Info().Msg("PUT CreateSecurityGroup") Key := common.GenResourceKey(nsId, resourceType, content.Id) Val, _ := json.Marshal(content) - err = common.CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("") return content, err diff --git a/src/core/mcir/spec.go b/src/core/mcir/spec.go index 385ebb06f..58cfadacd 100644 --- a/src/core/mcir/spec.go +++ b/src/core/mcir/spec.go @@ -24,6 +24,7 @@ import ( "time" "github.com/cloud-barista/cb-tumblebug/src/core/common" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" validator "github.com/go-playground/validator/v10" "github.com/go-resty/resty/v2" "github.com/rs/zerolog/log" @@ -390,11 +391,10 @@ func RegisterSpecWithCspSpecName(nsId string, u *TbSpecReq, update bool) (TbSpec //content.StorageGiB = res.StorageGiB //content.Description = res.Description - // cb-store log.Trace().Msg("PUT registerSpec") Key := common.GenResourceKey(nsId, resourceType, content.Id) Val, _ := json.Marshal(content) - err = common.CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("Cannot put data to Key Value Store") return content, err @@ -448,11 +448,10 @@ func RegisterSpecWithInfo(nsId string, content *TbSpecInfo, update bool) (TbSpec content.Id = content.Name content.AssociatedObjectList = []string{} - // cb-store log.Trace().Msg("PUT registerSpec") Key := common.GenResourceKey(nsId, resourceType, content.Id) Val, _ := json.Marshal(content) - err = common.CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("") return *content, err @@ -723,10 +722,9 @@ func UpdateSpec(nsId string, specId string, fieldsToUpdate TbSpecInfo) (TbSpecIn toBeSpecJSON, _ := json.Marshal(fieldsToUpdate) err = json.Unmarshal(toBeSpecJSON, &toBeSpec) - // cb-store Key := common.GenResourceKey(nsId, resourceType, toBeSpec.Id) Val, _ := json.Marshal(toBeSpec) - err = common.CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { temp := TbSpecInfo{} log.Error().Err(err).Msg("") diff --git a/src/core/mcir/sshkey.go b/src/core/mcir/sshkey.go index 68568e2a8..f8dc3c54a 100644 --- a/src/core/mcir/sshkey.go +++ b/src/core/mcir/sshkey.go @@ -19,6 +19,7 @@ import ( "fmt" "github.com/cloud-barista/cb-tumblebug/src/core/common" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" validator "github.com/go-playground/validator/v10" "github.com/go-resty/resty/v2" "github.com/rs/zerolog/log" @@ -227,15 +228,15 @@ func CreateSshKey(nsId string, u *TbSshKeyReq, option string) (TbSshKeyInfo, err content.PrivateKey = u.PrivateKey } - // cb-store log.Info().Msg("PUT CreateSshKey") Key := common.GenResourceKey(nsId, resourceType, content.Id) Val, _ := json.Marshal(content) - err = common.CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("") return content, err } + return content, nil } @@ -288,19 +289,19 @@ func UpdateSshKey(nsId string, sshKeyId string, fieldsToUpdate TbSshKeyInfo) (Tb toBeSshKeyJSON, _ := json.Marshal(fieldsToUpdate) err = json.Unmarshal(toBeSshKeyJSON, &toBeSshKey) - // cb-store log.Info().Msg("PUT UpdateSshKey") Key := common.GenResourceKey(nsId, resourceType, toBeSshKey.Id) Val, _ := json.Marshal(toBeSshKey) - err = common.CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("") return emptyObj, err } - keyValue, err := common.CBStore.Get(Key) + + keyValue, err := kvstore.GetKv(Key) if err != nil { log.Error().Err(err).Msg("") - err = fmt.Errorf("In UpdateSshKey(); CBStore.Get() returned an error.") + err = fmt.Errorf("In UpdateSshKey(); kvstore.GetKv() returned an error.") log.Error().Err(err).Msg("") // return nil, err } diff --git a/src/core/mcir/subnet.go b/src/core/mcir/subnet.go index 18d401399..deb3c6784 100644 --- a/src/core/mcir/subnet.go +++ b/src/core/mcir/subnet.go @@ -20,6 +20,7 @@ import ( "strconv" "github.com/cloud-barista/cb-tumblebug/src/core/common" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" validator "github.com/go-playground/validator/v10" "github.com/go-resty/resty/v2" "github.com/rs/zerolog/log" @@ -86,7 +87,7 @@ func CreateSubnet(nsId string, vNetId string, req TbSubnetReq, objectOnly bool) } vNetKey := common.GenResourceKey(nsId, common.StrVNet, vNetId) - vNetKeyValue, _ := common.CBStore.Get(vNetKey) + vNetKeyValue, _ := kvstore.GetKv(vNetKey) oldVNet := TbVNetInfo{} err = json.Unmarshal([]byte(vNetKeyValue.Value), &oldVNet) if err != nil { @@ -129,12 +130,11 @@ func CreateSubnet(nsId string, vNetId string, req TbSubnetReq, objectOnly bool) } - // cb-store log.Info().Msg("POST CreateSubnet") SubnetKey := common.GenChildResourceKey(nsId, common.StrSubnet, vNetId, req.Name) Val, _ := json.Marshal(req) - err = common.CBStore.Put(SubnetKey, string(Val)) + err = kvstore.Put(SubnetKey, string(Val)) if err != nil { temp := TbVNetInfo{} log.Error().Err(err).Msg("") @@ -161,7 +161,7 @@ func CreateSubnet(nsId string, vNetId string, req TbSubnetReq, objectOnly bool) newVNet.SubnetInfoList = append(newVNet.SubnetInfoList, tbSubnetInfo) Val, _ = json.Marshal(newVNet) - err = common.CBStore.Put(vNetKey, string(Val)) + err = kvstore.Put(vNetKey, string(Val)) if err != nil { log.Error().Err(err).Msg("") return oldVNet, err diff --git a/src/core/mcir/vnet.go b/src/core/mcir/vnet.go index 1a6e54384..4ddb80b58 100644 --- a/src/core/mcir/vnet.go +++ b/src/core/mcir/vnet.go @@ -19,6 +19,7 @@ import ( "fmt" "github.com/cloud-barista/cb-tumblebug/src/core/common" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" validator "github.com/go-playground/validator/v10" "github.com/go-resty/resty/v2" "github.com/rs/zerolog/log" @@ -254,11 +255,10 @@ func CreateVNet(nsId string, u *TbVNetReq, option string) (TbVNetInfo, error) { content.SystemLabel = "Registered from CSP resource" } - // cb-store Key := common.GenResourceKey(nsId, common.StrVNet, content.Id) Val, _ := json.Marshal(content) - err = common.CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("") return content, err @@ -284,10 +284,10 @@ func CreateVNet(nsId string, u *TbVNetReq, option string) (TbVNetInfo, error) { } } - keyValue, err := common.CBStore.Get(Key) + keyValue, err := kvstore.GetKv(Key) if err != nil { log.Error().Err(err).Msg("") - err = fmt.Errorf("In CreateVNet(); CBStore.Get() returned an error.") + err = fmt.Errorf("In CreateVNet(); kvstore.GetKv() returned an error.") log.Error().Err(err).Msg("") // return nil, err } diff --git a/src/core/mcis/control.go b/src/core/mcis/control.go index bc1f329b9..c1046cd66 100644 --- a/src/core/mcis/control.go +++ b/src/core/mcis/control.go @@ -33,6 +33,7 @@ import ( "github.com/cloud-barista/cb-tumblebug/src/core/common" "github.com/cloud-barista/cb-tumblebug/src/core/mcir" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" "github.com/go-resty/resty/v2" "github.com/rs/zerolog/log" ) @@ -373,10 +374,10 @@ func ControlVmAsync(wg *sync.WaitGroup, nsId string, mcisId string, vmId string, key := common.GenMcisKey(nsId, mcisId, vmId) log.Debug().Msg("[ControlVmAsync] " + key) - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) - if keyValue == nil || err != nil { - callResult.Error = fmt.Errorf("CBStoreGetErr in ControlVmAsync. key[" + key + "]") + if keyValue == (kvstore.KeyValue{}) || err != nil { + callResult.Error = fmt.Errorf("kvstore.Get() Err in ControlVmAsync. key[" + key + "]") log.Fatal().Err(callResult.Error).Msg("Error in ControlVmAsync") results <- callResult diff --git a/src/core/mcis/k8scluster.go b/src/core/mcis/k8scluster.go index e7470a43f..259b13c50 100644 --- a/src/core/mcis/k8scluster.go +++ b/src/core/mcis/k8scluster.go @@ -21,9 +21,10 @@ import ( "strings" "time" - cbstore_utils "github.com/cloud-barista/cb-store/utils" "github.com/cloud-barista/cb-tumblebug/src/core/common" "github.com/cloud-barista/cb-tumblebug/src/core/mcir" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvutil" validator "github.com/go-playground/validator/v10" "github.com/go-resty/resty/v2" "github.com/rs/zerolog/log" @@ -674,20 +675,20 @@ func CreateK8sCluster(nsId string, u *TbK8sClusterReq, option string) (TbK8sClus } /* - * Put/Get TbK8sClusterInfo to/from cb-store + * Put/Get TbK8sClusterInfo to/from kvstore */ k := GenK8sClusterKey(nsId, tbK8sCInfo.Id) Val, _ := json.Marshal(tbK8sCInfo) - err = common.CBStore.Put(k, string(Val)) + err = kvstore.Put(k, string(Val)) if err != nil { log.Err(err).Msg("Failed to Create a K8sCluster") return tbK8sCInfo, err } - kv, err := common.CBStore.Get(k) + kv, err := kvstore.GetKv(k) if err != nil { - err = fmt.Errorf("In CreateK8sCluster(); CBStore.Get() returned an error: " + err.Error()) + err = fmt.Errorf("In CreateK8sCluster(); kvstore.GetKv() returned an error: " + err.Error()) log.Err(err).Msg("") } @@ -742,13 +743,13 @@ func AddK8sNodeGroup(nsId string, k8sClusterId string, u *TbK8sNodeGroupReq) (Tb } /* - * Get TbK8sClusterInfo from cb-store + * Get TbK8sClusterInfo from kvstore */ oldTbK8sCInfo := TbK8sClusterInfo{} k := GenK8sClusterKey(nsId, k8sClusterId) - kv, err := common.CBStore.Get(k) + kv, err := kvstore.GetKv(k) if err != nil { - err = fmt.Errorf("In AddK8sNodeGroup(); CBStore.Get() returned an error: " + err.Error()) + err = fmt.Errorf("In AddK8sNodeGroup(); kvstore.GetKv() returned an error: " + err.Error()) log.Err(err).Msg("Failed to Add K8sNodeGroup") return emptyObj, err } @@ -853,20 +854,20 @@ func AddK8sNodeGroup(nsId string, k8sClusterId string, u *TbK8sNodeGroupReq) (Tb newTbK8sCInfo := convertSpiderClusterInfoToTbK8sClusterInfo(&spClusterRes.ClusterInfo, oldTbK8sCInfo.Id, oldTbK8sCInfo.ConnectionName, oldTbK8sCInfo.Description) /* - * Put/Get TbK8sClusterInfo to/from cb-store + * Put/Get TbK8sClusterInfo to/from kvstore */ k = GenK8sClusterKey(nsId, newTbK8sCInfo.Id) Val, _ := json.Marshal(newTbK8sCInfo) - err = common.CBStore.Put(k, string(Val)) + err = kvstore.Put(k, string(Val)) if err != nil { log.Err(err).Msg("Failed to Add K8sNodeGroup") return newTbK8sCInfo, err } - kv, err = common.CBStore.Get(k) + kv, err = kvstore.GetKv(k) if err != nil { - err = fmt.Errorf("In AddK8sNodeGroup(); CBStore.Get() returned an error: " + err.Error()) + err = fmt.Errorf("In AddK8sNodeGroup(); kvstore.GetKv() returned an error: " + err.Error()) log.Err(err).Msg("") // return nil, err } @@ -913,7 +914,7 @@ func RemoveK8sNodeGroup(nsId string, k8sClusterId string, k8sNodeGroupName strin k := GenK8sClusterKey(nsId, k8sClusterId) log.Debug().Msg("key: " + k) - kv, _ := common.CBStore.Get(k) + kv, _ := kvstore.GetKv(k) // Create Req body type JsonTemplate struct { @@ -1006,13 +1007,13 @@ func SetK8sNodeGroupAutoscaling(nsId string, k8sClusterId string, k8sNodeGroupNa } /* - * Get TbK8sClusterInfo object from cb-store + * Get TbK8sClusterInfo object from kvstore */ k := GenK8sClusterKey(nsId, k8sClusterId) log.Debug().Msg("key: " + k) - kv, _ := common.CBStore.Get(k) + kv, _ := kvstore.GetKv(k) tbK8sCInfo := TbK8sClusterInfo{} err = json.Unmarshal([]byte(kv.Value), &tbK8sCInfo) @@ -1093,13 +1094,13 @@ func ChangeK8sNodeGroupAutoscaleSize(nsId string, k8sClusterId string, k8sNodeGr } /* - * Get TbK8sClusterInfo object from cb-store + * Get TbK8sClusterInfo object from kvstore */ k := GenK8sClusterKey(nsId, k8sClusterId) log.Debug().Msg("key: " + k) - kv, _ := common.CBStore.Get(k) + kv, _ := kvstore.GetKv(k) tbK8sCInfo := TbK8sClusterInfo{} err = json.Unmarshal([]byte(kv.Value), &tbK8sCInfo) @@ -1176,18 +1177,18 @@ func GetK8sCluster(nsId string, k8sClusterId string) (TbK8sClusterInfo, error) { log.Debug().Msg("[Get K8sCluster] " + k8sClusterId) /* - * Get TbK8sClusterInfo object from cb-store + * Get TbK8sClusterInfo object from kvstore */ k := GenK8sClusterKey(nsId, k8sClusterId) - kv, err := common.CBStore.Get(k) + kv, err := kvstore.GetKv(k) if err != nil { log.Error().Err(err).Msg("") return emptyObj, err } storedTbK8sCInfo := TbK8sClusterInfo{} - if kv == nil { + if kv == (kvstore.KeyValue{}) { err = fmt.Errorf("Cannot get the k8s cluster " + k8sClusterId + ".") log.Err(err).Msg("Failed to Get K8sCluster") return storedTbK8sCInfo, err @@ -1296,12 +1297,12 @@ func CheckK8sCluster(nsId string, k8sClusterId string) (bool, error) { key := GenK8sClusterKey(nsId, k8sClusterId) - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Err(err).Msg("Failed to Check K8sCluster") return false, err } - if keyValue != nil { + if keyValue != (kvstore.KeyValue{}) { return true, nil } return false, nil @@ -1338,7 +1339,7 @@ func ListK8sClusterId(nsId string) ([]string, error) { k := fmt.Sprintf("/ns/%s/", nsId) log.Debug().Msg(k) - kv, err := common.CBStore.GetList(k, true) + kv, err := kvstore.GetKvList(k) if err != nil { log.Error().Err(err).Msg("Failed to Get K8sClusterId List") @@ -1380,11 +1381,11 @@ func ListK8sCluster(nsId string, filterKey string, filterVal string) (interface{ log.Debug().Msg(k) /* - * Get TbK8sClusterInfo objects from cb-store + * Get TbK8sClusterInfo objects from kvstore */ - kv, err := common.CBStore.GetList(k, true) - kv = cbstore_utils.GetChildList(kv, k) + kv, err := kvstore.GetKvList(k) + kv = kvutil.FilterKvListBy(kv, k, 1) if err != nil { log.Err(err).Msg("Failed to List K8sCluster") @@ -1447,13 +1448,13 @@ func DeleteK8sCluster(nsId string, k8sClusterId string, forceFlag string) (bool, } /* - * Get TbK8sClusterInfo object from cb-store + * Get TbK8sClusterInfo object from kvstore */ k := GenK8sClusterKey(nsId, k8sClusterId) log.Debug().Msg("key: " + k) - kv, _ := common.CBStore.Get(k) + kv, _ := kvstore.GetKv(k) // Create Req body type JsonTemplate struct { @@ -1492,7 +1493,7 @@ func DeleteK8sCluster(nsId string, k8sClusterId string, forceFlag string) (bool, ) if forceFlag == "true" { - err = common.CBStore.Delete(k) + err = kvstore.Delete(k) if err != nil { log.Err(err).Msg("Failed to Delete K8sCluster") return false, err @@ -1509,7 +1510,7 @@ func DeleteK8sCluster(nsId string, k8sClusterId string, forceFlag string) (bool, result := mapRes["Result"] if result == "true" { if forceFlag != "true" { - err = common.CBStore.Delete(k) + err = kvstore.Delete(k) if err != nil { log.Err(err).Msg("Failed to Delete K8sCluster") return false, err @@ -1589,13 +1590,13 @@ func UpgradeK8sCluster(nsId string, k8sClusterId string, u *TbUpgradeK8sClusterR } /* - * Get TbK8sClusterInfo from cb-store + * Get TbK8sClusterInfo from kvstore */ oldTbK8sCInfo := TbK8sClusterInfo{} k := GenK8sClusterKey(nsId, k8sClusterId) - kv, err := common.CBStore.Get(k) + kv, err := kvstore.GetKv(k) if err != nil { - err = fmt.Errorf("In UpgradeK8sCluster(); CBStore.Get() returned an error: " + err.Error()) + err = fmt.Errorf("In UpgradeK8sCluster(); kvstore.GetKv() returned an error: " + err.Error()) log.Err(err).Msg("Failed to Upgrade a K8sCluster") return emptyObj, err } @@ -1666,20 +1667,20 @@ func UpgradeK8sCluster(nsId string, k8sClusterId string, u *TbUpgradeK8sClusterR newTbK8sCInfo := convertSpiderClusterInfoToTbK8sClusterInfo(&spClusterRes.ClusterInfo, oldTbK8sCInfo.Id, oldTbK8sCInfo.ConnectionName, oldTbK8sCInfo.Description) /* - * Put/Get TbK8sClusterInfo to/from cb-store + * Put/Get TbK8sClusterInfo to/from kvstore */ k = GenK8sClusterKey(nsId, newTbK8sCInfo.Id) Val, _ := json.Marshal(newTbK8sCInfo) - err = common.CBStore.Put(k, string(Val)) + err = kvstore.Put(k, string(Val)) if err != nil { log.Err(err).Msg("Failed to Upgrade a K8sCluster") return emptyObj, err } - kv, err = common.CBStore.Get(k) + kv, err = kvstore.GetKv(k) if err != nil { - err = fmt.Errorf("In UpgradeK8sCluster(); CBStore.Get() returned an error: " + err.Error()) + err = fmt.Errorf("In UpgradeK8sCluster(); kvstore.GetKv() returned an error: " + err.Error()) log.Err(err).Msg("") // return nil, err } diff --git a/src/core/mcis/manageInfo.go b/src/core/mcis/manageInfo.go index 9f0848809..c20087392 100644 --- a/src/core/mcis/manageInfo.go +++ b/src/core/mcis/manageInfo.go @@ -35,6 +35,7 @@ import ( "github.com/cloud-barista/cb-tumblebug/src/core/common" "github.com/cloud-barista/cb-tumblebug/src/core/mcir" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" "github.com/go-resty/resty/v2" "github.com/rs/zerolog/log" ) @@ -109,7 +110,7 @@ func ListMcisId(nsId string) ([]string, error) { key := common.GenMcisKey(nsId, "", "") key += "/" - keyValue, err := common.CBStore.GetList(key, true) + keyValue, err := kvstore.GetKvList(key) if err != nil { log.Error().Err(err).Msg("") @@ -150,14 +151,14 @@ func ListVmId(nsId string, mcisId string) ([]string, error) { key := common.GenMcisKey(nsId, mcisId, "") key += "/" - _, err = common.CBStore.Get(key) + _, err = kvstore.GetKv(key) if err != nil { log.Debug().Msg("[Not found] " + mcisId) log.Error().Err(err).Msg("") return vmList, err } - keyValue, err := common.CBStore.GetList(key, true) + keyValue, err := kvstore.GetKvList(key) if err != nil { log.Error().Err(err).Msg("") @@ -289,7 +290,7 @@ func ListSubGroupId(nsId string, mcisId string) ([]string, error) { key := common.GenMcisKey(nsId, mcisId, "") key += "/" - keyValue, err := common.CBStore.GetList(key, true) + keyValue, err := kvstore.GetKvList(key) if err != nil { log.Error().Err(err).Msg("") return nil, err @@ -501,15 +502,15 @@ func ListMcisInfo(nsId string, option string) ([]TbMcisInfo, error) { for _, v := range mcisList { key := common.GenMcisKey(nsId, v, "") - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") - err = fmt.Errorf("In CoreGetAllMcis(); CBStore.Get() returned an error.") + err = fmt.Errorf("In CoreGetAllMcis(); kvstore.GetKv() returned an error.") log.Error().Err(err).Msg("") // return nil, err } - if keyValue == nil { + if keyValue == (kvstore.KeyValue{}) { return nil, fmt.Errorf("in CoreGetAllMcis() mcis loop; Cannot find " + key) } mcisTmp := TbMcisInfo{} @@ -540,14 +541,14 @@ func ListMcisInfo(nsId string, option string) ([]TbMcisInfo, error) { for _, v1 := range vmList { vmKey := common.GenMcisKey(nsId, mcisId, v1) - vmKeyValue, err := common.CBStore.Get(vmKey) + vmKeyValue, err := kvstore.GetKv(key) if err != nil { - err = fmt.Errorf("In CoreGetAllMcis(); CBStore.Get() returned an error") + err = fmt.Errorf("In CoreGetAllMcis(); kvstore.GetKv() returned an error") log.Error().Err(err).Msg("") // return nil, err } - if vmKeyValue == nil { + if vmKeyValue == (kvstore.KeyValue{}) { return nil, fmt.Errorf("in CoreGetAllMcis() vm loop; Cannot find " + vmKey) } vmTmp := TbVmInfo{} @@ -616,15 +617,15 @@ func ListVmInfo(nsId string, mcisId string, vmId string) (*TbVmInfo, error) { key := common.GenMcisKey(nsId, mcisId, "") vmKey := common.GenMcisKey(nsId, mcisId, vmId) - vmKeyValue, err := common.CBStore.Get(vmKey) + vmKeyValue, err := kvstore.GetKv(vmKey) if err != nil { log.Error().Err(err).Msg("") - err = fmt.Errorf("In CoreGetMcisVmInfo(); CBStore.Get() returned an error.") + err = fmt.Errorf("In CoreGetMcisVmInfo(); kvstore.GetKv() returned an error.") log.Error().Err(err).Msg("") // return nil, err } - if vmKeyValue == nil { + if vmKeyValue == (kvstore.KeyValue{}) { return nil, fmt.Errorf("Cannot find " + key) } vmTmp := TbVmInfo{} @@ -648,7 +649,7 @@ func ListVmInfo(nsId string, mcisId string, vmId string) (*TbVmInfo, error) { func GetMcisObject(nsId string, mcisId string) (TbMcisInfo, error) { log.Debug().Msg("[GetMcisObject]" + mcisId) key := common.GenMcisKey(nsId, mcisId, "") - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") return TbMcisInfo{}, err @@ -677,8 +678,8 @@ func GetMcisObject(nsId string, mcisId string) (TbMcisInfo, error) { // GetVmObject is func to get VM object func GetVmObject(nsId string, mcisId string, vmId string) (TbVmInfo, error) { key := common.GenMcisKey(nsId, mcisId, vmId) - keyValue, err := common.CBStore.Get(key) - if keyValue == nil || err != nil { + keyValue, err := kvstore.GetKv(key) + if keyValue == (kvstore.KeyValue{}) || err != nil { err = fmt.Errorf("failed to get GetVmObject (ID: %s)", key) log.Error().Err(err).Msg("") return TbVmInfo{}, err @@ -696,8 +697,8 @@ func GetVmObject(nsId string, mcisId string, vmId string) (TbVmInfo, error) { // GetVmIdNameInDetail is func to get ID and Name details func GetVmIdNameInDetail(nsId string, mcisId string, vmId string) (*TbIdNameInDetailInfo, error) { key := common.GenMcisKey(nsId, mcisId, vmId) - keyValue, err := common.CBStore.Get(key) - if keyValue == nil || err != nil { + keyValue, err := kvstore.GetKv(key) + if keyValue == (kvstore.KeyValue{}) || err != nil { log.Error().Err(err).Msg("") return &TbIdNameInDetailInfo{}, err } @@ -770,12 +771,12 @@ func GetMcisStatus(nsId string, mcisId string) (*McisStatusInfo, error) { key := common.GenMcisKey(nsId, mcisId, "") - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") return &McisStatusInfo{}, err } - if keyValue == nil { + if keyValue == (kvstore.KeyValue{}) { err := fmt.Errorf("Not found [" + key + "]") log.Error().Err(err).Msg("") return &McisStatusInfo{}, err @@ -968,9 +969,9 @@ func GetVmCurrentPublicIp(nsId string, mcisId string, vmId string) (TbVmStatusIn errorInfo.Status = StatusFailed key := common.GenMcisKey(nsId, mcisId, vmId) - keyValue, err := common.CBStore.Get(key) - if err != nil || keyValue == nil { - if keyValue == nil { + keyValue, err := kvstore.GetKv(key) + if err != nil || keyValue == (kvstore.KeyValue{}) { + if keyValue == (kvstore.KeyValue{}) { log.Error().Err(err).Msgf("Not found: %s keyValue is nil", key) return errorInfo, fmt.Errorf("Not found: %s keyValue is nil", key) } @@ -1055,10 +1056,10 @@ func GetVmSpecId(nsId string, mcisId string, vmId string) string { log.Debug().Msg("[getVmSpecID]" + vmId) key := common.GenMcisKey(nsId, mcisId, vmId) - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") - err = fmt.Errorf("In GetVmSpecId(); CBStore.Get() returned an error.") + err = fmt.Errorf("In GetVmSpecId(); kvstore.GetKv() returned an error.") log.Error().Err(err).Msg("") // return nil, err } @@ -1347,8 +1348,8 @@ func UpdateMcisInfo(nsId string, mcisInfoData TbMcisInfo) { key := common.GenMcisKey(nsId, mcisInfoData.Id, "") // Check existence of the key. If no key, no update. - keyValue, err := common.CBStore.Get(key) - if keyValue == nil || err != nil { + keyValue, err := kvstore.GetKv(key) + if keyValue == (kvstore.KeyValue{}) || err != nil { return } @@ -1357,7 +1358,7 @@ func UpdateMcisInfo(nsId string, mcisInfoData TbMcisInfo) { if !reflect.DeepEqual(mcisTmp, mcisInfoData) { val, _ := json.Marshal(mcisInfoData) - err = common.CBStore.Put(key, string(val)) + err = kvstore.Put(key, string(val)) if err != nil { log.Error().Err(err).Msg("") } @@ -1369,8 +1370,8 @@ func UpdateVmInfo(nsId string, mcisId string, vmInfoData TbVmInfo) { key := common.GenMcisKey(nsId, mcisId, vmInfoData.Id) // Check existence of the key. If no key, no update. - keyValue, err := common.CBStore.Get(key) - if keyValue == nil || err != nil { + keyValue, err := kvstore.GetKv(key) + if keyValue == (kvstore.KeyValue{}) || err != nil { return } @@ -1379,7 +1380,7 @@ func UpdateVmInfo(nsId string, mcisId string, vmInfoData TbVmInfo) { if !reflect.DeepEqual(vmTmp, vmInfoData) { val, _ := json.Marshal(vmInfoData) - err = common.CBStore.Put(key, string(val)) + err = kvstore.Put(key, string(val)) if err != nil { log.Error().Err(err).Msg("") } @@ -1425,8 +1426,8 @@ func AttachDetachDataDisk(nsId string, mcisId string, vmId string, command strin vmKey := common.GenMcisKey(nsId, mcisId, vmId) // Check existence of the key. If no key, no update. - keyValue, err := common.CBStore.Get(vmKey) - if keyValue == nil || err != nil { + keyValue, err := kvstore.GetKv(vmKey) + if keyValue == (kvstore.KeyValue{}) || err != nil { err := fmt.Errorf("Failed to find 'ns/mcis/vm': %s/%s/%s \n", nsId, mcisId, vmId) log.Error().Err(err).Msg("") return TbVmInfo{}, err @@ -1449,8 +1450,8 @@ func AttachDetachDataDisk(nsId string, mcisId string, vmId string, command strin dataDiskKey := common.GenResourceKey(nsId, common.StrDataDisk, dataDiskId) // Check existence of the key. If no key, no update. - keyValue, err = common.CBStore.Get(dataDiskKey) - if keyValue == nil || err != nil { + keyValue, err = kvstore.GetKv(dataDiskKey) + if keyValue == (kvstore.KeyValue{}) || err != nil { return TbVmInfo{}, err } @@ -1603,8 +1604,8 @@ func GetAvailableDataDisks(nsId string, mcisId string, vmId string, option strin vmKey := common.GenMcisKey(nsId, mcisId, vmId) // Check existence of the key. If no key, no update. - keyValue, err := common.CBStore.Get(vmKey) - if keyValue == nil || err != nil { + keyValue, err := kvstore.GetKv(vmKey) + if keyValue == (kvstore.KeyValue{}) || err != nil { err := fmt.Errorf("Failed to find 'ns/mcis/vm': %s/%s/%s \n", nsId, mcisId, vmId) log.Error().Err(err).Msg("") return nil, err @@ -1759,7 +1760,7 @@ func DelMcis(nsId string, mcisId string, option string) (common.IdList, error) { return deletedResources, err } - err = common.CBStore.Delete(vmKey) + err = kvstore.Delete(vmKey) if err != nil { log.Error().Err(err).Msg("") return deletedResources, err @@ -1792,7 +1793,7 @@ func DelMcis(nsId string, mcisId string, option string) (common.IdList, error) { } for _, v := range subGroupList { subGroupKey := common.GenMcisSubGroupKey(nsId, mcisId, v) - err := common.CBStore.Delete(subGroupKey) + err := kvstore.Delete(subGroupKey) if err != nil { log.Error().Err(err).Msg("") return deletedResources, err @@ -1825,7 +1826,7 @@ func DelMcis(nsId string, mcisId string, option string) (common.IdList, error) { } // delete mcis info - err = common.CBStore.Delete(key) + err = kvstore.Delete(key) if err != nil { log.Error().Err(err).Msg("") return deletedResources, err @@ -1892,7 +1893,7 @@ func DelMcisVm(nsId string, mcisId string, vmId string, option string) error { // delete vms info key := common.GenMcisKey(nsId, mcisId, vmId) - err = common.CBStore.Delete(key) + err = kvstore.Delete(key) if err != nil { log.Error().Err(err).Msg("") return err diff --git a/src/core/mcis/nlb.go b/src/core/mcis/nlb.go index 051c7921c..636f855c3 100644 --- a/src/core/mcis/nlb.go +++ b/src/core/mcis/nlb.go @@ -22,9 +22,10 @@ import ( "strings" "time" - cbstore_utils "github.com/cloud-barista/cb-store/utils" "github.com/cloud-barista/cb-tumblebug/src/core/common" "github.com/cloud-barista/cb-tumblebug/src/core/mcir" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvutil" validator "github.com/go-playground/validator/v10" "github.com/go-resty/resty/v2" "github.com/rs/zerolog/log" @@ -624,21 +625,21 @@ func CreateNLB(nsId string, mcisId string, u *TbNLBReq, option string) (TbNLBInf content.SystemLabel = "Registered from CSP resource" } - // cb-store + // kvstore // Key := common.GenResourceKey(nsId, common.StrNLB, content.Id) Key := GenNLBKey(nsId, mcisId, content.Id) Val, _ := json.Marshal(content) - err = common.CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("") return content, err } - keyValue, err := common.CBStore.Get(Key) + keyValue, err := kvstore.GetKv(Key) if err != nil { log.Error().Err(err).Msg("") - err = fmt.Errorf("In CreateNLB(); CBStore.Get() returned an error.") + err = fmt.Errorf("In CreateNLB(); kvstore.GetKv() returned an error.") log.Error().Err(err).Msg("") // return nil, err } @@ -690,7 +691,7 @@ func GetNLB(nsId string, mcisId string, resourceId string) (TbNLBInfo, error) { // key := common.GenResourceKey(nsId, resourceType, resourceId) key := GenNLBKey(nsId, mcisId, resourceId) - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") return emptyObj, err @@ -698,7 +699,7 @@ func GetNLB(nsId string, mcisId string, resourceId string) (TbNLBInfo, error) { res := TbNLBInfo{} - if keyValue != nil { + if keyValue != (kvstore.KeyValue{}) { err = json.Unmarshal([]byte(keyValue.Value), &res) if err != nil { log.Error().Err(err).Msg("") @@ -750,12 +751,12 @@ func CheckNLB(nsId string, mcisId string, resourceId string) (bool, error) { // key := common.GenResourceKey(nsId, resourceType, resourceId) key := GenNLBKey(nsId, mcisId, resourceId) - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") return false, err } - if keyValue != nil { + if keyValue != (kvstore.KeyValue{}) { return true, nil } return false, nil @@ -805,7 +806,7 @@ func ListNLBId(nsId string, mcisId string) ([]string, error) { key := fmt.Sprintf("/ns/%s/mcis/%s/", nsId, mcisId) fmt.Println(key) - keyValue, err := common.CBStore.GetList(key, true) + keyValue, err := kvstore.GetKvList(key) if err != nil { log.Error().Err(err).Msg("") @@ -852,8 +853,8 @@ func ListNLB(nsId string, mcisId string, filterKey string, filterVal string) (in key := fmt.Sprintf("/ns/%s/mcis/%s/nlb", nsId, mcisId) fmt.Println(key) - keyValue, err := common.CBStore.GetList(key, true) - keyValue = cbstore_utils.GetChildList(keyValue, key) + keyValue, err := kvstore.GetKvList(key) + keyValue = kvutil.FilterKvListBy(keyValue, key, 1) if err != nil { log.Error().Err(err).Msg("") @@ -927,8 +928,8 @@ func DelNLB(nsId string, mcisId string, resourceId string, forceFlag string) err key := GenNLBKey(nsId, mcisId, resourceId) fmt.Println("key: " + key) - keyValue, _ := common.CBStore.Get(key) - // In CheckResource() above, calling 'CBStore.Get()' and checking err parts exist. + keyValue, _ := kvstore.GetKv(key) + // In CheckResource() above, calling 'kvstore.GetKv()' and checking err parts exist. // So, in here, we don't need to check whether keyValue == nil or err != nil. // Deleting NLB should be possible, even if backend VMs still exist. @@ -1011,7 +1012,7 @@ func DelNLB(nsId string, mcisId string, resourceId string, forceFlag string) err } - err = common.CBStore.Delete(key) + err = kvstore.Delete(key) if err != nil { log.Error().Err(err).Msg("") return err @@ -1172,21 +1173,21 @@ func GetNLBHealth(nsId string, mcisId string, nlbId string) (TbNLBHealthInfo, er result.AllVMs = append(result.AllVMs, result.HealthyVMs...) result.AllVMs = append(result.AllVMs, result.UnHealthyVMs...) /* - // cb-store + // kvstore // Key := common.GenResourceKey(nsId, common.StrNLB, content.Id) Key := GenNLBKey(nsId, mcisId, content.Id) Val, _ := json.Marshal(content) - err = common.CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("") return content, err } - keyValue, err := common.CBStore.Get(Key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") - err = fmt.Errorf("In CreateNLB(); CBStore.Get() returned an error.") + err = fmt.Errorf("In CreateNLB(); kvstore.GetKv() returned an error.") log.Error().Err(err).Msg("") // return nil, err } @@ -1355,21 +1356,21 @@ func AddNLBVMs(nsId string, mcisId string, resourceId string, u *TbNLBAddRemoveV content.TargetGroup.VMs = append(content.TargetGroup.VMs, nlb.TargetGroup.VMs...) content.TargetGroup.VMs = append(content.TargetGroup.VMs, u.TargetGroup.VMs...) - // cb-store + // kvstore // Key := common.GenResourceKey(nsId, common.StrNLB, content.Id) Key := GenNLBKey(nsId, mcisId, content.Id) Val, _ := json.Marshal(content) - err = common.CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("") return content, err } - keyValue, err := common.CBStore.Get(Key) + keyValue, err := kvstore.GetKv(Key) if err != nil { log.Error().Err(err).Msg("") - err = fmt.Errorf("In CreateNLB(); CBStore.Get() returned an error.") + err = fmt.Errorf("In CreateNLB(); kvstore.GetKv() returned an error.") log.Error().Err(err).Msg("") // return nil, err } @@ -1557,12 +1558,12 @@ func RemoveNLBVMs(nsId string, mcisId string, resourceId string, u *TbNLBAddRemo nlb.TargetGroup.VMs = newVMList - // cb-store + // kvstore // Key := common.GenResourceKey(nsId, common.StrNLB, content.Id) Key := GenNLBKey(nsId, mcisId, nlb.Id) Val, _ := json.Marshal(nlb) - err = common.CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("") return err diff --git a/src/core/mcis/orchestration.go b/src/core/mcis/orchestration.go index b354d0cd0..57c911a8e 100644 --- a/src/core/mcis/orchestration.go +++ b/src/core/mcis/orchestration.go @@ -21,6 +21,7 @@ import ( "strings" "github.com/cloud-barista/cb-tumblebug/src/core/common" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" "github.com/rs/zerolog/log" ) @@ -126,15 +127,15 @@ func OrchestrationController() { for _, v := range mcisPolicyList { key := common.GenMcisPolicyKey(nsId, v, "") - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") - err = fmt.Errorf("In OrchestrationController(); CBStore.Get() returned an error.") + err = fmt.Errorf("In OrchestrationController(); kvstore.GetKv() returned an error.") log.Error().Err(err).Msg("") // return nil, err } - if keyValue == nil { + if keyValue == (kvstore.KeyValue{}) { log.Debug().Msg("keyValue is nil") } mcisPolicyTmp := McisPolicyInfo{} @@ -163,7 +164,7 @@ func OrchestrationController() { log.Debug().Msg("[Check MCIS Policy] " + mcisPolicyTmp.Id) check, err := CheckMcis(nsId, mcisPolicyTmp.Id) log.Debug().Msg("[Check existence of MCIS] " + mcisPolicyTmp.Id) - //keyValueMcis, _ := common.CBStore.Get(common.GenMcisKey(nsId, mcisPolicyTmp.Id, "")) + //keyValueMcis, _ := common.Ckvstore.GetKv(common.GenMcisKey(nsId, mcisPolicyTmp.Id, "")) if !check || err != nil { mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusError @@ -427,8 +428,8 @@ func OrchestrationController() { func UpdateMcisPolicyInfo(nsId string, mcisPolicyInfoData McisPolicyInfo) { key := common.GenMcisPolicyKey(nsId, mcisPolicyInfoData.Id, "") val, _ := json.Marshal(mcisPolicyInfoData) - err := common.CBStore.Put(key, string(val)) - if err != nil && !strings.Contains(err.Error(), common.CbStoreKeyNotFoundErrorString) { + err := kvstore.Put(key, string(val)) + if err != nil && !strings.Contains(err.Error(), common.ErrStrKeyNotFound) { log.Error().Err(err).Msg("") } } @@ -470,19 +471,19 @@ func CreateMcisPolicy(nsId string, mcisId string, u *McisPolicyReq) (McisPolicyI obj.Policy = req.Policy obj.Description = req.Description - // cb-store + // kvstore Key := common.GenMcisPolicyKey(nsId, obj.Id, "") Val, _ := json.Marshal(obj) - err = common.CBStore.Put(Key, string(Val)) + err = kvstore.Put(Key, string(Val)) if err != nil { log.Error().Err(err).Msg("") return obj, err } - keyValue, err := common.CBStore.Get(Key) + keyValue, err := kvstore.GetKv(Key) if err != nil { log.Error().Err(err).Msg("") - err = fmt.Errorf("In CreateMcisPolicy(); CBStore.Get() returned an error.") + err = fmt.Errorf("In CreateMcisPolicy(); kvstore.GetKv() returned an error.") log.Error().Err(err).Msg("") // return nil, err } @@ -498,12 +499,12 @@ func GetMcisPolicyObject(nsId string, mcisId string) (McisPolicyInfo, error) { key := common.GenMcisPolicyKey(nsId, mcisId, "") log.Debug().Msgf("Key: %v", key) - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") return McisPolicyInfo{}, err } - if keyValue == nil { + if keyValue == (kvstore.KeyValue{}) { return McisPolicyInfo{}, err } @@ -528,15 +529,15 @@ func GetAllMcisPolicyObject(nsId string) ([]McisPolicyInfo, error) { for _, v := range mcisList { key := common.GenMcisPolicyKey(nsId, v, "") - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") - err = fmt.Errorf("In GetAllMcisPolicyObject(); CBStore.Get() returned an error.") + err = fmt.Errorf("In GetAllMcisPolicyObject(); kvstore.GetKv() returned an error.") log.Error().Err(err).Msg("") // return nil, err } - if keyValue == nil { + if keyValue == (kvstore.KeyValue{}) { return nil, fmt.Errorf("Cannot find " + key) } mcisTmp := McisPolicyInfo{} @@ -557,10 +558,10 @@ func ListMcisPolicyId(nsId string) []string { } key := "/ns/" + nsId + "/policy/mcis" - keyValue, err := common.CBStore.GetList(key, true) + keyValue, err := kvstore.GetKvList(key) if err != nil { log.Error().Err(err).Msg("") - err = fmt.Errorf("In ListMcisPolicyId(); CBStore.Get() returned an error.") + err = fmt.Errorf("In ListMcisPolicyId(); kvstore.GetKv() returned an error.") log.Error().Err(err).Msg("") // return nil, err } @@ -601,7 +602,7 @@ func DelMcisPolicy(nsId string, mcisId string) error { log.Debug().Msg(key) // delete mcis Policy info - err = common.CBStore.Delete(key) + err = kvstore.Delete(key) if err != nil { log.Error().Err(err).Msg("") return err diff --git a/src/core/mcis/provisioning.go b/src/core/mcis/provisioning.go index 6f8d50bc3..6d2ed6135 100644 --- a/src/core/mcis/provisioning.go +++ b/src/core/mcis/provisioning.go @@ -24,6 +24,7 @@ import ( "github.com/cloud-barista/cb-tumblebug/src/core/common" "github.com/cloud-barista/cb-tumblebug/src/core/mcir" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" validator "github.com/go-playground/validator/v10" "github.com/go-resty/resty/v2" "github.com/rs/zerolog/log" @@ -729,17 +730,17 @@ func CreateMcisGroupVm(nsId string, mcisId string, vmRequest *TbVmReq, newSubGro subGroupInfoData.SubGroupSize = vmRequest.SubGroupSize key := common.GenMcisSubGroupKey(nsId, mcisId, vmRequest.Name) - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { - err = fmt.Errorf("In CreateMcisGroupVm(); CBStore.Get(): " + err.Error()) + err = fmt.Errorf("In CreateMcisGroupVm(); kvstore.GetKv(): " + err.Error()) log.Error().Err(err).Msg("") } - if keyValue != nil { + if keyValue != (kvstore.KeyValue{}) { if newSubGroup { json.Unmarshal([]byte(keyValue.Value), &subGroupInfoData) existingVmSize, err := strconv.Atoi(subGroupInfoData.SubGroupSize) if err != nil { - err = fmt.Errorf("In CreateMcisGroupVm(); CBStore.Get(): " + err.Error()) + err = fmt.Errorf("In CreateMcisGroupVm(); kvstore.GetKv(): " + err.Error()) log.Error().Err(err).Msg("") } // add the number of existing VMs in the SubGroup with requested number for additions @@ -757,14 +758,14 @@ func CreateMcisGroupVm(nsId string, mcisId string, vmRequest *TbVmReq, newSubGro } val, _ := json.Marshal(subGroupInfoData) - err = common.CBStore.Put(key, string(val)) + err = kvstore.Put(key, string(val)) if err != nil { log.Error().Err(err).Msg("") } // check stored subGroup object - keyValue, err = common.CBStore.Get(key) + keyValue, err = kvstore.GetKv(key) if err != nil { - err = fmt.Errorf("In CreateMcisGroupVm(); CBStore.Get(): " + err.Error()) + err = fmt.Errorf("In CreateMcisGroupVm(); kvstore.GetKv(): " + err.Error()) log.Error().Err(err).Msg("") // return nil, err } @@ -942,9 +943,9 @@ func CreateMcis(nsId string, req *TbMcisReq, option string) (*TbMcisInfo, error) return nil, err } - err = common.CBStore.Put(key, string(val)) + err = kvstore.Put(key, string(val)) if err != nil { - err := fmt.Errorf("System Error: CreateMcis CBStore.Put Error") + err := fmt.Errorf("System Error: CreateMcis kvstore.Put Error") log.Error().Err(err).Msg("") return nil, err } @@ -1013,7 +1014,7 @@ func CreateMcis(nsId string, req *TbMcisReq, option string) (*TbMcisInfo, error) } val, _ := json.Marshal(subGroupInfoData) - err := common.CBStore.Put(key, string(val)) + err := kvstore.Put(key, string(val)) if err != nil { log.Error().Err(err).Msg("") } @@ -1556,19 +1557,19 @@ func AddVmToMcis(wg *sync.WaitGroup, nsId string, mcisId string, vmInfoData *TbV defer wg.Done() key := common.GenMcisKey(nsId, mcisId, "") - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { - log.Fatal().Err(err).Msg("AddVmToMcis(); CBStore.Get() returned an error.") + log.Fatal().Err(err).Msg("AddVmToMcis(); kvstore.GetKv() returned an error.") return err } - if keyValue == nil { + if keyValue == (kvstore.KeyValue{}) { return fmt.Errorf("AddVmToMcis: Cannot find mcisId. Key: %s", key) } // Make VM object key = common.GenMcisKey(nsId, mcisId, vmInfoData.Id) val, _ := json.Marshal(vmInfoData) - err = common.CBStore.Put(key, string(val)) + err = kvstore.Put(key, string(val)) if err != nil { log.Error().Err(err).Msg("") return err @@ -1584,7 +1585,7 @@ func AddVmToMcis(wg *sync.WaitGroup, nsId string, mcisId string, vmInfoData *TbV //AddVmInfoToMcis(nsId, mcisId, *vmInfoData) // Update VM object val, _ = json.Marshal(vmInfoData) - err = common.CBStore.Put(key, string(val)) + err = kvstore.Put(key, string(val)) if err != nil { log.Error().Err(err).Msg("") return err diff --git a/src/core/mcis/recommendation.go b/src/core/mcis/recommendation.go index a31a38f04..2667cd4be 100644 --- a/src/core/mcis/recommendation.go +++ b/src/core/mcis/recommendation.go @@ -29,9 +29,9 @@ import ( "github.com/cloud-barista/cb-tumblebug/src/core/common" "github.com/cloud-barista/cb-tumblebug/src/core/mcir" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvutil" "github.com/rs/zerolog/log" - - cbstore_utils "github.com/cloud-barista/cb-store/utils" ) // DeploymentPlan is struct for . @@ -663,8 +663,8 @@ func GetRecommendList(nsId string, cpuSize string, memSize string, diskSize stri key := common.GenMcisKey(nsId, "", "") + "/cpuSize/" + cpuSize + "/memSize/" + memSize + "/diskSize/" + diskSize log.Debug().Msg(key) - keyValue, err := common.CBStore.GetList(key, true) - keyValue = cbstore_utils.GetChildList(keyValue, key) + keyValue, err := kvstore.GetKvList(key) + keyValue = kvutil.FilterKvListBy(keyValue, key, 1) if err != nil { log.Error().Err(err).Msg("") return []TbVmPriority{}, err @@ -683,7 +683,7 @@ func GetRecommendList(nsId string, cpuSize string, memSize string, diskSize stri content2 := mcir.TbSpecInfo{} key2 := common.GenResourceKey(nsId, common.StrSpec, content.Id) - keyValue2, err := common.CBStore.Get(key2) + keyValue2, err := kvstore.GetKv(key2) if err != nil { log.Error().Err(err).Msg("") return []TbVmPriority{}, err diff --git a/src/core/mcis/remoteCommand.go b/src/core/mcis/remoteCommand.go index 3e93a2d99..875c66429 100644 --- a/src/core/mcis/remoteCommand.go +++ b/src/core/mcis/remoteCommand.go @@ -29,6 +29,7 @@ import ( "github.com/cloud-barista/cb-tumblebug/src/core/common" "github.com/cloud-barista/cb-tumblebug/src/core/mcir" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" validator "github.com/go-playground/validator/v10" "github.com/rs/zerolog/log" "golang.org/x/crypto/ssh" @@ -394,7 +395,7 @@ func GetVmSshKey(nsId string, mcisId string, vmId string) (string, string, strin key := common.GenMcisKey(nsId, mcisId, vmId) - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") err = fmt.Errorf("Cannot find the key from DB. key: " + key) @@ -408,8 +409,8 @@ func GetVmSshKey(nsId string, mcisId string, vmId string) (string, string, strin } sshKey := common.GenResourceKey(nsId, common.StrSSHKey, content.SshKeyId) - keyValue, err = common.CBStore.Get(sshKey) - if err != nil || keyValue == nil { + keyValue, err = kvstore.GetKv(sshKey) + if err != nil || keyValue == (kvstore.KeyValue{}) { log.Error().Err(err).Msg("") return "", "", "", err } @@ -436,10 +437,10 @@ func UpdateVmSshKey(nsId string, mcisId string, vmId string, verifiedUserName st } key := common.GenMcisKey(nsId, mcisId, vmId) - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") - err = fmt.Errorf("In UpdateVmSshKey(); CBStore.Get() returned an error.") + err = fmt.Errorf("In UpdateVmSshKey(); kvstore.GetKv() returned an error.") log.Error().Err(err).Msg("") // return nil, err } @@ -447,7 +448,7 @@ func UpdateVmSshKey(nsId string, mcisId string, vmId string, verifiedUserName st json.Unmarshal([]byte(keyValue.Value), &content) sshKey := common.GenResourceKey(nsId, common.StrSSHKey, content.SshKeyId) - keyValue, _ = common.CBStore.Get(sshKey) + keyValue, _ = kvstore.GetKv(sshKey) tmpSshKeyInfo := mcir.TbSshKeyInfo{} json.Unmarshal([]byte(keyValue.Value), &tmpSshKeyInfo) @@ -455,7 +456,7 @@ func UpdateVmSshKey(nsId string, mcisId string, vmId string, verifiedUserName st tmpSshKeyInfo.VerifiedUsername = verifiedUserName val, _ := json.Marshal(tmpSshKeyInfo) - err = common.CBStore.Put(keyValue.Key, string(val)) + err = kvstore.Put(keyValue.Key, string(val)) if err != nil { log.Error().Err(err).Msg("") return err diff --git a/src/core/mcis/snapshot.go b/src/core/mcis/snapshot.go index 311832b68..f319b6391 100644 --- a/src/core/mcis/snapshot.go +++ b/src/core/mcis/snapshot.go @@ -20,6 +20,7 @@ import ( "github.com/cloud-barista/cb-tumblebug/src/core/common" "github.com/cloud-barista/cb-tumblebug/src/core/mcir" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" "github.com/go-resty/resty/v2" "github.com/rs/zerolog/log" ) @@ -33,8 +34,8 @@ func CreateVmSnapshot(nsId string, mcisId string, vmId string, snapshotName stri vmKey := common.GenMcisKey(nsId, mcisId, vmId) // Check existence of the key. If no key, no update. - keyValue, err := common.CBStore.Get(vmKey) - if keyValue == nil || err != nil { + keyValue, err := kvstore.GetKv(vmKey) + if keyValue == (kvstore.KeyValue{}) || err != nil { err := fmt.Errorf("Failed to find 'ns/mcis/vm': %s/%s/%s \n", nsId, mcisId, vmId) log.Error().Err(err).Msg("") return mcir.TbCustomImageInfo{}, err diff --git a/src/core/mcis/utility.go b/src/core/mcis/utility.go index 997b51ba6..52885fc3c 100644 --- a/src/core/mcis/utility.go +++ b/src/core/mcis/utility.go @@ -24,6 +24,7 @@ import ( "github.com/cloud-barista/cb-tumblebug/src/core/common" "github.com/cloud-barista/cb-tumblebug/src/core/mcir" + "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" "github.com/rs/zerolog/log" "github.com/go-resty/resty/v2" @@ -118,15 +119,15 @@ func CheckMcis(nsId string, mcisId string) (bool, error) { key := common.GenMcisKey(nsId, mcisId, "") - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") - err = fmt.Errorf("In CheckMcis(); CBStore.Get() returned an error.") + err = fmt.Errorf("In CheckMcis(); kvstore.GetKv() returned an error.") log.Error().Err(err).Msg("") // return nil, err } - if keyValue != nil { + if keyValue != (kvstore.KeyValue{}) { return true, nil } return false, nil @@ -195,15 +196,15 @@ func CheckVm(nsId string, mcisId string, vmId string) (bool, error) { key := common.GenMcisKey(nsId, mcisId, vmId) - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") - err = fmt.Errorf("In CheckVm(); CBStore.Get() returned an error.") + err = fmt.Errorf("In CheckVm(); kvstore.GetKv() returned an error.") log.Error().Err(err).Msg("") // return nil, err } - if keyValue != nil { + if keyValue != (kvstore.KeyValue{}) { return true, nil } return false, nil @@ -236,15 +237,15 @@ func CheckMcisPolicy(nsId string, mcisId string) (bool, error) { key := common.GenMcisPolicyKey(nsId, mcisId, "") - keyValue, err := common.CBStore.Get(key) + keyValue, err := kvstore.GetKv(key) if err != nil { log.Error().Err(err).Msg("") - err = fmt.Errorf("In CheckMcisPolicy(); CBStore.Get() returned an error.") + err = fmt.Errorf("In CheckMcisPolicy(); kvstore.GetKv() returned an error.") log.Error().Err(err).Msg("") // return nil, err } - if keyValue != nil { + if keyValue != (kvstore.KeyValue{}) { return true, nil } return false, nil From a3980fc28c2aeea9958678a9b3cc40f5a6a1263f Mon Sep 17 00:00:00 2001 From: Yunkon Kim Date: Fri, 26 Jul 2024 11:34:22 +0900 Subject: [PATCH 06/11] Relocate `etcd` and `kvstore` initialization section --- src/main.go | 91 +++++++++++++++++++++++++++-------------------------- 1 file changed, 46 insertions(+), 45 deletions(-) diff --git a/src/main.go b/src/main.go index 6aca717f8..49082188f 100644 --- a/src/main.go +++ b/src/main.go @@ -225,6 +225,52 @@ func setConfig() { panic("Failed to confirm CB-Spider readiness within the allowed time. \nCheck the connection to CB-Spider.") } + // Setup etcd and kvstore + var etcdAuthEnabled bool + var etcdUsername string + var etcdPassword string + etcdAuthEnabled = os.Getenv("ETCD_AUTH_ENABLED") == "true" + if etcdAuthEnabled { + etcdUsername = common.NVL(os.Getenv("ETCD_USERNAME"), "default") + etcdPassword = common.NVL(os.Getenv("ETCD_PASSWORD"), "default") + } + + etcdEndpoints := strings.Split(common.EtcdClusterEndpoints, ",") + + ctx := context.Background() + config := etcd.Config{ + Endpoints: etcdEndpoints, + DialTimeout: 5 * time.Second, + Username: etcdUsername, + Password: etcdPassword, + } + + // Wait until etcd is ready + var etcdStore kvstore.Store + var err2 error + etcdMaxAttempts := 10 // (50 sec) + etcdAttempt := 1 + for ; etcdAttempt <= etcdMaxAttempts; etcdAttempt++ { + etcdStore, err2 = etcd.NewEtcdStore(ctx, config) + if err2 == nil { + log.Info().Msg("etcd is now available.") + break + } + log.Warn().Err(err2).Msgf("etcd at %s is not ready. Attempt %d/%d", common.EtcdClusterEndpoints, etcdAttempt, maxAttempts) + time.Sleep(5 * time.Second) + } + + if err2 != nil { + log.Fatal().Err(err2).Msg("failed to initialize etcd") + } + + err2 = kvstore.InitializeStore(etcdStore) + if err2 != nil { + log.Fatal().Err(err2).Msg("") + } + log.Info().Msg("kvstore is initialized successfully. Initializing CB-Tumblebug...") + + // Register all cloud info err = common.RegisterAllCloudInfo() if err != nil { log.Error().Err(err).Msg("Failed to register cloud info") @@ -367,51 +413,6 @@ func main() { }) }() - // Setup etcd and kvstore - var etcdAuthEnabled bool - var etcdUsername string - var etcdPassword string - etcdAuthEnabled = os.Getenv("ETCD_AUTH_ENABLED") == "true" - if etcdAuthEnabled { - etcdUsername = common.NVL(os.Getenv("ETCD_USERNAME"), "default") - etcdPassword = common.NVL(os.Getenv("ETCD_PASSWORD"), "default") - } - - etcdEndpoints := strings.Split(common.EtcdClusterEndpoints, ",") - - ctx := context.Background() - config := etcd.Config{ - Endpoints: etcdEndpoints, - DialTimeout: 5 * time.Second, - Username: etcdUsername, - Password: etcdPassword, - } - - // Wait until etcd is ready - var etcdStore kvstore.Store - var err error - maxAttempts := 10 // (50 sec) - attempt := 1 - for ; attempt <= maxAttempts; attempt++ { - etcdStore, err = etcd.NewEtcdStore(ctx, config) - if err == nil { - log.Info().Msg("etcd is now available.") - break - } - log.Warn().Err(err).Msgf("etcd at %s is not ready. Attempt %d/%d", common.EtcdClusterEndpoints, attempt, maxAttempts) - time.Sleep(5 * time.Second) - } - - if err != nil { - log.Fatal().Err(err).Msg("failed to initialize etcd") - } - - err = kvstore.InitializeStore(etcdStore) - if err != nil { - log.Fatal().Err(err).Msg("") - } - log.Info().Msg("kvstore is initialized successfully. Initializing CB-Tumblebug...") - // Launch API servers (REST) wg := new(sync.WaitGroup) wg.Add(1) From 39989e5ae5d6f837854df3bb6dba82a566b8d758 Mon Sep 17 00:00:00 2001 From: Yunkon Kim Date: Fri, 26 Jul 2024 12:07:44 +0900 Subject: [PATCH 07/11] Update configs and dependencies --- Dockerfile | 2 -- conf/setup.env | 2 -- docker-compose.yaml | 2 -- go.mod | 10 +--------- go.sum | 21 --------------------- go.work.sum | 3 +++ 6 files changed, 4 insertions(+), 36 deletions(-) diff --git a/Dockerfile b/Dockerfile index 74f9205c4..ce7882775 100644 --- a/Dockerfile +++ b/Dockerfile @@ -44,8 +44,6 @@ COPY --from=builder /go/src/github.com/cloud-barista/cb-tumblebug/src/cb-tumbleb # Setting various environment variables required by the application ENV CBTUMBLEBUG_ROOT=/app \ - CBSTORE_ROOT=/app \ - CBLOG_ROOT=/app \ SPIDER_CALL_METHOD=REST \ DRAGONFLY_CALL_METHOD=REST \ SPIDER_REST_URL=http://cb-spider:1024/spider \ diff --git a/conf/setup.env b/conf/setup.env index 6c3ac3fd4..fbcccc8df 100644 --- a/conf/setup.env +++ b/conf/setup.env @@ -18,8 +18,6 @@ export AUTH_MODE=basic export SELF_ENDPOINT=localhost:1323 # Set system endpoints -export CBSTORE_ROOT=$CBTUMBLEBUG_ROOT -export CBLOG_ROOT=$CBTUMBLEBUG_ROOT export SPIDER_CALL_METHOD=REST export SPIDER_REST_URL=http://localhost:1024/spider export DRAGONFLY_CALL_METHOD=REST diff --git a/docker-compose.yaml b/docker-compose.yaml index c8a16a587..3e145ba70 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -113,8 +113,6 @@ services: - ./container-volume/cb-tumblebug-container/log/:/app/log/ environment: # - CBTUMBLEBUG_ROOT=/app - # - CBSTORE_ROOT=/app - # - CBLOG_ROOT=/app # - SPIDER_CALL_METHOD=REST - SPIDER_REST_URL=http://cb-spider:1024/spider # - DRAGONFLY_CALL_METHOD=REST diff --git a/go.mod b/go.mod index 5caf234a2..abfbcb866 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/cloud-barista/cb-tumblebug go 1.21.6 require ( - github.com/cloud-barista/cb-store v0.8.0 github.com/cloud-barista/mc-terrarium v0.0.7 github.com/fsnotify/fsnotify v1.7.0 github.com/go-playground/validator/v10 v10.17.0 @@ -32,8 +31,6 @@ require ( require ( filippo.io/edwards25519 v1.1.0 // indirect github.com/KyleBanks/depth v1.2.1 // indirect - github.com/bwmarrin/snowflake v0.3.0 // indirect - github.com/cloud-barista/cb-log v0.8.0 // indirect github.com/coreos/go-semver v0.3.1 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect @@ -78,8 +75,6 @@ require ( github.com/rivo/uniseg v0.2.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect - github.com/sirupsen/logrus v1.9.3 // indirect - github.com/snowzach/rotatefilehook v0.0.0-20220211133110-53752135082d // indirect github.com/sourcegraph/conc v0.3.0 // indirect github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect @@ -91,12 +86,9 @@ require ( github.com/tidwall/pretty v1.2.1 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect - github.com/xujiajun/mmap-go v1.0.1 // indirect - github.com/xujiajun/nutsdb v0.11.1 // indirect - github.com/xujiajun/utils v0.0.0-20220904132955-5f7c5b914235 // indirect go.etcd.io/etcd/api/v3 v3.5.11 // indirect go.etcd.io/etcd/client/pkg/v3 v3.5.11 // indirect - go.etcd.io/etcd/client/v3 v3.5.11 // indirect + go.etcd.io/etcd/client/v3 v3.5.11 go.uber.org/multierr v1.11.0 // indirect go.uber.org/zap v1.26.0 // indirect golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 // indirect diff --git a/go.sum b/go.sum index 0ec830f7a..db2d2d560 100644 --- a/go.sum +++ b/go.sum @@ -10,16 +10,9 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= -github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0= -github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE= -github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/chzyer/logex v1.2.0/go.mod h1:9+9sk7u7pGNWYMkh0hdiL++6OeibzJccyQU4p4MedaY= github.com/chzyer/readline v1.5.0/go.mod h1:x22KAscuvRqlLoK9CsoYsmxoXZMMFVyOl86cAH8qUic= github.com/chzyer/test v0.0.0-20210722231415-061457976a23/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= -github.com/cloud-barista/cb-log v0.8.0 h1:ArWCs1EgpoD3ZnBgcC4cAw5ufI/JHmFKfJswlv4whgk= -github.com/cloud-barista/cb-log v0.8.0/go.mod h1:nGgfTFMPwl1MpCO3FBjexUkNdOYA0BNJoyM9Pd0lMms= -github.com/cloud-barista/cb-store v0.8.0 h1:0K47YEf+K3wx18D+m0XirlDbdTz229XxsTXw6WACjRA= -github.com/cloud-barista/cb-store v0.8.0/go.mod h1:6NuA5TdeVRExd59ULXv6LEhm4EE0ODn9L820g4VqApo= github.com/cloud-barista/mc-terrarium v0.0.7 h1:rXBbAxZyOuwWx77xekrBylGVj9ZIOmHyYcPZkykpCag= github.com/cloud-barista/mc-terrarium v0.0.7/go.mod h1:qey9GFrJidyJ3tVfeL/gcImgWLqsF64j/fVmBfaddDI= github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= @@ -271,10 +264,6 @@ github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFR github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= -github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= -github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= -github.com/snowzach/rotatefilehook v0.0.0-20220211133110-53752135082d h1:4660u5vJtsyrn3QwJNfESwCws+TM1CMhRn123xjVyQ8= -github.com/snowzach/rotatefilehook v0.0.0-20220211133110-53752135082d/go.mod h1:ZLVe3VfhAuMYLYWliGEydMBoRnfib8EFSqkBYu1ck9E= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= @@ -330,13 +319,6 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= -github.com/xujiajun/gorouter v1.2.0/go.mod h1:yJrIta+bTNpBM/2UT8hLOaEAFckO+m/qmR3luMIQygM= -github.com/xujiajun/mmap-go v1.0.1 h1:7Se7ss1fLPPRW+ePgqGpCkfGIZzJV6JPq9Wq9iv/WHc= -github.com/xujiajun/mmap-go v1.0.1/go.mod h1:CNN6Sw4SL69Sui00p0zEzcZKbt+5HtEnYUsc6BKKRMg= -github.com/xujiajun/nutsdb v0.11.1 h1:zLyIvp3ABHMohtcqi0sbt7gGOFWfse+ZbLv2GVb6ZYw= -github.com/xujiajun/nutsdb v0.11.1/go.mod h1:sAT5Kr8+53X2r1eFMHw2VSPLSAo/PiJCZPK5QtMsw7g= -github.com/xujiajun/utils v0.0.0-20220904132955-5f7c5b914235 h1:w0si+uee0iAaCJO9q86T6yrhdadgcsoNuh47LrUykzg= -github.com/xujiajun/utils v0.0.0-20220904132955-5f7c5b914235/go.mod h1:MR4+0R6A9NS5IABnIM3384FfOq8QFVnm7WDrBOhIaMU= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= @@ -421,7 +403,6 @@ golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20181221143128-b4a75ba826a6/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -436,9 +417,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220405210540-1e041c57c461/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/go.work.sum b/go.work.sum index 097da920f..cd81168ba 100644 --- a/go.work.sum +++ b/go.work.sum @@ -541,6 +541,8 @@ github.com/segmentio/ksuid v1.0.4/go.mod h1:/XUiZBD3kVx5SmUOl55voK5yeAbBNNIed+2O github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d h1:yKm7XZV6j9Ev6lojP2XaIshpT4ymkqhMeSghO5Ps00E= github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d/go.mod h1:UdhH50NIW0fCiwBSr0co2m7BnFLdv4fQTgdqdJTHFeE= github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e h1:qpG93cPwA5f7s/ZPBJnGOYQNK/vKsaDaseuKT5Asee8= @@ -556,6 +558,7 @@ github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/X github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= go.etcd.io/etcd/client/v2 v2.305.10/go.mod h1:m3CKZi69HzilhVqtPDcjhSGp+kA1OmbNn0qamH80xjA= go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= +go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= From e7a0670697af996c4cbe67c8648ab893e0a2c4e7 Mon Sep 17 00:00:00 2001 From: Yunkon Kim Date: Fri, 26 Jul 2024 12:08:24 +0900 Subject: [PATCH 08/11] Update README.md --- README.md | 127 +++++++++++++++++++++++++++++------------------------- 1 file changed, 68 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index 9408797d3..0946aa5b9 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,9 @@ [![Slack](https://img.shields.io/badge/Slack-SIG--TB-brightgreen)](https://cloud-barista.slack.com/archives/CJQ7575PU) + [![All Contributors](https://img.shields.io/badge/all_contributors-43-orange.svg?style=flat-square)](#contributors-) + CB-Tumblebug (CB-TB for short) is a system for managing multi-cloud infrastructure consisting of resources from multiple cloud service providers. (Cloud-Barista) @@ -28,7 +30,6 @@ CB-Tumblebug (CB-TB for short) is a system for managing multi-cloud infrastructu [![Multi-Cloud LLMs in parallel](https://github.com/cloud-barista/cb-tumblebug/assets/5966944/e15feb67-ba02-4066-af62-d9f8e8330a63)](https://www.youtube.com/watch?v=SD9ZoT_OZpQ) - [LLM-related scripts](https://github.com/cloud-barista/cb-tumblebug/tree/main/scripts/usecases/llm) -
[Note] Development of CB-Tumblebug is ongoing @@ -68,22 +69,22 @@ while the CB-TB repo accommodates local languages in their contents. 1. [How to Use](#how-to-use-cb-tb-features) 1. [How to Contribute](#how-to-contribute) - --- ## Prerequisites ### Envionment + - Linux (recommended: `Ubuntu 22.04`) - Golang (recommended: `v1.21.6`) ### Dependency + Open source packages used in this project -- [Dependencies](https://github.com/cloud-barista/cb-tumblebug/network/dependencies) +- [Dependencies](https://github.com/cloud-barista/cb-tumblebug/network/dependencies) - [SBOM](https://github.com/cloud-barista/cb-tumblebug/dependency-graph/sbom) - --- ## How to Contribute @@ -108,13 +109,13 @@ Check out [CONTRIBUTING](https://github.com/cloud-barista/cb-tumblebug/blob/main The `--depth 1` option reduces the size by limiting the commit history download. For contributing, it is recommended not to specify this option or to restore the commit history using the following command. - ```bash git fetch --unshallow ``` Register alias for the CB-TB directory (optional action for convenience: `cdtb`, `cbtbsrc`, `cdtbtest`). + ```bash echo "alias cdtb='cd $HOME/go/src/github.com/cloud-barista/cb-tumblebug'" >> ~/.bashrc echo "alias cdtbsrc='cd $HOME/go/src/github.com/cloud-barista/cb-tumblebug/src'" >> ~/.bashrc @@ -138,7 +139,9 @@ Check out [CONTRIBUTING](https://github.com/cloud-barista/cb-tumblebug/blob/main sudo apt install make gcc git ``` - Install: Golang + - Check https://golang.org/dl/ and setup Go + - Download ```bash wget https://go.dev/dl/go1.21.6.linux-amd64.tar.gz; @@ -161,26 +164,28 @@ Check out [CONTRIBUTING](https://github.com/cloud-barista/cb-tumblebug/blob/main ### (2) Build CB-TB - Build the Golang source code using the Makefile + ```bash cd ~/go/src/github.com/cloud-barista/cb-tumblebug/src make ``` - + All dependencies will be downloaded automatically by Go. - + The initial build will take some time, but subsequent builds will be faster by the Go build cache. - + **Note** To update the Swagger API documentation, run `make swag` in `cb-tumblebug/src/` - - API documentation file will be generated at `cb-tumblebug/src/api/rest/docs/swagger.yaml` - - API documentation can be viewed in a web browser at http://localhost:1323/tumblebug/swagger/ (provided when CB-TB is running) - - Detailed information on [how to update the API](https://github.com/cloud-barista/cb-tumblebug/wiki/API-Document-Update) + - API documentation file will be generated at `cb-tumblebug/src/api/rest/docs/swagger.yaml` + - API documentation can be viewed in a web browser at http://localhost:1323/tumblebug/swagger/ (provided when CB-TB is running) + - Detailed information on [how to update the API](https://github.com/cloud-barista/cb-tumblebug/wiki/API-Document-Update) ### (3) Run CB-TB system #### (3-1) Run dependant sub-project + - Run CB-Spider - + CB-Tumblebug requires [CB-Spider](https://github.com/cloud-barista/cb-spider) to control multiple cloud service providers. - (Recommended method) Run the CB-Spider container using the CB-TB script (preferably use the specified version) @@ -200,9 +205,11 @@ Check out [CONTRIBUTING](https://github.com/cloud-barista/cb-tumblebug/blob/main For installation methods other than the container, refer to [CB-Spider](https://github.com/cloud-barista/cb-spider) #### (3-2: option 1) Run CB-TB from the source code (recommended) + - [Clone the repository](#how-to-download) - [Build and Setup](#how-to-build-and-setup) - Set environment variables required to run CB-TB (in another tab) + - Check and configure the contents of `cb-tumblebug/conf/setup.env` (CB-TB environment variables, modify as needed) - Apply the environment variables to the system ```bash @@ -215,9 +222,7 @@ Check out [CONTRIBUTING](https://github.com/cloud-barista/cb-tumblebug/blob/main cd ~/go/src/github.com/cloud-barista/cb-tumblebug source ./scripts/setPublicIP.sh ``` - - Check and configure the contents of `store_conf.yaml` in `cb-tumblebug/conf` (cb-store environment variables, modify as needed) - - Specify storetype (NUTSDB or ETCD) - - When setting NUTSDB (local DB), it is necessary to specify the path (by default, `cb-tumblebug/meta_db/dat`) + - Execute the built cb-tumblebug binary by using `make run` ```bash cd ~/go/src/github.com/cloud-barista/cb-tumblebug/src @@ -228,6 +233,7 @@ Check out [CONTRIBUTING](https://github.com/cloud-barista/cb-tumblebug/blob/main - Check CB-TB available docker image tags(https://hub.docker.com/r/cloudbaristaorg/cb-tumblebug/tags) - Run the container image (two options) + - Run a script to excute CB-TB docker image (recommended) ```bash @@ -244,40 +250,40 @@ Check out [CONTRIBUTING](https://github.com/cloud-barista/cb-tumblebug/blob/main #### (3-3) Check the system is ready - You will see the following messages.. +You will see the following messages.. - ``` - ██████╗██████╗ ████████╗██████╗ - ██╔════╝██╔══██╗ ╚══██╔══╝██╔══██╗ - ██║ ██████╔╝█████╗██║ ██████╔╝ - ██║ ██╔══██╗╚════╝██║ ██╔══██╗ - ╚██████╗██████╔╝ ██║ ██████╔╝ - ╚═════╝╚═════╝ ╚═╝ ╚═════╝ +``` + ██████╗██████╗ ████████╗██████╗ + ██╔════╝██╔══██╗ ╚══██╔══╝██╔══██╗ + ██║ ██████╔╝█████╗██║ ██████╔╝ + ██║ ██╔══██╗╚════╝██║ ██╔══██╗ + ╚██████╗██████╔╝ ██║ ██████╔╝ + ╚═════╝╚═════╝ ╚═╝ ╚═════╝ - ██████╗ ███████╗ █████╗ ██████╗ ██╗ ██╗ - ██╔══██╗██╔════╝██╔══██╗██╔══██╗╚██╗ ██╔╝ - ██████╔╝█████╗ ███████║██║ ██║ ╚████╔╝ - ██╔══██╗██╔══╝ ██╔══██║██║ ██║ ╚██╔╝ - ██║ ██║███████╗██║ ██║██████╔╝ ██║ - ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚═════╝ ╚═╝ + ██████╗ ███████╗ █████╗ ██████╗ ██╗ ██╗ + ██╔══██╗██╔════╝██╔══██╗██╔══██╗╚██╗ ██╔╝ + ██████╔╝█████╗ ███████║██║ ██║ ╚████╔╝ + ██╔══██╗██╔══╝ ██╔══██║██║ ██║ ╚██╔╝ + ██║ ██║███████╗██║ ██║██████╔╝ ██║ + ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚═════╝ ╚═╝ - Multi-cloud infrastructure managemenet framework - ________________________________________________ + Multi-cloud infrastructure managemenet framework + ________________________________________________ - https://github.com/cloud-barista/cb-tumblebug + https://github.com/cloud-barista/cb-tumblebug - Access to API dashboard (username: default / password: default) - http://xxx.xxx.xxx.xxx:1323/tumblebug/api + Access to API dashboard (username: default / password: default) + http://xxx.xxx.xxx.xxx:1323/tumblebug/api - ⇨ http server started on [::]:1323 - ``` - - - In default (`cb-tumblebug/conf/setup.env`), - you can find the system log in `cb-tumblebug/log/tumblebug.log` (log is based on `zerolog`) +⇨ http server started on [::]:1323 +``` +- In default (`cb-tumblebug/conf/setup.env`), + you can find the system log in `cb-tumblebug/log/tumblebug.log` (log is based on `zerolog`) ### (4) Configure Multi-Cloud info + To provisioning multi-cloud infrastructures with CB-TB, it is necessary to register the connection information (credentials) for clouds, as well as commonly used images and specifications. - Create `credentials.yaml` file and input your cloud credentials @@ -286,17 +292,18 @@ To provisioning multi-cloud infrastructures with CB-TB, it is necessary to regis - It should be located in the `~/.cloud-barista/` directory and securely managed. - Refer to the [`template.credentials.yaml`](https://github.com/cloud-barista/cb-tumblebug/blob/main/scripts/init/template.credentials.yaml) for the template - Create `credentials.yaml` the file - + Automatically generate the `credentials.yaml` file in the `~/.cloud-barista/` directory using the CB-TB script - + ```bash cd ~/go/src/github.com/cloud-barista/cb-tumblebug ./scripts/init/genCredential.sh ``` + - Input credential data - + Put credential data to `~/.cloud-barista/credentials.yaml` ([Reference: How to obtain a credential for each CSP](https://github.com/cloud-barista/cb-tumblebug/wiki/How-to-get-public-cloud-credentials)) - + ```example ### Cloud credentials for credential holders (default: admin) credentialholder: @@ -317,41 +324,39 @@ To provisioning multi-cloud infrastructures with CB-TB, it is necessary to regis ClientSecret: ... ``` - - Encrypt `credentials.yaml` into `credentials.yaml.enc` - + To protect sensitive information, `credentials.yaml` is not used directly. Instead, it must be encrypted using `encCredential.sh`. The encrypted file `credentials.yaml.enc` is then used by `init.py`. This approach ensures that sensitive credentials are not stored in plain text. + - Encrypting Credentials ```bash scripts/init/encCredential.sh ``` ![image](https://github.com/user-attachments/assets/5e7a73a6-8746-4be3-9a74-50b051f345bb) - If you need to update your credentials, decrypt the encrypted file using `decCredential.sh`, make the necessary changes to `credentials.yaml`, and then re-encrypt it. - + - Decrypting Credentials ```bash scripts/init/decCredential.sh ``` ![image](https://github.com/user-attachments/assets/85c91124-317d-4877-a025-a53cfdf2725e) - - (INIT) Register all multi-cloud connection information and common resources + - How to register - + Refer to [README.md for init.py](https://github.com/cloud-barista/cb-tumblebug/blob/main/scripts/init/README.md), and execute the [`init.py`](https://github.com/cloud-barista/cb-tumblebug/blob/main/scripts/init/init.py) script. (enter 'y' for confirmation prompts) - + ```bash cd ~/go/src/github.com/cloud-barista/cb-tumblebug ./scripts/init/init.sh ``` - + - The credentials in `~/.cloud-barista/credentials.yaml.enc` (encrypted file from the `credentials.yaml`) will be automatically registered (all CSP and region information recorded in [`cloudinfo.yaml`](https://github.com/cloud-barista/cb-tumblebug/blob/main/assets/cloudinfo.yaml) will be automatically registered in the system) - Note: You can check the latest regions and zones of CSP using [`update-cloudinfo.py`](https://github.com/cloud-barista/cb-tumblebug/blob/main/scripts/misc/update-cloudinfo.py) and review the file for updates. (contributions to updates are welcome) - Common images and specifications recorded in the [`cloudimage.csv`](https://github.com/cloud-barista/cb-tumblebug/blob/main/assets/cloudimage.csv) and [`cloudspec.csv`](https://github.com/cloud-barista/cb-tumblebug/blob/main/assets/cloudspec.csv) files in the [`assets`](https://github.com/cloud-barista/cb-tumblebug/tree/main/assets) directory will be automatically registered. - ### (5) Shutting down and Version Upgrade - Shutting down the CB-TB & CB-Spider servers @@ -367,9 +372,9 @@ To provisioning multi-cloud infrastructures with CB-TB, it is necessary to regis ``` - Upgrading the CB-TB & CB-Spider versions - + The following cleanup steps are unnecessary if you clearly understand the impact of the upgrade - + - Check and delete resources created through CB-TB - Delete CB-TB & CB-Spider metadata ```bash @@ -378,7 +383,6 @@ To provisioning multi-cloud infrastructures with CB-TB, it is necessary to regis ``` - Restart with the upgraded version - --- --- @@ -403,13 +407,14 @@ To provisioning multi-cloud infrastructures with CB-TB, it is necessary to regis ### Using CB-TB REST API -- Access to REST API dashboard +- Access to REST API dashboard + - http://[IP]:1323/tumblebug/api - Upsteam online API document: [![Swagger API Doc](https://img.shields.io/badge/API%20Doc-Swagger-brightgreen)](https://cloud-barista.github.io/api/?url=https://raw.githubusercontent.com/cloud-barista/cb-tumblebug/main/src/api/rest/docs/swagger.yaml) - REST API AUTH - + CB-TB API is encoded with `basic access authentication` by default. (not fully secured yet!) - + You need to encode the `Username` and `Password` entered during server startup in Base64 and include it in the API header. - [A guide to quickly create a Multi-Cloud Infra](https://github.com/cloud-barista/cb-tumblebug/discussions/1570) @@ -488,6 +493,7 @@ To provisioning multi-cloud infrastructures with CB-TB, it is necessary to regis ``` - MCIS Creation Test + - `./create-all.sh -n shson -f ../testSetCustom.env` # Create MCIS with the cloud combination configured in ../testSetCustom.env - Automatically proceed with the process to check the MCIS creation configuration specified in `../testSetCustom.env` - Example of execution result @@ -513,6 +519,7 @@ To provisioning multi-cloud infrastructures with CB-TB, it is necessary to regis ``` - MCIS Removal Test (Use the input parameters used in creation for deletion) + - `./clean-all.sh -n shson -f ../testSetCustom.env` # Perform removal of created resources according to `../testSetCustom.env` - **Be aware!** - If you created MCIS (VMs) for testing in public clouds, the VMs may incur charges. @@ -520,8 +527,10 @@ To provisioning multi-cloud infrastructures with CB-TB, it is necessary to regis - Anyway, please be aware of cloud usage costs when using public CSPs. - Generate MCIS SSH access keys and access each VM + - `./gen-sshKey.sh -n shson -f ../testSetCustom.env` # Return access keys for all VMs configured in MCIS - Example of execution result + ```bash ... [GENERATED PRIVATE KEY (PEM, PPK)] @@ -543,7 +552,7 @@ To provisioning multi-cloud infrastructures with CB-TB, it is necessary to regis - `./command-mcis.sh -n shson -f ../testSetCustom.env` # Execute IP and hostname retrieval for all VMs in MCIS -- K8s Cluster Test (WIP: Stability work in progress for each CSP) +- K8s Cluster Test (WIP: Stability work in progress for each CSP) ```bash ./create-mcir-ns-cloud.sh -n tb -f ../testSet.env` # Create MCIR required for K8s cluster creation From cb2313662005a9f890fb4658ba5fb1257084e037 Mon Sep 17 00:00:00 2001 From: Yunkon Kim Date: Mon, 29 Jul 2024 14:58:30 +0900 Subject: [PATCH 09/11] Add and update docker-compose configuration to customize or set envs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add `.env` and set variables with interpolation in the compose file * Specify environment variables for Docker-Compose * Match the specified environment variables with the environment variables of each service * Set default values ​​for each service environment variable --- .env | 157 +++++++++++++++++ .gitignore | 6 +- docker-compose-custom.yaml | 175 +++++++++++++++++++ docker-compose.yaml | 32 ++-- scripts/etcd/.env | 18 +- scripts/etcd/{setup-auth.sh => etcd-conf.sh} | 34 ++-- 6 files changed, 388 insertions(+), 34 deletions(-) create mode 100644 .env create mode 100644 docker-compose-custom.yaml rename scripts/etcd/{setup-auth.sh => etcd-conf.sh} (80%) diff --git a/.env b/.env new file mode 100644 index 000000000..524e8f0b0 --- /dev/null +++ b/.env @@ -0,0 +1,157 @@ + +############################################################# +# Variables to customize servcies in the docker-compose.yml # +############################################################# + +## ETCD +# see https://github.com/etcd-io/etcd/releases +COMPOSE_ETCD_VERSION_TAG=${ETCD_VERSION_TAG:-v3.5.14} +# default: COMPOSE_ETCD_VERSION_TAG=v3.5.14 + +# see https://etcd.io/docs/v3.5/op-guide/configuration/ +COMPOSE_ETCD_LISTEN_CLIENT_PORT=${ETCD_LISTEN_CLIENT_PORT:-2379} +# default: COMPOSE_ETCD_LISTEN_CLIENT_PORT=2379 +COMPOSE_ETCD_LISTEN_PEER_PORT=${ETCD_LISTEN_PEER_PORT:-2380} +# default: COMPOSE_ETCD_LISTEN_PEER_PORT=2380 +COMPOSE_ETCD_VOLUME_HOST_PATH=${ETCD_VOLUME_HOST_PATH:-./container-volume/etcd/data} +# default: COMPOSE_ETCD_VOLUME_HOST_PATH=./container-volume/etcd/data +COMPOSE_ETCD_VOLUME_CONTAINTER_PATH=${ETCD_VOLUME_CONTAINTER_PATH:-/etcd-data} +# default: COMPOSE_ETCD_VOLUME_CONTAINTER_PATH=/etcd-data +COMPOSE_ETCD_NAME_OF_THIS_MEMBER=${ETCD_MEMBER_NAME:-s1} +# default: COMPOSE_ETCD_NAME_OF_THIS_MEMBER=s1 +COMPOSE_ETCD_LISTEN_CLIENT_URLS=${ETCD_LISTEN_CLIENT_URLS:-http://0.0.0.0:2379} +# default: COMPOSE_ETCD_LISTEN_CLIENT_URLS=http://localhost:2379 +COMPOSE_ETCD_ADVERTISE_CLIENT_URLS=${ETCD_ADVERTISE_CLIENT_URLS:-http://0.0.0.0:2379} +# default: COMPOSE_ETCD_ADVERTISE_CLIENT_URLS=http://localhost:2379 +COMPOSE_ETCD_LISTEN_PEER_URLS=${ETCD_LISTEN_PEER_URLS:-http://0.0.0.0:2380} +# default: COMPOSE_ETCD_LISTEN_PEER_URLS=http://localhost:2380 +COMPOSE_ETCD_INITIAL_ADVERTISE_PEER_URLS=${ETCD_INITIAL_ADVERTISE_PEER_URLS:-http://0.0.0.0:2380} +# default: COMPOSE_ETCD_INITIAL_ADVERTISE_PEER_URLS=http://localhost:2380 +COMPOSE_ETCD_INITIAL_CLUSTER=${ETCD_INITIAL_CLUSTER:-s1=http://0.0.0.0:2380} +# default: COMPOSE_ETCD_INITIAL_CLUSTER=s1=http://localhost:2380 +COMPOSE_ETCD_INITIAL_CLUSTER_TOKEN=${ETCD_INITIAL_CLUSTER_TOKEN:-tkn} +# default: COMPOSE_ETCD_INITIAL_CLUSTER_TOKEN=tkn +COMPOSE_ETCD_INITIAL_CLUSTER_STATE=${ETCD_INITIAL_CLUSTER_STATE:-new} +# default: COMPOSE_ETCD_INITIAL_CLUSTER_STATE=new +COMPOSE_ETCD_LOG_LEVEL=${ETCD_LOG_LEVEL:-info} +# default: COMPOSE_ETCD_LOG_LEVEL=info +COMPOSE_ETCD_LOGGER=${ETCD_LOGGER:-zap} +# default: COMPOSE_ETCD_LOGGER=zap +COMPOSE_ETCD_LOG_OUTPUTS=${ETCD_LOG_OUTPUTS:-stderr} +# default: COMPOSE_ETCD_LOG_OUTPUTS=stderr +COMPOSE_ETCD_AUTH_TOKEN=${ETCD_AUTH_TOKEN:-simple} +# default: COMPOSE_ETCD_AUTH_TOKEN=simple + + +## ETCD_CONF +COMPOSE_ETCD_CONF_VERSION_TAG=${ETCD_VERSION_TAG:-v3.5.14} +# default: COMPOSE_ETCD_CONF_VERSION_TAG=v3.5.14 +COMPOSE_ETCD_CONF_ENDPOINTS=${ETCD_ENDPOINTS:-http://etcd:2379} +# default: COMPOSE_ETCD_CONF_ENDPOINTS=http://etcd:2379 +COMPOSE_ETCD_CONF_ETCD_PATH=${ETCD_PATH:-/tmp/etcd-download-test} +# default: COMPOSE_ETCD_CONF_ETCD_PATH=/tmp/etcd-download-test +COMPOSE_ETCD_CONF_AUTH_ENABLED=${ETCD_AUTH_ENABLED:-true} +# default: COMPOSE_ETCD_CONF_AUTH_ENABLED=true +COMPOSE_ETCD_CONF_ROOT_PASSWORD=${ETCD_ROOT_PASSWORD:-default} +# default: COMPOSE_ETCD_CONF_ROOT_PASSWORD=default +COMPOSE_ETCD_CONF_ADMIN_USERNAME=${ETCD_ADMIN_USERNAME:-default} +# default: COMPOSE_ETCD_CONF_ADMIN_USERNAME=default +COMPOSE_ETCD_CONF_ADMIN_PASSWORD=${ETCD_ADMIN_PASSWORD:-default} +# default: COMPOSE_ETCD_CONF_ADMIN_PASSWORD=default + + +## CB_SPIDER +COMPOSE_SP_VERSION_TAG=${SP_VERSION_TAG:-0.9.0} +# default: COMPOSE_SP_VERSION_TAG=0.9.0 +COMPOSE_SP_REST_PORT=${SP_REST_PORT:-1024} +# default: COMPOSE_SP_REST_PORT=1024 +COMPOSE_SP_VOLUME_HOST_PATH=${SP_VOLUME_HOST_PATH:-./container-volume/cb-spider-container} +# default: COMPOSE_SP_VOLUME_HOST_PATH=./container-volume/cb-spider-container +COMPOSE_SP_VOLUME_CONTAINTER_PATH=${SP_VOLUME_CONTAINTER_PATH:-/root/go/src/github.com/cloud-barista/cb-spider} +# default: COMPOSE_SP_VOLUME_CONTAINTER_PATH=/root/go/src/github.com/cloud-barista/cb-spider +COMPOSE_SP_PLUGSIN_SW=${PLUGSIN_SW:-OFF} +# default: COMPOSE_SP_PLUGSIN_SW +COMPOSE_SP_SERVER_ADDRESS=${SERVER_ADDRESS:-localhost} +# default: COMPOSE_SP_SERVER_ADDRESS=localhost +COMPOSE_SP_API_USERNAME=${SP_API_USERNAME:-} +# default: COMPOSE_SP_API_USERNAME= +# Note - Use SP_API_USERNAME instead of API_USERNAME because API_USERNAME is used in CB-Tumblebug +COMPOSE_SP_API_PASSWORD=${SP_API_PASSWORD:-} +# default: COMPOSE_SP_API_PASSWORD= +# Note - Use SP_API_PASSWORD instead of API_PASSWORD because API_PASSWORD is used in CB-Tumblebug +COMPOSE_SP_LOG_LEVEL=${LOG_LEVEL:-error} +# default: COMPOSE_SP_LOG_LEVEL=error +COMPOSE_SP_HISCALL_LOG_LEVEL=${HISCALL_LOG_LEVEL:-error} +# default: COMPOSE_SP_HISCALL_LOG_LEVEL=error +COMPOSE_SP_ID_TRANSFORM_MODE=${ID_TRANSFORM_MODE:-ON} +# default: COMPOSE_SP_ID_TRANSFORM_MODE=ON + + +## CB_TUMBLEBUG +COMPOSE_TB_VERSION_TAG=${TB_VERSION_TAG:-0.9.0} +# default: COMPOSE_TB_VERSION_TAG=0.9.0 +COMPOSE_TB_REST_PORT=${TB_REST_PORT:-1323} +# default: COMPOSE_TB_REST_PORT=1323 +COMPOSE_TB_VOLUME_HOST_PATH=${TB_VOLUME_HOST_PATH:-./container-volume/cb-tumblebug-container} +# default: COMPOSE_TB_VOLUME_HOST_PATH=./container-volume/cb-tumblebug-container +COMPOSE_TB_VOLUME_CONTAINTER_PATH=${TB_VOLUME_CONTAINTER_PATH:-/app} +# default: COMPOSE_TB_VOLUME_CONTAINTER_PATH=/app +COMPOSE_TB_CBTUMBLEBUG_ROOT=${CBTUMBLEBUG_ROOT:-/app} +# default: COMPOSE_TB_CBTUMBLEBUG_ROOT=/app +COMPOSE_TB_SPIDER_CALL_METHOD=${SPIDER_CALL_METHOD:-REST} +# default: COMPOSE_TB_SPIDER_CALL_METHOD=REST +COMPOSE_TB_SPIDER_REST_URL=${SPIDER_REST_URL:-http://cb-spider:1024/spider} +# default: COMPOSE_TB_SPIDER_REST_URL=http://cb-spider:1024/spider +COMPOSE_TB_DRAGONFLY_CALL_METHOD=${DRAGONFLY_CALL_METHOD:-REST} +# default: COMPOSE_TB_DRAGONFLY_CALL_METHOD=REST +COMPOSE_TB_DRAGONFLY_REST_URL=${DRAGONFLY_REST_URL:-http://cb-dragonfly:9090/dragonfly} +# default: COMPOSE_TB_DRAGONFLY_REST_URL=http://cb-dragonfly:9090/dragonfly +COMPOSE_TB_DB_URL=${DB_URL:-localhost:3306} +# default: COMPOSE_TB_DB_URL=localhost:3306 +COMPOSE_TB_DB_DATABASE=${DB_DATABASE:-cb_tumblebug} +# default: COMPOSE_TB_DB_DATABASE=cb_tumblebug +COMPOSE_TB_DB_USER=${DB_USER:-cb_tumblebug} +# default: COMPOSE_TB_DB_USER=cb_tumblebug +COMPOSE_TB_DB_PASSWORD=${DB_PASSWORD:-cb_tumblebug} +# default: COMPOSE_TB_DB_PASSWORD=cb_tumblebug +COMPOSE_TB_ALLOW_ORIGINS=${ALLOW_ORIGINS:-*} +# default: COMPOSE_TB_ALLOW_ORIGINS=* +COMPOSE_TB_AUTH_ENABLED=${AUTH_ENABLED:-true} +# default: COMPOSE_TB_AUTH_ENABLED=true +COMPOSE_TB_API_USERNAME=${API_USERNAME:-default} +# default: COMPOSE_TB_API_USERNAME=default +COMPOSE_TB_API_PASSWORD=${API_PASSWORD:-default} +# default: COMPOSE_TB_API_PASSWORD=default +COMPOSE_TB_AUTOCONTROL_DURATION_MS=${AUTOCONTROL_DURATION_MS:-10000} +# default: COMPOSE_TB_AUTOCONTROL_DURATION_MS=10000 +COMPOSE_TB_SELF_ENDPOINT=${SELF_ENDPOINT:-localhost:1323} +# default: COMPOSE_TB_SELF_ENDPOINT=localhost:1323 +COMPOSE_TB_API_DOC_PATH=${API_DOC_PATH:-/app/src/api/rest/docs/swagger.json} +# default: COMPOSE_TB_API_DOC_PATH=/app/src/api/rest/docs/swagger.json +COMPOSE_TB_DEFAULT_NAMESPACE=${DEFAULT_NAMESPACE:-ns01} +# default: COMPOSE_TB_DEFAULT_NAMESPACE=ns01 +COMPOSE_TB_DEFAULT_CREDENTIALHOLDER=${DEFAULT_CREDENTIALHOLDER:-admin} +# default: COMPOSE_TB_DEFAULT_CREDENTIALHOLDER=admin +COMPOSE_TB_LOGFILE_PATH=${LOGFILE_PATH:-/app/log/tumblebug.log} +# default: COMPOSE_TB_LOGFILE_PATH=/app/log/tumblebug.log +COMPOSE_TB_LOGFILE_MAXSIZE=${LOGFILE_MAXSIZE:-10} +# default: COMPOSE_TB_LOGFILE_MAXSIZE=10 +COMPOSE_TB_LOGFILE_MAXBACKUPS=${LOGFILE_MAXBACKUPS:-3} +# default: COMPOSE_TB_LOGFILE_MAXBACKUPS=3 +COMPOSE_TB_LOGFILE_MAXAGE=${LOGFILE_MAXAGE:-30} +# default: COMPOSE_TB_LOGFILE_MAXAGE=30 +COMPOSE_TB_LOGFILE_COMPRESS=${LOGFILE_COMPRESS:-false} +# default: COMPOSE_TB_LOGFILE_COMPRESS=false +COMPOSE_TB_LOGLEVEL=${LOGLEVEL:-debug} +# default: COMPOSE_TB_LOGLEVEL=debug +COMPOSE_TB_LOGWRITER=${LOGWRITER:-both} +# default: COMPOSE_TB_LOGWRITER=both +COMPOSE_TB_NODE_ENV=${NODE_ENV:-development} +# default: COMPOSE_TB_NODE_ENV=development + + +## CB-MapUI +COMPOSE_MAP_VERSION_TAG=${MAP_VERSION_TAG:-0.9.0} +# default: COMPOSE_MAP_VERSION_TAG=0.9.0 +COMPOSE_MAP_PORT=${MAP_PORT:-1324} +# default: COMPOSE_MAP_PORT=1324 diff --git a/.gitignore b/.gitignore index 7cc4e8a4a..bec4e655b 100644 --- a/.gitignore +++ b/.gitignore @@ -63,8 +63,8 @@ log/ *.history # Ignore all docker-compose.yaml files created by users, except docker-compose.yaml -*docker-compose*.yaml +# *docker-compose*.yaml !docker-compose.yaml -# (TBD) .env file for docker-compose -# +# .env file for docker-compose +# .env diff --git a/docker-compose-custom.yaml b/docker-compose-custom.yaml new file mode 100644 index 000000000..6afbf5177 --- /dev/null +++ b/docker-compose-custom.yaml @@ -0,0 +1,175 @@ +version: "3.6" +services: + # etcd + etcd: + image: gcr.io/etcd-development/etcd:${COMPOSE_ETCD_VERSION_TAG} + container_name: etcd + ports: + - target: 2379 # Port assinged to etcd in the container + published: ${COMPOSE_ETCD_LISTEN_CLIENT_PORT} # Port to be exposed to the host + protocol: tcp # Protocol of the port + - target: 2380 # Port assinged to etcd in the container + published: ${COMPOSE_ETCD_LISTEN_PEER_PORT} # Port to be exposed to the host + protocol: tcp # Protocol of the port + volumes: + - ${COMPOSE_ETCD_VOLUME_HOST_PATH}:${COMPOSE_ETCD_VOLUME_CONTAINTER_PATH} + entrypoint: /usr/local/bin/etcd + command: + - --name + - ${COMPOSE_ETCD_NAME_OF_THIS_MEMBER} + - --data-dir + - ${COMPOSE_ETCD_DATA_DIR} + - --listen-client-urls + - ${COMPOSE_ETCD_LISTEN_CLIENT_URLS} + - --advertise-client-urls + - ${COMPOSE_ETCD_ADVERTISE_CLIENT_URLS} + - --listen-peer-urls + - ${COMPOSE_ETCD_LISTEN_PEER_URLS} + - --initial-advertise-peer-urls + - ${COMPOSE_ETCD_INITIAL_ADVERTISE_PEER_URLS} + - --initial-cluster + - ${COMPOSE_ETCD_INITIAL_CLUSTER} + - --initial-cluster-token + - ${COMPOSE_ETCD_INITIAL_CLUSTER_TOKEN} + - --initial-cluster-state + - ${COMPOSE_ETCD_INITIAL_CLUSTER_STATE} + - --log-level + - ${COMPOSE_ETCD_LOG_LEVEL} + - --logger + - ${COMPOSE_ETCD_LOGGER} + - --log-outputs + - ${COMPOSE_ETCD_LOG_OUTPUTS} + - --auth-token + - ${COMPOSE_ETCD_AUTH_TOKEN} + healthcheck: # for etcd + test: [ "CMD", "/usr/local/bin/etcd", "--version"] + interval: 1m + timeout: 5s + retries: 3 + start_period: 10s + + # etcd-conf + etcd-conf: + image: alpine:latest + container_name: etcd-conf + depends_on: + - etcd + volumes: + - ./scripts/etcd/:/scripts/etcd/ + # env_file: + # - ./scripts/etcd/.env + environment: + - ETCD_VERSION_TAG=${COMPOSE_ETCD_CONF_VERSION_TAG} + - ETCD_ENDPOINTS=${COMPOSE_ETCD_CONF_ENDPOINTS} + - ETCD_PATH=${COMPOSE_ETCD_CONF_ETCD_PATH} + - ETCD_AUTH_ENABLED=${COMPOSE_ETCD_CONF_AUTH_ENABLED} + - ETCD_ROOT_PASSWORD=${COMPOSE_ETCD_CONF_ROOT_PASSWORD} + - ETCD_ADMIN_USERNAME=${COMPOSE_ETCD_CONF_ADMIN_USERNAME} + - ETCD_ADMIN_PASSWORD=${COMPOSE_ETCD_CONF_ADMIN_PASSWORD} + command: sh -c "sh /scripts/etcd/etcd-conf.sh" + healthcheck: # for etcd-conf + test: ["CMD", "test", "-f", "/tmp/healthcheck"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 10s + + # CB-Spider + cb-spider: + image: cloudbaristaorg/cb-spider:${COMPOSE_SP_VERSION_TAG} + container_name: cb-spider + platform: linux/amd64 + ports: + - target: 1024 + published: ${COMPOSE_SP_REST_PORT} + protocol: tcp + volumes: + # - ./conf/log_conf.yaml:/root/go/src/github.com/cloud-barista/cb-spider/conf/log_conf.yaml + # - ./conf/store_conf.yaml:/root/go/src/github.com/cloud-barista/cb-spider/conf/store_conf.yaml + - ${COMPOSE_SP_VOLUME_HOST_PATH}/meta_db/:${COMPOSE_SP_VOLUME_CONTAINTER_PATH}/meta_db/ + - ${COMPOSE_SP_VOLUME_HOST_PATH}/log/:${COMPOSE_SP_VOLUME_CONTAINTER_PATH}/log/ + environment: + - PLUGIN_SW=${COMPOSE_SP_PLUGSIN_SW} + - SERVER_ADDRESS=${COMPOSE_SP_SERVER_ADDRESS} + # if you leave these values empty, REST Auth will be disabled. + # - API_USERNAME=${COMPOSE_SP_API_USERNAME} + # - API_PASSWORD=${COMPOSE_SP_API_PASSWORD} + - SPIDER_LOG_LEVEL=${COMPOSE_SP_LOG_LEVEL} + - SPIDER_HISCALL_LOG_LEVEL=${COMPOSE_SP_HISCALL_LOG_LEVEL} + - ID_TRANSFORM_MODE=${COMPOSE_SP_ID_TRANSFORM_MODE} + healthcheck: # for CB-Spider + test: [ "CMD", "curl", "-f", "http://localhost:1024/spider/readyz" ] + interval: 1m + timeout: 5s + retries: 3 + start_period: 10s + + # CB-Tumblebug + cb-tumblebug: + image: cloudbaristaorg/cb-tumblebug:${COMPOSE_TB_VERSION_TAG} + build: + context: . + dockerfile: Dockerfile + container_name: cb-tumblebug + platform: linux/amd64 + ports: + - target: 1323 + published: ${COMPOSE_TB_REST_PORT} + protocol: tcp + depends_on: + - cb-spider + - etcd-conf + volumes: + # - ./conf/:/app/conf/ + - ${COMPOSE_TB_VOLUME_HOST_PATH}/meta_db/:${COMPOSE_TB_VOLUME_CONTAINTER_PATH}/meta_db/ + - ${COMPOSE_TB_VOLUME_HOST_PATH}/log/:${COMPOSE_TB_VOLUME_CONTAINTER_PATH}/log/ + environment: + - CBTUMBLEBUG_ROOT=${COMPOSE_TB_CBTUMBLEBUG_ROOT} + - SPIDER_CALL_METHOD=${COMPOSE_TB_SPIDER_CALL_METHOD} + - SPIDER_REST_URL=${COMPOSE_TB_SPIDER_REST_URL} + - DRAGONFLY_CALL_METHOD=${COMPOSE_TB_DRAGONFLY_CALL_METHOD} + - DRAGONFLY_REST_URL=${COMPOSE_TB_DRAGONFLY_REST_URL} + - DB_URL=${COMPOSE_TB_DB_URL} + - DB_DATABASE=${COMPOSE_TB_DB_DATABASE} + - DB_USER=${COMPOSE_TB_DB_USER} + - DB_PASSWORD=${COMPOSE_TB_DB_PASSWORD} + - ALLOW_ORIGINS=${COMPOSE_TB_ALLOW_ORIGINS} + - AUTH_ENABLED=${COMPOSE_TB_AUTH_ENABLED} + - API_USERNAME=${COMPOSE_TB_API_USERNAME} + - API_PASSWORD=${COMPOSE_TB_API_PASSWORD} + - AUTOCONTROL_DURATION_MS=${COMPOSE_TB_AUTOCONTROL_DURATION_MS} + - SELF_ENDPOINT=${COMPOSE_TB_SELF_ENDPOINT} + - API_DOC_PATH=${COMPOSE_TB_API_DOC_PATH} + - DEFAULT_NAMESPACE=${COMPOSE_TB_DEFAULT_NAMESPACE} + - DEFAULT_CREDENTIALHOLDER=${COMPOSE_TB_DEFAULT_CREDENTIALHOLDER} + - LOGFILE_PATH=${COMPOSE_TB_LOGFILE_PATH} + - LOGFILE_MAXSIZE=${COMPOSE_TB_LOGFILE_MAXSIZE} + - LOGFILE_MAXBACKUPS=${COMPOSE_TB_LOGFILE_MAXBACKUPS} + - LOGFILE_MAXAGE=${COMPOSE_TB_LOGFILE_MAXAGE} + - LOGFILE_COMPRESS=${COMPOSE_TB_LOGFILE_COMPRESS} + - LOGLEVEL=${COMPOSE_TB_LOGLEVEL} + - LOGWRITER=${COMPOSE_TB_LOGWRITER} + - NODE_ENV=${COMPOSE_TB_NODE_ENV} + healthcheck: # for CB-Tumblebug + test: [ "CMD", "curl", "-f", "http://localhost:1323/tumblebug/readyz" ] + interval: 1m + timeout: 5s + retries: 3 + start_period: 10s + + # cb-mapui + cb-mapui: + image: cloudbaristaorg/cb-mapui:${COMPOSE_MAP_VERSION_TAG} + container_name: cb-mapui + ports: + - target: 1324 + published: ${COMPOSE_MAP_PORT} + protocol: tcp + # depends_on: + # - cb-tumblebug + healthcheck: # for cb-mapui + test: ["CMD", "nc", "-vz", "localhost", "1324"] + interval: 1m + timeout: 5s + retries: 3 + start_period: 10s diff --git a/docker-compose.yaml b/docker-compose.yaml index 3e145ba70..a33f9ae19 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -5,8 +5,12 @@ services: image: gcr.io/etcd-development/etcd:v3.5.14 container_name: etcd ports: - - "2379:2379" - - "2380:2380" + - target: 2379 # Port assinged to etcd in the container + published: 2379 # Port to be exposed to the host + protocol: tcp # Protocol of the port + - target: 2380 # Port assinged to etcd in the container + published: 2380 # Port to be exposed to the host + protocol: tcp # Protocol of the port volumes: - ./container-volume/etcd/data:/etcd-data entrypoint: /usr/local/bin/etcd @@ -44,18 +48,24 @@ services: retries: 3 start_period: 10s - # etcd-setup-auth - etcd-setup-auth: + # etcd-conf + etcd-conf: image: alpine:latest - container_name: etcd-setup-auth + container_name: etcd-conf depends_on: - etcd volumes: - ./scripts/etcd/:/scripts/etcd/ - env_file: - - ./scripts/etcd/.env - command: sh -c "sh /scripts/etcd/setup-auth.sh" - healthcheck: # for etcd-setup-auth + environment: + - ETCD_VERSION_TAG=v3.5.14 + - ETCD_ENDPOINTS=http://etcd:2379 + - ETCD_PATH=/tmp/etcd-download-test + - ETCD_AUTH_ENABLED=true + - ETCD_ROOT_PASSWORD=default + - ETCD_ADMIN_USERNAME=default + - ETCD_ADMIN_PASSWORD=default + command: sh -c "sh /scripts/etcd/etcd-conf.sh" + healthcheck: # for etcd-conf test: ["CMD", "test", "-f", "/tmp/healthcheck"] interval: 30s timeout: 10s @@ -73,7 +83,7 @@ services: protocol: tcp volumes: # - ./conf/log_conf.yaml:/root/go/src/github.com/cloud-barista/cb-spider/conf/log_conf.yaml - - ./conf/store_conf.yaml:/root/go/src/github.com/cloud-barista/cb-spider/conf/store_conf.yaml + # - ./conf/store_conf.yaml:/root/go/src/github.com/cloud-barista/cb-spider/conf/store_conf.yaml - ./container-volume/cb-spider-container/meta_db/:/root/go/src/github.com/cloud-barista/cb-spider/meta_db/ - ./container-volume/cb-spider-container/log/:/root/go/src/github.com/cloud-barista/cb-spider/log/ environment: @@ -106,7 +116,7 @@ services: protocol: tcp depends_on: - cb-spider - - etcd-setup-auth + - etcd-conf volumes: - ./conf/:/app/conf/ - ./container-volume/cb-tumblebug-container/meta_db/:/app/meta_db/ diff --git a/scripts/etcd/.env b/scripts/etcd/.env index 38dc90387..7b1746eb2 100644 --- a/scripts/etcd/.env +++ b/scripts/etcd/.env @@ -1,4 +1,14 @@ -ETCD_AUTH_ENABLED=true -ETCD_ROOT_PASSWORD=default -ETCD_ADMIN_USERNAME=default -ETCD_ADMIN_PASSWORD=default \ No newline at end of file +ETCD_VERSION_TAG= +# default: ETCD_VERSION_TAG=v3.5.14 +ETCD_ENDPOINTS= +# default: ETCD_ENDPOINTS=http://etcd:2379 +ETCD_PATH= +# default: ETCD_PATH=/tmp/etcd-download-test +ETCD_AUTH_ENABLED= +# default: =ETCD_AUTH_ENABLED=true +ETCD_ROOT_PASSWORD= +# default: ETCD_ROOT_PASSWORD=default +ETCD_ADMIN_USERNAME= +# default: ETCD_ADMIN_USERNAME=default +ETCD_ADMIN_PASSWORD= +# default: ETCD_ADMIN_PASSWORD=default diff --git a/scripts/etcd/setup-auth.sh b/scripts/etcd/etcd-conf.sh similarity index 80% rename from scripts/etcd/setup-auth.sh rename to scripts/etcd/etcd-conf.sh index 4ca7224d7..70cb1470d 100755 --- a/scripts/etcd/setup-auth.sh +++ b/scripts/etcd/etcd-conf.sh @@ -1,16 +1,18 @@ #!/bin/bash -ETCD_VER=v3.5.14 +ETCD_VERSION=${ETCD_VERSION_TAG:-v3.5.14} -ENDPOINTS="http://etcd:2379" -ETCD_PATH="/tmp/etcd-download-test" -ETCD_CTL="${ETCD_PATH}/etcdctl --endpoints=${ENDPOINTS}" +ENDPOINTS=${ETCD_ENDPOINTS:-"http://etcd:2379"} +ETCD_BINS=${ETCD_PATH:-"/tmp/etcd-download-test"} +ETCD_CTL="${ETCD_BINS}/etcdctl --endpoints=${ENDPOINTS}" +AUTH_ENABLED=${ETCD_AUTH_ENABLED:-true} ROOT_USERNAME="root" # Require 'root' to enable authentication ROOT_PASSWORD=${ETCD_ROOT_PASSWORD:-default} ADMIN_USERNAME=${ETCD_ADMIN_USERNAME:-default} ADMIN_PASSWORD=${ETCD_ADMIN_PASSWORD:-default} + # Choose either URL GOOGLE_URL=https://storage.googleapis.com/etcd GITHUB_URL=https://github.com/etcd-io/etcd/releases/download @@ -23,20 +25,20 @@ apk update > /dev/null apk add --no-cache curl tar > /dev/null # Clean up any previous downloads and create the target directory -rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz -rm -rf ${ETCD_PATH} && mkdir -p ${ETCD_PATH} +rm -f /tmp/etcd-${ETCD_VERSION}-linux-amd64.tar.gz +rm -rf ${ETCD_BINS} && mkdir -p ${ETCD_BINS} # Download the etcd tarball quietly, but show errors -curl -sSL ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz +curl -sSL ${DOWNLOAD_URL}/${ETCD_VERSION}/etcd-${ETCD_VERSION}-linux-amd64.tar.gz -o /tmp/etcd-${ETCD_VERSION}-linux-amd64.tar.gz # Extract the tarball to the target directory quietly, but show errors -tar xzf /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz -C ${ETCD_PATH} --strip-components=1 2>&1 +tar xzf /tmp/etcd-${ETCD_VERSION}-linux-amd64.tar.gz -C ${ETCD_BINS} --strip-components=1 2>&1 # Clean up the downloaded tarball -rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz +rm -f /tmp/etcd-${ETCD_VERSION}-linux-amd64.tar.gz -# Enable auth if ETCD_AUTH_ENABLED is true -if [ "$ETCD_AUTH_ENABLED" = "true" ]; then +# Enable auth if AUTH_ENABLED is true +if [ "$AUTH_ENABLED" = "true" ]; then - echo "ETCD_AUTH_ENABLED is true" + echo "AUTH_ENABLED is true" # Try to check auth status without authentication RET_CHECK_AUTH=$($ETCD_CTL auth status 2>/dev/null) @@ -86,10 +88,10 @@ if [ "$ETCD_AUTH_ENABLED" = "true" ]; then fi -# Disable auth if ETCD_AUTH_ENABLED is false -elif [ "$ETCD_AUTH_ENABLED" = "false" ]; then +# Disable auth if AUTH_ENABLED is false +elif [ "$AUTH_ENABLED" = "false" ]; then - echo "ETCD_AUTH_ENABLED is false" + echo "AUTH_ENABLED is false" # Try to check auth status without authentication RET_CHECK_AUTH=$($ETCD_CTL auth status 2>/dev/null) @@ -136,7 +138,7 @@ elif [ "$ETCD_AUTH_ENABLED" = "false" ]; then fi else - echo "Invalid value for ETCD_AUTH_ENABLED. Please set it to 'true' or 'false'." + echo "Invalid value for AUTH_ENABLED. Please set it to 'true' or 'false'." exit 1 fi From fc500b7377fbb336b8e48d089483a964dfb73c37 Mon Sep 17 00:00:00 2001 From: Yunkon Kim Date: Mon, 29 Jul 2024 14:59:13 +0900 Subject: [PATCH 10/11] Ignore docker-compose files and `.env` files --- .gitignore | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index bec4e655b..037e7ce28 100644 --- a/.gitignore +++ b/.gitignore @@ -63,8 +63,8 @@ log/ *.history # Ignore all docker-compose.yaml files created by users, except docker-compose.yaml -# *docker-compose*.yaml +*docker-compose*.yaml !docker-compose.yaml # .env file for docker-compose -# .env +.env From 6279ae71649c947407b7741edd1c290c1f9ceca4 Mon Sep 17 00:00:00 2001 From: Yunkon Kim Date: Mon, 29 Jul 2024 16:16:26 +0900 Subject: [PATCH 11/11] Fix env name and disable cb-tumblebug build --- docker-compose-custom.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docker-compose-custom.yaml b/docker-compose-custom.yaml index 6afbf5177..cb0986284 100644 --- a/docker-compose-custom.yaml +++ b/docker-compose-custom.yaml @@ -18,7 +18,7 @@ services: - --name - ${COMPOSE_ETCD_NAME_OF_THIS_MEMBER} - --data-dir - - ${COMPOSE_ETCD_DATA_DIR} + - ${COMPOSE_ETCD_VOLUME_CONTAINTER_PATH} - --listen-client-urls - ${COMPOSE_ETCD_LISTEN_CLIENT_URLS} - --advertise-client-urls @@ -107,9 +107,9 @@ services: # CB-Tumblebug cb-tumblebug: image: cloudbaristaorg/cb-tumblebug:${COMPOSE_TB_VERSION_TAG} - build: - context: . - dockerfile: Dockerfile + # build: + # context: . + # dockerfile: Dockerfile container_name: cb-tumblebug platform: linux/amd64 ports: