mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-17 00:43:36 +00:00
Merge pull request #9294 from burgerdev/burgerdev/genpolicy-configurable-pause
genpolicy: support insecure registries and custom pause containers
This commit is contained in:
commit
c26dad8fe5
@ -272,7 +272,8 @@
|
|||||||
"confidential_guest": false
|
"confidential_guest": false
|
||||||
},
|
},
|
||||||
"cluster_config": {
|
"cluster_config": {
|
||||||
"default_namespace": "default"
|
"default_namespace": "default",
|
||||||
|
"pause_container_image": "mcr.microsoft.com/oss/kubernetes/pause:3.6"
|
||||||
},
|
},
|
||||||
"request_defaults": {
|
"request_defaults": {
|
||||||
"CreateContainerRequest": {
|
"CreateContainerRequest": {
|
||||||
|
@ -834,9 +834,7 @@ fn compress_capabilities(capabilities: &mut Vec<String>, defaults: &policy::Comm
|
|||||||
pub async fn add_pause_container(containers: &mut Vec<Container>, config: &Config) {
|
pub async fn add_pause_container(containers: &mut Vec<Container>, config: &Config) {
|
||||||
debug!("Adding pause container...");
|
debug!("Adding pause container...");
|
||||||
let mut pause_container = Container {
|
let mut pause_container = Container {
|
||||||
// TODO: load this path from the settings file.
|
image: config.settings.cluster_config.pause_container_image.clone(),
|
||||||
image: "mcr.microsoft.com/oss/kubernetes/pause:3.6".to_string(),
|
|
||||||
|
|
||||||
name: String::new(),
|
name: String::new(),
|
||||||
imagePullPolicy: None,
|
imagePullPolicy: None,
|
||||||
securityContext: Some(SecurityContext {
|
securityContext: Some(SecurityContext {
|
||||||
|
@ -13,7 +13,6 @@ use crate::pod;
|
|||||||
use crate::policy;
|
use crate::policy;
|
||||||
use crate::registry;
|
use crate::registry;
|
||||||
use crate::secret;
|
use crate::secret;
|
||||||
use crate::settings;
|
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
use crate::yaml;
|
use crate::yaml;
|
||||||
|
|
||||||
@ -47,10 +46,7 @@ pub struct AgentPolicy {
|
|||||||
/// Rego rules read from a file (rules.rego).
|
/// Rego rules read from a file (rules.rego).
|
||||||
pub rules: String,
|
pub rules: String,
|
||||||
|
|
||||||
/// Settings loaded from genpolicy-settings.json.
|
/// Policy settings.
|
||||||
pub settings: settings::Settings,
|
|
||||||
|
|
||||||
/// Additional Policy settings.
|
|
||||||
pub config: utils::Config,
|
pub config: utils::Config,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,7 +69,7 @@ pub struct PolicyData {
|
|||||||
/// is ordered, thus resulting in the same output policy contents every time
|
/// is ordered, thus resulting in the same output policy contents every time
|
||||||
/// when this apps runs with the same inputs. Also, it preserves the upper
|
/// when this apps runs with the same inputs. Also, it preserves the upper
|
||||||
/// case field names, for consistency with the structs used by agent's rpc.rs.
|
/// case field names, for consistency with the structs used by agent's rpc.rs.
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
pub struct KataSpec {
|
pub struct KataSpec {
|
||||||
/// Version of the Open Container Initiative Runtime Specification with which the bundle complies.
|
/// Version of the Open Container Initiative Runtime Specification with which the bundle complies.
|
||||||
#[serde(default = "version_default")]
|
#[serde(default = "version_default")]
|
||||||
@ -366,6 +362,9 @@ pub struct CommonData {
|
|||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
pub struct ClusterConfig {
|
pub struct ClusterConfig {
|
||||||
default_namespace: String,
|
default_namespace: String,
|
||||||
|
|
||||||
|
/// Pause container image reference.
|
||||||
|
pub pause_container_image: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AgentPolicy {
|
impl AgentPolicy {
|
||||||
@ -402,8 +401,6 @@ impl AgentPolicy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let settings = settings::Settings::new(&config.json_settings_path);
|
|
||||||
|
|
||||||
if let Some(config_map_files) = &config.config_map_files {
|
if let Some(config_map_files) = &config.config_map_files {
|
||||||
for file in config_map_files {
|
for file in config_map_files {
|
||||||
config_maps.push(config_map::ConfigMap::new(file)?);
|
config_maps.push(config_map::ConfigMap::new(file)?);
|
||||||
@ -414,7 +411,6 @@ impl AgentPolicy {
|
|||||||
Ok(AgentPolicy {
|
Ok(AgentPolicy {
|
||||||
resources,
|
resources,
|
||||||
rules,
|
rules,
|
||||||
settings,
|
|
||||||
config_maps,
|
config_maps,
|
||||||
secrets,
|
secrets,
|
||||||
config: config.clone(),
|
config: config.clone(),
|
||||||
@ -460,8 +456,8 @@ impl AgentPolicy {
|
|||||||
|
|
||||||
let policy_data = policy::PolicyData {
|
let policy_data = policy::PolicyData {
|
||||||
containers: policy_containers,
|
containers: policy_containers,
|
||||||
request_defaults: self.settings.request_defaults.clone(),
|
request_defaults: self.config.settings.request_defaults.clone(),
|
||||||
common: self.settings.common.clone(),
|
common: self.config.settings.common.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let json_data = serde_json::to_string_pretty(&policy_data).unwrap();
|
let json_data = serde_json::to_string_pretty(&policy_data).unwrap();
|
||||||
@ -478,14 +474,21 @@ impl AgentPolicy {
|
|||||||
yaml_container: &pod::Container,
|
yaml_container: &pod::Container,
|
||||||
is_pause_container: bool,
|
is_pause_container: bool,
|
||||||
) -> ContainerPolicy {
|
) -> ContainerPolicy {
|
||||||
let c_settings = self.settings.get_container_settings(is_pause_container);
|
let c_settings = self
|
||||||
|
.config
|
||||||
|
.settings
|
||||||
|
.get_container_settings(is_pause_container);
|
||||||
let mut root = c_settings.Root.clone();
|
let mut root = c_settings.Root.clone();
|
||||||
root.Readonly = yaml_container.read_only_root_filesystem();
|
root.Readonly = yaml_container.read_only_root_filesystem();
|
||||||
|
|
||||||
let namespace = if let Some(ns) = resource.get_namespace() {
|
let namespace = if let Some(ns) = resource.get_namespace() {
|
||||||
ns
|
ns
|
||||||
} else {
|
} else {
|
||||||
self.settings.cluster_config.default_namespace.clone()
|
self.config
|
||||||
|
.settings
|
||||||
|
.cluster_config
|
||||||
|
.default_namespace
|
||||||
|
.clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
let use_host_network = resource.use_host_network();
|
let use_host_network = resource.use_host_network();
|
||||||
@ -510,7 +513,7 @@ impl AgentPolicy {
|
|||||||
|
|
||||||
let mut mounts = containerd::get_mounts(is_pause_container, is_privileged);
|
let mut mounts = containerd::get_mounts(is_pause_container, is_privileged);
|
||||||
mount_and_storage::get_policy_mounts(
|
mount_and_storage::get_policy_mounts(
|
||||||
&self.settings,
|
&self.config.settings,
|
||||||
&mut mounts,
|
&mut mounts,
|
||||||
yaml_container,
|
yaml_container,
|
||||||
is_pause_container,
|
is_pause_container,
|
||||||
@ -523,7 +526,7 @@ impl AgentPolicy {
|
|||||||
&mut mounts,
|
&mut mounts,
|
||||||
&mut storages,
|
&mut storages,
|
||||||
yaml_container,
|
yaml_container,
|
||||||
&self.settings,
|
&self.config.settings,
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut linux = containerd::get_linux(is_privileged);
|
let mut linux = containerd::get_linux(is_privileged);
|
||||||
@ -570,9 +573,9 @@ impl AgentPolicy {
|
|||||||
) -> KataProcess {
|
) -> KataProcess {
|
||||||
// Start with the Default Unix Spec from
|
// Start with the Default Unix Spec from
|
||||||
// https://github.com/containerd/containerd/blob/release/1.6/oci/spec.go#L132
|
// https://github.com/containerd/containerd/blob/release/1.6/oci/spec.go#L132
|
||||||
let mut process = containerd::get_process(is_privileged, &self.settings.common);
|
let mut process = containerd::get_process(is_privileged, &self.config.settings.common);
|
||||||
|
|
||||||
yaml_container.apply_capabilities(&mut process.Capabilities, &self.settings.common);
|
yaml_container.apply_capabilities(&mut process.Capabilities, &self.config.settings.common);
|
||||||
|
|
||||||
let (yaml_has_command, yaml_has_args) = yaml_container.get_process_args(&mut process.Args);
|
let (yaml_has_command, yaml_has_args) = yaml_container.get_process_args(&mut process.Args);
|
||||||
yaml_container
|
yaml_container
|
||||||
|
@ -15,7 +15,7 @@ use anyhow::{anyhow, bail, Result};
|
|||||||
use docker_credential::{CredentialRetrievalError, DockerCredential};
|
use docker_credential::{CredentialRetrievalError, DockerCredential};
|
||||||
use log::warn;
|
use log::warn;
|
||||||
use log::{debug, info, LevelFilter};
|
use log::{debug, info, LevelFilter};
|
||||||
use oci_distribution::client::{linux_amd64_resolver, ClientConfig};
|
use oci_distribution::client::{linux_amd64_resolver, ClientConfig, ClientProtocol};
|
||||||
use oci_distribution::{manifest, secrets::RegistryAuth, Client, Reference};
|
use oci_distribution::{manifest, secrets::RegistryAuth, Client, Reference};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sha2::{digest::typenum::Unsigned, digest::OutputSizeUser, Sha256};
|
use sha2::{digest::typenum::Unsigned, digest::OutputSizeUser, Sha256};
|
||||||
@ -64,13 +64,14 @@ pub struct ImageLayer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Container {
|
impl Container {
|
||||||
pub async fn new(use_cached_files: bool, image: &str) -> Result<Self> {
|
pub async fn new(config: &Config, image: &str) -> Result<Self> {
|
||||||
info!("============================================");
|
info!("============================================");
|
||||||
info!("Pulling manifest and config for {:?}", image);
|
info!("Pulling manifest and config for {:?}", image);
|
||||||
let reference: Reference = image.to_string().parse().unwrap();
|
let reference: Reference = image.to_string().parse().unwrap();
|
||||||
let auth = build_auth(&reference);
|
let auth = build_auth(&reference);
|
||||||
|
|
||||||
let mut client = Client::new(ClientConfig {
|
let mut client = Client::new(ClientConfig {
|
||||||
|
protocol: ClientProtocol::HttpsExcept(config.insecure_registries.clone()),
|
||||||
platform_resolver: Some(Box::new(linux_amd64_resolver)),
|
platform_resolver: Some(Box::new(linux_amd64_resolver)),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
@ -93,7 +94,7 @@ impl Container {
|
|||||||
let config_layer: DockerConfigLayer =
|
let config_layer: DockerConfigLayer =
|
||||||
serde_json::from_str(&config_layer_str).unwrap();
|
serde_json::from_str(&config_layer_str).unwrap();
|
||||||
let image_layers = get_image_layers(
|
let image_layers = get_image_layers(
|
||||||
use_cached_files,
|
config.use_cache,
|
||||||
&mut client,
|
&mut client,
|
||||||
&reference,
|
&reference,
|
||||||
&manifest,
|
&manifest,
|
||||||
@ -430,7 +431,7 @@ pub async fn get_container(config: &Config, image: &str) -> Result<Container> {
|
|||||||
if let Some(socket_path) = &config.containerd_socket_path {
|
if let Some(socket_path) = &config.containerd_socket_path {
|
||||||
return Container::new_containerd_pull(config.use_cache, image, socket_path).await;
|
return Container::new_containerd_pull(config.use_cache, image, socket_path).await;
|
||||||
}
|
}
|
||||||
Container::new(config.use_cache, image).await
|
Container::new(config, image).await
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_auth(reference: &Reference) -> RegistryAuth {
|
fn build_auth(reference: &Reference) -> RegistryAuth {
|
||||||
|
@ -14,7 +14,7 @@ use std::fs::File;
|
|||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
/// Policy settings loaded from genpolicy-settings.json.
|
/// Policy settings loaded from genpolicy-settings.json.
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
pub struct Settings {
|
pub struct Settings {
|
||||||
pub pause_container: policy::KataSpec,
|
pub pause_container: policy::KataSpec,
|
||||||
pub other_container: policy::KataSpec,
|
pub other_container: policy::KataSpec,
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
//
|
//
|
||||||
|
|
||||||
|
use crate::settings;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
#[derive(Debug, Parser)]
|
#[derive(Debug, Parser)]
|
||||||
@ -75,16 +76,23 @@ struct CommandLineOptions {
|
|||||||
require_equals= true
|
require_equals= true
|
||||||
)]
|
)]
|
||||||
containerd_socket_path: Option<String>,
|
containerd_socket_path: Option<String>,
|
||||||
|
|
||||||
|
#[clap(
|
||||||
|
long,
|
||||||
|
help = "Registry that uses plain HTTP. Can be passed more than once to configure multiple insecure registries."
|
||||||
|
)]
|
||||||
|
insecure_registry: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Application configuration, derived from on command line parameters.
|
/// Application configuration, derived from on command line parameters.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub use_cache: bool,
|
pub use_cache: bool,
|
||||||
|
pub insecure_registries: Vec<String>,
|
||||||
|
|
||||||
pub yaml_file: Option<String>,
|
pub yaml_file: Option<String>,
|
||||||
pub rego_rules_path: String,
|
pub rego_rules_path: String,
|
||||||
pub json_settings_path: String,
|
pub settings: settings::Settings,
|
||||||
pub config_map_files: Option<Vec<String>>,
|
pub config_map_files: Option<Vec<String>>,
|
||||||
|
|
||||||
pub silent_unsupported_fields: bool,
|
pub silent_unsupported_fields: bool,
|
||||||
@ -108,11 +116,14 @@ impl Config {
|
|||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let settings = settings::Settings::new(&args.json_settings_path);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
use_cache: args.use_cached_files,
|
use_cache: args.use_cached_files,
|
||||||
|
insecure_registries: args.insecure_registry,
|
||||||
yaml_file: args.yaml_file,
|
yaml_file: args.yaml_file,
|
||||||
rego_rules_path: args.rego_rules_path,
|
rego_rules_path: args.rego_rules_path,
|
||||||
json_settings_path: args.json_settings_path,
|
settings,
|
||||||
config_map_files: cm_files,
|
config_map_files: cm_files,
|
||||||
silent_unsupported_fields: args.silent_unsupported_fields,
|
silent_unsupported_fields: args.silent_unsupported_fields,
|
||||||
raw_out: args.raw_out,
|
raw_out: args.raw_out,
|
||||||
|
Loading…
Reference in New Issue
Block a user