Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
ksonnet
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ijaz Ahmad
ksonnet
Commits
e515524e
Commit
e515524e
authored
7 years ago
by
Angus Lees
Committed by
GitHub
7 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #67 from anguslees/toyaml
Add manifestJson and manifestYaml functions
parents
86dc7ebd
1f85d5dd
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
lib/kubecfg.libsonnet
+21
-0
21 additions, 0 deletions
lib/kubecfg.libsonnet
lib/kubecfg_test.jsonnet
+31
-4
31 additions, 4 deletions
lib/kubecfg_test.jsonnet
utils/nativefuncs.go
+27
-0
27 additions, 0 deletions
utils/nativefuncs.go
with
79 additions
and
4 deletions
lib/kubecfg.libsonnet
+
21
−
0
View file @
e515524e
...
...
@@ -13,6 +13,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// NB: libjsonnet native functions can only pass primitive types, so
// some functions json-encode the arg. These "*FromJson" functions
// will be replaced by regular native version when libjsonnet is able
// to support this. This file strives to hide this implementation
// detail.
{
// parseJson(data): parses the `data` string as a json document, and
// returns the resulting jsonnet object.
...
...
@@ -24,6 +30,21 @@
// element.
parseYaml
::
std
.
native
(
"parseYaml"
),
// manifestJson(value, indent): convert the jsonnet object `value`
// to a string encoded as "pretty" (multi-line) JSON, with each
// nesting level indented by `indent` spaces.
manifestJson
(
value
,
indent
=
4
)::
(
local
f
=
std
.
native
(
"manifestJsonFromJson"
);
f
(
std
.
toString
(
value
),
indent
)
),
// manifestYaml(value): convert the jsonnet object `value` to a
// string encoded as a single YAML document.
manifestYaml
(
value
)::
(
local
f
=
std
.
native
(
"manifestYamlFromJson"
);
f
(
std
.
toString
(
value
))
),
// escapeStringRegex(s): Quote the regex metacharacters found in s.
// The result is a regex that will match the original literal
// characters.
...
...
This diff is collapsed.
Click to expand it.
lib/kubecfg_test.jsonnet
+
31
−
4
View file @
e515524e
...
...
@@ -27,16 +27,43 @@ baz: xyzzy
"
);
assert
x
==
[[
3
,
4
],
{
foo
:
"bar"
,
baz
:
"xyzzy"
}]
:
"got "
+
x
;
local
x
=
kubecfg
.
manifestJson
({
foo
:
"bar"
,
baz
:
[
3
,
4
]});
assert
x
==
'{
"baz": [
3,
4
],
"foo": "bar"
}
'
:
"got "
+
x
;
local
x
=
kubecfg
.
manifestJson
({
foo
:
"bar"
,
baz
:
[
3
,
4
]},
indent
=
2
);
assert
x
==
'{
"baz": [
3,
4
],
"foo": "bar"
}
'
:
"got "
+
x
;
local
x
=
kubecfg
.
manifestYaml
({
foo
:
"bar"
,
baz
:
[
3
,
4
]});
assert
x
==
"baz:
- 3
- 4
foo: bar
"
:
"got "
+
x
;
local
i
=
kubecfg
.
resolveImage
(
"busybox"
);
assert
i
==
"busybox:latest"
:
"got "
+
i
;
assert
kubecfg
.
regexMatch
(
"o$"
,
"foo"
);
local
r
1
=
kubecfg
.
escapeStringRegex
(
"f[o"
);
assert
r
1
==
"f
\\
[o"
:
"got "
+
r
1
;
local
r
=
kubecfg
.
escapeStringRegex
(
"f[o"
);
assert
r
==
"f
\\
[o"
:
"got "
+
r
;
local
r
2
=
kubecfg
.
regexSubst
(
"e"
,
"tree"
,
"oll"
);
assert
r
2
==
"trolloll"
:
"got "
+
r
2
;
local
r
=
kubecfg
.
regexSubst
(
"e"
,
"tree"
,
"oll"
);
assert
r
==
"trolloll"
:
"got "
+
r
;
// Kubecfg wants to see something that looks like a k8s object
{
...
...
This diff is collapsed.
Click to expand it.
utils/nativefuncs.go
+
27
−
0
View file @
e515524e
...
...
@@ -20,6 +20,9 @@ import (
"encoding/json"
"io"
"regexp"
"strings"
goyaml
"github.com/ghodss/yaml"
jsonnet
"github.com/strickyak/jsonnet_cgo"
"k8s.io/apimachinery/pkg/util/yaml"
...
...
@@ -40,6 +43,11 @@ func resolveImage(resolver Resolver, image string) (string, error) {
// RegisterNativeFuncs adds kubecfg's native jsonnet functions to provided VM
func
RegisterNativeFuncs
(
vm
*
jsonnet
.
VM
,
resolver
Resolver
)
{
// NB: libjsonnet native functions can only pass primitive
// types, so some functions json-encode the arg. These
// "*FromJson" functions will be replaced by regular native
// version when libjsonnet is able to support this.
vm
.
NativeCallback
(
"parseJson"
,
[]
string
{
"json"
},
func
(
data
[]
byte
)
(
res
interface
{},
err
error
)
{
err
=
json
.
Unmarshal
(
data
,
&
res
)
return
...
...
@@ -61,6 +69,25 @@ func RegisterNativeFuncs(vm *jsonnet.VM, resolver Resolver) {
return
ret
,
nil
})
vm
.
NativeCallback
(
"manifestJsonFromJson"
,
[]
string
{
"json"
,
"indent"
},
func
(
data
[]
byte
,
indent
int
)
(
string
,
error
)
{
data
=
bytes
.
TrimSpace
(
data
)
buf
:=
bytes
.
Buffer
{}
if
err
:=
json
.
Indent
(
&
buf
,
data
,
""
,
strings
.
Repeat
(
" "
,
indent
));
err
!=
nil
{
return
""
,
err
}
buf
.
WriteString
(
"
\n
"
)
return
buf
.
String
(),
nil
})
vm
.
NativeCallback
(
"manifestYamlFromJson"
,
[]
string
{
"json"
},
func
(
data
[]
byte
)
(
string
,
error
)
{
var
input
interface
{}
if
err
:=
json
.
Unmarshal
(
data
,
&
input
);
err
!=
nil
{
return
""
,
err
}
output
,
err
:=
goyaml
.
Marshal
(
input
)
return
string
(
output
),
err
})
vm
.
NativeCallback
(
"resolveImage"
,
[]
string
{
"image"
},
func
(
image
string
)
(
string
,
error
)
{
return
resolveImage
(
resolver
,
image
)
})
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
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