Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
beaver
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
orus-io
beaver
Commits
c4f0f586a2f1
Commit
c4f0f586a2f1
authored
2 years ago
by
Florent Aide
Browse files
Options
Downloads
Patches
Plain Diff
move hydration code to CmdConfig and add variable merging
parent
dba164c9d649
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
runner/cmd.go
+66
-0
66 additions, 0 deletions
runner/cmd.go
runner/config.go
+16
-65
16 additions, 65 deletions
runner/config.go
runner/config_test.go
+1
-0
1 addition, 0 deletions
runner/config_test.go
with
83 additions
and
65 deletions
runner/cmd.go
+
66
−
0
View file @
c4f0f586
package
runner
import
(
"bytes"
"fmt"
"os"
"path/filepath"
...
...
@@ -4,5 +6,6 @@
"os"
"path/filepath"
"text/template"
"github.com/go-cmd/cmd"
"github.com/rs/zerolog"
...
...
@@ -6,6 +9,7 @@
"github.com/go-cmd/cmd"
"github.com/rs/zerolog"
"gopkg.in/yaml.v2"
)
func
RunCMD
(
name
string
,
args
...
string
)
(
err
error
,
stdout
,
stderr
[]
string
)
{
...
...
@@ -71,3 +75,65 @@
Files
[]
string
Values
[]
Value
}
// hydrate expands templated variables in our config with concrete values
func
(
c
*
CmdConfig
)
hydrate
()
error
{
if
err
:=
c
.
hydrateHelmCharts
();
err
!=
nil
{
return
err
}
if
err
:=
c
.
hydrateYttCharts
();
err
!=
nil
{
return
err
}
return
nil
}
func
(
c
*
CmdConfig
)
prepareVariables
(
v
[]
Variable
)
map
[
string
]
string
{
variables
:=
make
(
map
[
string
]
string
)
for
_
,
variable
:=
range
v
{
variables
[
variable
.
Name
]
=
variable
.
Value
}
variables
[
"namespace"
]
=
c
.
Namespace
return
variables
}
func
(
c
*
CmdConfig
)
hydrateYttCharts
()
error
{
for
entryFileName
,
entry
:=
range
c
.
Spec
.
Charts
.
Ytt
{
for
valIndex
,
val
:=
range
entry
.
Values
{
valueTmpl
,
err
:=
template
.
New
(
"ytt entry value"
)
.
Parse
(
val
.
Value
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to parse ytt entry value as template: %q, %w"
,
val
.
Value
,
err
)
}
buf
:=
new
(
bytes
.
Buffer
)
if
err
:=
valueTmpl
.
Execute
(
buf
,
c
.
prepareVariables
(
c
.
Spec
.
Variables
));
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to hydrate ytt entry: %q, %w"
,
val
.
Value
,
err
)
}
// replace original content with hydrated version
c
.
Spec
.
Charts
.
Ytt
[
entryFileName
]
.
Values
[
valIndex
]
.
Value
=
buf
.
String
()
}
}
return
nil
}
func
(
c
*
CmdConfig
)
hydrateHelmCharts
()
error
{
for
name
,
chart
:=
range
c
.
Spec
.
Charts
.
Helm
{
var
newVals
[]
string
for
_
,
value
:=
range
chart
.
Values
{
rawChartValue
,
err
:=
yaml
.
Marshal
(
value
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to get chart values as string: %w"
,
err
)
}
valueTmpl
,
err
:=
template
.
New
(
"chart"
)
.
Parse
(
string
(
rawChartValue
))
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to parse chart values as template: %q, %w"
,
chart
.
Values
,
err
)
}
buf
:=
new
(
bytes
.
Buffer
)
if
err
:=
valueTmpl
.
Execute
(
buf
,
c
.
prepareVariables
(
c
.
Spec
.
Variables
));
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to hydrate chart values entry: %q, %w"
,
chart
.
Values
,
err
)
}
newVals
=
append
(
newVals
,
buf
.
String
())
}
chart
.
Values
=
newVals
c
.
Spec
.
Charts
.
Helm
[
name
]
=
chart
}
return
nil
}
This diff is collapsed.
Click to expand it.
runner/config.go
+
16
−
65
View file @
c4f0f586
package
runner
import
(
"bytes"
"errors"
"fmt"
"os"
"text/template"
"github.com/spf13/viper"
...
...
@@ -10,5 +4,4 @@
"github.com/spf13/viper"
yaml
"gopkg.in/yaml.v2"
)
// Variable ...
...
...
@@ -70,13 +63,9 @@
return
cfg
,
nil
}
func
checkExists
(
path
string
)
bool
{
_
,
err
:=
os
.
Stat
(
path
)
return
!
errors
.
Is
(
err
,
os
.
ErrNotExist
)
}
// hydrate expands templated variables in our config with concrete values
func
(
c
*
Config
)
hydrate
()
error
{
if
err
:=
c
.
hydrateHelmCharts
();
err
!=
nil
{
return
err
// MergeVariables takes another config and will import those variables into
// the current config by replacing old ones and adding the new ones
func
(
c
*
Config
)
MergeVariables
(
other
*
Config
)
{
for
_
,
variable
:=
range
other
.
Spec
.
Variables
{
c
.
overlayVariable
(
variable
)
}
...
...
@@ -82,16 +71,3 @@
}
if
err
:=
c
.
hydrateYttCharts
();
err
!=
nil
{
return
err
}
return
nil
}
func
(
c
*
Config
)
prepareVariables
(
v
[]
Variable
)
map
[
string
]
string
{
variables
:=
make
(
map
[
string
]
string
)
for
_
,
variable
:=
range
v
{
variables
[
variable
.
Name
]
=
variable
.
Value
}
variables
[
"namespace"
]
=
c
.
Namespace
return
variables
}
...
...
@@ -96,17 +72,14 @@
}
func
(
c
*
Config
)
hydrateYttCharts
()
error
{
for
entryFileName
,
entry
:=
range
c
.
Spec
.
Charts
.
Ytt
{
for
valIndex
,
val
:=
range
entry
.
Values
{
valueTmpl
,
err
:=
template
.
New
(
"ytt entry value"
)
.
Parse
(
val
.
Value
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to parse ytt entry value as template: %q, %w"
,
val
.
Value
,
err
)
}
buf
:=
new
(
bytes
.
Buffer
)
if
err
:=
valueTmpl
.
Execute
(
buf
,
c
.
prepareVariables
(
c
.
Spec
.
Variables
));
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to hydrate ytt entry: %q, %w"
,
val
.
Value
,
err
)
}
// replace original content with hydrated version
c
.
Spec
.
Charts
.
Ytt
[
entryFileName
]
.
Values
[
valIndex
]
.
Value
=
buf
.
String
()
// overlayVariable takes a variable in and either replaces an existing variable
// of the same name or create a new variable in the config if no matching name
// is found
func
(
c
*
Config
)
overlayVariable
(
v
Variable
)
{
// find same variable by name and replace is value
// if not found then create the variable
for
index
,
originalVariable
:=
range
c
.
Spec
.
Variables
{
if
originalVariable
.
Name
==
v
.
Name
{
c
.
Spec
.
Variables
[
index
]
.
Value
=
v
.
Value
return
}
}
...
...
@@ -111,4 +84,4 @@
}
}
return
nil
c
.
Spec
.
Variables
=
append
(
c
.
Spec
.
Variables
,
v
)
}
...
...
@@ -114,23 +87,1 @@
}
func
(
c
*
Config
)
hydrateHelmCharts
()
error
{
for
name
,
chart
:=
range
c
.
Spec
.
Charts
.
Helm
{
rawChartValues
,
err
:=
yaml
.
Marshal
(
chart
.
Values
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to get chart values as string: %w"
,
err
)
}
valueTmpl
,
err
:=
template
.
New
(
"chart"
)
.
Parse
(
string
(
rawChartValues
))
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to parse chart values as template: %q, %w"
,
chart
.
Values
,
err
)
}
buf
:=
new
(
bytes
.
Buffer
)
if
err
:=
valueTmpl
.
Execute
(
buf
,
c
.
prepareVariables
(
c
.
Spec
.
Variables
));
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to hydrate chart values entry: %q, %w"
,
chart
.
Values
,
err
)
}
// replace original content with hydrated version
hydratedChart
:=
chart
hydratedChart
.
Values
=
buf
.
String
()
c
.
Spec
.
Charts
.
Helm
[
name
]
=
hydratedChart
}
return
nil
}
This diff is collapsed.
Click to expand it.
runner/config_test.go
+
1
−
0
View file @
c4f0f586
...
...
@@ -37,3 +37,4 @@
// verify variables overwrite
assert
.
Equal
(
t
,
"admin"
,
config
.
Spec
.
Charts
.
Ytt
[
"odoo"
]
.
Values
[
2
]
.
Value
)
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment