Last active
December 18, 2019 11:46
-
-
Save odinsy/85cecdc023ff7ccea7f8a17313528fb3 to your computer and use it in GitHub Desktop.
erb-template to configmap migration libs example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Library for rendering config from template | |
| require 'erb' | |
| class ServiceConfig | |
| attr_accessor :template, :settings | |
| def initialize(template, settings) | |
| @template = template | |
| @settings = settings | |
| end | |
| def render | |
| ERB.new(@template, nil, '-').result(binding) | |
| end | |
| end | |
| def render_config(config_name, settings) | |
| template = ::File.read("/var/chef/cache/cookbooks/#{cookbook_name}/templates/default/#{config_name}.erb") | |
| ServiceConfig.new(template, settings).render | |
| end | |
| def patch_config_name(name) | |
| name.sub!('.', '') if name.start_with?('.') | |
| name.tr('_', '-') | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Client to Kubernetes | |
| begin | |
| require 'kubeclient' | |
| rescue LoadError | |
| run_context = Chef::RunContext.new(Chef::Node.new, {}, Chef::EventDispatch::Dispatcher.new) | |
| kubeclient = Chef::Resource::ChefGem.new('kubeclient', run_context) | |
| kubeclient.run_action(:install) | |
| require 'kubeclient' | |
| end | |
| def define_config_map(name, namespace) | |
| config_map = Kubeclient::Resource.new | |
| config_map.metadata = {} | |
| config_map.metadata.name = name | |
| config_map.metadata.namespace = namespace | |
| config_map.data = {} | |
| config_map | |
| end | |
| def appply_config_map(config_map) | |
| client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1') | |
| begin | |
| client.update_config_map(config_map) | |
| rescue Kubeclient::HttpError | |
| client.create_config_map(config_map) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Chef resource for configmap mapping | |
| resource_name :configure_app | |
| actions :apply_configmap | |
| property :service_name, kind_of: String, name_property: true, required: true | |
| property :profile, kind_of: String, default: '' | |
| property :settings, kind_of: Hash, required: true | |
| property :deploy_path, kind_of: String, required: true | |
| property :template_directory, kind_of: String, default: '' | |
| property :namespace, kind_of: String, default: node.read(:deploy, :namespace) || 'default' | |
| property :configs, kind_of: Array | |
| action :apply_configmap do | |
| settings = new_resource.settings | |
| service_name = new_resource.service_name | |
| configs = new_resource.configs | |
| namespace = new_resource.namespace | |
| config_map = define_config_map(service_name, namespace) | |
| configs.each do |config_name| | |
| rendered_config = render_config(config_name, settings) | |
| config_map.data[config_name] = rendered_config | |
| end | |
| appply_config_map(config_map) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment