Skip to content

Commit

Permalink
feat(UI): application data is now loaded in UI through the new GetApp…
Browse files Browse the repository at this point in the history
…Details endpoint (#2020)

Ref: SRX-FIC65Q

---------

Co-authored-by: Sven Urbanski <[email protected]>
  • Loading branch information
1 parent 0a0d0ac commit 0c57377
Show file tree
Hide file tree
Showing 31 changed files with 2,028 additions and 628 deletions.
7 changes: 7 additions & 0 deletions pkg/api/v1/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -434,12 +434,19 @@ message GetOverviewRequest {
string git_revision = 1;
}

//Lightweight version of application. Only contains name and team.
message OverviewApplication {
string name = 1;
string team = 2;
}

message GetOverviewResponse {
map<string, Application> applications = 2;
repeated EnvironmentGroup environment_groups = 3;
string git_revision = 4;
string branch = 5;
string manifest_repo_url = 6;
repeated OverviewApplication lightweight_apps = 7;
}

message EnvironmentGroup {
Expand Down
1 change: 1 addition & 0 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5662,6 +5662,7 @@ func (h *DBHandler) ReadLatestOverviewCache(ctx context.Context, transaction *sq
Branch: "",
ManifestRepoUrl: "",
Applications: map[string]*api.Application{},
LightweightApps: []*api.OverviewApplication{},
EnvironmentGroups: []*api.EnvironmentGroup{},
GitRevision: "",
}
Expand Down
12 changes: 11 additions & 1 deletion services/cd-service/pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2466,6 +2466,16 @@ func (s *State) DBInsertApplicationWithOverview(ctx context.Context, transaction
}
}
}
if shouldDelete {
lApps := make([]*api.OverviewApplication, len(cache.LightweightApps)-1)

for _, curr := range cache.LightweightApps {
if curr.Name != appName {
lApps = append(lApps, curr)
}
}
cache.LightweightApps = lApps
}

err = h.WriteOverviewCache(ctx, transaction, cache)
if err != nil {
Expand Down Expand Up @@ -2528,7 +2538,6 @@ func (s *State) UpdateTopLevelAppInOverview(ctx context.Context, transaction *sq
}
rels = retrievedReleasesOfApp
}

if releasesInDb, err := s.GetApplicationReleasesDB(ctx, transaction, appName, rels); err != nil {
return err
} else {
Expand Down Expand Up @@ -2556,6 +2565,7 @@ func (s *State) UpdateTopLevelAppInOverview(ctx context.Context, transaction *sq
result.Applications = map[string]*api.Application{}
}
result.Applications[appName] = &app
result.LightweightApps = append(result.LightweightApps, &api.OverviewApplication{Name: appName, Team: app.Team})
return nil
}

Expand Down
19 changes: 19 additions & 0 deletions services/cd-service/pkg/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2188,6 +2188,12 @@ func TestUpdateOverviewCache(t *testing.T) {
Warnings: nil,
},
},
LightweightApps: []*api.OverviewApplication{
{
Name: "app1",
Team: "",
},
},
EnvironmentGroups: []*api.EnvironmentGroup{},
},
},
Expand Down Expand Up @@ -2228,6 +2234,12 @@ func TestUpdateOverviewCache(t *testing.T) {
Warnings: nil,
},
},
LightweightApps: []*api.OverviewApplication{
{
Name: "app1",
Team: "",
},
},
EnvironmentGroups: []*api.EnvironmentGroup{
{
EnvironmentGroupName: "dev",
Expand Down Expand Up @@ -2311,6 +2323,12 @@ func TestUpdateOverviewCache(t *testing.T) {
Priority: 0,
},
},
LightweightApps: []*api.OverviewApplication{
{
Name: "app1",
Team: "",
},
},
GitRevision: "123",
Branch: "main",
ManifestRepoUrl: "https://example.com",
Expand All @@ -2337,6 +2355,7 @@ func TestUpdateOverviewCache(t *testing.T) {
GitRevision: "123",
Branch: "main",
ManifestRepoUrl: "https://example.com",
LightweightApps: []*api.OverviewApplication{},
},
},
}
Expand Down
1 change: 1 addition & 0 deletions services/cd-service/pkg/repository/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2779,6 +2779,7 @@ func (c *CreateEnvironment) Transform(
Applications: map[string]*api.Application{},
EnvironmentGroups: []*api.EnvironmentGroup{},
GitRevision: "0000000000000000000000000000000000000000",
LightweightApps: make([]*api.OverviewApplication, 0),
}
}
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions services/cd-service/pkg/repository/transformer_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1735,6 +1735,12 @@ func TestCreateEnvironmentUpdatesOverview(t *testing.T) {
},
},
},
LightweightApps: []*api.OverviewApplication{
{
Name: "app",
Team: "",
},
},
EnvironmentGroups: []*api.EnvironmentGroup{
&api.EnvironmentGroup{
EnvironmentGroupName: "development",
Expand Down
129 changes: 95 additions & 34 deletions services/cd-service/pkg/service/overview.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,22 @@ import (
"database/sql"
"errors"
"fmt"
"github.com/freiheit-com/kuberpult/pkg/mapper"
"google.golang.org/protobuf/types/known/timestamppb"
"os"
"sync"
"sync/atomic"

api "github.com/freiheit-com/kuberpult/pkg/api/v1"
"github.com/freiheit-com/kuberpult/pkg/db"
"github.com/freiheit-com/kuberpult/pkg/grpc"
"github.com/freiheit-com/kuberpult/pkg/logger"
"go.uber.org/zap"

"github.com/freiheit-com/kuberpult/pkg/mapper"
"github.com/freiheit-com/kuberpult/services/cd-service/pkg/notify"
"github.com/freiheit-com/kuberpult/services/cd-service/pkg/repository"
git "github.com/libgit2/git2go/v34"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

api "github.com/freiheit-com/kuberpult/pkg/api/v1"
"github.com/freiheit-com/kuberpult/pkg/db"
"github.com/freiheit-com/kuberpult/services/cd-service/pkg/notify"
"github.com/freiheit-com/kuberpult/services/cd-service/pkg/repository"
"google.golang.org/protobuf/types/known/timestamppb"
"os"
"sort"
"sync"
"sync/atomic"
)

type OverviewServiceServer struct {
Expand Down Expand Up @@ -96,7 +94,10 @@ func (o *OverviewServiceServer) GetAppDetails(
if retrievedReleasesOfApp != nil {
rels = retrievedReleasesOfApp.Metadata.Releases
}

//Highest to lowest
sort.Slice(rels, func(i, j int) bool {
return rels[j] < rels[i]
})
for _, id := range rels {
uid := uint64(id)
// we could optimize this by making one query that does return multiples:
Expand Down Expand Up @@ -142,9 +143,6 @@ func (o *OverviewServiceServer) GetAppDetails(
}
envGroups := mapper.MapEnvironmentsToGroups(envConfigs)

result.UndeploySummary = deriveUndeploySummary(appName, envGroups)
result.Warnings = db.CalculateWarnings(ctx, appName, envGroups)

// App Locks
appLocks, err := o.DBHandler.DBSelectAllActiveAppLocksForApp(ctx, transaction, appName)
if err != nil {
Expand Down Expand Up @@ -217,14 +215,19 @@ func (o *OverviewServiceServer) GetAppDetails(
}
response.Deployments[envName] = deployment
}
result.UndeploySummary = deriveUndeploySummary(appName, response.Deployments)
warnings, err := CalculateWarnings(ctx, transaction, o.Repository.State(), appName, envGroups)
if err != nil {
return nil, err
}
result.Warnings = warnings
return result, nil
})
if err != nil {
return nil, err
}
response.Application = resultApp
return response, nil

}

func (o *OverviewServiceServer) GetOverview(
Expand Down Expand Up @@ -302,6 +305,7 @@ func (o *OverviewServiceServer) getOverview(
Applications: map[string]*api.Application{},
EnvironmentGroups: []*api.EnvironmentGroup{},
GitRevision: rev,
LightweightApps: make([]*api.OverviewApplication, 0),
}
result.ManifestRepoUrl = o.RepositoryConfig.URL
result.Branch = o.RepositoryConfig.Branch
Expand Down Expand Up @@ -453,24 +457,14 @@ func (o *OverviewServiceServer) update(s *repository.State) {
o.notify.Notify()
}

func deriveUndeploySummary(appName string, groups []*api.EnvironmentGroup) api.UndeploySummary {
func deriveUndeploySummary(appName string, deployments map[string]*api.Deployment) api.UndeploySummary {
var allNormal = true
var allUndeploy = true
for _, group := range groups {
for _, environment := range group.Environments {
var app, exists = environment.Applications[appName]
if !exists {
continue
}
if app.Version == 0 {
// if the app exists but nothing is deployed, we ignore this
continue
}
if app.UndeployVersion {
allNormal = false
} else {
allUndeploy = false
}
for _, currentDeployment := range deployments {
if currentDeployment.UndeployVersion {
allNormal = false
} else {
allUndeploy = false
}
}
if allUndeploy {
Expand All @@ -481,3 +475,70 @@ func deriveUndeploySummary(appName string, groups []*api.EnvironmentGroup) api.U
}
return api.UndeploySummary_MIXED
}

func CalculateWarnings(ctx context.Context, transaction *sql.Tx, state *repository.State, appName string, groups []*api.EnvironmentGroup) ([]*api.Warning, error) {
result := make([]*api.Warning, 0)
for e := 0; e < len(groups); e++ {
group := groups[e]
for i := 0; i < len(groups[e].Environments); i++ {
env := group.Environments[i]
if env.Config.Upstream == nil || env.Config.Upstream.Environment == nil {
// if the env has no upstream, there's nothing to warn about
continue
}
upstreamEnvName := env.Config.GetUpstream().Environment
if upstreamEnvName == nil {
// this is already checked on startup and therefore shouldn't happen here
continue
}

versionInEnv, err := state.GetEnvironmentApplicationVersion(ctx, transaction, env.Name, appName)
if err != nil {
return nil, err
}

if versionInEnv == nil {
// appName is not deployed here, ignore it
continue
}

versionInUpstreamEnv, err := state.GetEnvironmentApplicationVersion(ctx, transaction, *upstreamEnvName, appName)
if err != nil {
return nil, err
}
if versionInUpstreamEnv == nil {
// appName is not deployed upstream... that's unusual!
var warning = api.Warning{
WarningType: &api.Warning_UpstreamNotDeployed{
UpstreamNotDeployed: &api.UpstreamNotDeployed{
UpstreamEnvironment: *upstreamEnvName,
ThisVersion: *versionInEnv,
ThisEnvironment: env.Name,
},
},
}
result = append(result, &warning)
continue
}

appLocks, err := state.GetEnvironmentApplicationLocks(ctx, transaction, env.Name, appName)
if err != nil {
return nil, err
}
if *versionInEnv > *versionInUpstreamEnv && len(appLocks) == 0 {
var warning = api.Warning{
WarningType: &api.Warning_UnusualDeploymentOrder{
UnusualDeploymentOrder: &api.UnusualDeploymentOrder{
UpstreamVersion: *versionInUpstreamEnv,
UpstreamEnvironment: *upstreamEnvName,
ThisVersion: *versionInEnv,
ThisEnvironment: env.Name,
},
},
}
result = append(result, &warning)
}
}
}
return result, nil
}
6 changes: 6 additions & 0 deletions services/cd-service/pkg/service/overview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,12 @@ func TestOverviewService(t *testing.T) {
Team: "team-123",
},
},
LightweightApps: []*api.OverviewApplication{
{
Name: "test",
Team: "team-123",
},
},
GitRevision: "0",
},
Setup: []repository.Transformer{
Expand Down
12 changes: 6 additions & 6 deletions services/frontend-service/pkg/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,18 +650,18 @@ func (p *GrpcProxy) GetFailedEsls(
return p.EslServiceClient.GetFailedEsls(ctx, in)
}

func (p *GrpcProxy) GetAppDetails(
ctx context.Context,
in *api.GetAppDetailsRequest) (*api.GetAppDetailsResponse, error) {
return p.OverviewClient.GetAppDetails(ctx, in)
}

func (p *GrpcProxy) GetOverview(
ctx context.Context,
in *api.GetOverviewRequest) (*api.GetOverviewResponse, error) {
return p.OverviewClient.GetOverview(ctx, in)
}

func (p *GrpcProxy) GetAppDetails(
ctx context.Context,
in *api.GetAppDetailsRequest) (*api.GetAppDetailsResponse, error) {
return p.OverviewClient.GetAppDetails(ctx, in)
}

func (p *GrpcProxy) GetGitTags(
ctx context.Context,
in *api.GetGitTagsRequest) (*api.GetGitTagsResponse, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestHandleCommitDeployments(t *testing.T) {
inputTail: "123456/",
failGrpcCall: false,
expectedStatusCode: http.StatusOK,
expectedResponse: "{\"deploymentStatus\":{\"app1\":{\"deploymentStatus\":{\"dev\":\"DEPLOYED\",\"prod\":\"UNKNOWN\",\"stage\":\"PENDING\"}}}}\n",
expectedResponse: "{\"deploymentStatus\":{\"app1\":{\"deploymentStatus\":{\"dev\":\"DEPLOYED\", \"prod\":\"UNKNOWN\", \"stage\":\"PENDING\"}}}}\n",
},
}
for _, tc := range tcs {
Expand Down
2 changes: 2 additions & 0 deletions services/frontend-service/src/ui/App/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
FlushRolloutStatus,
PanicOverview,
showSnackbarWarn,
updateAppDetails,
UpdateFrontendConfig,
UpdateOverview,
UpdateRolloutStatus,
Expand Down Expand Up @@ -99,6 +100,7 @@ export const App: React.FC = () => {
UpdateOverview.set(result);
UpdateOverview.set({ loaded: true });
PanicOverview.set({ error: '' });
updateAppDetails.set({});
},
(error) => {
PanicOverview.set({ error: JSON.stringify({ msg: 'error in streamoverview', error }) });
Expand Down
Loading

0 comments on commit 0c57377

Please sign in to comment.