Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
**/.bundle/
# Ignore bundler config.
/.bundle

# Ignore all environment files (except templates).
/.env*
!/.env*.erb

# Ignore editor config.
/.idea
/.vscode
/.zed

# Ignore all log files and temp files.
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/

# Ignore all cache files.
/Gemfile.lock
**/*.gemfile.lock

# rspec failure tracking
# Ignore rspec status files.
.rspec_status
7 changes: 4 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ AllCops:
Exclude:
- "gemfiles/*"
SuggestExtensions: false
TargetRubyVersion: 3.2
TargetRubyVersion: 3.4
NewCops: enable

Documentation:
Exclude:
Expand All @@ -14,8 +15,8 @@ Style/StringLiterals:
Layout/LineLength:
Max: 120
Layout/HashAlignment:
EnforcedHashRocketStyle: table
EnforcedColonStyle: table
EnforcedHashRocketStyle: key
EnforcedColonStyle: key
Layout/SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space
Style/RaiseArgs:
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.2
3.4.2
2 changes: 1 addition & 1 deletion Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ appraise "kubeclient-3" do
end

appraise "kubeclient-4" do
gem "kubeclient", "4.0.0"
gem "kubeclient", "4.12.0"
end
13 changes: 13 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,16 @@ source "https://rubygems.org"
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

gemspec

group :development, :test do
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some reason you moved these out of the gemspec? I seem to recall that keeping dev dependencies in the gemspec is more standard / better practice

gem "appraisal"
gem "bundler", "~> 2.6"
gem "bundler-audit", "~> 0"
gem "mocha", "~> 2.7"
gem "rake", "~> 13.2"
gem "rspec", "~> 3.13"
gem "rubocop", "~> 1.75"

# For connecting to a GKE cluster in development/test
gem "googleauth"
end
14 changes: 2 additions & 12 deletions cron-kubernetes.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
spec.description = "Configure and deploy Kubernetes CronJobs from ruby with a single schedule."
spec.homepage = "https://github.com/keylimetoolbox/cron-kubernetes"
spec.license = "MIT"
spec.required_ruby_version = ">= 3.2"
spec.required_ruby_version = "~> 3.4"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless it's absolutely required to accomplish the goals in this PR, I'd recommend separating out ruby version changes


spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
Expand All @@ -25,15 +25,5 @@ Gem::Specification.new do |spec|
spec.executables << "cron_kubernetes"

spec.add_dependency "kubeclient", ">= 3.1.2", "< 5.0"

spec.add_development_dependency "appraisal"
spec.add_development_dependency "bundler", "~> 2.4"
spec.add_development_dependency "bundler-audit", "~> 0"
spec.add_development_dependency "mocha", "~> 1.3"
spec.add_development_dependency "rake", "~> 13.1"
spec.add_development_dependency "rspec", "~> 3.12"
spec.add_development_dependency "rubocop", "~> 1.57"

# For connecting to a GKE cluster in development/test
spec.add_development_dependency "googleauth"
spec.metadata["rubygems_mfa_required"] = "true"
end
2 changes: 1 addition & 1 deletion gemfiles/kubeclient_4.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

source "https://rubygems.org"

gem "kubeclient", "4.0.0"
gem "kubeclient", "4.12.0"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


gemspec path: "../"
4 changes: 2 additions & 2 deletions lib/cron_kubernetes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ module CronKubernetes
define_setting :kubeclient, nil

class << self
def schedule(&block)
CronKubernetes::Scheduler.instance.instance_eval(&block)
def schedule(&)
CronKubernetes::Scheduler.instance.instance_eval(&)
end
end
end
4 changes: 2 additions & 2 deletions lib/cron_kubernetes/configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ def define_setting(name, default = nil)

private

def define_class_method(name, &block)
def define_class_method(name, &)
(class << self; self; end).instance_eval do
define_method(name, &block)
define_method(name, &)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/cron_kubernetes/context/kubectl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ def context
config.context.api_version,
config.context.namespace,
auth_options: auth_options(config),
ssl_options: config.context.ssl_options
ssl_options: config.context.ssl_options
)
end

private

def kubeconfig
File.join(ENV["HOME"], ".kube", "config")
File.join(Dir.home, ".kube", "config")
end

def auth_options(config)
Expand All @@ -34,7 +34,7 @@ def auth_options(config)
end

def google_application_default_credentials
return unless defined?(Google) && defined?(Google::Auth)
return unless defined?(Google::Auth)

{bearer_token: Kubeclient::GoogleApplicationDefaultCredentials.token}
end
Expand Down
48 changes: 27 additions & 21 deletions lib/cron_kubernetes/cron_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,29 @@
module CronKubernetes
# A single job to run on a given schedule.
class CronJob
attr_accessor :schedule, :command, :job_manifest, :name, :identifier

def initialize(schedule: nil, command: nil, job_manifest: nil, name: nil, identifier: nil)
@schedule = schedule
@command = command
@job_manifest = job_manifest
@name = name
@identifier = identifier
attr_accessor :schedule, :command, :job_manifest, :name, :identifier, :cron_job_settings

def initialize(options = {})
@schedule = options[:schedule]
@command = options[:command]
@job_manifest = options[:job_manifest]
@name = options[:name]
@identifier = options[:identifier]
@cron_job_settings = options[:cron_job_settings] || {}
end

# rubocop:disable Metrics/MethodLength
def cron_job_manifest
{
"apiVersion" => "batch/v1",
"kind" => "CronJob",
"metadata" => {
"name" => "#{identifier}-#{cron_job_name}",
"kind" => "CronJob",
"metadata" => {
"name" => "#{identifier}-#{cron_job_name}",
"namespace" => namespace,
"labels" => {"cron-kubernetes-identifier" => identifier}
"labels" => {"cron-kubernetes-identifier" => identifier}
},
"spec" => {
"schedule" => schedule,
"jobTemplate" => {
"metadata" => job_metadata,
"spec" => job_spec
}
}
"spec" => build_cron_job_spec
}
end
# rubocop:enable Metrics/MethodLength

private

Expand All @@ -44,6 +37,19 @@ def namespace
"default"
end

def build_cron_job_spec
default_spec = {
"schedule" => schedule,
"concurrencyPolicy" => "Forbid",
"jobTemplate" => {
"metadata" => job_metadata,
"spec" => job_spec
}
}

default_spec.merge(cron_job_settings)
end

def job_spec
spec = job_manifest["spec"].dup
first_container = spec["template"]["spec"]["containers"][0]
Expand Down
4 changes: 2 additions & 2 deletions lib/cron_kubernetes/cron_tab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ def update_cron_job(job)
end

def index_cron_jobs(jobs)
jobs.map { |job| ["#{job.identifier}-#{job.name}", job] }.to_h
jobs.to_h { |job| ["#{job.identifier}-#{job.name}", job] }
end

def index_kubernetes_cron_jobs(jobs)
jobs.map { |job| [job.metadata.name, job] }.to_h
jobs.to_h { |job| [job.metadata.name, job] }
end
end
end
21 changes: 11 additions & 10 deletions lib/cron_kubernetes/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ def initialize
@identifier = CronKubernetes.identifier
end

def rake(task, schedule:, name: nil)
def rake(task, schedule:, name: nil, cron_job_settings: {})
rake_command = "bundle exec rake #{task} --silent"
rake_command = "RAILS_ENV=#{rails_env} #{rake_command}" if rails_env
@schedule << new_cron_job(schedule, rake_command, name)
@schedule << new_cron_job(schedule, rake_command, name, cron_job_settings)
end

def runner(ruby_command, schedule:, name: nil)
def runner(ruby_command, schedule:, name: nil, cron_job_settings: {})
env = nil
env = "-e #{rails_env} " if rails_env
runner_command = "bin/rails runner #{env}'#{ruby_command}'"
@schedule << new_cron_job(schedule, runner_command, name)
@schedule << new_cron_job(schedule, runner_command, name, cron_job_settings)
end

def command(command, schedule:, name: nil)
@schedule << new_cron_job(schedule, command, name)
def command(command, schedule:, name: nil, cron_job_settings: {})
@schedule << new_cron_job(schedule, command, name, cron_job_settings)
end

private
Expand All @@ -42,18 +42,19 @@ def make_command(command)
end
end

def new_cron_job(schedule, command, name)
def new_cron_job(schedule, command, name, cron_job_settings)
CronJob.new(
command: make_command(command),
schedule:,
command: make_command(command),
job_manifest: CronKubernetes.manifest,
cron_job_settings:,
name:,
identifier: @identifier
identifier: @identifier
)
end

def rails_env
ENV["RAILS_ENV"]
ENV.fetch("RAILS_ENV", nil)
end

def root
Expand Down
23 changes: 12 additions & 11 deletions spec/cron_kubernetes/cron_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@

it "accepts schedule, command, job_manifest, name parameters" do
job = CronKubernetes::CronJob.new(
schedule: "30 0 * * *",
command: "/bin/bash -l -c ls\\ -l",
schedule: "30 0 * * *",
command: "/bin/bash -l -c ls\\ -l",
job_manifest: manifest,
name: "cron-job",
identifier: "my-app"
name: "cron-job",
identifier: "my-app"
)
expect(job.schedule).to eq "30 0 * * *"
expect(job.command).to eq "/bin/bash -l -c ls\\ -l"
Expand Down Expand Up @@ -68,11 +68,11 @@
context "#cron_job_manifest" do
subject do
CronKubernetes::CronJob.new(
schedule: "*/1 * * * *",
command: ["/bin/bash", "-l", "-c", "echo Hello from the Kubernetes cluster"],
schedule: "*/1 * * * *",
command: ["/bin/bash", "-l", "-c", "echo Hello from the Kubernetes cluster"],
job_manifest: manifest,
name: "hello",
identifier: "my-app"
name: "hello",
identifier: "my-app"
)
end

Expand All @@ -88,6 +88,7 @@
cron-kubernetes-identifier: my-app
spec:
schedule: "*/1 * * * *"
concurrencyPolicy: Forbid
jobTemplate:
metadata:
spec:
Expand All @@ -108,10 +109,10 @@
context "when no name is provided" do
subject do
CronKubernetes::CronJob.new(
schedule: "*/1 * * * *",
command: ["/bin/bash", "-l", "-c", "echo Hello from the Kubernetes cluster"],
schedule: "*/1 * * * *",
command: ["/bin/bash", "-l", "-c", "echo Hello from the Kubernetes cluster"],
job_manifest: manifest,
identifier: "my-app"
identifier: "my-app"
)
end

Expand Down
24 changes: 12 additions & 12 deletions spec/cron_kubernetes/cron_tab_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@
let(:cron_job_manifest) do
{
apiVersion: "batch/v1",
kind: "CronJob",
metadata: {
name: "spec-minutely",
kind: "CronJob",
metadata: {
name: "spec-minutely",
namespace: "default",
labels: {"cron-kubernetes-identifier": "spec"}
labels: {"cron-kubernetes-identifier": "spec"}
},
spec: {
schedule: "*/1 * * * *",
spec: {
schedule: "*/1 * * * *",
jobTemplate: {
metadata: nil,
spec: {
spec: {
template: {
spec: {
containers: [{image: "ubuntu", command: "ls -l"}],
containers: [{image: "ubuntu", command: "ls -l"}],
restartPolicy: "OnFailure"
}
}
Expand All @@ -43,11 +43,11 @@
end
let(:job) do
CronKubernetes::CronJob.new(
schedule: "*/1 * * * *",
command: "ls -l",
schedule: "*/1 * * * *",
command: "ls -l",
job_manifest:,
name: "minutely",
identifier: "spec"
name: "minutely",
identifier: "spec"
)
end
let(:cron_job) do
Expand Down
Loading