-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3984 from mgsharm/port-ecs-settings-extension
Port ecs settings extension
- Loading branch information
Showing
13 changed files
with
306 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "settings-ecs" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
build = "../build.rs" | ||
|
||
[lib] | ||
path = "../packages.rs" | ||
|
||
[package.metadata.build-package] | ||
source-groups = [ | ||
"settings-extensions/ecs" | ||
] | ||
|
||
# RPM BuildRequires | ||
[build-dependencies] | ||
glibc = { path = "../glibc" } | ||
|
||
# RPM Requires | ||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
%global _cross_first_party 1 | ||
%undefine _debugsource_packages | ||
|
||
%global extension_name ecs | ||
|
||
Name: %{_cross_os}settings-%{extension_name} | ||
Version: 0.0 | ||
Release: 0%{?dist} | ||
Summary: settings-%{extension_name} | ||
License: Apache-2.0 OR MIT | ||
URL: https://github.com/bottlerocket-os/bottlerocket | ||
|
||
BuildRequires: %{_cross_os}glibc-devel | ||
|
||
%description | ||
%{summary}. | ||
|
||
%prep | ||
%setup -T -c | ||
%cargo_prep | ||
|
||
%build | ||
%cargo_build --manifest-path %{_builddir}/sources/Cargo.toml \ | ||
-p settings-extension-%{extension_name} | ||
|
||
%install | ||
install -d %{buildroot}%{_cross_libexecdir} | ||
install -p -m 0755 \ | ||
${HOME}/.cache/%{__cargo_target}/release/settings-extension-%{extension_name} \ | ||
%{buildroot}%{_cross_libexecdir} | ||
|
||
install -d %{buildroot}%{_cross_libexecdir}/settings | ||
ln -sf \ | ||
../settings-extension-%{extension_name} \ | ||
%{buildroot}%{_cross_libexecdir}/settings/%{extension_name} | ||
|
||
%files | ||
%{_cross_libexecdir}/settings-extension-%{extension_name} | ||
%{_cross_libexecdir}/settings/%{extension_name} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "settings-extension-ecs" | ||
version = "0.1.0" | ||
authors = ["Gaurav Sharma <[email protected]>"] | ||
license = "Apache-2.0 OR MIT" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
env_logger = "0.10" | ||
modeled-types = { path = "../../models/modeled-types", version = "0.1" } | ||
model-derive = { path = "../../models/model-derive", version = "0.1" } | ||
serde = { version = "1", features = ["derive"] } | ||
serde_json = "1" | ||
|
||
[dependencies.bottlerocket-settings-sdk] | ||
git = "https://github.com/bottlerocket-os/bottlerocket-settings-sdk" | ||
tag = "bottlerocket-settings-sdk-v0.1.0-alpha.2" | ||
version = "0.1.0-alpha" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[extension] | ||
supported-versions = [ | ||
"v1" | ||
] | ||
default-version = "v1" | ||
|
||
[v1] | ||
[v1.validation.cross-validates] | ||
|
||
[v1.templating] | ||
helpers = [] | ||
|
||
[v1.generation.requires] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/// Settings related to Amazon ECS | ||
use bottlerocket_settings_sdk::{GenerateResult, SettingsModel}; | ||
use model_derive::model; | ||
use modeled_types::{ | ||
ECSAgentImagePullBehavior, ECSAgentLogLevel, ECSAttributeKey, ECSAttributeValue, | ||
ECSDurationValue, SingleLineString, | ||
}; | ||
use std::{collections::HashMap, convert::Infallible}; | ||
|
||
#[model(impl_default = true)] | ||
pub struct ECSSettingsV1 { | ||
cluster: String, | ||
instance_attributes: HashMap<ECSAttributeKey, ECSAttributeValue>, | ||
allow_privileged_containers: bool, | ||
logging_drivers: Vec<SingleLineString>, | ||
loglevel: ECSAgentLogLevel, | ||
enable_spot_instance_draining: bool, | ||
image_pull_behavior: ECSAgentImagePullBehavior, | ||
container_stop_timeout: ECSDurationValue, | ||
task_cleanup_wait: ECSDurationValue, | ||
metadata_service_rps: i64, | ||
metadata_service_burst: i64, | ||
reserved_memory: u16, | ||
image_cleanup_wait: ECSDurationValue, | ||
image_cleanup_delete_per_cycle: i64, | ||
image_cleanup_enabled: bool, | ||
image_cleanup_age: ECSDurationValue, | ||
backend_host: String, | ||
awsvpc_block_imds: bool, | ||
enable_container_metadata: bool, | ||
} | ||
|
||
type Result<T> = std::result::Result<T, Infallible>; | ||
|
||
impl SettingsModel for ECSSettingsV1 { | ||
type PartialKind = Self; | ||
type ErrorKind = Infallible; | ||
|
||
fn get_version() -> &'static str { | ||
"v1" | ||
} | ||
|
||
fn set(_current_value: Option<Self>, _target: Self) -> Result<()> { | ||
// Set anything that can be parsed as ECSSettingsV1. | ||
Ok(()) | ||
} | ||
|
||
fn generate( | ||
existing_partial: Option<Self::PartialKind>, | ||
_dependent_settings: Option<serde_json::Value>, | ||
) -> Result<GenerateResult<Self::PartialKind, Self>> { | ||
Ok(GenerateResult::Complete( | ||
existing_partial.unwrap_or_default(), | ||
)) | ||
} | ||
|
||
fn validate(_value: Self, _validated_settings: Option<serde_json::Value>) -> Result<()> { | ||
// ECSSettingsV1 is validated during deserialization. | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use serde_json::json; | ||
|
||
#[test] | ||
fn test_generate_ecs_settings() { | ||
assert_eq!( | ||
ECSSettingsV1::generate(None, None), | ||
Ok(GenerateResult::Complete(ECSSettingsV1 { | ||
cluster: None, | ||
instance_attributes: None, | ||
allow_privileged_containers: None, | ||
logging_drivers: None, | ||
loglevel: None, | ||
enable_spot_instance_draining: None, | ||
image_pull_behavior: None, | ||
container_stop_timeout: None, | ||
task_cleanup_wait: None, | ||
metadata_service_rps: None, | ||
metadata_service_burst: None, | ||
reserved_memory: None, | ||
image_cleanup_wait: None, | ||
image_cleanup_delete_per_cycle: None, | ||
image_cleanup_enabled: None, | ||
image_cleanup_age: None, | ||
backend_host: None, | ||
awsvpc_block_imds: None, | ||
enable_container_metadata: None, | ||
})) | ||
) | ||
} | ||
|
||
#[test] | ||
fn test_serde_ecs() { | ||
let test_json = json!({ | ||
"cluster": "test-cluster", | ||
"instance-attributes": { | ||
"attribute1": "value1", | ||
"attribute2": "value2" | ||
}, | ||
"allow-privileged-containers": true, | ||
"logging-drivers": ["json-file", "awslogs"], | ||
"loglevel": "info", | ||
"enable-spot-instance-draining": true, | ||
"image-pull-behavior": "always", | ||
"container-stop-timeout": "30s", | ||
"task-cleanup-wait": "1h", | ||
"metadata-service-rps": 50, | ||
"metadata-service-burst": 100, | ||
"reserved-memory": 512, | ||
"image-cleanup-wait": "1h", | ||
"image-cleanup-delete-per-cycle": 2, | ||
"image-cleanup-enabled": true, | ||
"image-cleanup-age": "1h", | ||
"backend-host": "ecs.us-east-1.amazonaws.com", | ||
"awsvpc-block-imds": true, | ||
"enable-container-metadata": true, | ||
}); | ||
|
||
let test_json_str = test_json.to_string(); | ||
|
||
let ecs_settings: ECSSettingsV1 = serde_json::from_str(&test_json_str).unwrap(); | ||
|
||
let mut expected_instance_attributes: HashMap<ECSAttributeKey, ECSAttributeValue> = | ||
HashMap::new(); | ||
expected_instance_attributes.insert( | ||
ECSAttributeKey::try_from("attribute1").unwrap(), | ||
ECSAttributeValue::try_from("value1").unwrap(), | ||
); | ||
expected_instance_attributes.insert( | ||
ECSAttributeKey::try_from("attribute2").unwrap(), | ||
ECSAttributeValue::try_from("value2").unwrap(), | ||
); | ||
|
||
let expected_ecs_settings = ECSSettingsV1 { | ||
cluster: Some("test-cluster".to_string()), | ||
instance_attributes: Some(expected_instance_attributes), | ||
allow_privileged_containers: Some(true), | ||
logging_drivers: Some(vec![ | ||
SingleLineString::try_from("json-file").unwrap(), | ||
SingleLineString::try_from("awslogs").unwrap(), | ||
]), | ||
loglevel: Some(ECSAgentLogLevel::Info), | ||
enable_spot_instance_draining: Some(true), | ||
image_pull_behavior: Some(ECSAgentImagePullBehavior::Always), | ||
container_stop_timeout: Some(ECSDurationValue::try_from("30s").unwrap()), | ||
task_cleanup_wait: Some(ECSDurationValue::try_from("1h").unwrap()), | ||
metadata_service_rps: Some(50), | ||
metadata_service_burst: Some(100), | ||
reserved_memory: Some(512), | ||
image_cleanup_wait: Some(ECSDurationValue::try_from("1h").unwrap()), | ||
image_cleanup_delete_per_cycle: Some(2), | ||
image_cleanup_enabled: Some(true), | ||
image_cleanup_age: Some(ECSDurationValue::try_from("1h").unwrap()), | ||
backend_host: Some("ecs.us-east-1.amazonaws.com".to_string()), | ||
awsvpc_block_imds: Some(true), | ||
enable_container_metadata: Some(true), | ||
}; | ||
|
||
assert_eq!(ecs_settings, expected_ecs_settings); | ||
|
||
let serialized_json: serde_json::Value = serde_json::to_string(&ecs_settings) | ||
.map(|s| serde_json::from_str(&s).unwrap()) | ||
.unwrap(); | ||
|
||
assert_eq!(serialized_json, test_json); | ||
} | ||
} |
Oops, something went wrong.