Skip to content

Commit

Permalink
Merge pull request #78 from natron-io/pvc_list
Browse files Browse the repository at this point in the history
Pvc list
  • Loading branch information
janlauber authored Feb 10, 2022
2 parents d87bd78 + 82b315b commit f813c6a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ You can add `<tenant>` in front of the path to get the tenant specific data (of
`/api/v1/notifications` - Get the Slack notification messages of the broadcast channel provided via envs

##### general tenant resources
`/api/v1/<tenant>/pods` - Get pods of a tenant
`/api/v1/<tenant>/pods` - Get a list of pods of a tenant \
`/api/v1/<tenant>/pvcs` - Get a list of pvcs of a tenant \
`/api/v1/<tenant>/ingress` - Get a list of ingresses of a tenant

##### specific tenant resources
`/api/v1/<tenant>/requests/cpu` - Get cpurequests in **Milicores** of a tenant \
`/api/v1/<tenant>/requests/memory` - Get memoryrequests in **Bytes** of a tenant \
`/api/v1/<tenant>/requests/storage` - Get storagerequests in **Bytes** of a tenant by storageclass \
`/api/v1/<tenant>/requests/ingress` - Get ingress resourcess total of a tenant by ingressclass

##### tenant resources costs
`/api/v1/<tenant>/costs/cpu` - Get the CPU costs by CPU \
Expand Down
41 changes: 36 additions & 5 deletions controllers/tenantController.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,37 @@ func GetPods(c *fiber.Ctx) error {
}
}

// GetPVCs returns a list of PVCs by authenticated users tenants
func GetPVCs(c *fiber.Ctx) error {
util.InfoLogger.Printf("%s %s %s", c.IP(), c.Method(), c.Path())
tenant := c.Params("tenant")
tenants := CheckAuth(c)
if len(tenants) == 0 {
return c.Status(401).JSON(fiber.Map{
"message": "Unauthorized",
})
}
if tenant != "" && !util.Contains(tenant, tenants) {
return c.Status(403).JSON(fiber.Map{
"message": "Forbidden",
})
}

// create a map for each tenant with a added memory requests
tenantPVCs, err := util.GetPVCsByTenant(tenants)
if err != nil {
return c.Status(500).JSON(fiber.Map{
"message": "Internal Server Error",
})
}

if tenant == "" {
return c.JSON(tenantPVCs)
} else {
return c.JSON(tenantPVCs[tenant])
}
}

// GetCPURequestsSum returns the sum of all cpu requests by authenticated users tenants
func GetCPURequestsSum(c *fiber.Ctx) error {

Expand Down Expand Up @@ -142,22 +173,22 @@ func GetStorageRequestsSum(c *fiber.Ctx) error {
}

// create a map for each tenant with a map of storage classes with calculated pvcs in it
tenantPVCs, err := util.GetStorageRequestsSumByTenant(tenants)
storageRequestsSum, err := util.GetStorageRequestsSumByTenant(tenants)
if err != nil {
return c.Status(500).JSON(fiber.Map{
"message": "Internal Server Error",
})
}

if tenant == "" {
return c.JSON(tenantPVCs)
return c.JSON(storageRequestsSum)
} else {
return c.JSON(tenantPVCs[tenant])
return c.JSON(storageRequestsSum[tenant])
}
}

// GetIngressRequestsSum returns the sum of all ingress requests by authenticated users tenants
func GetIngressRequestsSum(c *fiber.Ctx) error {
// GetIngresses returns the sum of all ingress requests by authenticated users tenants
func GetIngresses(c *fiber.Ctx) error {

util.InfoLogger.Printf("%s %s %s", c.IP(), c.Method(), c.Path())
tenant := c.Params("tenant")
Expand Down
3 changes: 2 additions & 1 deletion routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ func Setup(app *fiber.App, clientset *kubernetes.Clientset) {

// Specific Tenant
v1.Get(":tenant/pods", controllers.GetPods)
v1.Get(":tenant/pvcs", controllers.GetPVCs)
v1.Get(":tenant/ingresses", controllers.GetIngresses)

// Specific Tenant
requests := v1.Group(":tenant/requests")
requests.Get("/cpu", controllers.GetCPURequestsSum)
requests.Get("/memory", controllers.GetMemoryRequestsSum)
requests.Get("/storage", controllers.GetStorageRequestsSum)
requests.Get("/ingress", controllers.GetIngressRequestsSum)

// Per tenant
costs := v1.Group(":tenant/costs")
Expand Down
15 changes: 15 additions & 0 deletions util/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ func GetPodsByTenant(tenants []string) (map[string][]string, error) {
return tenantPods, nil
}

func GetPVCsByTenant(tenants []string) (map[string][]string, error) {
tenantPVCs := make(map[string][]string)
for _, tenant := range tenants {
pvcList, err := Clientset.CoreV1().PersistentVolumeClaims(tenant).List(context.TODO(), metav1.ListOptions{})
if err != nil && !strings.Contains(err.Error(), "not found") {
return nil, err
}

for _, pvc := range pvcList.Items {
tenantPVCs[tenant] = append(tenantPVCs[tenant], pvc.Name)
}
}
return tenantPVCs, nil
}

// GetCPURequestsSumByTenant returns the sum of CPU requests for each tenant
func GetCPURequestsSumByTenant(tenants []string) (map[string]int64, error) {
tenantCPURequests := make(map[string]int64)
Expand Down

0 comments on commit f813c6a

Please sign in to comment.