diff --git a/capten/agent/internal/capten-store/managed_cluster.go b/capten/agent/internal/capten-store/managed_cluster.go index 577dff39..60c3efa7 100644 --- a/capten/agent/internal/capten-store/managed_cluster.go +++ b/capten/agent/internal/capten-store/managed_cluster.go @@ -11,32 +11,39 @@ import ( ) const ( - insertManagedCluster = "INSERT INTO %s.ManagedClusters(id, cluster_name, cluster_endpoint, cluster_deploy_status, app_deploy_status, last_update_time) VALUES (?,?,?,?,?,?)" - insertManagedClusterId = "INSERT INTO %s.ManagedClusters(id) VALUES (?)" - updateManagedClusterById = "UPDATE %s.ManagedClusters SET %s WHERE id=?" - deleteManagedClusterById = "DELETE FROM %s.ManagedClusters WHERE id= ?" - selectAllManagedClusters = "SELECT id, cluster_name, cluster_endpoint, cluster_deploy_status, app_deploy_status, last_update_time FROM %s.ManagedClusters" - selectAllManagedClustersByLabels = "SELECT id, cluster_name, cluster_endpoint, cluster_deploy_status, app_deploy_status, last_update_time FROM %s.ManagedClusters WHERE %s" - selectGetManagedClusterById = "SELECT id, cluster_name, cluster_endpoint, cluster_deploy_status, app_deploy_status, last_update_time FROM %s.ManagedClusters WHERE id=%s;" + insertManagedCluster = "INSERT INTO %s.ManagedClusters(id, cluster_name, cluster_endpoint, cluster_deploy_status, app_deploy_status, last_update_time) VALUES (?,?,?,?,?,?)" + insertManagedClusterId = "INSERT INTO %s.ManagedClusters(id) VALUES (?)" + updateManagedClusterById = "UPDATE %s.ManagedClusters SET %s WHERE id=?" + deleteManagedClusterById = "DELETE FROM %s.ManagedClusters WHERE id= ?" + selectAllManagedClusters = "SELECT id, cluster_name, cluster_endpoint, cluster_deploy_status, app_deploy_status, last_update_time FROM %s.ManagedClusters" + selectAllManagedClustersByLabels = "SELECT id, cluster_name, cluster_endpoint, cluster_deploy_status, app_deploy_status, last_update_time FROM %s.ManagedClusters WHERE %s" + selectGetManagedClusterById = "SELECT id, cluster_name, cluster_endpoint, cluster_deploy_status, app_deploy_status, last_update_time FROM %s.ManagedClusters WHERE id=%s;" + selectGetManagedClusterByClusterName = "SELECT id, cluster_name, cluster_endpoint, cluster_deploy_status, app_deploy_status, last_update_time FROM %s.ManagedClusters WHERE cluster_name=%s ALLOW FILTERING;" ) func (a *Store) UpsertManagedCluster(config *captenpluginspb.ManagedCluster) error { config.LastUpdateTime = time.Now().Format(time.RFC3339) batch := a.client.Session().NewBatch(gocql.LoggedBatch) - batch.Query(fmt.Sprintf(insertManagedCluster, a.keyspace), config.Id, config.ClusterName, config.ClusterEndpoint, config.ClusterDeployStatus, config.AppDeployStatus, config.LastUpdateTime) - err := a.client.Session().ExecuteBatch(batch) + + query := fmt.Sprintf(selectGetManagedClusterByClusterName, a.keyspace, config.ClusterName) + clusters, err := a.executeManagedClustersSelectQuery(query) if err != nil { + batch.Query(fmt.Sprintf(insertManagedCluster, a.keyspace), config.Id, config.ClusterName, config.ClusterEndpoint, config.ClusterDeployStatus, config.AppDeployStatus, config.LastUpdateTime) + } else if len(clusters) > 0 { updatePlaceholders, values := formUpdateKvPairsForManagedCluster(config) if updatePlaceholders == "" { - return err + return fmt.Errorf("empty values found") } query := fmt.Sprintf(updateManagedClusterById, a.keyspace, updatePlaceholders) args := append(values, config.Id) - batch = a.client.Session().NewBatch(gocql.LoggedBatch) batch.Query(query, args...) - err = a.client.Session().ExecuteBatch(batch) } - return err + + if err := a.client.Session().ExecuteBatch(batch); err != nil { + return err + } + + return nil } func (a *Store) DeleteManagedClusterById(id string) error { diff --git a/capten/agent/internal/crossplane/cluster_claims.go b/capten/agent/internal/crossplane/cluster_claims.go index 69034330..79246257 100644 --- a/capten/agent/internal/crossplane/cluster_claims.go +++ b/capten/agent/internal/crossplane/cluster_claims.go @@ -344,10 +344,10 @@ func (h *ClusterClaimSyncHandler) triggerClusterDelete(clusterName string, manag return fmt.Errorf("failed to send event to workflow to configure %s, %v", managedCluster.ClusterEndpoint, err) } - h.log.Infof("Crossplane project delete %s config workflow %s created", managedCluster.ClusterEndpoint, wkfId) - go h.monitorCrossplaneWorkflow(managedCluster, wkfId) + h.log.Infof("Crossplane project delete %s config workflow %s created", managedCluster.ClusterEndpoint, wkfId) + return nil } diff --git a/capten/common-pkg/plugins/git/git.go b/capten/common-pkg/plugins/git/git.go index 54fa44cb..0ca6cbe8 100644 --- a/capten/common-pkg/plugins/git/git.go +++ b/capten/common-pkg/plugins/git/git.go @@ -49,12 +49,6 @@ func (g *GitClient) Add(path string) error { return err } - s, err := w.Status() - if err != nil { - fmt.Println("Error => " + err.Error()) - } - fmt.Printf("Status = \n %+v \n", s) - _, err = w.Add(path) if err != nil { return err diff --git a/capten/config-worker/internal/crossplane/config_cluster_updates.go b/capten/config-worker/internal/crossplane/config_cluster_updates.go index 14373d12..4da34c6e 100644 --- a/capten/config-worker/internal/crossplane/config_cluster_updates.go +++ b/capten/config-worker/internal/crossplane/config_cluster_updates.go @@ -217,8 +217,7 @@ func (cp *CrossPlaneApp) configureClusterDelete(ctx context.Context, req *model. return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to remove cluster folder") } - err = cp.helper.AddFilesToRepo([]string{"."}) - if err != nil { + if err := cp.helper.AddFilesToRepo([]string{"."}); err != nil { return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to add git repo") } @@ -261,8 +260,6 @@ func removeClusterValues(valuesFileName, clusterName string) error { } } - fmt.Printf("newclusters \n %+v \n", newclusters) - clusterConfig.Clusters = &newclusters jsonBytes, err := json.Marshal(clusterConfig) if err != nil { @@ -274,9 +271,6 @@ func removeClusterValues(valuesFileName, clusterName string) error { return err } - fmt.Println("final file") - fmt.Printf("%s \n", string(yamlBytes)) - err = os.WriteFile(valuesFileName, yamlBytes, os.ModeAppend) return err }