in projects/gke-optimization/binpacker/api/pkg/infrastructure/repository/metric/kubeapi/metric.go [40:95]
func (mr *MetricKubeapiRepository) FetchMetrics(ctx context.Context, projectID string) (*metric.Metric, error) {
machineTypes, err := CreateMachineTypes(ctx, projectID)
if err != nil {
return nil, err
}
c, err := container.NewClusterManagerClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get client for google kubernetes engine: %v", err)
}
defer c.Close()
req := &containerpb.ListClustersRequest{
Parent: fmt.Sprintf("projects/%s/locations/-", projectID),
}
resp, err := c.ListClusters(ctx, req)
if err != nil {
return nil, fmt.Errorf("failed to list clusters: %s", err)
}
standardClusters, err := extractStandardClusters(ctx, resp.GetClusters(), machineTypes, projectID)
if err != nil {
return nil, err
}
glog.Infof("Found %d GKE standard clusters", len(standardClusters))
authConfigs, err := GenerateK8sClustersConfig(resp.Clusters, projectID)
if err != nil {
return nil, err
}
allClusters := make([]metric.Cluster, 0)
allPods := make([]metric.Pod, 0)
allNodePools := make([]metric.NodePool, 0)
for _, cluster := range standardClusters {
pods, err := cluster.GetWorkloads(ctx, authConfigs)
if err != nil {
return nil, err
}
allPods = append(allPods, pods...)
allNodePools = append(allNodePools, cluster.NodePools...)
allClusters = append(allClusters, metric.Cluster{
Name: cluster.Name,
Location: cluster.Location,
NumNodes: cluster.getTotalNumNodes(),
TotalCPU: cluster.getTotalVCPUs(),
TotalMemoryInGiB: cluster.getTotalMemoryInGiB(),
})
}
return &metric.Metric{
Pods: allPods,
NodePools: allNodePools,
Clusters: allClusters,
}, nil
}