Skip to content

Commit

Permalink
Merge branch 'main' into mc/remove-use-release-diff
Browse files Browse the repository at this point in the history
  • Loading branch information
miguel-crespo-fdc authored Oct 22, 2024
2 parents 6d95a61 + 0c57377 commit cae0503
Show file tree
Hide file tree
Showing 35 changed files with 2,111 additions and 657 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
49 changes: 39 additions & 10 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,27 +789,55 @@ func (h *DBHandler) processReleaseManifestRows(ctx context.Context, err error, r
return result, nil
}

func (h *DBHandler) DBSelectReleasesByApp(ctx context.Context, tx *sql.Tx, app string, deleted bool, ignorePrepublishes bool) ([]*DBReleaseWithMetaData, error) {
span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleasesByApp")
// DBSelectReleasesByAppLatestEslVersion returns the latest eslversion
// for each release of an app. It includes deleted releases and loads manifests.
func (h *DBHandler) DBSelectReleasesByAppLatestEslVersion(ctx context.Context, tx *sql.Tx, app string, ignorePrepublishes bool) ([]*DBReleaseWithMetaData, error) {
span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleasesByAppLatestEslVersion")
defer span.Finish()
selectQuery := h.AdaptQuery(fmt.Sprintf(
"SELECT eslVersion, created, appName, metadata, manifests, releaseVersion, deleted, environments " +
" FROM releases " +
" WHERE appName=? AND deleted=?" +
" ORDER BY releaseVersion DESC, eslVersion DESC, created DESC;"))
selectQuery := h.AdaptQuery(
`SELECT
releases.eslVersion,
releases.created,
releases.appName,
releases.metadata,
releases.manifests,
releases.releaseVersion,
releases.deleted,
releases.environments
FROM (
SELECT
MAX(eslVersion) AS latestEslVersion,
appname,
releaseversion
FROM
releases
WHERE
appname=?
GROUP BY
appname, releaseversion
) as currentEslReleases
JOIN
releases
ON
currentEslReleases.appname = releases.appname
AND
currentEslReleases.latesteslversion = releases.eslversion
AND
currentEslReleases.releaseversion = releases.releaseversion
ORDER BY currentEslReleases.releaseversion DESC;`,
)
span.SetTag("query", selectQuery)
rows, err := tx.QueryContext(
ctx,
selectQuery,
app,
deleted,
)

return h.processReleaseRows(ctx, err, rows, ignorePrepublishes, true)
}

func (h *DBHandler) DBSelectReleasesByAppLatestEslVersion(ctx context.Context, tx *sql.Tx, app string, deleted bool, ignorePrepublishes bool) ([]*DBReleaseWithMetaData, error) {
span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleasesByApp")
func (h *DBHandler) DBSelectReleasesByAppOrderedByEslVersion(ctx context.Context, tx *sql.Tx, app string, deleted bool, ignorePrepublishes bool) ([]*DBReleaseWithMetaData, error) {
span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleasesByAppOrderedByEslVersion")
defer span.Finish()
selectQuery := h.AdaptQuery(fmt.Sprintf(
"SELECT eslVersion, created, appName, metadata, releaseVersion, deleted, environments " +
Expand Down Expand Up @@ -5634,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
31 changes: 30 additions & 1 deletion pkg/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2311,6 +2311,35 @@ func TestReadReleasesByApp(t *testing.T) {
},
},
},
{
Name: "Retrieve deleted release",
Releases: []DBReleaseWithMetaData{
{
EslVersion: 1,
ReleaseNumber: 10,
App: "app1",
Manifests: DBReleaseManifests{Manifests: map[string]string{"dev": "manifest1"}},
},
{
EslVersion: 2,
ReleaseNumber: 10,
App: "app1",
Deleted: true,
Manifests: DBReleaseManifests{Manifests: map[string]string{"dev": "manifest1"}},
},
},
AppName: "app1",
Expected: []*DBReleaseWithMetaData{
{
EslVersion: 2,
ReleaseNumber: 10,
Deleted: true,
App: "app1",
Manifests: DBReleaseManifests{Manifests: map[string]string{"dev": "manifest1"}},
Environments: []string{"dev"},
},
},
},
{
Name: "Retrieve multiple releases",
Releases: []DBReleaseWithMetaData{
Expand Down Expand Up @@ -2525,7 +2554,7 @@ func TestReadReleasesByApp(t *testing.T) {
return fmt.Errorf("error while writing release, error: %w", err)
}
}
releases, err := dbHandler.DBSelectReleasesByApp(ctx, transaction, tc.AppName, false, !tc.RetrievePrepublishes)
releases, err := dbHandler.DBSelectReleasesByAppLatestEslVersion(ctx, transaction, tc.AppName, !tc.RetrievePrepublishes)
if err != nil {
return fmt.Errorf("error while selecting release, error: %w", err)
}
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
5 changes: 3 additions & 2 deletions services/cd-service/pkg/repository/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ func (c *CreateApplicationVersion) Transform(
sortedKeys := sorting.SortKeys(c.Manifests)

if state.DBHandler.ShouldUseOtherTables() {
prevRelease, err := state.DBHandler.DBSelectReleasesByAppLatestEslVersion(ctx, transaction, c.Application, false, false)
prevRelease, err := state.DBHandler.DBSelectReleasesByAppOrderedByEslVersion(ctx, transaction, c.Application, false, false)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -1711,7 +1711,7 @@ func (u *DeleteEnvFromApp) Transform(
return "", err
}
if state.DBHandler.ShouldUseOtherTables() {
releases, err := state.DBHandler.DBSelectReleasesByApp(ctx, transaction, u.Application, false, true)
releases, err := state.DBHandler.DBSelectReleasesByAppLatestEslVersion(ctx, transaction, u.Application, true)
if err != nil {
return "", err
}
Expand Down 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
13 changes: 10 additions & 3 deletions services/cd-service/pkg/repository/transformer_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/lib/pq"
"regexp"
"testing"
gotime "time"

"github.com/lib/pq"

"github.com/freiheit-com/kuberpult/pkg/api/v1"
"github.com/freiheit-com/kuberpult/pkg/event"

Expand Down Expand Up @@ -1734,6 +1735,12 @@ func TestCreateEnvironmentUpdatesOverview(t *testing.T) {
},
},
},
LightweightApps: []*api.OverviewApplication{
{
Name: "app",
Team: "",
},
},
EnvironmentGroups: []*api.EnvironmentGroup{
&api.EnvironmentGroup{
EnvironmentGroupName: "development",
Expand Down Expand Up @@ -2264,7 +2271,7 @@ func TestDeleteEnvFromAppWithDB(t *testing.T) {
if err != nil {
return fmt.Errorf("error: %v", err)
}
releases, err2 := state.DBHandler.DBSelectReleasesByApp(ctx, transaction, appName, false, true)
releases, err2 := state.DBHandler.DBSelectReleasesByAppLatestEslVersion(ctx, transaction, appName, true)
if err2 != nil {
return fmt.Errorf("error retrieving release: %v", err2)
}
Expand Down Expand Up @@ -3649,7 +3656,7 @@ func TestTimestampConsistency(t *testing.T) {
t.Fatalf("error mismatch on envAcceptance(-want, +got):\n%s", diff)
}
//Release
releases, err := state.DBHandler.DBSelectReleasesByApp(ctx, transaction, testAppName, false, true)
releases, err := state.DBHandler.DBSelectReleasesByAppLatestEslVersion(ctx, transaction, testAppName, true)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit cae0503

Please sign in to comment.