dtk_crd_parser 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e69f417b48d667384312f79a580f591788d3e677
4
+ data.tar.gz: e77320a777a0c480e2defc94db3c3660796215bf
5
+ SHA512:
6
+ metadata.gz: 7009538c93dc66dcaa01df238cef75eeea2b0d057582c24a6dff709c48cfac148dea0bb11f7f2503c08f7ac4a85b7137a76fc761cbe3f1a99c12b31a9b8ebe11
7
+ data.tar.gz: c19e34dad7e41474b19460ad95efba219b4ba4c9d952605460209f0afc6bfbb25c0e94bbe85e94452d6f03606b98710ef7f7ec379b180b11f66c830dfa059ff3
@@ -0,0 +1,52 @@
1
+ module DTK::CrdParser
2
+ class BaseClass
3
+ module Helper
4
+ module ClassAndInstanceMixin
5
+ # Helper functions want to have available in all classes
6
+
7
+ def fail_if_not_type(object, type)
8
+ if object.kind_of?(type)
9
+ object
10
+ else
11
+ fail "Expected a #{type}, but given a '#{object.class}'"
12
+ end
13
+ end
14
+
15
+ def fail_if_not_concrete(parent)
16
+ klass = (parent.kind_of?(::Module) ? parent : parent.class)
17
+ the_method = "The method"
18
+ if caller.first =~ /in `(.+)'/
19
+ method_name = $1
20
+ the_method << " '#{method_name}'"
21
+ end
22
+ fail "#{the_method} is not defined in the concrete class #{klass}"
23
+ end
24
+
25
+ def isOnSpecificNode(actionComponent)
26
+ actionComponent.include? "/"
27
+ end
28
+
29
+ def destructureActionComponent(actionComponent)
30
+ # regex to match component: module::name[attrName].action
31
+ regex = /(.*)::(.*)\[(.*)\].?(.*)/
32
+ # if component on node group node, get full name first
33
+ # i.e ec2::node[ng-1]/(node-utility::ssh-access[ubuntu])
34
+ nodeName, actionComponent = actionComponent.match(/(.*)\/(.*)/).captures if isOnSpecificNode(actionComponent)
35
+ compModule, compName, attributeName, action = actionComponent.match(regex).captures
36
+ action = !action.empty? ? action.to_sym : :create
37
+
38
+ if(compModule.nil? || compName.nil? || attributeName.nil?)
39
+ raise `Could not resolve component module, name or attribute name for component: #{actionComponent}`
40
+ end
41
+ {
42
+ moduleName: compModule,
43
+ componentName: compName,
44
+ attributeName: attributeName,
45
+ action: action
46
+ }
47
+ end
48
+
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,8 @@
1
+ module DTK::CrdParser
2
+ class BaseClass
3
+ require_relative('base_class/helper_mixins')
4
+
5
+ include Helper::ClassAndInstanceMixin
6
+ extend Helper::ClassAndInstanceMixin
7
+ end
8
+ end
@@ -0,0 +1,18 @@
1
+ module DTK::CrdParser
2
+ class Component
3
+ class Attribute < BaseClass
4
+ def initialize(name, value)
5
+ @name = name
6
+ @value = value
7
+ end
8
+
9
+ def self.create_from_kube(attribute)
10
+ new Attribute(attribute[:name], attribute[:value])
11
+ end
12
+
13
+ protected
14
+
15
+ attr_reader :name, :value
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,45 @@
1
+ module DTK::CrdParser
2
+ class Component < BaseClass
3
+ require_relative('component/attribute')
4
+
5
+ MODE_DEFAULT = :component_instance
6
+ def initialize(name, component_def, attributes)
7
+ @name = name
8
+ @component_def = component_def # this wil point to the corresponding component def
9
+ @attributes = attributes # array that has an Attribute object for each attribute; these are name-value pairs
10
+ end
11
+
12
+ def self.create_from_kube(client, component_instance, component_name)
13
+ puts component_instance
14
+ unless client.discovered
15
+ client.discover
16
+ end
17
+ puts client.methods - Object.methods
18
+ component_instance_obj = client.get_component(component_instance[:name], component_instance[:namespace])
19
+ puts component_instance_obj
20
+ component_def = ComponentDef.create_from_kube(client, component_instance_obj, component_name)
21
+ component_attributes = getComponentAttributes(component_name, component_instance_obj[:spec][:components])
22
+ attributes = []
23
+ component_attributes.each do |attr|
24
+ attributes.push Attribute.create_from_kube(attr)
25
+ end
26
+ Component.new(component_name, component_def, attributes)
27
+ end
28
+
29
+ protected
30
+
31
+ def self.getComponentAttributes(fullComponentName, components)
32
+ fullComponentName.gsub!('_', '-')
33
+ component = components.each do |component|
34
+ componentObj = component[fullComponentName]
35
+ if(componentObj && componentObj.is_a?(Kubeclient::Resource))
36
+ return componentObj[:attributes]
37
+ end
38
+ end
39
+ return nil
40
+ end
41
+
42
+ attr_reader :name, :component_def, :attributes
43
+
44
+ end
45
+ end
@@ -0,0 +1,20 @@
1
+ module DTK::CrdParser
2
+ class ComponentDef
3
+ class AttributeTypeInfo < BaseClass
4
+ def initialize(params)
5
+ @name = params[:name]
6
+ @type = params[:type]
7
+ @required = params[:required] || false
8
+ @dynamic = params[:dynamic] || false
9
+ end
10
+
11
+ def self.resolveAttr(attribute)
12
+ new AttributeTypeInfo(attribute)
13
+ end
14
+
15
+ protected
16
+
17
+ attr_reader :name
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,60 @@
1
+ module DTK::CrdParser
2
+ class ComponentDef < BaseClass
3
+ require_relative('component_def/attribute_type_info')
4
+
5
+ MODE_DEFAULT = :component_instance
6
+
7
+ def initialize(metadata, componentDefAction, componentDefAttributes)
8
+ @name = metadata[:name]
9
+ @namespace = metadata[:namespace]
10
+ @resourceVersion = metadata[:resourceVersion]
11
+
12
+ @bash_script = componentDefAction[:bash_script]
13
+ @entrypoint = componentDefAction[:entrypoint]
14
+ @type = componentDefAction[:type]
15
+
16
+ @attribute_type_info = componentDefAttributes
17
+ end
18
+
19
+ def self.create_from_kube(client, component_instance, component_def_name)
20
+ puts 'COMPONENT DEF'
21
+ module_ref = resolveModuleReference(component_instance.module_refs, component_def_name, destructureActionComponent(component_def_name)[:moduleName])
22
+ componentDef = client.get_componentdef(component_def_name, module_ref[:namespace])
23
+
24
+ name = componentDef[:metadata][:name]
25
+ namespace = componentDef[:metadata][:namespace]
26
+ resourceVersion = componentDef[:metadata][:namespace]
27
+
28
+ componentDefSpec = componentDef[:spec]
29
+ componentDefAction = componentDefSpec[:actions][component.action.to_sym]
30
+ componentDefAttributes = []
31
+
32
+ componentDefSpec[:attributes].each do |attribute|
33
+ componentDefAttributes.push AttributeTypeInfo.resolveAttr(attribute)
34
+ end
35
+
36
+ metadata = {
37
+ name: name,
38
+ namespace: namespace,
39
+ resourceVersion: resourceVersion
40
+ }
41
+
42
+ ComponentDef.new(metadata, componentDefAction, componentDefAttributes)
43
+ end
44
+
45
+ private
46
+
47
+ def self.resolveModuleReference(module_refs, component_full_name, module_name)
48
+ module_ref = module_refs.find { |module_ref| module_ref[:name].gsub!('_', '-') == module_name.gsub!('_', '-') }
49
+ if(module_ref.nil?)
50
+ raise "Could not resolve module reference for component #{component_full_name} in component instance. Expected to find: #{module_name}"
51
+ end
52
+ module_ref
53
+ end
54
+
55
+ protected
56
+
57
+ attr_reader :name, :namespace, :attribute_type_info, :bash_script, :entrypoint, :type
58
+
59
+ end
60
+ end
data/lib/crd_parser.rb ADDED
@@ -0,0 +1,15 @@
1
+ module DTK
2
+ module DTK::CrdParser
3
+ require_relative('crd_parser/base_class')
4
+
5
+ require_relative('crd_parser/component')
6
+ require_relative('crd_parser/component_def')
7
+
8
+ def self.parse_crd_to_component(client, component_instance, component_name)
9
+ #component instance has a name and a namespace
10
+ #client is kube client
11
+ #component name is, for example, network-aws::vpc[vpc1]
12
+ Component.create_from_kube(client, component_instance, component_name)
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dtk_crd_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - DTK
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Parse kubernetes DTK crd to ruby in memory object
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/crd_parser.rb
20
+ - lib/crd_parser/base_class.rb
21
+ - lib/crd_parser/base_class/helper_mixins.rb
22
+ - lib/crd_parser/component.rb
23
+ - lib/crd_parser/component/attribute.rb
24
+ - lib/crd_parser/component_def.rb
25
+ - lib/crd_parser/component_def/attribute_type_info.rb
26
+ homepage: https://rubygems.org/gems/dtk_crd_parser
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.5.2.1
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Parse kubernetes DTK crd to ruby in memory object!
50
+ test_files: []