Merge pull request #9428 from arc9693/archana1/genplicyfixes

genpolicy: implement default methods for K8sResource trait
This commit is contained in:
Dan Mihai
2024-04-24 08:04:19 -07:00
committed by GitHub
5 changed files with 33 additions and 127 deletions

View File

@@ -9,13 +9,11 @@
use crate::obj_meta;
use crate::pod;
use crate::policy;
use crate::settings;
use crate::utils::Config;
use crate::yaml;
use async_trait::async_trait;
use log::debug;
use protocols::agent;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fs::File;
@@ -89,20 +87,6 @@ impl yaml::K8sResource for ConfigMap {
self.doc_mapping = doc_mapping.clone();
}
fn get_sandbox_name(&self) -> Option<String> {
panic!("Unsupported");
}
fn get_container_mounts_and_storages(
&self,
_policy_mounts: &mut Vec<policy::KataMount>,
_storages: &mut Vec<agent::Storage>,
_container: &pod::Container,
_settings: &settings::Settings,
) {
panic!("Unsupported");
}
fn generate_policy(&self, _agent_policy: &policy::AgentPolicy) -> String {
"".to_string()
}
@@ -111,19 +95,7 @@ impl yaml::K8sResource for ConfigMap {
serde_yaml::to_string(&self.doc_mapping).unwrap()
}
fn get_containers(&self) -> &Vec<pod::Container> {
panic!("Unsupported");
}
fn get_annotations(&self) -> &Option<BTreeMap<String, String>> {
&self.metadata.annotations
}
fn use_host_network(&self) -> bool {
panic!("Unsupported");
}
fn use_sandbox_pidns(&self) -> bool {
panic!("Unsupported");
}
}

View File

@@ -18,7 +18,6 @@ use protocols::agent;
use serde::{Deserialize, Serialize};
use serde_yaml::Value;
use std::boxed;
use std::collections::BTreeMap;
use std::marker::{Send, Sync};
#[derive(Debug, Serialize, Deserialize)]
@@ -50,10 +49,6 @@ impl yaml::K8sResource for List {
}
}
fn get_sandbox_name(&self) -> Option<String> {
panic!("Unsupported");
}
fn get_container_mounts_and_storages(
&self,
_policy_mounts: &mut Vec<policy::KataMount>,
@@ -85,20 +80,4 @@ impl yaml::K8sResource for List {
}
serde_yaml::to_string(&self).unwrap()
}
fn get_containers(&self) -> &Vec<pod::Container> {
panic!("Unsupported");
}
fn get_annotations(&self) -> &Option<BTreeMap<String, String>> {
panic!("Unsupported");
}
fn use_host_network(&self) -> bool {
panic!("Unsupported");
}
fn use_sandbox_pidns(&self) -> bool {
panic!("Unsupported");
}
}

View File

@@ -6,15 +6,11 @@
// Allow K8s YAML field names.
#![allow(non_snake_case)]
use crate::pod;
use crate::policy;
use crate::settings;
use crate::utils::Config;
use crate::yaml;
use async_trait::async_trait;
use protocols::agent;
use std::collections::BTreeMap;
#[derive(Clone, Debug)]
pub struct NoPolicyResource {
@@ -31,20 +27,6 @@ impl yaml::K8sResource for NoPolicyResource {
) {
}
fn get_sandbox_name(&self) -> Option<String> {
panic!("Unsupported");
}
fn get_container_mounts_and_storages(
&self,
_policy_mounts: &mut Vec<policy::KataMount>,
_storages: &mut Vec<agent::Storage>,
_container: &pod::Container,
_settings: &settings::Settings,
) {
panic!("Unsupported");
}
fn generate_policy(&self, _agent_policy: &policy::AgentPolicy) -> String {
"".to_string()
}
@@ -52,20 +34,4 @@ impl yaml::K8sResource for NoPolicyResource {
fn serialize(&mut self, _policy: &str) -> String {
self.yaml.clone()
}
fn get_containers(&self) -> &Vec<pod::Container> {
panic!("Unsupported");
}
fn get_annotations(&self) -> &Option<BTreeMap<String, String>> {
panic!("Unsupported");
}
fn use_host_network(&self) -> bool {
panic!("Unsupported");
}
fn use_sandbox_pidns(&self) -> bool {
panic!("Unsupported");
}
}

View File

@@ -9,13 +9,11 @@
use crate::obj_meta;
use crate::pod;
use crate::policy;
use crate::settings;
use crate::utils::Config;
use crate::yaml;
use async_trait::async_trait;
use base64::{engine::general_purpose, Engine as _};
use protocols::agent;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
@@ -78,20 +76,6 @@ impl yaml::K8sResource for Secret {
self.doc_mapping = doc_mapping.clone();
}
fn get_sandbox_name(&self) -> Option<String> {
panic!("Unsupported");
}
fn get_container_mounts_and_storages(
&self,
_policy_mounts: &mut Vec<policy::KataMount>,
_storages: &mut Vec<agent::Storage>,
_container: &pod::Container,
_settings: &settings::Settings,
) {
panic!("Unsupported");
}
fn generate_policy(&self, _agent_policy: &policy::AgentPolicy) -> String {
"".to_string()
}
@@ -99,20 +83,4 @@ impl yaml::K8sResource for Secret {
fn serialize(&mut self, _policy: &str) -> String {
serde_yaml::to_string(&self.doc_mapping).unwrap()
}
fn get_containers(&self) -> &Vec<pod::Container> {
panic!("Unsupported");
}
fn get_annotations(&self) -> &Option<BTreeMap<String, String>> {
panic!("Unsupported");
}
fn use_host_network(&self) -> bool {
panic!("Unsupported");
}
fn use_sandbox_pidns(&self) -> bool {
panic!("Unsupported");
}
}

View File

@@ -49,26 +49,47 @@ pub trait K8sResource {
silent_unsupported_fields: bool,
);
fn generate_policy(&self, agent_policy: &policy::AgentPolicy) -> String;
fn serialize(&mut self, policy: &str) -> String;
fn generate_policy(&self, _agent_policy: &policy::AgentPolicy) -> String {
panic!("Unsupported");
}
fn serialize(&mut self, _policy: &str) -> String {
panic!("Unsupported");
}
fn get_sandbox_name(&self) -> Option<String> {
panic!("Unsupported");
}
fn get_sandbox_name(&self) -> Option<String>;
fn get_namespace(&self) -> Option<String> {
panic!("Unsupported");
}
fn get_container_mounts_and_storages(
&self,
policy_mounts: &mut Vec<policy::KataMount>,
storages: &mut Vec<agent::Storage>,
container: &pod::Container,
settings: &settings::Settings,
);
_policy_mounts: &mut Vec<policy::KataMount>,
_storages: &mut Vec<agent::Storage>,
_container: &pod::Container,
_settings: &settings::Settings,
) {
panic!("Unsupported");
}
fn get_containers(&self) -> &Vec<pod::Container>;
fn get_annotations(&self) -> &Option<BTreeMap<String, String>>;
fn use_host_network(&self) -> bool;
fn use_sandbox_pidns(&self) -> bool;
fn get_containers(&self) -> &Vec<pod::Container> {
panic!("Unsupported");
}
fn get_annotations(&self) -> &Option<BTreeMap<String, String>> {
panic!("Unsupported");
}
fn use_host_network(&self) -> bool {
panic!("Unsupported");
}
fn use_sandbox_pidns(&self) -> bool {
panic!("Unsupported");
}
}
/// See Reference / Kubernetes API / Common Definitions / LabelSelector.