Skip to content

Commit

Permalink
feat: db driven platform settings (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 authored May 15, 2024
1 parent f58e94d commit b1e221d
Show file tree
Hide file tree
Showing 28 changed files with 1,159 additions and 23 deletions.
4 changes: 2 additions & 2 deletions action/dashboard/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (c *Config) Update(client *vela.Client) error {
dashboard.SetRepos(newRepos)
}

// add specified repositories from the dashboard
// add specified repositories to the dashboard
if len(c.AddRepos) > 0 {
repos := dashboard.GetRepos()

Expand Down Expand Up @@ -91,7 +91,7 @@ func (c *Config) Update(client *vela.Client) error {
dashboard.SetAdmins(newAdmins)
}

// add specified admins from the dashboard
// add specified admins to the dashboard
if len(c.AddAdmins) > 0 {
admins := dashboard.GetAdmins()

Expand Down
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_Validate(t *testing.T) {

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)

// setup tests
tests := []struct {
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" ]
249 changes: 249 additions & 0 deletions action/settings/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
// 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() {
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() {
if !slices.Contains(c.RepoAllowlistDropRepos, r) {
newRepos = append(newRepos, r)
}
}

sUpdate.SetRepoAllowlist(newRepos)
}

// add specified repositories to 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() {
if !slices.Contains(c.ScheduleAllowlistDropRepos, r) {
newRepos = append(newRepos, r)
}
}

sUpdate.SetScheduleAllowlist(newRepos)
}

// add specified repositories to 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
}

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

0 comments on commit b1e221d

Please sign in to comment.