Skip to content

Commit

Permalink
genpolicy: add --version flag
Browse files Browse the repository at this point in the history
- Add --version flag to the genpolicy tool that prints the current
version
- Add version.rs.in template to store the version information
- Update build and release makefile recipes
- Add license to Cargo.toml

Signed-off-by: Saul Paredes <[email protected]>
  • Loading branch information
Redent0r committed Apr 16, 2024
1 parent 9b7dcc0 commit b3901b1
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/tools/genpolicy/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
layers_cache
src/version.rs
2 changes: 1 addition & 1 deletion src/tools/genpolicy/Cargo.lock

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

3 changes: 2 additions & 1 deletion src/tools/genpolicy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

[package]
name = "genpolicy"
version = "0.1.0"
version = "3.2.0-azl0.genpolicy1"
authors = ["The Kata Containers community <[email protected]>"]
edition = "2021"
license = "Apache-2.0"

[dependencies]
# Logging.
Expand Down
26 changes: 22 additions & 4 deletions src/tools/genpolicy/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,45 @@ ifeq ($(ARCH), ppc64le)
override ARCH = powerpc64le
endif

COMMIT_HASH := $(shell git rev-parse HEAD 2>/dev/null || true)
# appends '-dirty' to the commit hash if there are uncommitted changes
COMMIT_INFO := $(if $(shell git status --porcelain --untracked-files=no 2>/dev/null || true),${COMMIT_HASH}-dirty,${COMMIT_HASH})

GENERATED_CODE = src/version.rs

GENERATED_REPLACEMENTS= COMMIT_INFO
GENERATED_FILES :=

GENERATED_FILES += $(GENERATED_CODE)

$(GENERATED_FILES): %: %.in
sed $(foreach r,$(GENERATED_REPLACEMENTS),-e 's|@$r@|$($r)|g') "$<" > "$@"

.DEFAULT_GOAL := default
default: build

build:
@RUSTFLAGS="$(EXTRA_RUSTFLAGS) --deny warnings" cargo build --target $(TRIPLE) --$(BUILD_TYPE)
build: $(GENERATED_FILES)
cargo build

release: $(GENERATED_FILES)
RUSTFLAGS="$(EXTRA_RUSTFLAGS) --deny warnings" cargo build --release

static-checks-build:
@echo "INFO: static-checks-build do nothing.."

clean:
cargo clean
rm -f $(GENERATED_FILES)

vendor:
cargo vendor

test:

install:
install: $(GENERATED_FILES)
@RUSTFLAGS="$(EXTRA_RUSTFLAGS) --deny warnings" cargo install --locked --target $(TRIPLE) --path .

check: standard_rust_check
check: $(GENERATED_CODE) standard_rust_check

.PHONY: \
build \
Expand Down
11 changes: 11 additions & 0 deletions src/tools/genpolicy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod settings;
mod stateful_set;
mod utils;
mod verity;
mod version;
mod volume;
mod yaml;

Expand All @@ -37,6 +38,16 @@ async fn main() {
env_logger::init();
let config = utils::Config::new();

if config.version {
println!(
"Kata Containers policy tool (Rust): id: {}, version: {}, commit: {}",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
version::COMMIT_INFO
);
return;
}

debug!("Creating policy from yaml, settings, and rules.rego files...");
let mut policy = policy::AgentPolicy::from_files(&config).await.unwrap();

Expand Down
5 changes: 5 additions & 0 deletions src/tools/genpolicy/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ struct CommandLineOptions {
require_equals= true
)]
containerd_socket_path: Option<String>,

#[clap(short, long, help = "Print version information and exit")]
version: bool,
}

/// Application configuration, derived from on command line parameters.
Expand All @@ -91,6 +94,7 @@ pub struct Config {
pub raw_out: bool,
pub base64_out: bool,
pub containerd_socket_path: Option<String>,
pub version: bool,
}

impl Config {
Expand Down Expand Up @@ -118,6 +122,7 @@ impl Config {
raw_out: args.raw_out,
base64_out: args.base64_out,
containerd_socket_path: args.containerd_socket_path,
version: args.version,
}
}
}
11 changes: 11 additions & 0 deletions src/tools/genpolicy/src/version.rs.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) 2020 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//

//
// WARNING: This file is auto-generated - DO NOT EDIT!
//

#![allow(dead_code)]
pub const COMMIT_INFO: &str = "@COMMIT_INFO@";

0 comments on commit b3901b1

Please sign in to comment.