Skip to content

Commit

Permalink
feat: add list execution contexts api
Browse files Browse the repository at this point in the history
  • Loading branch information
zhujian7 committed Aug 19, 2019
1 parent 5dafb56 commit e408f15
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 109 deletions.
14 changes: 4 additions & 10 deletions pkg/server/apis/v1alpha1/descriptors/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,18 @@ var tenant = []definition.Descriptor{
},
},
{
Path: "/tenants/{tenant}/precheck",
Description: "Checks worker clusters' status before running workflows",
Path: "/tenants/{tenant}/executioncontexts",
Description: "List execution contexts of a tenant, an execution context describes context of workflow execution, including information about cluster, namespace, pvc, etc.",
Definitions: []definition.Definition{
{
Method: definition.Get,
Method: definition.List,
Parameters: []definition.Parameter{
{
Source: definition.Path,
Name: httputil.TenantNamePathParameterName,
},
{
Source: definition.Query,
Name: "checklist",
Description: "Items to check",
Default: "ReservedResources",
},
},
Function: handler.Precheck,
Function: handler.ListExecutionContexts,
Results: definition.DataErrorResults("worker cluster status"),
},
},
Expand Down
37 changes: 37 additions & 0 deletions pkg/server/apis/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,40 @@ type StorageUsage struct {
type StorageCleanup struct {
Paths []string `json:"paths"`
}

// ExecutionContextSpec describes the execution context
type ExecutionContextSpec struct {
Cluster string `json:"cluster"`
Namespace string `json:"namespace"`
Integration string `json:"integration"`
PVC string `json:"pvc"`
}

// ExecutionContextStatus describe the status of execution context, it contains information that affects
// pipeline execution, like reserved resources, pvc status.
type ExecutionContextStatus struct {
// Phase of the execution context, could be 'Ready', 'NotReady' or 'Unknown'
Phase ExecutionContextPhase `json:"phase"`
ReservedResources map[core_v1.ResourceName]string `json:"reservedResources"`
PVC *core_v1.PersistentVolumeClaimStatus `json:"pvc"`
}

// ExecutionContext represtents a context used to execute workflows.
type ExecutionContext struct {
// Metadata for the particular object, including name, namespace, labels, etc
meta_v1.ObjectMeta `json:"metadata,omitempty"`
Spec ExecutionContextSpec `json:"spec"`
Status ExecutionContextStatus `json:"status"`
}

// ExecutionContextPhase represents the phase of ExecutionContext.
type ExecutionContextPhase string

const (
// ExecutionContextReady is ready phase
ExecutionContextReady ExecutionContextPhase = "Ready"
// ExecutionContextNotReady is not ready phase
ExecutionContextNotReady ExecutionContextPhase = "NotReady"
// ExecutionContextNotUnknown is unknown phase
ExecutionContextNotUnknown ExecutionContextPhase = "Unknown"
)
83 changes: 83 additions & 0 deletions pkg/server/handler/v1alpha1/executioncontext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package v1alpha1

import (
"context"

"github.com/caicloud/nirvana/log"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/caicloud/cyclone/pkg/common"
api "github.com/caicloud/cyclone/pkg/server/apis/v1alpha1"
"github.com/caicloud/cyclone/pkg/server/biz/integration/cluster"
svrcommon "github.com/caicloud/cyclone/pkg/server/common"
"github.com/caicloud/cyclone/pkg/server/config"
"github.com/caicloud/cyclone/pkg/server/handler"
"github.com/caicloud/cyclone/pkg/server/types"
)

// ListExecutionContexts list execution contexts of a tenant
func ListExecutionContexts(ctx context.Context, tenant string) (*types.ListResponse, error) {
var executionContexts []api.ExecutionContext

integrations, err := cluster.GetSchedulableClusters(handler.K8sClient, tenant)
if err != nil {
return nil, err
}

for _, integration := range integrations {
cluster := integration.Spec.Cluster
if cluster == nil {
log.Warningf("Cluster of integration %s is nil", integration.Name)
continue
}

pvcName := cluster.PVC
if pvcName == "" {
pvcName = svrcommon.TenantPVC(tenant)
}

executionContext := api.ExecutionContext{
Spec: api.ExecutionContextSpec{
Integration: integration.Name,
Cluster: cluster.ClusterName,
Namespace: cluster.Namespace,
PVC: pvcName,
},
Status: api.ExecutionContextStatus{
Phase: api.ExecutionContextNotUnknown,
ReservedResources: config.Config.StorageUsageWatcher.ResourceRequirements,
},
}

pvcStatus, err := getPVCStatus(tenant, pvcName, cluster)
if err != nil {
log.Warningf("Get PVC status in %s/%s", cluster.ClusterName, cluster.Namespace)
} else {
executionContext.Status.PVC = pvcStatus
if pvcStatus.Phase == "Bound" {
executionContext.Status.Phase = api.ExecutionContextReady
} else {
executionContext.Status.Phase = api.ExecutionContextNotReady
}
}

executionContexts = append(executionContexts, executionContext)
}

return types.NewListResponse(len(executionContexts), executionContexts), nil
}

func getPVCStatus(tenant, pvcName string, cluster *api.ClusterSource) (*corev1.PersistentVolumeClaimStatus, error) {
client, err := common.NewClusterClient(&cluster.Credential, cluster.IsControlCluster)
if err != nil {
return nil, err
}

pvc, err := client.CoreV1().PersistentVolumeClaims(cluster.Namespace).Get(pvcName, metav1.GetOptions{})
if err != nil {
return nil, err
}

return &pvc.Status, nil
}
99 changes: 0 additions & 99 deletions pkg/server/handler/v1alpha1/precheck.go

This file was deleted.

0 comments on commit e408f15

Please sign in to comment.