Skip to content

Commit

Permalink
Merge pull request #328 from openinfradev/move_kubernetes
Browse files Browse the repository at this point in the history
implementation alert system
  • Loading branch information
ktkfree authored Apr 3, 2024
2 parents 58dc49e + 1eb9032 commit c624181
Show file tree
Hide file tree
Showing 19 changed files with 108 additions and 32 deletions.
8 changes: 8 additions & 0 deletions internal/delivery/http/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ func (h *OrganizationHandler) Admin_CreateOrganization(w http.ResponseWriter, r
}
organization.AdminId = &admin.ID

// Default systemNotificationRules 생성
err = h.usecase.MakeDefaultSystemNotificationRules(r.Context(), organizationId, &organization)
if err != nil {
log.Errorf(r.Context(), "error is :%s(%T)", err.Error(), err)
ErrorJSON(w, r, err)
return
}

var out domain.CreateOrganizationResponse
if err = serializer.Map(r.Context(), organization, &out); err != nil {
log.Error(r.Context(), err)
Expand Down
2 changes: 2 additions & 0 deletions internal/model/system-notification-rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type SystemNotificationRule struct {
Description string
OrganizationId string
Organization Organization `gorm:"foreignKey:OrganizationId"`
IsSystem bool `gorm:"default:false"`
SystemNotificationTemplate SystemNotificationTemplate `gorm:"foreignKey:SystemNotificationTemplateId"`
SystemNotificationTemplateId string
SystemNotificationConditions []SystemNotificationCondition `gorm:"foreignKey:SystemNotificationRuleId;constraint:OnUpdate:RESTRICT,OnDelete:RESTRICT"`
Expand All @@ -36,6 +37,7 @@ type SystemNotificationRule struct {
MessageTitle string
MessageContent string
MessageActionProposal string
Status domain.SystemNotificationRuleStatus
CreatorId *uuid.UUID `gorm:"type:uuid"`
Creator *User `gorm:"foreignKey:CreatorId"`
UpdatorId *uuid.UUID `gorm:"type:uuid"`
Expand Down
1 change: 1 addition & 0 deletions internal/model/system-notification-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type SystemNotificationTemplate struct {

ID uuid.UUID `gorm:"primarykey"`
Name string `gorm:"index:idx_name,unique"`
IsSystem bool `gorm:"default:false"`
Organizations []Organization `gorm:"many2many:system_notification_template_organizations;constraint:OnUpdate:RESTRICT,OnDelete:RESTRICT"`
OrganizationIds []string `gorm:"-:all"`
Description string
Expand Down
2 changes: 1 addition & 1 deletion internal/policy-template/tkscluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"

"github.com/openinfradev/tks-api/internal/kubernetes"
"github.com/openinfradev/tks-api/pkg/kubernetes"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/policy-template/tkspolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"encoding/json"

"github.com/openinfradev/tks-api/internal/kubernetes"
"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/kubernetes"
"gopkg.in/yaml.v3"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down
2 changes: 1 addition & 1 deletion internal/policy-template/tkspolicytemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"strings"

"github.com/openinfradev/tks-api/internal/kubernetes"
"github.com/openinfradev/tks-api/pkg/kubernetes"
"gopkg.in/yaml.v3"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

Expand Down
10 changes: 10 additions & 0 deletions internal/repository/system-notification-rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ISystemNotificationRuleRepository interface {
Fetch(ctx context.Context, pg *pagination.Pagination) ([]model.SystemNotificationRule, error)
FetchWithOrganization(ctx context.Context, organizationId string, pg *pagination.Pagination) (out []model.SystemNotificationRule, err error)
Create(ctx context.Context, dto model.SystemNotificationRule) (systemNotificationRuleId uuid.UUID, err error)
Creates(ctx context.Context, dto []model.SystemNotificationRule) (err error)
Update(ctx context.Context, dto model.SystemNotificationRule) (err error)
Delete(ctx context.Context, dto model.SystemNotificationRule) (err error)
}
Expand Down Expand Up @@ -86,6 +87,15 @@ func (r *SystemNotificationRuleRepository) Create(ctx context.Context, dto model
return dto.ID, nil
}

func (r *SystemNotificationRuleRepository) Creates(ctx context.Context, rules []model.SystemNotificationRule) (err error) {
res := r.db.WithContext(ctx).Create(&rules)
if res.Error != nil {
return res.Error
}

return nil
}

func (r *SystemNotificationRuleRepository) Update(ctx context.Context, dto model.SystemNotificationRule) (err error) {
var m model.SystemNotificationRule
res := r.db.WithContext(ctx).Preload(clause.Associations).First(&m, "id = ?", dto.ID)
Expand Down
2 changes: 1 addition & 1 deletion internal/usecase/app-serve-app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
"github.com/spf13/viper"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/openinfradev/tks-api/internal/kubernetes"
"github.com/openinfradev/tks-api/internal/model"
"github.com/openinfradev/tks-api/internal/pagination"
"github.com/openinfradev/tks-api/internal/repository"
argowf "github.com/openinfradev/tks-api/pkg/argo-client"
"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/httpErrors"
"github.com/openinfradev/tks-api/pkg/kubernetes"
"github.com/openinfradev/tks-api/pkg/log"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/usecase/cloud-account.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import (
"github.com/aws/aws-sdk-go-v2/service/servicequotas"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/google/uuid"
"github.com/openinfradev/tks-api/internal/kubernetes"
"github.com/openinfradev/tks-api/internal/middleware/auth/request"
"github.com/openinfradev/tks-api/internal/model"
"github.com/openinfradev/tks-api/internal/pagination"
"github.com/openinfradev/tks-api/internal/repository"
argowf "github.com/openinfradev/tks-api/pkg/argo-client"
"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/httpErrors"
"github.com/openinfradev/tks-api/pkg/kubernetes"
"github.com/openinfradev/tks-api/pkg/log"
"github.com/pkg/errors"
"gorm.io/gorm"
Expand Down
2 changes: 1 addition & 1 deletion internal/usecase/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/google/uuid"
"github.com/openinfradev/tks-api/internal/helper"
"github.com/openinfradev/tks-api/internal/kubernetes"
"github.com/openinfradev/tks-api/internal/middleware/auth/request"
"github.com/openinfradev/tks-api/internal/model"
"github.com/openinfradev/tks-api/internal/pagination"
Expand All @@ -19,6 +18,7 @@ import (
argowf "github.com/openinfradev/tks-api/pkg/argo-client"
"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/httpErrors"
"github.com/openinfradev/tks-api/pkg/kubernetes"
"github.com/openinfradev/tks-api/pkg/log"
gcache "github.com/patrickmn/go-cache"
"github.com/pkg/errors"
Expand Down
2 changes: 1 addition & 1 deletion internal/usecase/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (

"github.com/google/uuid"
"github.com/openinfradev/tks-api/internal/helper"
"github.com/openinfradev/tks-api/internal/kubernetes"
"github.com/openinfradev/tks-api/internal/model"
"github.com/openinfradev/tks-api/internal/repository"
"github.com/openinfradev/tks-api/internal/serializer"
"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/httpErrors"
"github.com/openinfradev/tks-api/pkg/kubernetes"
"github.com/openinfradev/tks-api/pkg/log"
thanos "github.com/openinfradev/tks-api/pkg/thanos-client"
gcache "github.com/patrickmn/go-cache"
Expand Down
44 changes: 28 additions & 16 deletions internal/usecase/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,30 @@ type IOrganizationUsecase interface {
UpdatePrimaryClusterId(ctx context.Context, organizationId string, clusterId string) (err error)
ChangeAdminId(ctx context.Context, organizationId string, adminId uuid.UUID) error
Delete(ctx context.Context, organizationId string, accessToken string) error
MakeDefaultSystemNotificationRules(ctx context.Context, organizationId string, organization *model.Organization) (err error)
}

type OrganizationUsecase struct {
repo repository.IOrganizationRepository
userRepo repository.IUserRepository
roleRepo repository.IRoleRepository
clusterRepo repository.IClusterRepository
stackTemplateRepo repository.IStackTemplateRepository
systemNotificationTemplateRepo repository.ISystemNotificationTemplateRepository
argo argowf.ArgoClient
kc keycloak.IKeycloak
repo repository.IOrganizationRepository
userRepo repository.IUserRepository
roleRepo repository.IRoleRepository
clusterRepo repository.IClusterRepository
stackTemplateRepo repository.IStackTemplateRepository
systemNotificationRuleRepo repository.ISystemNotificationRuleRepository
argo argowf.ArgoClient
kc keycloak.IKeycloak
}

func NewOrganizationUsecase(r repository.Repository, argoClient argowf.ArgoClient, kc keycloak.IKeycloak) IOrganizationUsecase {
return &OrganizationUsecase{
repo: r.Organization,
userRepo: r.User,
roleRepo: r.Role,
clusterRepo: r.Cluster,
stackTemplateRepo: r.StackTemplate,
systemNotificationTemplateRepo: r.SystemNotificationTemplate,
argo: argoClient,
kc: kc,
repo: r.Organization,
userRepo: r.User,
roleRepo: r.Role,
clusterRepo: r.Cluster,
stackTemplateRepo: r.StackTemplate,
systemNotificationRuleRepo: r.SystemNotificationRule,
argo: argoClient,
kc: kc,
}
}

Expand Down Expand Up @@ -218,3 +219,14 @@ func (u *OrganizationUsecase) ChangeAdminId(ctx context.Context, organizationId

return nil
}

func (u *OrganizationUsecase) MakeDefaultSystemNotificationRules(ctx context.Context, organizationId string, dto *model.Organization) error {
var rules []model.SystemNotificationRule

err := u.systemNotificationRuleRepo.Creates(ctx, rules)
if err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion internal/usecase/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (

"github.com/google/uuid"
"github.com/openinfradev/tks-api/internal/keycloak"
"github.com/openinfradev/tks-api/internal/kubernetes"
"github.com/openinfradev/tks-api/internal/model"
"github.com/openinfradev/tks-api/internal/pagination"
"github.com/openinfradev/tks-api/internal/repository"
"github.com/openinfradev/tks-api/internal/serializer"
argowf "github.com/openinfradev/tks-api/pkg/argo-client"
"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/kubernetes"
"github.com/openinfradev/tks-api/pkg/log"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
Expand Down
2 changes: 1 addition & 1 deletion internal/usecase/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/google/uuid"
"github.com/openinfradev/tks-api/internal/helper"
"github.com/openinfradev/tks-api/internal/kubernetes"
"github.com/openinfradev/tks-api/internal/middleware/auth/request"
"github.com/openinfradev/tks-api/internal/model"
"github.com/openinfradev/tks-api/internal/pagination"
Expand All @@ -18,6 +17,7 @@ import (
argowf "github.com/openinfradev/tks-api/pkg/argo-client"
"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/httpErrors"
"github.com/openinfradev/tks-api/pkg/kubernetes"
"github.com/openinfradev/tks-api/pkg/log"
"github.com/pkg/errors"
"github.com/spf13/viper"
Expand Down
25 changes: 25 additions & 0 deletions pkg/domain/system-notification-rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@ import (
"time"
)

// enum
type SystemNotificationRuleStatus int32

const (
SystemNotificationRuleStatus_PENDING SystemNotificationRuleStatus = iota
SystemNotificationRuleStatus_APPLYED
SystemNotificationRuleStatus_ERROR
)

var systemNotificationRuleStatus = [...]string{
"PENDING",
"APPLYED",
"ERROR",
}

func (m SystemNotificationRuleStatus) String() string { return systemNotificationRuleStatus[(m)] }
func (m SystemNotificationRuleStatus) FromString(s string) SystemNotificationRuleStatus {
for i, v := range systemNotificationRuleStatus {
if v == s {
return SystemNotificationRuleStatus(i)
}
}
return SystemNotificationRuleStatus_PENDING
}

type SystemNotificationRuleResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/system-notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type CreateSystemNotificationRequest struct {
Version string `json:"version"`
GroupKey string `json:"groupKey"`
TruncateSystemNotifications int `json:"truncateSystemNotifications"`
SystemNotifications []SystemNotification `json:"systemNotifications"`
SystemNotifications []SystemNotification `json:"alerts"`
GroupLabels struct {
SystemNotificationname string `json:"systemNotificationname"`
} `json:"groupLabels"`
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package kubernetes_test

import (
"context"
"github.com/openinfradev/tks-api/internal/kubernetes"
"gopkg.in/yaml.v3"
"os"
"reflect"
"testing"

"github.com/openinfradev/tks-api/pkg/kubernetes"
"gopkg.in/yaml.v3"
)

const path = "/Users/1113433/local_vm_kube/kubeconfig"
Expand Down
Loading

0 comments on commit c624181

Please sign in to comment.