-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Get deployment information for an app for a commit (#1894)
* Added release number to `new-release` commit event. * Added a new grpc endpoint in the cd-service that accepts a commit hash and an application name. The output of the grpc message is a the deployment information for the commit and the app on all environments. Example output: ``` map[development:DEPLOYED development2:DEPLOYED fakeprod-ca:DEPLOYED fakeprod-de:UNKNOWN staging:UNKNOWN] ``` Ref: SRX-514GKC
- Loading branch information
1 parent
40bc8da
commit 91a826e
Showing
12 changed files
with
481 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -490,6 +490,21 @@ func TestCommitEvents(t *testing.T) { | |
}, | ||
}, | ||
}, | ||
{ | ||
Name: "New release event", | ||
commitHash: "abcdefabcdef", | ||
email: "[email protected]", | ||
event: event.DBEventGo{ | ||
EventData: &event.NewRelease{ | ||
Environments: map[string]struct{}{"dev": {}}, | ||
}, | ||
EventMetadata: event.Metadata{ | ||
Uuid: "00000000-0000-0000-0000-000000000001", | ||
EventType: "new-release", | ||
ReleaseVersion: 1, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tc := range tcs { | ||
tc := tc | ||
|
@@ -592,7 +607,7 @@ func TestReadLockPreventedEvents(t *testing.T) { | |
Uuid: "00000000-0000-0000-0000-000000000000", | ||
CommitHash: "test", | ||
EventType: "lock-prevented-deployment", | ||
EventJson: `{"EventData":{"Application":"app","Environment":"env","LockMessage":"test lock message","LockType":"Application"},"EventMetadata":{"Uuid":"00000000-0000-0000-0000-000000000000","EventType":"lock-prevented-deployment"}}`, | ||
EventJson: `{"EventData":{"Application":"app","Environment":"env","LockMessage":"test lock message","LockType":"Application"},"EventMetadata":{"Uuid":"00000000-0000-0000-0000-000000000000","EventType":"lock-prevented-deployment","ReleaseVersion":0}}`, | ||
}, | ||
}, | ||
}, | ||
|
@@ -618,13 +633,13 @@ func TestReadLockPreventedEvents(t *testing.T) { | |
Uuid: "00000000-0000-0000-0000-000000000000", | ||
CommitHash: "test", | ||
EventType: "lock-prevented-deployment", | ||
EventJson: `{"EventData":{"Application":"app","Environment":"env","LockMessage":"test lock message","LockType":"Application"},"EventMetadata":{"Uuid":"00000000-0000-0000-0000-000000000000","EventType":"lock-prevented-deployment"}}`, | ||
EventJson: `{"EventData":{"Application":"app","Environment":"env","LockMessage":"test lock message","LockType":"Application"},"EventMetadata":{"Uuid":"00000000-0000-0000-0000-000000000000","EventType":"lock-prevented-deployment","ReleaseVersion":0}}`, | ||
}, | ||
{ | ||
Uuid: "00000000-0000-0000-0000-000000000001", | ||
CommitHash: "test", | ||
EventType: "lock-prevented-deployment", | ||
EventJson: `{"EventData":{"Application":"app2","Environment":"env2","LockMessage":"message2","LockType":"Environment"},"EventMetadata":{"Uuid":"00000000-0000-0000-0000-000000000001","EventType":"lock-prevented-deployment"}}`, | ||
EventJson: `{"EventData":{"Application":"app2","Environment":"env2","LockMessage":"message2","LockType":"Environment"},"EventMetadata":{"Uuid":"00000000-0000-0000-0000-000000000001","EventType":"lock-prevented-deployment","ReleaseVersion":0}}`, | ||
}, | ||
}, | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/*This file is part of kuberpult. | ||
Kuberpult is free software: you can redistribute it and/or modify | ||
it under the terms of the Expat(MIT) License as published by | ||
the Free Software Foundation. | ||
Kuberpult is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
MIT License for more details. | ||
You should have received a copy of the MIT License | ||
along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>. | ||
Copyright freiheit.com*/ | ||
|
||
package service | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
|
||
api "github.com/freiheit-com/kuberpult/pkg/api/v1" | ||
"github.com/freiheit-com/kuberpult/pkg/db" | ||
"github.com/freiheit-com/kuberpult/pkg/event" | ||
) | ||
|
||
type CommitStatus map[string]api.CommitDeploymentStatus | ||
|
||
type CommitDeploymentServer struct { | ||
DBHandler *db.DBHandler | ||
} | ||
|
||
func (s *CommitDeploymentServer) GetCommitDeploymentInfo(ctx context.Context, in *api.GetCommitDeploymentInfoRequest) (*api.GetCommitDeploymentInfoResponse, error) { | ||
status, err := getCommitDeploymentInfoForApp(ctx, s.DBHandler, in.CommitId, in.Application) | ||
if err != nil { | ||
return nil, fmt.Errorf("Could not get commit deployment info: %v", err) | ||
} | ||
return &api.GetCommitDeploymentInfoResponse{ | ||
DeploymentStatus: status, | ||
}, nil | ||
} | ||
|
||
func getCommitDeploymentInfoForApp(ctx context.Context, h *db.DBHandler, commit, app string) (CommitStatus, error) { | ||
var jsonCommitEventsMetadata []byte | ||
var jsonAllDeploymentsMetadata []byte | ||
var jsonAllEnvironmentsMetadata []byte | ||
|
||
err := h.WithTransaction(ctx, true, func(ctx context.Context, transaction *sql.Tx) error { | ||
// Get the latest new-release event for the commit | ||
query := h.AdaptQuery("SELECT json FROM commit_events WHERE commithash = ? AND eventtype = ? ORDER BY timestamp DESC LIMIT 1;") | ||
row := transaction.QueryRow(query, commit, "new-release") | ||
err := row.Scan(&jsonCommitEventsMetadata) | ||
if err != nil { | ||
if errors.Is(err, sql.ErrNoRows) { | ||
return fmt.Errorf("commit \"%s\" could not be found", commit) | ||
} | ||
return err | ||
} | ||
|
||
// Get all deployments for the commit | ||
query = h.AdaptQuery("SELECT json FROM all_deployments WHERE appname = ? ORDER BY eslversion DESC LIMIT 1;") | ||
row = transaction.QueryRow(query, app) | ||
err = row.Scan(&jsonAllDeploymentsMetadata) | ||
if err != nil { | ||
if errors.Is(err, sql.ErrNoRows) { | ||
return fmt.Errorf("application \"%s\" does not exist or has no deployments yet.", app) | ||
} | ||
return err | ||
} | ||
|
||
// Get all environments | ||
query = h.AdaptQuery("SELECT json FROM all_environments ORDER BY version DESC LIMIT 1;") | ||
row = transaction.QueryRow(query) | ||
err = row.Scan(&jsonAllEnvironmentsMetadata) | ||
if err != nil { | ||
if errors.Is(err, sql.ErrNoRows) { | ||
return fmt.Errorf("no environments exist") | ||
} | ||
return err | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
releaseNumber, err := getCommitReleaseNumber(jsonCommitEventsMetadata) | ||
if err != nil { | ||
return nil, fmt.Errorf("Could not get commit release number from commit_events metadata: %v", err) | ||
} | ||
allEnvironments, err := getAllEnvironments(jsonAllEnvironmentsMetadata) | ||
if err != nil { | ||
return nil, fmt.Errorf("Could not get all environments from all_environments metadata: %v", err) | ||
} | ||
environmentReleases, err := getEnvironmentReleases(jsonAllDeploymentsMetadata) | ||
if err != nil { | ||
return nil, fmt.Errorf("Could not get environment releases from all_deployments metadata: %v", err) | ||
} | ||
|
||
commitStatus := getCommitStatus(releaseNumber, environmentReleases, allEnvironments) | ||
return commitStatus, nil | ||
} | ||
|
||
func getCommitStatus(commitReleaseNumber uint64, environmentReleases map[string]uint64, allEnvironments []string) CommitStatus { | ||
commitStatus := make(CommitStatus) | ||
if commitReleaseNumber == 0 { | ||
// Since 0 is the default value for uint64, it might mean that the release version was not known when the commit was created. | ||
// In this case, the commit status is unkown for all environments. | ||
for _, env := range allEnvironments { | ||
commitStatus[env] = api.CommitDeploymentStatus_UNKNOWN | ||
} | ||
return commitStatus | ||
} | ||
|
||
for _, env := range allEnvironments { | ||
// by defauly, a commit is pending in all environments | ||
commitStatus[env] = api.CommitDeploymentStatus_PENDING | ||
} | ||
|
||
for env, environmentReleaseVersion := range environmentReleases { | ||
if environmentReleaseVersion >= commitReleaseNumber { | ||
commitStatus[env] = api.CommitDeploymentStatus_DEPLOYED | ||
} else { | ||
commitStatus[env] = api.CommitDeploymentStatus_PENDING | ||
} | ||
} | ||
return commitStatus | ||
} | ||
|
||
func getCommitReleaseNumber(jsonInput []byte) (uint64, error) { | ||
releaseEvent := event.DBEventGo{ | ||
EventData: &event.NewRelease{ | ||
Environments: map[string]struct{}{}, | ||
}, | ||
EventMetadata: event.Metadata{ | ||
Uuid: "", | ||
EventType: "", | ||
ReleaseVersion: 0, | ||
}, | ||
} | ||
|
||
err := json.Unmarshal(jsonInput, &releaseEvent) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return releaseEvent.EventMetadata.ReleaseVersion, nil | ||
} | ||
|
||
func getAllEnvironments(jsonInput []byte) ([]string, error) { | ||
environments := []string{} | ||
err := json.Unmarshal(jsonInput, &environments) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return environments, nil | ||
} | ||
|
||
func getEnvironmentReleases(jsonInput []byte) (map[string]uint64, error) { | ||
releases := map[string]uint64{} | ||
err := json.Unmarshal(jsonInput, &releases) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return releases, nil | ||
} |
Oops, something went wrong.