Skip to content

Commit

Permalink
add github actions tests on k3d + release setup
Browse files Browse the repository at this point in the history
Signed-off-by: clux <[email protected]>
  • Loading branch information
clux committed Nov 27, 2021
1 parent 0cabcad commit 23256d4
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 4 deletions.
12 changes: 12 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# These are supported funding model platforms

github: [clux]
#patreon: # Replace with a single Patreon username
#open_collective: # Replace with a single Open Collective username
#ko_fi: # Replace with a single Ko-fi username
#tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
#community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
#liberapay: # Replace with a single Liberapay username
#issuehunt: # Replace with a single IssueHunt username
#otechie: # Replace with a single Otechie username
#custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
46 changes: 46 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: CI
on:
pull_request:

jobs:
cargo-test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
override: true
toolchain: stable
profile: minimal
- uses: Swatinem/rust-cache@v1
- uses: nolar/setup-k3d-k3s@v1
with:
version: v1.21
k3d-name: kube
github-token: ${{ secrets.GITHUB_TOKEN }}
- run: kubectl apply --force-conflicts --server-side -f https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/v0.52.0/example/prometheus-operator-crd/monitoring.coreos.com_prometheusrules.yaml
- run: cargo build --all
- run: cargo run --bin kopium -- prometheusrules.monitoring.coreos.com > gen.rs
- run: echo "pub type CR = PrometheusRule;" >> gen.rs
- run: kubectl apply -f test.yaml
- run: cargo run --bin tester -- PrometheusRule


rustfmt:
name: Run rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/[email protected]
with:
profile: minimal
toolchain: nightly
components: rustfmt
override: true
- name: Run rustfmt
id: rustfmt
run: rustfmt +nightly --edition 2018 --check $(find . -type f -iname *.rs)
102 changes: 102 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
name: Release

on:
pull_request:
#push:
# tags:
# - 'v[0-9]+.[0-9]+.[0-9]+'

jobs:
build:
strategy:
fail-fast: false
matrix:
target:
- x86_64-unknown-linux-musl
- x86_64-apple-darwin
include:
- target: x86_64-unknown-linux-musl
os: ubuntu-latest
- target: x86_64-apple-darwin
os: macos-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Build Linux
if: matrix.os == 'ubuntu-latest'
run: |
docker run --rm -t \
-v $HOME/.cargo/registry/:/root/.cargo/registry \
-v "$(pwd)":/volume \
clux/muslrust:stable \
cargo build --release --target ${{ matrix.target }}
- name: Prepare macOS
if: matrix.os == 'macos-latest'
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
target: ${{ matrix.target }}
override: true
- name: Build macOS
if: matrix.os == 'macos-latest'
uses: actions-rs/cargo@v1
with:
toolchain: stable
command: build
args: --release --target ${{ matrix.target }}
- name: Upload
uses: actions/upload-artifact@v2
with:
name: kopium-${{ matrix.os }}-amd64
path: target/${{ matrix.target }}/release/kopium
if-no-files-found: error

release:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Download
uses: actions/download-artifact@v2
- name: Layout
run: |
mv kopium-ubuntu-latest-amd64/kopium ./kopium-linux-amd64
mv kopium-macos-latest-amd64/kopium ./kopium-darwin-amd64
rm -rf kopium-ubuntu-latest-amd64 kopium-macos-latest-amd64
- name: Checksum
run: make SHA256SUMS
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
- name: Release SHA256SUMS
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: SHA256SUMS
asset_name: SHA256SUMS
asset_content_type: text/plain
- name: Release kopium-linux-amd64
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_name: kopium-linux-amd64
asset_path: kopium-linux-amd64
asset_content_type: application/octet-stream
- name: Release kopium-macos-amd64
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: kopium-darwin-amd64
asset_name: kopium-darwin-amd64
asset_content_type: application/octet-stream
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
gen.rs
56 changes: 56 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ repository = "https://github.com/kube-rs/kopium"
keywords = ["kubernetes", "openapi"]
categories = ["command-line-utilities"]

[[bin]]
doc = false
name = "kopium"
path = "src/main.rs"

[[bin]]
doc = false
name = "tester"
path = "test.rs"

[dependencies]
kube = "0.64.0"
kube = { version = "0.64.0", features = ["derive"] }
k8s-openapi = { version = "0.13.1", features = ["v1_22"] }
tokio = { version = "1.14.0", features = ["full"] }
anyhow = "1.0.48"
Expand All @@ -22,3 +32,4 @@ env_logger = "0.9.0"
serde_json = "1.0.72"
clap = "3.0.0-beta.5"
quote = "1.0.10"
serde = { version = "1.0.130", features = ["derive"] }
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ cargo install kopium

```sh
kopium prometheusrules.monitoring.coreos.com > prometheusrule.rs
rustfmt +nightly --edition 2021 prometheusrule.rs
```

## Output
Expand All @@ -27,8 +28,13 @@ use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

#[derive(CustomResource, Serialize, Deserialize, Clone, Debug)]
#[kube(group = "monitoring.coreos.com", version = "v1", kind = "PrometheusRule", plural = "prometheusrules")]
#[kube(Namespaced)]
#[kube(
group = "monitoring.coreos.com",
version = "v1",
kind = "PrometheusRule",
plural = "prometheusrules"
)]
#[kube(namespaced)]
#[kube(schema = "disabled")]
pub struct PrometheusRuleSpec {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
Expand All @@ -52,4 +58,5 @@ pub struct PrometheusRuleRules {
pub labels: BTreeMap<String, String>,
pub record: Option<String>,
}

```
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async fn main() -> Result<()> {
group, version, kind, plural
);
if scope == "Namespaced" {
println!(r#"#[kube(Namespaced)]"#);
println!(r#"#[kube(namespaced)]"#);
}
// don't support grabbing original schema atm so disable schemas:
// (we coerce IntToString to String anyway so it wont match anyway)
Expand Down
27 changes: 27 additions & 0 deletions test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use anyhow::Result;
use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::{
CustomResourceDefinition
};
use kube::{Api, Client, Resource, ResourceExt};

include!("./gen.rs"); // import generated test structs in scope

#[tokio::main]
async fn main() -> Result<()> {
let client = Client::try_default().await?;

let api: Api<CustomResourceDefinition> = Api::all(client.clone());
let cr: Api<CR> = Api::default_namespaced(client);

println!("crd gvk {}-{}-{}", CR::group(&()), CR::version(&()), CR::kind(&()));
let canonical = api.get(&format!("{}.{}", CR::plural(&()), CR::group(&()))).await?;
assert_eq!(canonical.spec.names.kind, CR::kind(&()).to_string());
assert_eq!(canonical.spec.names.plural, CR::plural(&()).to_string());
assert_eq!(canonical.spec.group, CR::group(&()).to_string());

// assumes a resource of type CR has been applied with name 'gen' in the namespace
let crd = cr.get("gen").await?;
assert_eq!(crd.name(), "gen");

Ok(())
}
15 changes: 15 additions & 0 deletions test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: gen
spec:
groups:
- name: ./pr.rules
rules:
- alert: PRFakeAlert
labels:
severity: info
expr: "0"
for: 10m
annotations:
summary: "Parsing this"

0 comments on commit 23256d4

Please sign in to comment.