Skip to content

Commit

Permalink
feat: Introduce reconcileId in results
Browse files Browse the repository at this point in the history
  • Loading branch information
codablock committed Sep 5, 2023
1 parent 735dd71 commit 14ef8e5
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 13 deletions.
27 changes: 15 additions & 12 deletions pkg/controllers/kluctl_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/kluctl/go-jinja2"
"github.com/kluctl/kluctl/v2/pkg/controllers/internal/sops"
internal_metrics "github.com/kluctl/kluctl/v2/pkg/controllers/metrics"
Expand Down Expand Up @@ -657,8 +658,9 @@ func (pt *preparedTarget) loadTarget(ctx context.Context, p *kluctl_project.Load
return targetContext, nil
}

func (pt *preparedTarget) addCommandResultInfo(ctx context.Context, cmdResult *result.CommandResult, crId string, objectsHash string) error {
cmdResult.Id = crId
func (pt *preparedTarget) addCommandResultInfo(ctx context.Context, cmdResult *result.CommandResult, reconcileId string, objectsHash string) error {
cmdResult.Id = uuid.NewString()
cmdResult.ReconcileId = reconcileId
cmdResult.Command.Initiator = result.CommandInititiator_KluctlDeployment
cmdResult.KluctlDeployment = &result.KluctlDeploymentInfo{
Name: pt.pp.obj.Name,
Expand All @@ -678,7 +680,7 @@ func (pt *preparedTarget) addCommandResultInfo(ctx context.Context, cmdResult *r
return nil
}

func (pt *preparedTarget) writeCommandResult(ctx context.Context, cmdErr error, cmdResult *result.CommandResult, commandName string, triggeredByRequest bool) error {
func (pt *preparedTarget) writeCommandResult(ctx context.Context, cmdErr error, cmdResult *result.CommandResult, commandName string, forceStore bool) error {
log := ctrl.LoggerFrom(ctx)

if cmdErr != nil {
Expand All @@ -695,9 +697,9 @@ func (pt *preparedTarget) writeCommandResult(ctx context.Context, cmdErr error,
summary.DeletedObjects != 0 ||
len(summary.Errors) != 0 ||
len(summary.Warnings) != 0
if !needStore && triggeredByRequest {
if !needStore && forceStore {
needStore = true
log.Info("forcing storing of empty command result because the deploy was requested")
log.Info("forcing storing of empty command result because the command was requested")
}

if !needStore {
Expand Down Expand Up @@ -746,15 +748,16 @@ func (pt *preparedTarget) writeCommandResult(ctx context.Context, cmdErr error,
return err
}

func (pt *preparedTarget) writeValidateResult(ctx context.Context, cmdErr error, validateResult *result.ValidateResult, crId string, objectsHash string) error {
func (pt *preparedTarget) writeValidateResult(ctx context.Context, cmdErr error, validateResult *result.ValidateResult, reconcileId string, objectsHash string) error {
log := ctrl.LoggerFrom(ctx)

if cmdErr != nil {
pt.pp.r.event(ctx, pt.pp.obj, true, fmt.Sprintf("validation failed. %s", cmdErr.Error()), nil)
return cmdErr
}

validateResult.Id = crId
validateResult.Id = uuid.NewString()
validateResult.ReconcileId = reconcileId
validateResult.KluctlDeployment = &result.KluctlDeploymentInfo{
Name: pt.pp.obj.Name,
Namespace: pt.pp.obj.Namespace,
Expand All @@ -778,17 +781,17 @@ func (pt *preparedTarget) writeValidateResult(ctx context.Context, cmdErr error,
return nil
}

func (pt *preparedTarget) kluctlDeployOrPokeImages(ctx context.Context, deployMode string, targetContext *kluctl_project.TargetContext, crId string, objectsHash string, needDeployByRequest bool) (*result.CommandResult, error) {
func (pt *preparedTarget) kluctlDeployOrPokeImages(ctx context.Context, deployMode string, targetContext *kluctl_project.TargetContext, reconcileId string, objectsHash string, needDeployByRequest bool) (*result.CommandResult, error) {
if deployMode == kluctlv1.KluctlDeployModeFull {
return pt.kluctlDeploy(ctx, targetContext, crId, objectsHash, needDeployByRequest)
return pt.kluctlDeploy(ctx, targetContext, reconcileId, objectsHash, needDeployByRequest)
} else if deployMode == kluctlv1.KluctlDeployPokeImages {
return pt.kluctlPokeImages(ctx, targetContext, crId, objectsHash, needDeployByRequest)
return pt.kluctlPokeImages(ctx, targetContext, reconcileId, objectsHash, needDeployByRequest)
} else {
return nil, fmt.Errorf("deployMode '%s' not supported", deployMode)
}
}

func (pt *preparedTarget) kluctlDeploy(ctx context.Context, targetContext *kluctl_project.TargetContext, crId string, objectsHash string, triggeredByRequest bool) (*result.CommandResult, error) {
func (pt *preparedTarget) kluctlDeploy(ctx context.Context, targetContext *kluctl_project.TargetContext, reconcileId string, objectsHash string, triggeredByRequest bool) (*result.CommandResult, error) {
timer := prometheus.NewTimer(internal_metrics.NewKluctlDeploymentDuration(pt.pp.obj.ObjectMeta.Namespace, pt.pp.obj.ObjectMeta.Name, pt.pp.obj.Spec.DeployMode))
defer timer.ObserveDuration()
cmd := commands.NewDeployCommand(targetContext)
Expand All @@ -802,7 +805,7 @@ func (pt *preparedTarget) kluctlDeploy(ctx context.Context, targetContext *kluct
cmd.WaitPrune = false

cmdResult, cmdErr := cmd.Run(nil)
err := pt.addCommandResultInfo(ctx, cmdResult, crId, objectsHash)
err := pt.addCommandResultInfo(ctx, cmdResult, reconcileId, objectsHash)
if err != nil {
return cmdResult, err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/types/result/command_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ type ResultObject struct {

type CommandResult struct {
Id string `json:"id"`
ReconcileId string `json:"reconcileId"`
ProjectKey ProjectKey `json:"projectKey"`
TargetKey TargetKey `json:"targetKey"`
Target types.Target `json:"target"`
Expand Down
2 changes: 2 additions & 0 deletions pkg/types/result/command_result_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

type CommandResultSummary struct {
Id string `json:"id"`
ReconcileId string `json:"reconcileId"`
ProjectKey ProjectKey `json:"projectKey"`
TargetKey TargetKey `json:"targetKey"`
Target types.Target `json:"target"`
Expand Down Expand Up @@ -49,6 +50,7 @@ func (cr *CommandResult) BuildSummary() *CommandResultSummary {

ret := &CommandResultSummary{
Id: cr.Id,
ReconcileId: cr.ReconcileId,
ProjectKey: cr.ProjectKey,
TargetKey: cr.TargetKey,
Target: cr.Target,
Expand Down
2 changes: 2 additions & 0 deletions pkg/types/result/drift_detection_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type DriftedObject struct {

type DriftDetectionResult struct {
Id string `json:"id"`
ReconcileId string `json:"reconcileId"`
ProjectKey ProjectKey `json:"projectKey"`
TargetKey TargetKey `json:"targetKey"`
KluctlDeployment *KluctlDeploymentInfo `json:"kluctlDeployment,omitempty"`
Expand All @@ -32,6 +33,7 @@ func (cr *CommandResult) BuildDriftDetectionResult() *DriftDetectionResult {

ret := &DriftDetectionResult{
Id: cr.Id,
ReconcileId: cr.ReconcileId,
ProjectKey: cr.ProjectKey,
TargetKey: cr.TargetKey,
KluctlDeployment: cr.KluctlDeployment,
Expand Down
3 changes: 3 additions & 0 deletions pkg/types/result/validate_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type ValidateResultEntry struct {

type ValidateResult struct {
Id string `json:"id"`
ReconcileId string `json:"reconcileId"`
ProjectKey ProjectKey `json:"projectKey"`
TargetKey TargetKey `json:"targetKey"`
KluctlDeployment *KluctlDeploymentInfo `json:"kluctlDeployment,omitempty"`
Expand All @@ -27,6 +28,7 @@ type ValidateResult struct {

type ValidateResultSummary struct {
Id string `json:"id"`
ReconcileId string `json:"reconcileId"`
ProjectKey ProjectKey `json:"projectKey"`
TargetKey TargetKey `json:"targetKey"`
KluctlDeployment *KluctlDeploymentInfo `json:"kluctlDeployment,omitempty"`
Expand All @@ -43,6 +45,7 @@ type ValidateResultSummary struct {
func (vr *ValidateResult) BuildSummary() ValidateResultSummary {
return ValidateResultSummary{
Id: vr.Id,
ReconcileId: vr.ReconcileId,
ProjectKey: vr.ProjectKey,
TargetKey: vr.TargetKey,
KluctlDeployment: vr.KluctlDeployment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class CommandResultNodeData extends NodeData {
tabs.push({
label: "Logs", content: <LogsViewer
cluster={this.commandResult.clusterInfo.clusterId}
reconcileId={this.commandResult.id}/>
reconcileId={this.commandResult.reconcileId}/>
})
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/webui/ui/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ export class ProjectKey {
}
export class CommandResult {
id: string;
reconcileId: string;
projectKey: ProjectKey;
targetKey: TargetKey;
target: Target;
Expand All @@ -635,6 +636,7 @@ export class CommandResult {
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.id = source["id"];
this.reconcileId = source["reconcileId"];
this.projectKey = this.convertValues(source["projectKey"], ProjectKey);
this.targetKey = this.convertValues(source["targetKey"], TargetKey);
this.target = this.convertValues(source["target"], Target);
Expand Down Expand Up @@ -670,6 +672,7 @@ export class CommandResult {
}
export class CommandResultSummary {
id: string;
reconcileId: string;
projectKey: ProjectKey;
targetKey: TargetKey;
target: Target;
Expand All @@ -693,6 +696,7 @@ export class CommandResultSummary {
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.id = source["id"];
this.reconcileId = source["reconcileId"];
this.projectKey = this.convertValues(source["projectKey"], ProjectKey);
this.targetKey = this.convertValues(source["targetKey"], TargetKey);
this.target = this.convertValues(source["target"], Target);
Expand Down Expand Up @@ -764,6 +768,7 @@ export class ValidateResultEntry {
}
export class ValidateResult {
id: string;
reconcileId: string;
projectKey: ProjectKey;
targetKey: TargetKey;
kluctlDeployment?: KluctlDeploymentInfo;
Expand All @@ -778,6 +783,7 @@ export class ValidateResult {
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.id = source["id"];
this.reconcileId = source["reconcileId"];
this.projectKey = this.convertValues(source["projectKey"], ProjectKey);
this.targetKey = this.convertValues(source["targetKey"], TargetKey);
this.kluctlDeployment = this.convertValues(source["kluctlDeployment"], KluctlDeploymentInfo);
Expand Down Expand Up @@ -810,6 +816,7 @@ export class ValidateResult {
}
export class ValidateResultSummary {
id: string;
reconcileId: string;
projectKey: ProjectKey;
targetKey: TargetKey;
kluctlDeployment?: KluctlDeploymentInfo;
Expand All @@ -824,6 +831,7 @@ export class ValidateResultSummary {
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.id = source["id"];
this.reconcileId = source["reconcileId"];
this.projectKey = this.convertValues(source["projectKey"], ProjectKey);
this.targetKey = this.convertValues(source["targetKey"], TargetKey);
this.kluctlDeployment = this.convertValues(source["kluctlDeployment"], KluctlDeploymentInfo);
Expand Down Expand Up @@ -894,6 +902,7 @@ export class DriftedObject {
}
export class DriftDetectionResult {
id: string;
reconcileId: string;
projectKey: ProjectKey;
targetKey: TargetKey;
kluctlDeployment?: KluctlDeploymentInfo;
Expand All @@ -907,6 +916,7 @@ export class DriftDetectionResult {
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.id = source["id"];
this.reconcileId = source["reconcileId"];
this.projectKey = this.convertValues(source["projectKey"], ProjectKey);
this.targetKey = this.convertValues(source["targetKey"], TargetKey);
this.kluctlDeployment = this.convertValues(source["kluctlDeployment"], KluctlDeploymentInfo);
Expand Down

0 comments on commit 14ef8e5

Please sign in to comment.