Skip to content

Commit c2e75ef

Browse files
authored
Merge pull request #1 from kuberhealthy/codex/add-github-actions-for-validation-and-publishing
Add Ruby client gem and CI workflows for Kuberhealthy checks
2 parents 838c00d + 8515c96 commit c2e75ef

File tree

12 files changed

+166
-46
lines changed

12 files changed

+166
-46
lines changed

.github/workflows/build.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: Build example container
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
- uses: docker/setup-buildx-action@v3
13+
- name: Build example
14+
run: docker build -t test-image .

.github/workflows/publish.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Publish gem
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
publish:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: read
12+
packages: write
13+
steps:
14+
- uses: actions/checkout@v4
15+
- uses: ruby/setup-ruby@v1
16+
with:
17+
ruby-version: '3.1'
18+
bundler-cache: true
19+
- name: Build gem
20+
run: gem build kuberhealthy-client.gemspec
21+
- name: Publish gem
22+
env:
23+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
24+
run: |
25+
mkdir -p ~/.gem
26+
printf -- '---\n:github: Bearer ${GITHUB_TOKEN}\n' > ~/.gem/credentials
27+
chmod 0600 ~/.gem/credentials
28+
gem push --key github --host https://rubygems.pkg.github.com/${GITHUB_REPOSITORY_OWNER} kuberhealthy-client-*.gem

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.gem
2+
.bundle/

Dockerfile

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
FROM ruby:3.2-alpine
22
WORKDIR /app
3-
COPY exampleCheck.rb /app/exampleCheck.rb
3+
4+
# Build and install the kuberhealthy-client gem
5+
COPY kuberhealthy-client.gemspec .
6+
COPY lib ./lib
7+
RUN gem build kuberhealthy-client.gemspec \
8+
&& gem install kuberhealthy-client-*.gem \
9+
&& rm kuberhealthy-client-*.gem
10+
11+
# Copy the example check script
12+
COPY exampleCheck.rb ./exampleCheck.rb
13+
414
CMD ["ruby", "/app/exampleCheck.rb"]

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
3+
gemspec

Gemfile.lock

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
PATH
2+
remote: .
3+
specs:
4+
kuberhealthy-client (0.1.0)
5+
6+
GEM
7+
remote: https://rubygems.org/
8+
specs:
9+
10+
PLATFORMS
11+
ruby
12+
x86_64-linux
13+
14+
DEPENDENCIES
15+
kuberhealthy-client!
16+
17+
BUNDLED WITH
18+
2.6.7

README.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
# Ruby Client Example
22

3-
This directory contains a minimal Ruby client for creating Kuberhealthy external checks.
4-
The script reads the `KH_REPORTING_URL` and `KH_RUN_UUID` environment variables that
5-
Kuberhealthy sets on checker pods and uses them to report status back to the
6-
Kuberhealthy service.
3+
This repository contains a minimal Ruby client for creating Kuberhealthy external checks. The reusable client library is packaged as the `kuberhealthy-client` gem.
4+
5+
## Installing the client library
6+
7+
Add the gem to your application. When using Bundler, reference GitHub Packages:
8+
9+
```ruby
10+
source 'https://rubygems.pkg.github.com/OWNER' do
11+
gem 'kuberhealthy-client'
12+
end
13+
```
14+
15+
Replace `OWNER` with the GitHub organization or user that hosts this repository.
16+
17+
In your check script:
18+
19+
```ruby
20+
require 'kuberhealthy/client'
21+
22+
# perform check logic...
23+
Kuberhealthy::Client.report_success
24+
```
725

826
## Using this example
927

exampleCheck.rb

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,10 @@
11
#!/usr/bin/env ruby
22

3-
require 'json'
4-
require 'net/http'
5-
require 'uri'
6-
7-
# reporting_url fetches the Kuberhealthy reporting endpoint from the environment.
8-
def reporting_url
9-
url = ENV['KH_REPORTING_URL']
10-
return url if url && !url.empty?
11-
raise 'KH_REPORTING_URL not set'
12-
end
13-
14-
# run_uuid fetches the unique run identifier from the environment.
15-
def run_uuid
16-
uuid = ENV['KH_RUN_UUID']
17-
return uuid if uuid && !uuid.empty?
18-
raise 'KH_RUN_UUID not set'
19-
end
20-
21-
# post_status sends a status report back to Kuberhealthy.
22-
def post_status(ok:, errors: [])
23-
uri = URI(reporting_url)
24-
req = Net::HTTP::Post.new(uri)
25-
req['Content-Type'] = 'application/json'
26-
req['kh-run-uuid'] = run_uuid
27-
req.body = JSON.generate({ ok: ok, errors: errors })
28-
Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
29-
http.request(req)
30-
end
31-
end
32-
33-
# report_success tells Kuberhealthy the check passed.
34-
def report_success
35-
post_status(ok: true, errors: [])
36-
end
37-
38-
# report_failure tells Kuberhealthy the check failed with messages.
39-
def report_failure(messages)
40-
post_status(ok: false, errors: messages)
41-
end
3+
require 'kuberhealthy/client'
424

435
begin
446
# TODO: add check logic here. For now, always succeed.
45-
report_success
7+
Kuberhealthy::Client.report_success
468
rescue StandardError => e
47-
report_failure([e.message])
9+
Kuberhealthy::Client.report_failure([e.message])
4810
end

kuberhealthy-client.gemspec

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require_relative 'lib/kuberhealthy/version'
2+
3+
Gem::Specification.new do |spec|
4+
spec.name = 'kuberhealthy-client'
5+
spec.version = Kuberhealthy::VERSION
6+
spec.authors = ['Kuberhealthy Maintainers']
7+
spec.summary = 'Ruby client for reporting Kuberhealthy check results'
8+
spec.description = 'A simple helper library for sending check results back to Kuberhealthy.'
9+
spec.homepage = 'https://github.com/kuberhealthy/ruby'
10+
spec.required_ruby_version = '>= 2.5'
11+
12+
spec.files = Dir['lib/**/*.rb']
13+
spec.require_paths = ['lib']
14+
15+
spec.metadata['allowed_push_host'] = "https://rubygems.pkg.github.com/#{ENV['GITHUB_REPOSITORY_OWNER']}"
16+
end

lib/kuberhealthy-client.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require_relative 'kuberhealthy/client'

0 commit comments

Comments
 (0)