rewrite

parent 75280a92
Pipeline #65087 failed with stages
in 24 seconds
......@@ -7,7 +7,7 @@ Metrics/BlockLength:
Max: 64
Metrics/LineLength:
Max: 82
Max: 90
Metrics/MethodLength:
Max: 21
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'colorize'
require 'erb'
require 'optparse'
require 'ostruct'
require 'yaml'
require_relative '../lib/kubernetes_rules'
input_dir = './rules'
output_dir = './rules-k8s'
require 'optparse'
options = {}
OptionParser.new do |opts|
......@@ -26,117 +21,5 @@ OptionParser.new do |opts|
end
end.parse!
def k8s_template
%(---
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: <%= rule_name %>
labels:
app: prometheus-operator
release: gitlab-monitoring
spec:
<%= template %>
)
end
def generate_rule_name(filename)
File.basename(filename).match('[\w\-_]+')[0].tr('_', '-')
end
def gather_vars(input_file, rule_name)
template = File
.readlines(input_file)
.each(&:chomp)
.join(' ')
OpenStruct.new(rule_name: rule_name, template: template)
end
def render_for_k8s(template_vars)
render_k8s = ERB.new(k8s_template)
render_k8s.result(template_vars.instance_eval { binding })
end
def yaml_valid?(file)
puts "Validating YAML on #{file}"
begin
YAML.load_file(file)
rescue StandardError => e
puts "Validation Failed on #{file}:\n#{e}".red
return false
end
true
end
def rules_valid?(file)
puts "Validating Rule(s) on #{file}"
error_count = 0
begin
render = YAML.load_file(file)
rescue StandardError
puts "Skipping Rule Validation on #{file} due to improper yaml".yellow
return true
end
render['spec']['groups'].each do |group|
group['rules'].each do |rule|
rule.each do |k, v|
next if k == 'labels'
next if k == 'annotations'
unless v.is_a?(String)
puts "#{k}: #{v} is the incorrect type, must be string".red
error_count += 1
end
end
end
end
error_count.zero?
end
def yaml_files_valid?(files)
valid = true
files.each do |file|
unless yaml_valid?(file)
puts "File #{file} did not pass YAML validation"
valid = false
end
unless rules_valid?(file)
puts "File #{file} did not pass rule validation"
valid = false
end
end
valid
end
if options[:create]
files = Dir.glob("#{input_dir}/*.yml")
files.each do |file|
file_name = File.basename(file)
puts "Rendering #{file_name}"
output_file = "#{output_dir}/#{File.basename(file)}"
rule_name = generate_rule_name(file)
template_variables = gather_vars(file, rule_name)
rendered_template = render_for_k8s(template_variables)
File.write(output_file, rendered_template)
end
end
if options[:validate]
files = Dir.glob("#{output_dir}/*.yml")
if files.empty?
puts "No file exist to validate in #{output_dir}".red
exit 1
end
unless yaml_files_valid?(files)
puts "\nOne or more files failed validation!".red
exit 1
end
end
KubernetesRules::Create.new if options[:create]
KubernetesRules::Validate.new if options[:validate]
# frozen_string_literal: true
require 'colorize'
require 'erb'
require 'optparse'
require 'ostruct'
require 'yaml'
require_relative 'kubernetes_rules/create'
require_relative 'kubernetes_rules/validate'
#!/usr/bin/env ruby
# frozen_string_literal: true
module KubernetesRules
# Create will render our template files
class Create
def initialize
input_dir = './rules'
output_dir = './rules-k8s'
files = Dir.glob("#{input_dir}/*.yml")
files.each do |file|
file_name = File.basename(file)
puts "Rendering #{file_name}"
output_file = "#{output_dir}/#{File.basename(file)}"
rule_name = generate_rule_name(file)
template_variables = gather_vars(file, rule_name)
rendered_template = render_for_k8s(template_variables)
File.write(output_file, rendered_template)
end
end
def k8s_template
%(---
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: <%= rule_name %>
labels:
app: prometheus-operator
release: gitlab-monitoring
spec:
<%= template %>
)
end
def generate_rule_name(filename)
File.basename(filename).match('[\w\-_]+')[0].tr('_', '-')
end
def gather_vars(input_file, rule_name)
template = File.readlines(input_file).each(&:chomp).join(' ')
OpenStruct.new(rule_name: rule_name, template: template)
end
def render_for_k8s(template_vars)
render_k8s = ERB.new(k8s_template)
render_k8s.result(template_vars.instance_eval { binding })
end
end
end
#!/usr/bin/env ruby
# frozen_string_literal: true
module KubernetesRules
# Validate confirms both the yaml and rules are legit
class Validate
def initialize
@output_dir = './rules-k8s'
end
def validate!
files = Dir.glob("#{@output_dir}/*.yml")
if files.empty?
puts "No file exist to validate in #{@output_dir}".red
exit 1
end
unless yaml_files_valid?(files)
puts "\nOne or more files failed validation!".red
exit 1
end
end
def yaml_valid?(file)
puts "Validating YAML on #{file}"
begin
YAML.load_file(file)
rescue StandardError => e
puts "Validation Failed on #{file}:\n#{e}".red
return false
end
true
end
def rules_valid?(file)
puts "Validating Rule(s) on #{file}"
error_count = 0
begin
render = YAML.load_file(file)
rescue StandardError
puts "Skipping Rule Validation on #{file} due to improper yaml".yellow
return true
end
render['spec']['groups'].each do |group|
group['rules'].each do |rule|
rule.each do |k, v|
next if k == 'labels'
next if k == 'annotations'
unless v.is_a?(String)
puts "#{k}: #{v} is the incorrect type, must be string".red
error_count += 1
end
end
end
end
error_count.zero?
end
def yaml_files_valid?(files)
valid = true
files.each do |file|
unless yaml_valid?(file)
puts "File #{file} did not pass YAML validation"
valid = false
end
unless rules_valid?(file)
puts "File #{file} did not pass rule validation"
valid = false
end
end
valid
end
end
end
# frozen_string_literal: true
require '../bin/create_kubernetes_rules.rb'
require 'rspec'
describe 'create_kubernetes_rules' do
it 'sets the name of the rule properly' do
rf = generate_rule_name('foobar.yml')
expect(rf).to eq('foobar')
rp = generate_rule_name('rules/foobar.yml')
expect(rp).to eq('foobar')
rh = generate_rule_name('foo-bar.yml')
expect(rh).to eq('foo-bar')
ru = generate_rule_name('foo_bar.yml')
expect(ru).to eq('foo-bar')
end
it 'creates our desired demplate' do
vars = OpenStruct.new(
rule_name: 'foobar',
template: <<~ENDOFRULESPEC
global:
- rule: expression
ENDOFRULESPEC
)
result = render_for_k8s(vars)
expected_result = <<~ENDOFEXPECT
---
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: foobar
labels:
app: prometheus-operator
release: gitlab-monitoring
spec:
global:
- rule: expression
ENDOFEXPECT
expect(result).to eq(expected_result)
end
it 'validates yaml' do
file = 'valid-yaml'
expect(YAML).to receive(:load_file).with(file).and_return(true)
result = yaml_valid?(file)
expect(result).to eq(true)
end
it 'reports invalid yaml' do
file = 'invalid-yaml'
expect(YAML).to receive(:load_file).with(file).and_raise(
StandardError.new('error')
)
result = yaml_valid?(file)
expect(result).to eq(false)
end
it 'validates rules' do
file = 'valid-rules'
expect(YAML).to receive(:load_file).with(file).and_return(true)
result = rules_valid?(file)
expect(result).to eq(true)
end
it 'reports invalid rules' do
file = 'invalid-rules'
rendered_template = '{spec: {groups: [rules: [foo: {}]]}}'
expect(YAML).to receive(:load_file).with(file).and_return(rendered_template)
result = rules_valid?(file)
expect(result).to eq(false)
end
it 'skip known objects that are not strings' do
file = 'string-skip'
rendered_template = '{spec: {groups: [rules: [labels: {}, annotations: {}]]}}'
expect(YAML).to receive(:load_file).with(file).and_return(rendered_template)
result = rules_valid?(file)
expect(result).to eq(true)
end
end
# frozen_string_literal: true
require 'spec_helper'
describe KubernetesRules::Create do
describe '#generate_rule_name' do
let(:rule) { described_class.new }
pending 'sets the name of the rule properly' do
rf = rule.generate_rule_name('foobar.yml')
expect(rf).to eq('foobar')
rp = rule.generate_rule_name('rules/foobar.yml')
expect(rp).to eq('foobar')
rh = rule.generate_rule_name('foo-bar.yml')
expect(rh).to eq('foo-bar')
ru = rule.generate_rule_name('foo_bar.yml')
expect(ru).to eq('foo-bar')
end
pending 'creates our desired demplate' do
vars = OpenStruct.new(
rule_name: 'foobar',
template: <<~ENDOFRULESPEC
global:
- rule: expression
ENDOFRULESPEC
)
result = rule.render_for_k8s(vars)
expected_result = <<~ENDOFEXPECT
---
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: foobar
labels:
app: prometheus-operator
release: gitlab-monitoring
spec:
global:
- rule: expression
ENDOFEXPECT
expect(result).to eq(expected_result)
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe KubernetesRules::Validate do
describe '#yaml_valid?' do
let(:yaml) { described_class.new }
pending 'validates yaml' do
file = 'valid-yaml'
expect(YAML).to receive(:load_file).with(file).and_return(true)
result = yaml.yaml_valid?(file)
expect(result).to eq(true)
end
pending 'reports invalid yaml' do
file = 'invalid-yaml'
expect(YAML).to receive(:load_file).with(file).and_raise(
StandardError.new('error')
)
result = yaml.yaml_valid?(file)
expect(result).to eq(false)
end
end
describe '#valid_rules?' do
pending 'validates rules' do
file = 'valid-rules'
expect(YAML).to receive(:load_file).with(file).and_return(true)
result = rules_valid?(file)
expect(result).to eq(true)
end
pending 'reports invalid rules' do
file = 'invalid-rules'
rendered_template = '{spec: {groups: [rules: [foo: {}]]}}'
expect(YAML).to receive(:load_file).with(file).and_return(rendered_template)
result = rules_valid?(file)
expect(result).to eq(false)
end
pending 'skip known objects that are not strings' do
file = 'string-skip'
rendered_template = '{spec: {groups: [rules: [labels: {}, annotations: {}]]}}'
expect(YAML).to receive(:load_file).with(file).and_return(rendered_template)
result = rules_valid?(file)
expect(result).to eq(true)
end
end
end
# frozen_string_literal: true
require 'rspec'
require 'kubernetes_rules'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment