Skip to content

Commit

Permalink
Merge pull request #246 from openinfradev/project_k8s_info
Browse files Browse the repository at this point in the history
feature. implementation k8s resources for project namespace
  • Loading branch information
cho4036 authored Feb 27, 2024
2 parents 70159f8 + 8a83524 commit 191a896
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 25 deletions.
5 changes: 4 additions & 1 deletion api/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8038,7 +8038,7 @@ const docTemplate = `{
"cronjobs": {
"type": "integer"
},
"demonsets": {
"daemonsets": {
"type": "integer"
},
"deployments": {
Expand All @@ -8061,6 +8061,9 @@ const docTemplate = `{
},
"statefulsets": {
"type": "integer"
},
"updatedAt": {
"type": "string"
}
}
},
Expand Down
5 changes: 4 additions & 1 deletion api/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -8032,7 +8032,7 @@
"cronjobs": {
"type": "integer"
},
"demonsets": {
"daemonsets": {
"type": "integer"
},
"deployments": {
Expand All @@ -8055,6 +8055,9 @@
},
"statefulsets": {
"type": "integer"
},
"updatedAt": {
"type": "string"
}
}
},
Expand Down
4 changes: 3 additions & 1 deletion api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1759,7 +1759,7 @@ definitions:
properties:
cronjobs:
type: integer
demonsets:
daemonsets:
type: integer
deployments:
type: integer
Expand All @@ -1775,6 +1775,8 @@ definitions:
type: integer
statefulsets:
type: integer
updatedAt:
type: string
type: object
domain.ProjectNamespaceResponse:
properties:
Expand Down
2 changes: 1 addition & 1 deletion internal/delivery/http/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -1532,7 +1532,7 @@ func (p ProjectHandler) GetProjectNamespaceK8sResources(w http.ResponseWriter, r
return
}

k8sResources, err := p.usecase.GetK8sResources(r.Context(), organizationId, projectId, projectNamespace, stackId)
k8sResources, err := p.usecase.GetK8sResources(r.Context(), organizationId, projectId, projectNamespace, domain.StackId(stackId))
if err != nil {
log.ErrorWithContext(r.Context(), "Failed to get project resources.", err)
ErrorJSON(w, r, err)
Expand Down
83 changes: 71 additions & 12 deletions internal/usecase/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package usecase

import (
"context"
"fmt"
"time"

"github.com/google/uuid"
"github.com/openinfradev/tks-api/internal/keycloak"
Expand All @@ -13,6 +15,7 @@ import (
"github.com/openinfradev/tks-api/pkg/log"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
Expand Down Expand Up @@ -52,7 +55,7 @@ type IProjectUsecase interface {
CreateK8SNSRoleBinding(organizationId string, projectId string, stackId string, namespace string) error
DeleteK8SNSRoleBinding(organizationId string, projectId string, stackId string, namespace string) error
GetProjectKubeconfig(organizationId string, projectId string) (string, error)
GetK8sResources(ctx context.Context, organizationId string, projectId string, projectNamespace string, stackId string) (out domain.ProjectNamespaceK8sResources, err error)
GetK8sResources(ctx context.Context, organizationId string, projectId string, namespace string, stackId domain.StackId) (out domain.ProjectNamespaceK8sResources, err error)

AssignKeycloakClientRoleToMember(organizationId string, projectId string, stackId string, projectMemberId string) error
UnassignKeycloakClientRoleToMember(organizationId string, projectId string, stackId string, projectMemberId string) error
Expand Down Expand Up @@ -702,18 +705,74 @@ func (u *ProjectUsecase) GetProjectKubeconfig(organizationId string, projectId s
return kubernetes.MergeKubeconfigsWithSingleUser(kubeconfigs)
}

func (u *ProjectUsecase) GetK8sResources(ctx context.Context, organizationId string, projectId string, projectNamespace string, stackId string) (out domain.ProjectNamespaceK8sResources, err error) {
func (u *ProjectUsecase) GetK8sResources(ctx context.Context, organizationId string, projectId string, namespace string, stackId domain.StackId) (out domain.ProjectNamespaceK8sResources, err error) {
_, err = u.clusterRepository.Get(domain.ClusterId(stackId))
if err != nil {
return out, errors.Wrap(err, fmt.Sprintf("Failed to get cluster : stackId %s", stackId))
}

clientset_user, err := kubernetes.GetClientFromClusterId(stackId.String())
if err != nil {
return out, errors.Wrap(err, fmt.Sprintf("Failed to get clientset : stackId %s", stackId))
}

out.UpdatedAt = time.Now()

pods, err := clientset_user.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{})
if err == nil {
out.Pods = len(pods.Items)
} else {
log.Error("Failed to get pods. err : ", err)
}

pvcs, err := clientset_user.CoreV1().PersistentVolumeClaims(namespace).List(ctx, metav1.ListOptions{})
if err == nil {
out.PVCs = len(pvcs.Items)
} else {
log.Error("Failed to get pvcs. err : ", err)
}

services, err := clientset_user.CoreV1().Services(namespace).List(ctx, metav1.ListOptions{})
if err == nil {
out.Services = len(services.Items)
} else {
log.Error("Failed to get services. err : ", err)
}

// to be implemented
out.Pods = 1
out.Deployments = 2
out.Statefulsets = 3
out.Demonsets = 4
out.Jobs = 5
out.Cronjobs = 6
out.PVCs = 7
out.Services = 8
out.Ingresses = 9
ingresses, err := clientset_user.NetworkingV1().Ingresses(namespace).List(ctx, metav1.ListOptions{})
if err == nil {
out.Ingresses = len(ingresses.Items)
} else {
log.Error("Failed to get ingresses. err : ", err)
}

deployments, err := clientset_user.AppsV1().Deployments(namespace).List(ctx, metav1.ListOptions{})
if err == nil {
out.Deployments = len(deployments.Items)
} else {
log.Error("Failed to get deployments. err : ", err)
}

statefulsets, err := clientset_user.AppsV1().StatefulSets(namespace).List(ctx, metav1.ListOptions{})
if err == nil {
out.Statefulsets = len(statefulsets.Items)
} else {
log.Error("Failed to get statefulsets. err : ", err)
}

daemonsets, err := clientset_user.AppsV1().DaemonSets(namespace).List(ctx, metav1.ListOptions{})
if err == nil {
out.Daemonsets = len(daemonsets.Items)
} else {
log.Error("Failed to get daemonsets. err : ", err)
}

jobs, err := clientset_user.BatchV1().Jobs(namespace).List(ctx, metav1.ListOptions{})
if err == nil {
out.Jobs = len(jobs.Items)
} else {
log.Error("Failed to get jobs. err : ", err)
}

return
}
19 changes: 10 additions & 9 deletions pkg/domain/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,16 @@ type GetProjectKubeconfigResponse struct {
}

type ProjectNamespaceK8sResources struct {
Pods int `json:"pods"`
Deployments int `json:"deployments"`
Statefulsets int `json:"statefulsets"`
Demonsets int `json:"demonsets"`
Jobs int `json:"jobs"`
Cronjobs int `json:"cronjobs"`
PVCs int `json:"pvcs"`
Services int `json:"services"`
Ingresses int `json:"ingresses"`
Pods int `json:"pods"`
Deployments int `json:"deployments"`
Statefulsets int `json:"statefulsets"`
Daemonsets int `json:"daemonsets"`
Jobs int `json:"jobs"`
Cronjobs int `json:"cronjobs"`
PVCs int `json:"pvcs"`
Services int `json:"services"`
Ingresses int `json:"ingresses"`
UpdatedAt time.Time `json:"updatedAt"`
}

type GetProjectNamespaceK8sResourcesResponse struct {
Expand Down

0 comments on commit 191a896

Please sign in to comment.