Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

20240403 policy status #358

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
856 changes: 582 additions & 274 deletions api/swagger/docs.go

Large diffs are not rendered by default.

856 changes: 582 additions & 274 deletions api/swagger/swagger.json

Large diffs are not rendered by default.

563 changes: 382 additions & 181 deletions api/swagger/swagger.yaml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions internal/delivery/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ const (
GetChartDashboard // 대시보드/대시보드/조회
GetStacksDashboard // 대시보드/대시보드/조회
GetResourcesDashboard // 대시보드/대시보드/조회
GetPolicyStatusDashboard

// SystemNotificationTemplate
Admin_CreateSystemNotificationTemplate
Expand Down
8 changes: 8 additions & 0 deletions internal/delivery/api/generated_endpoints.go.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ var ApiMap = map[Endpoint]EndpointInfo{
Name: "GetResourcesDashboard",
Group: "Dashboard",
},
GetPolicyStatusDashboard: {
Name: "GetPolicyStatusDashboard",
Group: "Dashboard",
},
Admin_CreateSystemNotificationTemplate: {
Name: "Admin_CreateSystemNotificationTemplate",
Group: "SystemNotificationTemplate",
Expand Down Expand Up @@ -1054,6 +1058,8 @@ func (e Endpoint) String() string {
return "GetStacksDashboard"
case GetResourcesDashboard:
return "GetResourcesDashboard"
case GetPolicyStatusDashboard:
return "GetPolicyStatusDashboard"
case Admin_CreateSystemNotificationTemplate:
return "Admin_CreateSystemNotificationTemplate"
case Admin_UpdateSystemNotificationTemplate:
Expand Down Expand Up @@ -1496,6 +1502,8 @@ func GetEndpoint(name string) Endpoint {
return GetStacksDashboard
case "GetResourcesDashboard":
return GetResourcesDashboard
case "GetPolicyStatusDashboard":
return GetPolicyStatusDashboard
case "Admin_CreateSystemNotificationTemplate":
return Admin_CreateSystemNotificationTemplate
case "Admin_UpdateSystemNotificationTemplate":
Expand Down
61 changes: 59 additions & 2 deletions internal/delivery/http/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/gorilla/mux"
"github.com/openinfradev/tks-api/internal/middleware/auth/request"
"github.com/openinfradev/tks-api/internal/model"
policytemplate "github.com/openinfradev/tks-api/internal/policy-template"
"github.com/openinfradev/tks-api/internal/serializer"
"github.com/openinfradev/tks-api/internal/usecase"
"github.com/openinfradev/tks-api/pkg/domain"
Expand All @@ -22,15 +23,18 @@ type IDashboardHandler interface {
GetChart(w http.ResponseWriter, r *http.Request)
GetStacks(w http.ResponseWriter, r *http.Request)
GetResources(w http.ResponseWriter, r *http.Request)
GetPolicyStatus(w http.ResponseWriter, r *http.Request)
}

type DashboardHandler struct {
usecase usecase.IDashboardUsecase
usecase usecase.IDashboardUsecase
organizationUsecase usecase.IOrganizationUsecase
}

func NewDashboardHandler(h usecase.Usecase) IDashboardHandler {
return &DashboardHandler{
usecase: h.Dashboard,
usecase: h.Dashboard,
organizationUsecase: h.Organization,
}
}

Expand Down Expand Up @@ -414,3 +418,56 @@ func (h *DashboardHandler) GetResources(w http.ResponseWriter, r *http.Request)

ResponseJSON(w, r, http.StatusOK, out)
}

// GetPolicyStatus godoc
//
// @Tags Dashboards
// @Summary Get policy status
// @Description Get policy status
// @Accept json
// @Produce json
// @Param organizationId path string true "Organization ID"
// @Success 200 {object} domain.GetDashboardPolicyStatusResponse
// @Router /organizations/{organizationId}/dashboard/policy-status [get]
// @Security JWT
func (h *DashboardHandler) GetPolicyStatus(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
organizationId, ok := vars["organizationId"]
if !ok {
ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("%s: invalid organizationId", organizationId),
"C_INVALID_ORGANIZATION_ID", ""))
return
}

organization, err := h.organizationUsecase.Get(r.Context(), organizationId)
if err != nil {
log.Error(r.Context(), "Failed to retrieve organization")
ErrorJSON(w, r, fmt.Errorf("failed to retrieve organization"))
return
}

tksClusters, err := policytemplate.GetTksClusterCRs(r.Context(), organization.PrimaryClusterId)
if err != nil {
log.Error(r.Context(), "Failed to retrieve tkscluster list", err)
ErrorJSON(w, r, err)
return
}

var policyStatus domain.DashboardPolicyStatus
for _, c := range tksClusters {
switch status := c.Status.TKSProxy.Status; status {
case "ready":
policyStatus.Normal++
case "warn":
policyStatus.Warning++
case "error":
policyStatus.Error++
default:
continue
}
}

var out domain.GetDashboardPolicyStatusResponse
out.PolicyStatus = policyStatus
ResponseJSON(w, r, http.StatusOK, out)
}
41 changes: 28 additions & 13 deletions internal/policy-template/tkscluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package policytemplate

import (
"context"
"encoding/json"

"github.com/openinfradev/tks-api/pkg/kubernetes"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

Expand All @@ -14,7 +14,7 @@ var TKSClusterGVR = schema.GroupVersionResource{
Resource: "tksclusters",
}

// ============== Copied Fron Operator Start ==============
// ============== Copied From Operator Start ==============

// TemplateReference defines the desired state of TKSCluster
type TemplateReference struct {
Expand Down Expand Up @@ -70,35 +70,50 @@ type TKSClusterList struct {
Items []TKSCluster `json:"items"`
}

// ============== Copied Fron Operator End ==============
// ============== Copied From Operator End ==============

func GetTksClusterCR(ctx context.Context, primaryClusterId string, name string) (*TKSCluster, error) {
func GetTksClusterCR(ctx context.Context, primaryClusterId string, resourceName string) (*TKSCluster, error) {
dynamicClient, err := kubernetes.GetDynamicClientAdminCluster(ctx)

if err != nil {
return nil, err
}

result, err := dynamicClient.Resource(TKSClusterGVR).Namespace(primaryClusterId).
Get(ctx, name, metav1.GetOptions{})
resource, err := dynamicClient.Resource(TKSClusterGVR).Namespace(primaryClusterId).
Get(ctx, resourceName, metav1.GetOptions{})
if err != nil {
return nil, err
}

var tksCluster TKSCluster
unstructuredObj := resource.UnstructuredContent()
err = runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredObj, &tksCluster)
if err != nil {
return nil, err
}

// Unstructured를 바로 TKSPolicyTemplate으로 컨버팅할 수 없기 때문에 json으로 변환
jsonBytes, err := json.Marshal(result.Object)
return &tksCluster, nil
}

func GetTksClusterCRs(ctx context.Context, primaryClusterId string) (tksClusters []TKSCluster, err error) {
dynamicClient, err := kubernetes.GetDynamicClientAdminCluster(ctx)
if err != nil {
return nil, err
}

var tksCluster TKSCluster
err = json.Unmarshal(jsonBytes, &tksCluster)

resources, err := dynamicClient.Resource(TKSClusterGVR).Namespace(primaryClusterId).
List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}

return &tksCluster, nil
var tksCluster TKSCluster
for _, resource := range resources.Items {
if err = runtime.DefaultUnstructuredConverter.
FromUnstructured(resource.UnstructuredContent(), &tksCluster); err != nil {
return nil, err
}
tksClusters = append(tksClusters, tksCluster)
}

return tksClusters, nil
}
1 change: 1 addition & 0 deletions internal/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboard/charts/{chartType}", customMiddleware.Handle(internalApi.GetChartDashboard, http.HandlerFunc(dashboardHandler.GetChart))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboard/stacks", customMiddleware.Handle(internalApi.GetStacksDashboard, http.HandlerFunc(dashboardHandler.GetStacks))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboard/resources", customMiddleware.Handle(internalApi.GetResourcesDashboard, http.HandlerFunc(dashboardHandler.GetResources))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboard/policy-status", customMiddleware.Handle(internalApi.GetPolicyStatusDashboard, http.HandlerFunc(dashboardHandler.GetPolicyStatus))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboards", customMiddleware.Handle(internalApi.CreateDashboard, http.HandlerFunc(dashboardHandler.CreateDashboard))).Methods(http.MethodPost)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboards", customMiddleware.Handle(internalApi.GetDashboard, http.HandlerFunc(dashboardHandler.GetDashboard))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboards", customMiddleware.Handle(internalApi.UpdateDashboard, http.HandlerFunc(dashboardHandler.UpdateDashboard))).Methods(http.MethodPut)
Expand Down
14 changes: 10 additions & 4 deletions internal/usecase/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func (u *DashboardUsecase) GetResources(ctx context.Context, organizationId stri
// Stack
clusters, err := u.clusterRepo.FetchByOrganizationId(ctx, organizationId, uuid.Nil, nil)
if err != nil {
log.Error(ctx, err)
return out, err
}

Expand All @@ -201,10 +202,12 @@ func (u *DashboardUsecase) GetResources(ctx context.Context, organizationId stri
log.Debugf(ctx, "Failed to get cluster info: %v\n", err)
continue
}
if clusterInfo.Items[0].ObjectMeta.Labels["kubernetes.io/cluster-service"] == "true" {
normal++
} else {
abnormal++
if clusterInfo != nil && len(clusterInfo.Items) > 0 {
if clusterInfo.Items[0].ObjectMeta.Labels["kubernetes.io/cluster-service"] == "true" {
normal++
} else {
abnormal++
}
}
}
}
Expand All @@ -217,6 +220,7 @@ func (u *DashboardUsecase) GetResources(ctx context.Context, organizationId stri
*/
result, err := thanosClient.Get(ctx, "sum by (taco_cluster) (machine_cpu_cores)")
if err != nil {
log.Error(ctx, err)
return out, err
}
cpu := 0
Expand All @@ -234,6 +238,7 @@ func (u *DashboardUsecase) GetResources(ctx context.Context, organizationId stri
// Memory
result, err = thanosClient.Get(ctx, "sum by (taco_cluster) (machine_memory_bytes)")
if err != nil {
log.Error(ctx, err)
return out, err
}
memory := float64(0)
Expand All @@ -252,6 +257,7 @@ func (u *DashboardUsecase) GetResources(ctx context.Context, organizationId stri
// Storage
result, err = thanosClient.Get(ctx, "sum by (taco_cluster) (kubelet_volume_stats_capacity_bytes)")
if err != nil {
log.Error(ctx, err)
return out, err
}
storage := float64(0)
Expand Down
10 changes: 10 additions & 0 deletions pkg/domain/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,13 @@ type UpdateDashboardRequest struct {
type CommonDashboardResponse struct {
Result string `json:"result"`
}

type DashboardPolicyStatus struct {
Normal int `json:"normal"`
Warning int `json:"warning"`
Error int `json:"error"`
}

type GetDashboardPolicyStatusResponse struct {
PolicyStatus DashboardPolicyStatus `json:"statuses"`
}
Loading