...
 
Commits (3)
......@@ -41,17 +41,16 @@ test-rules:
rubocop:
stage: test
script:
- gem install bundler --no-document
- gem install bundler etc json --no-document
- bundle
- bundle exec rubocop ./bin/create_kubernetes_rules.rb ./spec/*
- rubocop ./bin/create_kubernetes_rules.rb ./lib/* ./spec/*
rspec:
stage: test
script:
- gem install bundler --no-document
- bundle
- cd spec
- rspec create_kubernetes_rules_spec.rb
- bundle exec rspec
deploy-rules-gstg:
environment: gstg
......
......@@ -4,10 +4,10 @@ Metrics/AbcSize:
Max: 16
Metrics/BlockLength:
Max: 64
Metrics/LineLength:
Max: 90
Max: 41
Metrics/MethodLength:
Max: 21
Metrics/LineLength:
Max: 100
......@@ -5,6 +5,7 @@ source 'https://rubygems.org'
gem 'colorize'
group :development, :test do
gem 'pry'
gem 'rspec'
gem 'rubocop'
end
......@@ -2,12 +2,17 @@ GEM
remote: https://rubygems.org/
specs:
ast (2.4.0)
coderay (1.1.2)
colorize (0.8.1)
diff-lcs (1.3)
jaro_winkler (1.5.3)
method_source (0.9.2)
parallel (1.17.0)
parser (2.6.3.0)
ast (~> 2.4.0)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
rainbow (3.0.0)
rspec (3.8.0)
rspec-core (~> 3.8.0)
......@@ -37,6 +42,7 @@ PLATFORMS
DEPENDENCIES
colorize
pry
rspec
rubocop
......
......@@ -21,5 +21,5 @@ OptionParser.new do |opts|
end
end.parse!
KubernetesRules::Create.new if options[:create]
KubernetesRules::Validate.new if options[:validate]
KubernetesRules::Create.new.create! if options[:create]
KubernetesRules::Validate.new.validate! if options[:validate]
......@@ -5,15 +5,18 @@ 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")
@input_dir = './rules'
@output_dir = './rules-k8s'
end
def 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)}"
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)
......@@ -22,20 +25,6 @@ module KubernetesRules
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
......@@ -47,6 +36,18 @@ spec:
end
def render_for_k8s(template_vars)
k8s_template = <<~ENDOFEXPECT
---
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: <%= rule_name %>
labels:
app: prometheus-operator
release: gitlab-monitoring
spec:
<%= template %>
ENDOFEXPECT
render_k8s = ERB.new(k8s_template)
render_k8s.result(template_vars.instance_eval { binding })
end
......
......@@ -16,10 +16,7 @@ module KubernetesRules
exit 1
end
unless yaml_files_valid?(files)
puts "\nOne or more files failed validation!".red
exit 1
end
abort "\nOne or more files failed validation!".red unless yaml_files_valid?(files)
end
def yaml_valid?(file)
......
......@@ -6,7 +6,7 @@ describe KubernetesRules::Create do
describe '#generate_rule_name' do
let(:rule) { described_class.new }
pending 'sets the name of the rule properly' do
it 'sets the name of the rule properly' do
rf = rule.generate_rule_name('foobar.yml')
expect(rf).to eq('foobar')
......@@ -20,7 +20,7 @@ describe KubernetesRules::Create do
expect(ru).to eq('foo-bar')
end
pending 'creates our desired demplate' do
it 'creates our desired demplate' do
vars = OpenStruct.new(
rule_name: 'foobar',
template: <<~ENDOFRULESPEC
......@@ -43,6 +43,7 @@ describe KubernetesRules::Create do
spec:
global:
- rule: expression
ENDOFEXPECT
expect(result).to eq(expected_result)
......
......@@ -6,7 +6,7 @@ describe KubernetesRules::Validate do
describe '#yaml_valid?' do
let(:yaml) { described_class.new }
pending 'validates yaml' do
it 'validates yaml' do
file = 'valid-yaml'
expect(YAML).to receive(:load_file).with(file).and_return(true)
result = yaml.yaml_valid?(file)
......@@ -14,7 +14,7 @@ describe KubernetesRules::Validate do
expect(result).to eq(true)
end
pending 'reports invalid yaml' do
it 'reports invalid yaml' do
file = 'invalid-yaml'
expect(YAML).to receive(:load_file).with(file).and_raise(
StandardError.new('error')
......@@ -27,28 +27,31 @@ describe KubernetesRules::Validate do
end
describe '#valid_rules?' do
pending 'validates rules' do
let(:yaml) { described_class.new }
it 'validates rules' do
file = 'valid-rules'
expect(YAML).to receive(:load_file).with(file).and_return(true)
result = rules_valid?(file)
rendered_template = YAML.safe_load('{spec: {groups: [rules: [foo: "hi"]]}}')
expect(YAML).to receive(:load_file).with(file).and_return(rendered_template)
result = yaml.rules_valid?(file)
expect(result).to eq(true)
end
pending 'reports invalid rules' do
it 'reports invalid rules' do
file = 'invalid-rules'
rendered_template = '{spec: {groups: [rules: [foo: {}]]}}'
rendered_template = YAML.safe_load('{spec: {groups: [rules: [foo: {}]]}}')
expect(YAML).to receive(:load_file).with(file).and_return(rendered_template)
result = rules_valid?(file)
result = yaml.rules_valid?(file)
expect(result).to eq(false)
end
pending 'skip known objects that are not strings' do
it 'skip known objects that are not strings' do
file = 'string-skip'
rendered_template = '{spec: {groups: [rules: [labels: {}, annotations: {}]]}}'
rendered_template = YAML.safe_load('{spec: {groups: [rules: [labels: {}, annotations: {}]]}}')
expect(YAML).to receive(:load_file).with(file).and_return(rendered_template)
result = rules_valid?(file)
result = yaml.rules_valid?(file)
expect(result).to eq(true)
end
......