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
6ee63476c95b
Commit
6ee63476c95b
authored
2 years ago
by
Florent Aide
Browse files
Options
Downloads
Patches
Plain Diff
implement helm chart merging
parent
c4f0f586a2f1
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
runner/cmd.go
+91
-3
91 additions, 3 deletions
runner/cmd.go
runner/cmd_test.go
+14
-0
14 additions, 0 deletions
runner/cmd_test.go
runner/config.go
+0
-23
0 additions, 23 deletions
runner/config.go
runner/config_test.go
+21
-20
21 additions, 20 deletions
runner/config_test.go
with
126 additions
and
46 deletions
runner/cmd.go
+
91
−
3
View file @
6ee63476
...
...
@@ -27,7 +27,9 @@
}
func
NewCmdConfig
(
logger
zerolog
.
Logger
,
configDir
string
,
namespace
string
)
(
*
CmdConfig
,
error
)
{
var
cmdConfig
CmdConfig
cmdConfig
:=
&
CmdConfig
{}
cmdConfig
.
Spec
.
Charts
.
Helm
=
make
(
map
[
string
]
CmdHelmChart
)
cmdConfig
.
Spec
.
Charts
.
Ytt
=
make
(
map
[
string
]
CmdYttChart
)
cmdConfig
.
Namespace
=
namespace
cmdConfig
.
Logger
=
logger
...
...
@@ -42,4 +44,9 @@
return
nil
,
err
}
// first "import" all variables from baseCfg
cmdConfig
.
Spec
.
Variables
=
baseCfg
.
Spec
.
Variables
// then merge in all variables from the nsCfg
cmdConfig
.
MergeVariables
(
nsCfg
)
// TODO:
...
...
@@ -45,3 +52,10 @@
// TODO:
// - merge baseCfg & nsCfg according to magic
// - merge baseCfg & nsCfg charts
if
err
:=
cmdConfig
.
importCharts
(
baseCfg
);
err
!=
nil
{
return
nil
,
err
}
if
err
:=
cmdConfig
.
importCharts
(
nsCfg
);
err
!=
nil
{
return
nil
,
err
}
// - hydrate
...
...
@@ -47,5 +61,9 @@
// - hydrate
return
&
cmdConfig
,
nil
if
err
:=
cmdConfig
.
hydrate
();
err
!=
nil
{
return
nil
,
err
}
return
cmdConfig
,
nil
}
type
CmdConfig
struct
{
...
...
@@ -137,3 +155,73 @@
}
return
nil
}
// MergeVariables takes a config (from a file, not a cmd one) and import its
// variables into the current cmdconfig by replacing old ones
// and adding the new ones
func
(
c
*
CmdConfig
)
MergeVariables
(
other
*
Config
)
{
for
_
,
variable
:=
range
other
.
Spec
.
Variables
{
c
.
overlayVariable
(
variable
)
}
}
// 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
*
CmdConfig
)
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
}
}
c
.
Spec
.
Variables
=
append
(
c
.
Spec
.
Variables
,
v
)
}
func
(
c
*
CmdConfig
)
importCharts
(
other
*
Config
)
error
{
if
err
:=
c
.
importHelmCharts
(
other
.
Spec
.
Charts
.
Helm
);
err
!=
nil
{
return
nil
}
if
err
:=
c
.
importYttCharts
(
other
.
Spec
.
Charts
.
Ytt
);
err
!=
nil
{
return
nil
}
return
nil
}
func
(
c
*
CmdConfig
)
importHelmCharts
(
helmCharts
map
[
string
]
HelmChart
)
error
{
for
id
,
chart
:=
range
helmCharts
{
convertedChart
,
err
:=
cmdHelmChartFromHelmChart
(
chart
)
if
err
!=
nil
{
return
err
}
_
,
ok
:=
c
.
Spec
.
Charts
.
Helm
[
id
]
if
!
ok
{
// we have no chart by that name yet...
// create one
c
.
Spec
.
Charts
.
Helm
[
id
]
=
*
convertedChart
continue
}
// else just append values to existing one
convertedChart
.
Values
=
append
(
c
.
Spec
.
Charts
.
Helm
[
id
]
.
Values
,
convertedChart
.
Values
...
)
c
.
Spec
.
Charts
.
Helm
[
id
]
=
*
convertedChart
}
return
nil
}
func
cmdHelmChartFromHelmChart
(
c
HelmChart
)
(
*
CmdHelmChart
,
error
)
{
strValues
,
err
:=
yaml
.
Marshal
(
c
.
Values
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
CmdHelmChart
{
Type
:
c
.
Type
,
Name
:
c
.
Name
,
Values
:
[]
string
{
string
(
strValues
)},
},
nil
}
func
(
c
*
CmdConfig
)
importYttCharts
(
yttCharts
map
[
string
]
YttChart
)
error
{
return
nil
}
This diff is collapsed.
Click to expand it.
runner/cmd_test.go
+
14
−
0
View file @
6ee63476
...
...
@@ -6,6 +6,7 @@
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"orus.io/cloudcrane/beaver/testutils"
)
func
TestRunCMD
(
t
*
testing
.
T
)
{
...
...
@@ -19,3 +20,16 @@
fmt
.
Println
(
errMsg
)
}
}
func
TestCmdConfig
(
t
*
testing
.
T
)
{
tl
:=
testutils
.
NewTestLogger
(
t
)
testNS
:=
"ns1"
c
,
err
:=
NewCmdConfig
(
tl
.
Logger
(),
"fixtures/"
,
testNS
)
require
.
NoError
(
t
,
err
)
// TODO: make sure this is the expected values including the leading | ...
assert
.
Equal
(
t
,
[]
string
{
"|
\n
config:
\n
datasource:
\n
password: <path:cnpp.k8s.cloudcrane.io/data/ns1/postgres#password>
\n
role: 'admin'
\n
fullnameoverride: pg-exporter-ns1
\n
"
},
c
.
Spec
.
Charts
.
Helm
[
"postgres"
]
.
Values
,
)
}
This diff is collapsed.
Click to expand it.
runner/config.go
+
0
−
23
View file @
6ee63476
...
...
@@ -62,26 +62,3 @@
return
cfg
,
nil
}
// 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
)
}
}
// 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
}
}
c
.
Spec
.
Variables
=
append
(
c
.
Spec
.
Variables
,
v
)
}
This diff is collapsed.
Click to expand it.
runner/config_test.go
+
21
−
20
View file @
6ee63476
...
...
@@ -5,7 +5,7 @@
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"
orus.io/cloudcrane/beaver/testutils
"
"
gopkg.in/yaml.v2
"
)
func
TestConfig
(
t
*
testing
.
T
)
{
...
...
@@ -9,5 +9,4 @@
)
func
TestConfig
(
t
*
testing
.
T
)
{
logger
:=
testutils
.
GetLogger
(
t
)
configDir
:=
"fixtures/"
...
...
@@ -13,6 +12,5 @@
configDir
:=
"fixtures/"
testNS
:=
"ns1"
config
,
err
:=
NewConfig
(
logger
,
configDir
,
testNS
)
config
,
err
:=
NewConfig
(
configDir
)
require
.
NoError
(
t
,
err
)
// first config.spec.variables entry name should be VAULT_KV in our test file
assert
.
Equal
(
t
,
"VAULT_KV"
,
config
.
Spec
.
Variables
[
0
]
.
Name
)
...
...
@@ -16,8 +14,9 @@
require
.
NoError
(
t
,
err
)
// first config.spec.variables entry name should be VAULT_KV in our test file
assert
.
Equal
(
t
,
"VAULT_KV"
,
config
.
Spec
.
Variables
[
0
]
.
Name
)
// the postgres chart should have been expanded with our variables
assert
.
Equal
(
t
,
`config:
dumped
,
err
:=
yaml
.
Marshal
(
config
.
Spec
.
Charts
.
Helm
[
"postgres"
]
.
Values
)
require
.
NoError
(
t
,
err
)
// this config entry is just read from the base file, and not yet hydrated
assert
.
Equal
(
t
,
`config:
datasource:
...
...
@@ -23,4 +22,5 @@
datasource:
password: <path:cnpp.k8s.cloudcrane.io/data/ns1/postgres#password>
fullnameoverride: pg-exporter-ns1
password: <path:{{.VAULT_KV}}/data/{{.namespace}}/postgres#password>
role: '{{.ROLE}}'
fullnameoverride: pg-exporter-{{.namespace}}
`
,
...
...
@@ -26,4 +26,3 @@
`
,
config
.
Spec
.
Charts
.
Helm
[
"postgres"
]
.
Values
,
)
string
(
dumped
))
...
...
@@ -29,5 +28,6 @@
// verify ytt entries
assert
.
Equal
(
t
,
"cnpp.k8s.cloudcrane.io"
,
config
.
Spec
.
Charts
.
Ytt
[
"odoo"
]
.
Values
[
0
]
.
Value
)
assert
.
Equal
(
t
,
testNS
,
config
.
Spec
.
Charts
.
Ytt
[
"odoo"
]
.
Values
[
1
]
.
Value
)
/*
// verify ytt entries
assert.Equal(t, "cnpp.k8s.cloudcrane.io", config.Spec.Charts.Ytt["odoo"].Values[0].Value)
assert.Equal(t, testNS, config.Spec.Charts.Ytt["odoo"].Values[1].Value)
...
...
@@ -33,5 +33,5 @@
// yaml support should let toto entry use DEFAULT_VALUES defined in odoo
assert
.
Equal
(
t
,
"cnpp.k8s.cloudcrane.io"
,
config
.
Spec
.
Charts
.
Ytt
[
"toto"
]
.
Values
[
0
]
.
Value
)
assert
.
Equal
(
t
,
testNS
,
config
.
Spec
.
Charts
.
Ytt
[
"toto"
]
.
Values
[
1
]
.
Value
)
// yaml support should let toto entry use DEFAULT_VALUES defined in odoo
assert.Equal(t, "cnpp.k8s.cloudcrane.io", config.Spec.Charts.Ytt["toto"].Values[0].Value)
assert.Equal(t, testNS, config.Spec.Charts.Ytt["toto"].Values[1].Value)
...
...
@@ -37,4 +37,5 @@
// verify variables overwrite
assert
.
Equal
(
t
,
"admin"
,
config
.
Spec
.
Charts
.
Ytt
[
"odoo"
]
.
Values
[
2
]
.
Value
)
// 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