Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.
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
45 changes: 43 additions & 2 deletions apps/console/internal/domain/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/kloudlite/api/grpc-interfaces/kloudlite.io/rpc/infra"

"github.com/kloudlite/api/common/fields"
"github.com/kloudlite/api/pkg/errors"
Expand Down Expand Up @@ -59,6 +60,26 @@ func (d *domain) getClusterAttachedToProject(ctx K8sContext, projectName string)
return fn.New(string(clusterName)), nil
}

func (d *domain) clusterStatus(ctx ConsoleContext, projectName string) (*infra.ClusterExistsOut, error) {
prj, err := d.findProject(ctx, projectName)
if err != nil {
return nil, errors.NewE(err)
}

clusterExistStatus, err := d.infraClient.ClusterExists(ctx, &infra.ClusterExistsIn{
UserId: string(ctx.UserId),
UserName: ctx.UserName,
UserEmail: ctx.UserEmail,
AccountName: ctx.AccountName,
ClusterName: *prj.ClusterName,
})
if err != nil {
return nil, errors.NewE(err)
}

return clusterExistStatus, nil
}

func (d *domain) ListProjects(ctx context.Context, userId repos.ID, accountName string, search map[string]repos.MatchFilter, pagination repos.CursorPagination) (*repos.PaginatedRecord[*entities.Project], error) {
co, err := d.iamClient.Can(ctx, &iam.CanIn{
UserId: string(userId),
Expand Down Expand Up @@ -223,6 +244,26 @@ func (d *domain) DeleteProject(ctx ConsoleContext, name string) error {
return errors.Newf("unauthorized to delete project")
}

cluster, err := d.clusterStatus(ctx, name)
if err != nil {
return errors.NewE(err)
}

if !cluster.Exists {
err := d.projectRepo.DeleteOne(
ctx,
repos.Filter{
fields.AccountName: ctx.AccountName,
fields.MetadataName: name,
},
)
if err != nil {
return errors.NewE(err)
}
d.resourceEventPublisher.PublishConsoleEvent(ctx, entities.ResourceTypeProject, name, PublishDelete)
return nil
}

uproj, err := d.projectRepo.Patch(
ctx,
repos.Filter{
Expand Down Expand Up @@ -302,7 +343,7 @@ func (d *domain) OnProjectDeleteMessage(ctx ConsoleContext, project entities.Pro
if err != nil {
return errors.NewE(err)
}
d.resourceEventPublisher.PublishConsoleEvent(ctx, entities.ResourceTypeApp, project.Name, PublishDelete)
d.resourceEventPublisher.PublishConsoleEvent(ctx, entities.ResourceTypeProject, project.Name, PublishDelete)
return nil
}

Expand Down Expand Up @@ -358,7 +399,7 @@ func (d *domain) OnProjectApplyError(ctx ConsoleContext, errMsg string, name str
return errors.NewE(err)
}

d.resourceEventPublisher.PublishConsoleEvent(ctx, entities.ResourceTypeApp, uproject.Name, PublishDelete)
d.resourceEventPublisher.PublishConsoleEvent(ctx, entities.ResourceTypeProject, uproject.Name, PublishDelete)

return errors.NewE(err)
}
Expand Down
23 changes: 23 additions & 0 deletions apps/infra/internal/app/grpc-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,29 @@ func (g *grpcServer) GetNodepool(ctx context.Context, in *infra.GetNodepoolIn) (
}, nil
}

func (g *grpcServer) ClusterExists(ctx context.Context, in *infra.ClusterExistsIn) (*infra.ClusterExistsOut, error) {
infraCtx := domain.InfraContext{
Context: ctx,
UserId: repos.ID(in.UserId),
UserEmail: in.UserEmail,
UserName: in.UserName,
AccountName: in.AccountName,
}
cluster, err := g.d.GetCluster(infraCtx, in.ClusterName)
if err != nil {
if !errors.Is(err, domain.ErrClusterNotFound) {
return &infra.ClusterExistsOut{Exists: false}, nil
}
return nil, errors.NewE(err)
}

if cluster == nil {
return &infra.ClusterExistsOut{Exists: false}, nil
}

return &infra.ClusterExistsOut{Exists: true}, nil
}

func newGrpcServer(d domain.Domain) infra.InfraServer {
return &grpcServer{
d: d,
Expand Down
4 changes: 3 additions & 1 deletion apps/infra/internal/domain/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type ErrClusterAlreadyExists struct {
AccountName string
}

var ErrClusterNotFound error = fmt.Errorf("cluster not found")

func (e ErrClusterAlreadyExists) Error() string {
return fmt.Sprintf("cluster with name %q already exists for account: %s", e.ClusterName, e.AccountName)
}
Expand Down Expand Up @@ -504,7 +506,7 @@ func (d *domain) findCluster(ctx InfraContext, clusterName string) (*entities.Cl
}

if cluster == nil {
return nil, errors.Newf("cluster with name %q not found", clusterName)
return nil, ErrClusterNotFound
}
return cluster, nil
}
16 changes: 16 additions & 0 deletions grpc-interfaces/infra.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ option go_package = "kloudlite.io/rpc/infra";
service Infra {
rpc GetCluster(GetClusterIn) returns (GetClusterOut);
rpc GetNodepool(GetNodepoolIn) returns (GetNodepoolOut);
rpc ClusterExists(ClusterExistsIn) returns (ClusterExistsOut);
}

message GetClusterIn {
Expand Down Expand Up @@ -38,3 +39,18 @@ message GetNodepoolOut {
string IACJobName = 1;
string IACJobNamespace = 2;
}

message ClusterExistsIn {
string userId = 1;
string userName = 2;
string userEmail = 3;

string accountName = 4;
string clusterName = 5;
}

message ClusterExistsOut {
bool exists = 1;
}


2 changes: 1 addition & 1 deletion grpc-interfaces/kloudlite.io/rpc/accounts/accounts.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions grpc-interfaces/kloudlite.io/rpc/accounts/accounts_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion grpc-interfaces/kloudlite.io/rpc/agent/kubeagent.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions grpc-interfaces/kloudlite.io/rpc/agent/kubeagent_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion grpc-interfaces/kloudlite.io/rpc/auth/auth.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 7 additions & 13 deletions grpc-interfaces/kloudlite.io/rpc/auth/auth_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion grpc-interfaces/kloudlite.io/rpc/ci/ci.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 5 additions & 10 deletions grpc-interfaces/kloudlite.io/rpc/ci/ci_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion grpc-interfaces/kloudlite.io/rpc/comms/comms.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading