Skip to content

Commit

Permalink
fix(cd-service): Remove overview cache recalculation, update overview…
Browse files Browse the repository at this point in the history
… on create environment and undeployVersion (#1996)

Ref: SRX-09Z4A6

* Removed `ForceOverviewRecalculation`. Instead updated overview
accordingly when creating a new environment or a new undeployVersion.
  • Loading branch information
AminSlk authored Oct 1, 2024
1 parent 7543f4d commit 878e1f8
Show file tree
Hide file tree
Showing 11 changed files with 428 additions and 199 deletions.
4 changes: 0 additions & 4 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4881,10 +4881,6 @@ func (h *DBHandler) DBWriteEnvironment(ctx context.Context, tx *sql.Tx, environm
if err != nil {
return fmt.Errorf("could not write environment %s with config %v to environments table, error: %w", environmentName, environmentConfig, err)
}
err = h.ForceOverviewRecalculation(ctx, tx)
if err != nil {
return fmt.Errorf("error while forcing overview recalculation, error: %w", err)
}
return nil
}

Expand Down
32 changes: 6 additions & 26 deletions pkg/db/overview.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ func (h *DBHandler) UpdateOverviewRelease(ctx context.Context, transaction *sql.
}
app := getApplicationByName(latestOverview.Applications, release.App)
if app == nil {
if release.Deleted {
return nil
}
return fmt.Errorf("could not find application '%s' in overview", release.App)
}
apiRelease := &api.Release{
Expand Down Expand Up @@ -279,34 +282,11 @@ func (h *DBHandler) UpdateOverviewRelease(ctx context.Context, transaction *sql.
app.Releases = append(app.Releases, apiRelease)
}

if release.Metadata.UndeployVersion { //We need to force recalculation as we need to determine undeploySummary
err = h.ForceOverviewRecalculation(ctx, transaction)
} else {
err = h.WriteOverviewCache(ctx, transaction, latestOverview)
if release.Metadata.UndeployVersion {
app.UndeploySummary = deriveUndeploySummary(app.Name, latestOverview.EnvironmentGroups)
}

if err != nil {
return err
}
return nil
}

func (h *DBHandler) ForceOverviewRecalculation(ctx context.Context, transaction *sql.Tx) error {
latestOverview, err := h.ReadLatestOverviewCache(ctx, transaction)
if err != nil {
return err
}
if h.IsOverviewEmpty(latestOverview) {
return nil
}
emptyOverview := &api.GetOverviewResponse{
Applications: map[string]*api.Application{},
EnvironmentGroups: []*api.EnvironmentGroup{},
GitRevision: "",
Branch: "",
ManifestRepoUrl: "",
}
err = h.WriteOverviewCache(ctx, transaction, emptyOverview)
err = h.WriteOverviewCache(ctx, transaction, latestOverview)
if err != nil {
return err
}
Expand Down
35 changes: 0 additions & 35 deletions pkg/db/overview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1251,41 +1251,6 @@ func TestUpdateOverviewRelease(t *testing.T) {
}
}

func TestForceOverviewRecalculation(t *testing.T) {
tcs := []struct {
Name string
}{
{
Name: "Check if ForceOverviewRecalculation creates an empty overview",
},
}

for _, tc := range tcs {
t.Run("ForceOverviewRecalculation", func(t *testing.T) {
t.Parallel()
ctx := testutil.MakeTestContext()
dbHandler := setupDB(t)
err := dbHandler.WithTransaction(ctx, false, func(ctx context.Context, transaction *sql.Tx) error {
err := dbHandler.ForceOverviewRecalculation(ctx, transaction)
if err != nil {
return err
}
latestOverview, err := dbHandler.ReadLatestOverviewCache(ctx, transaction)
if err != nil {
return err
}
if !dbHandler.IsOverviewEmpty(latestOverview) {
t.Fatalf("%s overview should be empty", tc.Name)
}
return nil
})
if err != nil {
t.Fatal(err)
}
})
}
}

func makeApps(apps ...*api.Environment_Application) map[string]*api.Environment_Application {
var result map[string]*api.Environment_Application = map[string]*api.Environment_Application{}
for i := 0; i < len(apps); i++ {
Expand Down
4 changes: 4 additions & 0 deletions services/cd-service/pkg/argocd/reposerver/reposerver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package reposerver

import (
"context"
"database/sql"
"fmt"
"github.com/freiheit-com/kuberpult/pkg/db"
"github.com/freiheit-com/kuberpult/pkg/testutil"
Expand Down Expand Up @@ -641,6 +642,9 @@ func SetupRepositoryTestWithDBOptions(t *testing.T, writeEslOnly bool) (reposito
if err != nil {
t.Fatal(err)
}
repo.State().DBHandler.InsertAppFun = func(ctx context.Context, transaction *sql.Tx, appName string, previousEslVersion db.EslVersion, stateChange db.AppStateChange, metaData db.DBAppMetaData) error {
return repo.State().DBInsertApplicationWithOverview(ctx, transaction, appName, previousEslVersion, stateChange, metaData)
}
return repo, &repoCfg
}

Expand Down
86 changes: 86 additions & 0 deletions services/cd-service/pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2521,6 +2521,19 @@ func getEnvironmentByName(groups []*api.EnvironmentGroup, envNameToReturn string
return nil
}

func getEnvironmentInGroup(groups []*api.EnvironmentGroup, groupNameToReturn string, envNameToReturn string) *api.Environment {
for _, currentGroup := range groups {
if currentGroup.EnvironmentGroupName == groupNameToReturn {
for _, currentEnv := range currentGroup.Environments {
if currentEnv.Name == envNameToReturn {
return currentEnv
}
}
}
}
return nil
}

/*
CalculateWarnings returns warnings for the User to be displayed in the UI.
For really unusual configurations, these will be logged and not returned.
Expand Down Expand Up @@ -2730,6 +2743,79 @@ func (s *State) UpdateOneAppEnvInOverview(ctx context.Context, transaction *sql.
return &app, nil
}

func (s *State) UpdateEnvironmentsInOverview(ctx context.Context, transaction *sql.Tx, result *api.GetOverviewResponse) error {
if envs, err := s.GetAllEnvironmentConfigs(ctx, transaction); err != nil {
return err
} else {
result.EnvironmentGroups = mapper.MapEnvironmentsToGroups(envs)
for envName, config := range envs {
var groupName = mapper.DeriveGroupName(config, envName)
var envInGroup = getEnvironmentInGroup(result.EnvironmentGroups, groupName, envName)

argocd := &api.EnvironmentConfig_ArgoCD{
SyncWindows: []*api.EnvironmentConfig_ArgoCD_SyncWindows{},
Destination: &api.EnvironmentConfig_ArgoCD_Destination{
Name: "",
Server: "",
Namespace: nil,
AppProjectNamespace: nil,
ApplicationNamespace: nil,
},
AccessList: []*api.EnvironmentConfig_ArgoCD_AccessEntry{},
ApplicationAnnotations: map[string]string{},
IgnoreDifferences: []*api.EnvironmentConfig_ArgoCD_IgnoreDifferences{},
SyncOptions: []string{},
}
if config.ArgoCd != nil {
argocd = mapper.TransformArgocd(*config.ArgoCd)
}
env := api.Environment{
DistanceToUpstream: 0,
Priority: api.Priority_PROD,
Name: envName,
Config: &api.EnvironmentConfig{
Upstream: mapper.TransformUpstream(config.Upstream),
Argocd: argocd,
EnvironmentGroup: &groupName,
},
Locks: map[string]*api.Lock{},
Applications: map[string]*api.Environment_Application{},
}
envInGroup.Config = env.Config
if locks, err := s.GetEnvironmentLocks(ctx, transaction, envName); err != nil {
return err
} else {
for lockId, lock := range locks {
env.Locks[lockId] = &api.Lock{
Message: lock.Message,
LockId: lockId,
CreatedAt: timestamppb.New(lock.CreatedAt),
CreatedBy: &api.Actor{
Name: lock.CreatedBy.Name,
Email: lock.CreatedBy.Email,
},
}
}
envInGroup.Locks = env.Locks
}

if apps, err := s.GetEnvironmentApplications(ctx, transaction, envName); err != nil {
return err
} else {
for _, appName := range apps {
app, err2 := s.UpdateOneAppEnvInOverview(ctx, transaction, appName, envName, &config, map[string]*int64{})
if err2 != nil {
return err
}
env.Applications[appName] = app
}
}
envInGroup.Applications = env.Applications
}
}
return nil
}

func (s *State) GetAppsAndTeams() (map[string]string, error) {
result, err := s.GetApplicationsFromFile()
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions services/cd-service/pkg/repository/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2748,6 +2748,27 @@ func (c *CreateEnvironment) Transform(
return "", fmt.Errorf("unable to write to all_environments table, error: %w", err)
}
}
overview, err := state.DBHandler.ReadLatestOverviewCache(ctx, transaction)
if overview == nil {
overview = &api.GetOverviewResponse{
Branch: "",
ManifestRepoUrl: "",
Applications: map[string]*api.Application{},
EnvironmentGroups: []*api.EnvironmentGroup{},
GitRevision: "0000000000000000000000000000000000000000",
}
}
if err != nil {
return "", fmt.Errorf("Unable to read overview cache, error: %w", err)
}
err = state.UpdateEnvironmentsInOverview(ctx, transaction, overview)
if err != nil {
return "", fmt.Errorf("Unable to udpate overview cache, error: %w", err)
}
err = state.DBHandler.WriteOverviewCache(ctx, transaction, overview)
if err != nil {
return "", fmt.Errorf("Unable to write overview cache, error: %w", err)
}
} else {
fs := state.Filesystem
envDir := fs.Join("environments", c.Environment)
Expand Down
Loading

0 comments on commit 878e1f8

Please sign in to comment.