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

feat: db driven platform settings #554

Merged
merged 36 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
8cbe1b2
wip: platform settings, errors from refactor
plyr4 Apr 3, 2024
7bdb79c
chore: delete legacy events fields:
plyr4 Apr 3, 2024
8001c15
chore: update secret validate switch
plyr4 Apr 3, 2024
ea0237e
chore: verbose test types
plyr4 Apr 3, 2024
e4705ca
chore: more valid events
plyr4 Apr 3, 2024
70c38a0
fix: use generated validevents list
plyr4 Apr 3, 2024
a80af93
fix: use generated validevents list
plyr4 Apr 3, 2024
6cd06f0
fix: pr validator
plyr4 Apr 3, 2024
1909d1b
chore: bump go.mod
plyr4 Apr 3, 2024
0b6c66b
chore: appease linter
plyr4 Apr 3, 2024
0e780e4
fix: bundling api worker types changes
plyr4 Apr 3, 2024
6692668
fix: use hardcoded unlisted event tags
plyr4 Apr 3, 2024
5e1e606
fix: remove debug
plyr4 Apr 3, 2024
e867117
fix: remove debug
plyr4 Apr 3, 2024
f2c7307
chore: appease linter
plyr4 Apr 3, 2024
628a091
chore: merge with upstream
plyr4 Apr 3, 2024
34a5697
chore: merge with main
plyr4 Apr 30, 2024
0acbebf
chore: remove validatevents
plyr4 Apr 30, 2024
9f770cb
chore: remove go mod replace
plyr4 Apr 30, 2024
a01afd2
feat: settings view and update with optionals
plyr4 May 3, 2024
21c6392
feat: tests
plyr4 May 3, 2024
0e35dc5
feat: more tests
plyr4 May 3, 2024
60cc758
chore: comments
plyr4 May 3, 2024
7302855
Merge branch 'main' of github.com:go-vela/cli into feat/platform-sett…
plyr4 May 13, 2024
6df02f8
feat: platform settings with file update
plyr4 May 13, 2024
7249022
chore: bump mod
plyr4 May 13, 2024
acb79ff
chore: cleanup and command examples
plyr4 May 13, 2024
2a43f71
enhance: add and drop flags for allowlists
plyr4 May 13, 2024
5204027
enhance: add and drop flags for queue routes
plyr4 May 13, 2024
c2c7978
chore: lint
plyr4 May 13, 2024
bf84804
fix: whitespace
plyr4 May 13, 2024
e8e04c7
fix: typos
plyr4 May 13, 2024
41988ff
fix: typo
plyr4 May 14, 2024
e4a0688
fix: rm alias in example
plyr4 May 14, 2024
5426ea3
fix: example quote recommendation
plyr4 May 14, 2024
a1bf687
fix: example quote recommendation
plyr4 May 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions action/pipeline/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,18 @@

func TestPipeline_Config_ValidateLocal(t *testing.T) {
// setup types
c := cli.NewContext(&cli.App{Name: "vela", Version: "v0.0.0"}, flag.NewFlagSet("test", 0), nil)
set := flag.NewFlagSet("test", 0)
set.String("clone-image", "target/vela-git:latest", "doc")

c := cli.NewContext(&cli.App{Name: "vela", Version: "v0.0.0"}, set, nil)

// create a vela client
client, err := native.New(c)
client, err := native.FromCLIContext(c)
if err != nil {
t.Errorf("unable to create client: %v", err)
}

client.TemplateDepth = 1
client.SetTemplateDepth(1)
plyr4 marked this conversation as resolved.
Show resolved Hide resolved

// setup tests
tests := []struct {
Expand Down Expand Up @@ -476,12 +479,12 @@
t.Error(err)
}
}
got, err := validateFile(tt.args.path)

Check failure on line 482 in action/pipeline/validate_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] action/pipeline/validate_test.go#L482

assignments should only be cuddled with other assignments (wsl)
Raw output
action/pipeline/validate_test.go:482:4: assignments should only be cuddled with other assignments (wsl)
			got, err := validateFile(tt.args.path)
			^
if (err != nil) != tt.wantErr {

Check failure on line 483 in action/pipeline/validate_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] action/pipeline/validate_test.go#L483

only one cuddle assignment allowed before if statement (wsl)
Raw output
action/pipeline/validate_test.go:483:4: only one cuddle assignment allowed before if statement (wsl)
			if (err != nil) != tt.wantErr {
			^
t.Errorf("validateFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {

Check failure on line 487 in action/pipeline/validate_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] action/pipeline/validate_test.go#L487

if statements should only be cuddled with assignments (wsl)
Raw output
action/pipeline/validate_test.go:487:4: if statements should only be cuddled with assignments (wsl)
			if got != tt.want {
			^
t.Errorf("validateFile() got = %v, want %v", got, tt.want)
}
})
Expand Down
8 changes: 8 additions & 0 deletions action/settings/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: Apache-2.0

// The defined CLI settings actions for Vela.
//
// Usage:
//
// import "github.com/go-vela/cli/action/settings"
package settings
11 changes: 11 additions & 0 deletions action/settings/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: Apache-2.0

package settings

import "github.com/go-vela/server/api/types/settings"

// ConfigFile represents the configuration necessary
// to perform settings related requests from a file with Vela.
type ConfigFile struct {
*settings.Platform `yaml:"platform,omitempty"`
}
37 changes: 37 additions & 0 deletions action/settings/settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: Apache-2.0

package settings

// Config represents the configuration necessary
// to perform settings related requests with Vela.
type Config struct {
Action string
Output string
File string
Compiler
Queue

RepoAllowlist *[]string
RepoAllowlistAddRepos []string
RepoAllowlistDropRepos []string

ScheduleAllowlist *[]string
ScheduleAllowlistAddRepos []string
ScheduleAllowlistDropRepos []string
}

// Compiler represents the compiler configurations used
// to modify the compiler settings for Vela.
type Compiler struct {
CloneImage *string
TemplateDepth *int
StarlarkExecLimit *uint64
}

// Queue represents the compiler configurations used
// to modify the queue settings for Vela.
type Queue struct {
Routes *[]string
AddRoutes []string
DropRoutes []string
}
10 changes: 10 additions & 0 deletions action/settings/testdata/all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
platform:
compiler:
clone_image: "target/vela-git:latest"
template_depth: 10
starlark_exec_limit: 100
queue:
routes: [ "vela", "foobar" ]
repo_allowlist: [ "foo/bar", "octocat/hello-world" ]
schedule_allowlist: [ "*", "octocat/*" ]
6 changes: 6 additions & 0 deletions action/settings/testdata/compiler.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
platform:
compiler:
clone_image: "target/vela-git:latest"
template_depth: 10
starlark_exec_limit: 100
2 changes: 2 additions & 0 deletions action/settings/testdata/noplatform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
foo: bar
4 changes: 4 additions & 0 deletions action/settings/testdata/platform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
platform:
repo_allowlist: [ "foo/bar", "octocat/hello-world" ]
schedule_allowlist: [ "*", "octocat/*" ]
4 changes: 4 additions & 0 deletions action/settings/testdata/queue.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
platform:
queue:
routes: [ "vela", "foobar" ]
244 changes: 244 additions & 0 deletions action/settings/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
// SPDX-License-Identifier: Apache-2.0

package settings

import (
"bytes"
"errors"
"os"
"path/filepath"
"slices"

"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"

"github.com/go-vela/cli/internal"
"github.com/go-vela/cli/internal/output"
"github.com/go-vela/sdk-go/vela"
"github.com/go-vela/server/api/types/settings"
)

// Update modifies settings based off the provided configuration.
func (c *Config) Update(client *vela.Client) error {
logrus.Debug("executing update for settings configuration")

// send API call to retrieve current settings
s, _, err := client.Admin.Settings.Get()
if err != nil {
return err
}

// create the settings object
sUpdate := &settings.Platform{
Queue: &settings.Queue{
Routes: vela.Strings(s.GetRoutes()),
},
Compiler: &settings.Compiler{
CloneImage: c.Compiler.CloneImage,
TemplateDepth: c.Compiler.TemplateDepth,
StarlarkExecLimit: c.Compiler.StarlarkExecLimit,
},
RepoAllowlist: vela.Strings(s.GetRepoAllowlist()),
ScheduleAllowlist: vela.Strings(s.GetScheduleAllowlist()),
}

// drop specified routes
if len(c.Queue.DropRoutes) > 0 {
newRoutes := []string{}
for _, r := range sUpdate.GetRoutes() {

Check failure on line 48 in action/settings/update.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] action/settings/update.go#L48

ranges should only be cuddled with assignments used in the iteration (wsl)
Raw output
action/settings/update.go:48:3: ranges should only be cuddled with assignments used in the iteration (wsl)
		for _, r := range sUpdate.GetRoutes() {
		^
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
if !slices.Contains(c.Queue.DropRoutes, r) {
newRoutes = append(newRoutes, r)
}
}

sUpdate.SetRoutes(newRoutes)
}

// add specified routes
if len(c.Queue.AddRoutes) > 0 {
routes := sUpdate.GetRoutes()
for _, r := range c.Queue.AddRoutes {
if !slices.Contains(routes, r) {
routes = append(routes, r)
}
}

sUpdate.SetRoutes(routes)
}

// drop specified repositories from the allowlist
if len(c.RepoAllowlistDropRepos) > 0 {
newRepos := []string{}
for _, r := range sUpdate.GetRepoAllowlist() {

Check failure on line 72 in action/settings/update.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] action/settings/update.go#L72

ranges should only be cuddled with assignments used in the iteration (wsl)
Raw output
action/settings/update.go:72:3: ranges should only be cuddled with assignments used in the iteration (wsl)
		for _, r := range sUpdate.GetRepoAllowlist() {
		^
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
if !slices.Contains(c.RepoAllowlistDropRepos, r) {
newRepos = append(newRepos, r)
}
}

sUpdate.SetRepoAllowlist(newRepos)
}

// add specified repositories from the allowlist
if len(c.RepoAllowlistAddRepos) > 0 {
repos := sUpdate.GetRepoAllowlist()
for _, r := range c.RepoAllowlistAddRepos {
if !slices.Contains(repos, r) {
repos = append(repos, r)
}
}

sUpdate.SetRepoAllowlist(repos)
}

// drop specified repositories from the allowlist
if len(c.ScheduleAllowlistDropRepos) > 0 {
newRepos := []string{}
for _, r := range sUpdate.GetScheduleAllowlist() {

Check failure on line 96 in action/settings/update.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] action/settings/update.go#L96

ranges should only be cuddled with assignments used in the iteration (wsl)
Raw output
action/settings/update.go:96:3: ranges should only be cuddled with assignments used in the iteration (wsl)
		for _, r := range sUpdate.GetScheduleAllowlist() {
		^
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
if !slices.Contains(c.ScheduleAllowlistDropRepos, r) {
newRepos = append(newRepos, r)
}
}

sUpdate.SetScheduleAllowlist(newRepos)
}

// add specified repositories from the allowlist
if len(c.ScheduleAllowlistAddRepos) > 0 {
repos := sUpdate.GetScheduleAllowlist()
for _, r := range c.ScheduleAllowlistAddRepos {
if !slices.Contains(repos, r) {
repos = append(repos, r)
}
}

sUpdate.SetScheduleAllowlist(repos)
}

// manual overrides (from file)
if c.RepoAllowlist != nil {
sUpdate.RepoAllowlist = c.RepoAllowlist
}

if c.ScheduleAllowlist != nil {
sUpdate.ScheduleAllowlist = c.ScheduleAllowlist
}

if c.Queue.Routes != nil {
sUpdate.Queue.Routes = c.Queue.Routes
}
ecrupper marked this conversation as resolved.
Show resolved Hide resolved

logrus.Trace("updating settings")

// send API call to modify settings
sUpdated, _, err := client.Admin.Settings.Update(sUpdate)
if err != nil {
return err
}

// handle the output based off the provided configuration
switch c.Output {
case output.DriverDump:
// output in dump format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Dump
return output.Dump(sUpdated)
case output.DriverJSON:
// output in JSON format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#JSON
return output.JSON(sUpdated)
case output.DriverSpew:
// output in spew format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Spew
return output.Spew(sUpdated)
case output.DriverYAML:
// output in YAML format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#YAML
return output.YAML(sUpdated)
default:
// output in stdout format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout
return output.Stdout(sUpdated)
}
}

// UpdateFromFile updates from a file based on the provided configuration.
func (c *Config) UpdateFromFile(client *vela.Client) error {
logrus.Debug("executing update from file for platform settings configuration")

// capture absolute path to file
path, err := filepath.Abs(c.File)
if err != nil {
return err
}

logrus.Tracef("reading platform settings contents from %s", path)

// read contents of file
contents, err := os.ReadFile(path)
if err != nil {
return err
}

input := yaml.NewDecoder(bytes.NewReader(contents))

f := new(ConfigFile)

for input.Decode(f) == nil {
if f.Platform == nil {
return errors.New("invalid input, expected key 'platform'")
}

s := &Config{
Action: internal.ActionUpdate,
Output: c.Output,
Compiler: Compiler{},
Queue: Queue{},
}

if f.Platform.RepoAllowlist != nil {
s.RepoAllowlist = f.Platform.RepoAllowlist
}

if f.Platform.ScheduleAllowlist != nil {
s.ScheduleAllowlist = f.Platform.ScheduleAllowlist
}

// update values if set

if f.Compiler != nil {
if f.Compiler.CloneImage != nil {
s.Compiler.CloneImage = vela.String(f.Compiler.GetCloneImage())
}

if f.Compiler.TemplateDepth != nil {
s.Compiler.TemplateDepth = vela.Int(f.Compiler.GetTemplateDepth())
}

if f.Compiler.StarlarkExecLimit != nil {
s.Compiler.StarlarkExecLimit = vela.UInt64(f.Compiler.GetStarlarkExecLimit())
}
}

if f.Queue != nil {
if f.Queue.Routes != nil {
s.Queue.Routes = f.Queue.Routes
}
}

err = s.Validate()
if err != nil {
return err
}

err = s.Update(client)
if err != nil {
return err
}
}

return nil
}
Loading
Loading