diff options
Diffstat (limited to 'src/clm/pkg/cluster/cluster.go')
-rw-r--r-- | src/clm/pkg/cluster/cluster.go | 84 |
1 files changed, 65 insertions, 19 deletions
diff --git a/src/clm/pkg/cluster/cluster.go b/src/clm/pkg/cluster/cluster.go index 06faafd2..fb8768d6 100644 --- a/src/clm/pkg/cluster/cluster.go +++ b/src/clm/pkg/cluster/cluster.go @@ -17,9 +17,12 @@ package cluster import ( - appcontext "github.com/onap/multicloud-k8s/src/orchestrator/pkg/appcontext" + "time" + + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/appcontext" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" mtypes "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/types" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/state" pkgerrors "github.com/pkg/errors" ) @@ -28,7 +31,7 @@ type clientDbInfo struct { storeName string // name of the mongodb collection to use for client documents tagMeta string // attribute key name for the json data of a client document tagContent string // attribute key name for the file data of a client document - tagContext string // attribute key name for context object in App Context + tagState string // attribute key name for StateInfo object in the cluster } // ClusterProvider contains the parameters needed for ClusterProviders @@ -101,7 +104,7 @@ type ClusterManager interface { CreateCluster(provider string, pr Cluster, qr ClusterContent) (Cluster, error) GetCluster(provider, name string) (Cluster, error) GetClusterContent(provider, name string) (ClusterContent, error) - GetClusterContext(provider, name string) (appcontext.AppContext, error) + GetClusterState(provider, name string) (state.StateInfo, error) GetClusters(provider string) ([]Cluster, error) GetClustersWithLabel(provider, label string) ([]string, error) DeleteCluster(provider, name string) error @@ -129,7 +132,7 @@ func NewClusterClient() *ClusterClient { storeName: "cluster", tagMeta: "clustermetadata", tagContent: "clustercontent", - tagContext: "clustercontext", + tagState: "stateInfo", }, } } @@ -254,6 +257,20 @@ func (v *ClusterClient) CreateCluster(provider string, p Cluster, q ClusterConte return Cluster{}, pkgerrors.Wrap(err, "Creating DB Entry") } + // Add the stateInfo record + s := state.StateInfo{} + a := state.ActionEntry{ + State: state.StateEnum.Created, + ContextId: "", + TimeStamp: time.Now(), + } + s.Actions = append(s.Actions, a) + + err = db.DBconn.Insert(v.db.storeName, key, nil, v.db.tagState, s) + if err != nil { + return Cluster{}, pkgerrors.Wrap(err, "Creating cluster StateInfo") + } + return p, nil } @@ -309,31 +326,29 @@ func (v *ClusterClient) GetClusterContent(provider, name string) (ClusterContent return ClusterContent{}, pkgerrors.New("Error getting Cluster Content") } -// GetClusterContext returns the AppContext for corresponding provider and name -func (v *ClusterClient) GetClusterContext(provider, name string) (appcontext.AppContext, error) { +// GetClusterState returns the StateInfo structure for corresponding cluster provider and cluster +func (v *ClusterClient) GetClusterState(provider, name string) (state.StateInfo, error) { //Construct key and tag to select the entry key := ClusterKey{ ClusterProviderName: provider, ClusterName: name, } - value, err := db.DBconn.Find(v.db.storeName, key, v.db.tagContext) + result, err := db.DBconn.Find(v.db.storeName, key, v.db.tagState) if err != nil { - return appcontext.AppContext{}, pkgerrors.Wrap(err, "Get Cluster Context") + return state.StateInfo{}, pkgerrors.Wrap(err, "Get Cluster StateInfo") } - //value is a byte array - if value != nil { - ctxVal := string(value[0]) - var cc appcontext.AppContext - _, err = cc.LoadAppContext(ctxVal) + if result != nil { + s := state.StateInfo{} + err = db.DBconn.Unmarshal(result[0], &s) if err != nil { - return appcontext.AppContext{}, pkgerrors.Wrap(err, "Reinitializing Cluster AppContext") + return state.StateInfo{}, pkgerrors.Wrap(err, "Unmarshalling Cluster StateInfo") } - return cc, nil + return s, nil } - return appcontext.AppContext{}, pkgerrors.New("Error getting Cluster AppContext") + return state.StateInfo{}, pkgerrors.New("Error getting Cluster StateInfo") } // GetClusters returns all the Clusters for corresponding provider @@ -393,9 +408,40 @@ func (v *ClusterClient) DeleteCluster(provider, name string) error { ClusterProviderName: provider, ClusterName: name, } - _, err := v.GetClusterContext(provider, name) - if err == nil { - return pkgerrors.Errorf("Cannot delete cluster until context is deleted: %v, %v", provider, name) + s, err := v.GetClusterState(provider, name) + if err != nil { + return pkgerrors.Errorf("Error getting current state from Cluster: " + name) + } + + stateVal, err := state.GetCurrentStateFromStateInfo(s) + if err != nil { + return pkgerrors.Errorf("Error getting current state from Cluster stateInfo: " + name) + } + + if stateVal == state.StateEnum.Applied { + return pkgerrors.Errorf("Cluster network intents must be terminated before it can be deleted " + name) + } + + // remove the app contexts associated with this cluster + if stateVal == state.StateEnum.Terminated { + // Verify that the appcontext has completed terminating + ctxid := state.GetLastContextIdFromStateInfo(s) + acStatus, err := state.GetAppContextStatus(ctxid) + if err == nil && + !(acStatus.Status == appcontext.AppContextStatusEnum.Terminated || acStatus.Status == appcontext.AppContextStatusEnum.TerminateFailed) { + return pkgerrors.Errorf("Network intents for cluster have not completed terminating " + name) + } + + for _, id := range state.GetContextIdsFromStateInfo(s) { + context, err := state.GetAppContextFromId(id) + if err != nil { + return pkgerrors.Wrap(err, "Error getting appcontext from Cluster StateInfo") + } + err = context.DeleteCompositeApp() + if err != nil { + return pkgerrors.Wrap(err, "Error deleting appcontext for Cluster") + } + } } err = db.DBconn.Remove(v.db.storeName, key) |