Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move default purge on configuration #5888

Merged
merged 5 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion engine/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ type Configuration struct {
Error string `toml:"error" comment:"Help displayed to user on each error. Warning: this message could be view by anonymous user. Markdown accepted." json:"error" default:""`
} `toml:"help" comment:"######################\n 'Help' informations \n######################" json:"help"`
Workflow struct {
MaxRuns int64 `toml:"maxRuns" comment:"Maximum of runs by workflow" json:"maxRuns" default:"255"`
MaxRuns int64 `toml:"maxRuns" comment:"Maximum of runs by workflow" json:"maxRuns" default:"255"`
DefaultRetentionPolicy string `toml:"defaultRetentionPolicy" comment:"Default rule for workflow run retention policy, this rule can be overridden on each workflow.\n Example: 'return run_days_before < 365' keeps runs for one year." json:"defaultRetentionPolicy" default:"return run_days_before < 365"`
} `toml:"workflow" comment:"######################\n 'Workflow' global configuration \n######################" json:"workflow"`
}

Expand Down Expand Up @@ -814,6 +815,10 @@ func (a *API) Serve(ctx context.Context) error {
func(ctx context.Context) {
metrics.Init(ctx, a.DBConnectionFactory.GetDBMap(gorpmapping.Mapper))
})
// init purge
if err := purge.SetDefaultRunRetentionPolicy(a.Config.Workflow.DefaultRetentionPolicy); err != nil {
return err
}
a.GoRoutines.Run(ctx, "Purge-MarkRuns",
func(ctx context.Context) {
purge.MarkRunsAsDelete(ctx, a.Cache, a.DBConnectionFactory.GetDBMap(gorpmapping.Mapper), a.Metrics.WorkflowRunsMarkToDelete)
Expand Down
1 change: 1 addition & 0 deletions engine/api/api_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ func (api *API) InitRouter() {
r.Handle("/config/user", ScopeNone(), r.GET(api.ConfigUserHandler, service.OverrideAuth(service.NoAuthMiddleware)))
r.Handle("/config/vcs", ScopeNone(), r.GET(api.ConfigVCShandler))
r.Handle("/config/cdn", ScopeNone(), r.GET(api.ConfigCDNHandler))
r.Handle("/config/api", ScopeNone(), r.GET(api.ConfigAPIHandler))

// Users
r.Handle("/user", Scope(sdk.AuthConsumerScopeUser), r.GET(api.getUsersHandler))
Expand Down
8 changes: 8 additions & 0 deletions engine/api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ func (api *API) ConfigCDNHandler() service.Handler {
http.StatusOK)
}
}

func (api *API) ConfigAPIHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
return service.WriteJSON(w, sdk.APIConfig{
DefaultRunRetentionPolicy: api.Config.Workflow.DefaultRetentionPolicy,
}, http.StatusOK)
}
}
11 changes: 11 additions & 0 deletions engine/api/purge/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package purge
import (
"context"
"database/sql"
"fmt"
"net/http"
"time"

Expand All @@ -22,6 +23,16 @@ import (
"github.com/ovh/cds/sdk/telemetry"
)

var defaultRunRetentionPolicy string

func SetDefaultRunRetentionPolicy(rule string) error {
if rule == "" {
return sdk.WithStack(fmt.Errorf("invalid empty rule for default workflow run retention policy"))
}
defaultRunRetentionPolicy = rule
return nil
}

// MarkRunsAsDelete mark workflow run as delete
func MarkRunsAsDelete(ctx context.Context, store cache.Store, DBFunc func() *gorp.DbMap, workflowRunsMarkToDelete *stats.Int64Measure) {
tickMark := time.NewTicker(15 * time.Minute)
Expand Down
7 changes: 6 additions & 1 deletion engine/api/purge/purge_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,17 @@ func applyRetentionPolicyOnRun(ctx context.Context, db *gorp.DbMap, wf sdk.Workf
return true, err
}

retentionPolicy := defaultRunRetentionPolicy
if wf.RetentionPolicy != "" {
retentionPolicy = wf.RetentionPolicy
}

// Enabling strict checks on variables to prevent errors on rule definition
if err := luaCheck.EnableStrict(); err != nil {
return true, sdk.WithStack(err)
}

if err := luaCheck.Perform(wf.RetentionPolicy); err != nil {
if err := luaCheck.Perform(retentionPolicy); err != nil {
return true, sdk.NewErrorFrom(sdk.ErrWrongRequest, "unable to apply retention policy on workflow %s/%s: %v", wf.ProjectKey, wf.Name, err)
}

Expand Down
12 changes: 0 additions & 12 deletions engine/api/workflow/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,6 @@ func Insert(ctx context.Context, db gorpmapper.SqlExecutorWithTx, store cache.St
if w.HistoryLength == 0 {
w.HistoryLength = sdk.DefaultHistoryLength
}
if w.RetentionPolicy == "" {
w.RetentionPolicy = sdk.DefaultRetentionRule
}
w.MaxRuns = maxRuns

w.LastModified = time.Now()
Expand Down Expand Up @@ -638,15 +635,6 @@ func Update(ctx context.Context, db gorpmapper.SqlExecutorWithTx, store cache.St
}
}

// Set or keep RetentionPolicy
if wf.RetentionPolicy == "" {
if oldWf.RetentionPolicy == "" {
wf.RetentionPolicy = sdk.DefaultRetentionRule
} else {
wf.RetentionPolicy = oldWf.RetentionPolicy
}
}

// Keep MaxRun
wf.MaxRuns = oldWf.MaxRuns
if err := DeleteWorkflowData(db, *oldWf); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions engine/sql/api/227_workflow_retention.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- +migrate Up
UPDATE workflow SET retention_policy = '' WHERE retention_policy='return (git_branch_exist == "false" and run_days_before < 2) or run_days_before < 365';

-- +migrate Down
UPDATE workflow SET retention_policy = 'return (git_branch_exist == "false" and run_days_before < 2) or run_days_before < 365' WHERE retention_policy = '';
4 changes: 4 additions & 0 deletions sdk/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ type ConfigUser struct {
URLAPI string `json:"url.api"`
}

type APIConfig struct {
DefaultRunRetentionPolicy string `json:"default_run_retention_policy"`
}

type TCPServer struct {
Addr string `toml:"addr" default:"" comment:"Listen address without port, example: 127.0.0.1" json:"addr"`
Port int `toml:"port" default:"8090" json:"port"`
Expand Down
2 changes: 1 addition & 1 deletion sdk/exportentities/v2/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func NewWorkflow(ctx context.Context, w sdk.Workflow, version string, opts ...Ex
exportedWorkflow.HistoryLength = &w.HistoryLength
}

if w.RetentionPolicy != "" && w.RetentionPolicy != sdk.DefaultRetentionRule {
if w.RetentionPolicy != "" {
exportedWorkflow.RetentionPolicy = &w.RetentionPolicy
}

Expand Down
3 changes: 1 addition & 2 deletions sdk/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import (

// DefaultHistoryLength is the default history length
const (
DefaultHistoryLength int64 = 20
DefaultRetentionRule string = "return (git_branch_exist == \"false\" and run_days_before < 2) or run_days_before < 365"
DefaultHistoryLength int64 = 20
)

// ColorRegexp represent the regexp for a format to hexadecimal color
Expand Down
1 change: 1 addition & 0 deletions ui/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"node_modules/codemirror/mode/lua/lua.js",
"node_modules/codemirror/addon/edit/matchbrackets.js",
"node_modules/codemirror/addon/display/autorefresh.js",
"node_modules/codemirror/addon/display/placeholder.js",
"node_modules/js-yaml/dist/js-yaml.js",
"resources/cds-hint.js",
"node_modules/ansi_up/ansi_up.js",
Expand Down
3 changes: 3 additions & 0 deletions ui/src/app/model/config.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class APIConfig {
default_run_retention_policy: string;
}
5 changes: 5 additions & 0 deletions ui/src/app/service/config/config.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { APIConfig } from 'app/model/config.service';

/**
* Service to get config
Expand All @@ -13,4 +14,8 @@ export class ConfigService {
getConfig(): Observable<any> {
return this._http.get<any>('/config/user');
}

getAPIConfig() {
return this._http.get<APIConfig>('/config/api');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
[popupText]="'workflow_retention_maxruns_admin' | translate"></i>
</label>
</div>
<ng-container *ngIf="retentionRunsPolicyEnabled">
<ng-container *ngIf="retentionRunsPolicyEnabled && apiConfig">
<div class="field">
<label>
{{ 'workflow_retention_policy' | translate }}
Expand All @@ -152,7 +152,8 @@
[routerLink]="['/docs', 'docs', 'concepts', 'workflow', 'retention']">
here</a>.
</ng-template>
<codemirror name="policy" (change)="changeCodeMirror(codemirrorRetentionPolicy)"
<codemirror name="policy" [placeholder]="apiConfig?.default_run_retention_policy"
(change)="changeCodeMirror(codemirrorRetentionPolicy)"
[(ngModel)]="workflow.retention_policy" [config]="codeMirrorConfig"
#codemirrorRetentionPolicy></codemirror>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import { DragulaService } from 'ng2-dragula-sgu';
import { forkJoin, Observable, Subscription } from 'rxjs';
import { finalize, first } from 'rxjs/operators';
import { ProjectIntegration } from 'app/model/integration.model';
import { ConfigService } from 'app/service/config/config.service';
import { APIConfig } from 'app/model/config.service';

declare let CodeMirror: any;

Expand Down Expand Up @@ -72,7 +74,7 @@ export class WorkflowAdminComponent implements OnInit, OnDestroy {
}
}
get workflow() {
return this._workflow
return this._workflow;
}

@Input() editMode: boolean;
Expand All @@ -98,6 +100,8 @@ export class WorkflowAdminComponent implements OnInit, OnDestroy {
nbEventIntegrations: number;
selectedIntegration: ProjectIntegration;

apiConfig: APIConfig;

@ViewChild('updateWarning')
private warningUpdateModal: WarningModalComponent;
@ViewChild('codemirrorRetentionPolicy') codemirror: CodemirrorComponent;
Expand Down Expand Up @@ -136,8 +140,14 @@ export class WorkflowAdminComponent implements OnInit, OnDestroy {
private _dragularService: DragulaService,
private _theme: ThemeStore,
private _modalService: SuiModalService,
private _eventService: EventService
private _eventService: EventService,
private _configService: ConfigService
) {
this._configService.getAPIConfig().subscribe(c => {
this.apiConfig = c;
this._cd.markForCheck();
});

this._dragularService.createGroup('bag-tag', {
accepts(el, target, source, sibling) {
return sibling !== null;
Expand Down Expand Up @@ -177,7 +187,7 @@ export class WorkflowAdminComponent implements OnInit, OnDestroy {
lineWrapping: true,
lineNumbers: true,
autoRefresh: true,
gutters: ['CodeMirror-lint-markers'],
gutters: ['CodeMirror-lint-markers']
};

this.themeSubscription = this._theme.get().subscribe(t => {
Expand Down
20 changes: 14 additions & 6 deletions ui/src/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ a.disabled:hover {
overflow: hidden !important;
}

.CodeMirror-empty {
color: $polar_grey_2 !important;
}

app-workflow-admin .CodeMirror-scroll {
min-height: 180px;
}

.fullheight {
.CodeMirror {
height: 100% !important;
Expand Down Expand Up @@ -129,7 +137,7 @@ body ::-webkit-scrollbar-thumb {

.ui.selection.dropdown {
width: 100%;
min-width: 0px;
min-width: 0;
}

.ui.selection.classic.dropdown {
Expand Down Expand Up @@ -328,14 +336,14 @@ body ::-webkit-scrollbar-thumb {
}

.ui.right.dropdown {
border-bottom-left-radius: 0px;
border-top-left-radius: 0px;
border-bottom-left-radius: 0;
border-top-left-radius: 0;
border-left: none;
}

.ui.left.dropdown {
border-bottom-right-radius: 0px;
border-top-right-radius: 0px;
border-bottom-right-radius: 0;
border-top-right-radius: 0;
}
}

Expand All @@ -362,7 +370,7 @@ body ::-webkit-scrollbar-thumb {
padding-right: 1em;

>.text>.flag {
margin-right: 0em;
margin-right: 0;
}
}

Expand Down