-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add list execution contexts api
- Loading branch information
Showing
4 changed files
with
124 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.