Skip to content

Commit

Permalink
Deduplicate code for checking k8scluster enablement
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-github-robot authored Jul 26, 2024
2 parents 40d42fd + 1081fee commit 87d89c5
Showing 1 changed file with 80 additions and 104 deletions.
184 changes: 80 additions & 104 deletions src/core/mcis/k8scluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -1655,65 +1596,36 @@ 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
}

log.Debug().Msg("<" + kv.Key + "> \n" + kv.Value)

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
}

/*
* 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
}

/*
* 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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 87d89c5

Please sign in to comment.