devfile 0.0.2.pre.alpha1

Sign up to get free protection for your applications and to get access to all the features.
data/ext/main.go ADDED
@@ -0,0 +1,113 @@
1
+ package main
2
+
3
+ /*
4
+ struct result {
5
+ char *resources;
6
+ char *flattenedDevfile;
7
+ char *err;
8
+ };
9
+ */
10
+ import "C"
11
+ import "k8s.io/apimachinery/pkg/runtime"
12
+
13
+ func main() {}
14
+
15
+ // START C Compatibility
16
+
17
+ func NewResult(objs []runtime.Object, flattenedDevfile string, err error) C.struct_result {
18
+ if err != nil {
19
+ return C.struct_result{
20
+ err: C.CString(err.Error()),
21
+ }
22
+ }
23
+
24
+ resources, err := marshal(objs)
25
+ r := C.struct_result{
26
+ resources: C.CString(resources),
27
+ flattenedDevfile: C.CString(flattenedDevfile),
28
+ err: nil,
29
+ }
30
+ if err != nil {
31
+ r.err = C.CString(err.Error())
32
+ }
33
+
34
+ return r
35
+ }
36
+
37
+ func InGo(devfile *C.char) (Devfile, error) {
38
+ rawContent := C.GoString(devfile)
39
+ devfileObj, err := parseDevfile(rawContent)
40
+ return Devfile{
41
+ devfileObj: devfileObj,
42
+ }, err
43
+ }
44
+
45
+ //export getDeployment
46
+ func getDeployment(name *C.char, namespace *C.char, devfile *C.char) C.struct_result {
47
+ d, err := InGo(devfile)
48
+ if err != nil {
49
+ return NewResult(nil, "", err)
50
+ }
51
+ exists, err := d.hasContainerComponents()
52
+ if err != nil {
53
+ return NewResult(nil, "", err)
54
+ }
55
+ if exists == false {
56
+ return NewResult([]runtime.Object{nil}, "", err)
57
+ }
58
+ deployment, err := d.getDeployment(C.GoString(name), C.GoString(namespace))
59
+ return NewResult([]runtime.Object{deployment}, d.getFlattenedDevfileContent(), err)
60
+ }
61
+
62
+ //export getService
63
+ func getService(name *C.char, namespace *C.char, devfile *C.char) C.struct_result {
64
+ d, err := InGo(devfile)
65
+ if err != nil {
66
+ return NewResult(nil, "", err)
67
+ }
68
+ exists, err := d.hasContainerComponents()
69
+ if err != nil {
70
+ return NewResult(nil, "", err)
71
+ }
72
+ if exists == false {
73
+ return NewResult([]runtime.Object{nil}, "", err)
74
+ }
75
+ service, err := d.getService(C.GoString(name), C.GoString(namespace))
76
+ return NewResult([]runtime.Object{service}, d.getFlattenedDevfileContent(), err)
77
+ }
78
+
79
+ //export getIngress
80
+ func getIngress(name *C.char, namespace *C.char, devfile *C.char, domain *C.char) C.struct_result {
81
+ d, err := InGo(devfile)
82
+ if err != nil {
83
+ return NewResult(nil, "", err)
84
+ }
85
+ exists, err := d.hasContainerComponents()
86
+ if err != nil {
87
+ return NewResult(nil, "", err)
88
+ }
89
+ if exists == false {
90
+ return NewResult([]runtime.Object{nil}, "", err)
91
+ }
92
+ ingress, err := d.getIngress(C.GoString(name), C.GoString(namespace), C.GoString(domain))
93
+ return NewResult([]runtime.Object{ingress}, d.getFlattenedDevfileContent(), err)
94
+ }
95
+
96
+ //export getAll
97
+ func getAll(name *C.char, namespace *C.char, devfile *C.char, domain *C.char) C.struct_result {
98
+ d, err := InGo(devfile)
99
+ if err != nil {
100
+ return NewResult(nil, "", err)
101
+ }
102
+ exists, err := d.hasContainerComponents()
103
+ if err != nil {
104
+ return NewResult(nil, "", err)
105
+ }
106
+ if exists == false {
107
+ return NewResult([]runtime.Object{nil}, "", err)
108
+ }
109
+ resources, err := d.getAll(C.GoString(name), C.GoString(namespace), C.GoString(domain))
110
+ return NewResult(resources, d.getFlattenedDevfileContent(), err)
111
+ }
112
+
113
+ // END C Compatibility
data/lib/devfile.rb ADDED
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ffi'
4
+
5
+ # Module that works with the Devfile standard
6
+ module Devfile
7
+ extend FFI::Library
8
+
9
+ ffi_lib File.expand_path('./devfile.so', File.dirname(__FILE__))
10
+
11
+ # Result from go containing an error and the result
12
+ class Result < FFI::Struct
13
+ layout :resources, :string,
14
+ :flattenedDevfile, :string,
15
+ :err, :string
16
+
17
+ def process
18
+ raise self[:err] if self[:err]
19
+
20
+ [self[:resources], self[:flattenedDevfile]]
21
+ end
22
+ end
23
+
24
+ # Set of services to parse a devfile and output k8s manifests
25
+ class Parser
26
+ class << self
27
+ def get_deployment(name, namespace, devfile)
28
+ Devfile.getDeployment(name, namespace, devfile).process
29
+ end
30
+
31
+ def get_service(name, namespace, devfile)
32
+ Devfile.getService(name, namespace, devfile).process
33
+ end
34
+
35
+ def get_ingress(name, namespace, devfile, domain)
36
+ Devfile.getIngress(name, namespace, devfile, domain).process
37
+ end
38
+
39
+ def get_all(name, namespace, devfile, domain)
40
+ Devfile.getAll(name, namespace, devfile, domain).process
41
+ end
42
+ end
43
+ end
44
+
45
+ attach_function 'getDeployment', %i[string string string], Result.by_value
46
+ attach_function 'getService', %i[string string string], Result.by_value
47
+ attach_function 'getIngress', %i[string string string string], Result.by_value
48
+ attach_function 'getAll', %i[string string string string], Result.by_value
49
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: devfile
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2.pre.alpha1
5
+ platform: ruby
6
+ authors:
7
+ - GitLab
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-02-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Library used to generate kubernetes manifests from a Devfile.
14
+ email: spatnaik@gitlab.com
15
+ executables: []
16
+ extensions:
17
+ - "./ext/extconf.rb"
18
+ extra_rdoc_files: []
19
+ files:
20
+ - "./ext/extconf.rb"
21
+ - ext/devfile.go
22
+ - ext/extconf.rb
23
+ - ext/go.mod
24
+ - ext/go.sum
25
+ - ext/main.go
26
+ - lib/devfile.rb
27
+ homepage: https://gitlab.com
28
+ licenses:
29
+ - MIT
30
+ metadata:
31
+ source_code_uri: https://gitlab.com/gitlab-org/remote-development/devfile-gem
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '2.7'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">"
44
+ - !ruby/object:Gem::Version
45
+ version: 1.3.1
46
+ requirements: []
47
+ rubygems_version: 3.2.33
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Parse and generate kubernetes manifests from a Devfile
51
+ test_files: []