Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

genpolicy: support dynamic SMB storage class options #245

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/configmap/pod-cm1.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/configmap/pod-cm2.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/configmap/pod-cm3.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/job/test-job.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/job/test-job2.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/pod/pod-exec.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/pod/pod-lifecycle.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/pod/pod-many-layers.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/pod/pod-one-container.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/pod/pod-spark.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/pod/pod-ubuntu.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/replica-set/replica2.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/stateful-set/web.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/stateful-set/web2.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod1.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod2.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod3.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod4.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod5.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod6.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod7.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook2/webhook-pod8.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook2/webhook-pod9.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook3/dns-test.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook3/many-layers.yaml

Large diffs are not rendered by default.

16 changes: 13 additions & 3 deletions src/tools/genpolicy/genpolicy-settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,18 @@
"cc-managed-premium-csi"
],
"smb_storage_classes": [
"cc-azurefile-csi",
"cc-azurefile-premium-csi"
{
"name": "azurefile-csi-kata-cc",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want to use azurefile-csi and azurefile-premium-csi since we want the official driver to support conf pods?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default storage drivers do not have enableKataCCMount set to true.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @sprt, it would be good to include the default Azure File storage classes. For example, our team creates a custom storage class with name azurefile-csi and set the enableKataCCMount flag to true; we use the same name for compatibility with non-confidential deployments. Adding them would also align with the plan of removing the flag from the storage class and automatically enabling kata mount for confidential pods in the Azure File CSI driver.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be added, post migration discussion with Andy. As of now, it's not true by default for azurefile-csi. So, to reduce conflict we will keep as it is for now.

"mount_options": [
"dir_mode=0777",
"file_mode=0777",
"mfsymlinks",
"cache=strict",
"nosharesock",
"actimeo=30",
"nobrl"
]
}
]
},
"kata_config": {
Expand Down Expand Up @@ -322,4 +332,4 @@
"UpdateEphemeralMountsRequest": false,
"WriteStreamRequest": false
}
}
}
18 changes: 9 additions & 9 deletions src/tools/genpolicy/rules.rego
Original file line number Diff line number Diff line change
Expand Up @@ -941,15 +941,15 @@ allow_storage_options(p_storage, i_storage, layer_ids, root_hashes) {
print("allow_storage_options 4: start")

p_storage.driver == "smb"
count(i_storage.options) == 8
i_storage.options[0] == "dir_mode=0666"
i_storage.options[1] == "file_mode=0666"
i_storage.options[2] == "mfsymlinks"
i_storage.options[3] == "cache=strict"
i_storage.options[4] == "nosharesock"
i_storage.options[5] == "actimeo=30"
startswith(i_storage.options[6], "addr=")
creds = split(i_storage.options[7], ",")
p_opts_count := count(p_storage.options)
i_opts_count := count(i_storage.options)
i_opts_count == p_opts_count + 2

i_opt_matches := [i | i := idx; idx < p_opts_count; p_storage.options[idx] == i_storage.options[idx]]
count(i_opt_matches) == p_opts_count

startswith(i_storage.options[i_opts_count-2], "addr=")
creds = split(i_storage.options[i_opts_count-1], ",")
count(creds) == 2
startswith(creds[0], "username=")
startswith(creds[1], "password=")
Expand Down
77 changes: 60 additions & 17 deletions src/tools/genpolicy/src/mount_and_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,40 @@ fn adjust_termination_path(mount: &mut policy::KataMount, yaml_container: &pod::
}
}

pub fn get_mount_info(
storage_class: Option<&String>,
settings: &settings::Settings,
) -> (bool, bool, Option<Vec<String>>) {
if let Some(storage_class) = storage_class {
let is_blk_mount = settings
.common
.virtio_blk_storage_classes
.contains(storage_class);

let is_smb_mount = settings
.common
.smb_storage_classes
.iter()
.any(|smb_class| &smb_class.name == storage_class);

let smb_mount_options = if is_smb_mount {
settings
.common
.smb_storage_classes
.iter()
.find(|sc| &sc.name == storage_class)
.map(|sc| sc.mount_options.clone())
} else {
None
};

(is_blk_mount, is_smb_mount, smb_mount_options)
} else {
warn!("Storage class is None. Defaulting to no mounts.");
(false, false, None)
}
}

pub fn get_mount_and_storage(
settings: &settings::Settings,
p_mounts: &mut Vec<policy::KataMount>,
Expand Down Expand Up @@ -238,13 +272,17 @@ fn get_persistent_volume_claim_mount(
);
}

let is_blk_mount = pvc_resource
.and_then(|pvc_resource| pvc_resource.spec.storageClassName.as_ref())
.is_some_and(|sc| settings.common.virtio_blk_storage_classes.contains(sc));
let storage_class = if let Some(pvc_resource) = pvc_resource {
pvc_resource.spec.storageClassName.as_ref()
} else {
None
};

if storage_class.is_none() {
warn!("Storage class is missing for persistent volume claim '{pvc_name}'.");
}

let is_smb_mount = pvc_resource
.and_then(|pvc_resource| pvc_resource.spec.storageClassName.as_ref())
.is_some_and(|sc| settings.common.smb_storage_classes.contains(sc));
let (is_blk_mount, is_smb_mount, smb_mount_options) = get_mount_info(storage_class, settings);

handle_persistent_volume_claim(
is_blk_mount,
Expand All @@ -253,6 +291,7 @@ fn get_persistent_volume_claim_mount(
p_mounts,
storages,
mount_options,
smb_mount_options,
);
}

Expand Down Expand Up @@ -419,22 +458,16 @@ fn get_ephemeral_mount(
storages: &mut Vec<agent::Storage>,
mount_options: (&str, &str),
) {
let storage_class = &yaml_volume
let storage_class = yaml_volume
.ephemeral
.as_ref()
.unwrap()
.volumeClaimTemplate
.spec
.storageClassName;
.storageClassName
.as_ref();

let is_blk_mount = storage_class
.as_ref()
.map(|sc| settings.common.virtio_blk_storage_classes.contains(sc))
.unwrap_or(false);
let is_smb_mount = storage_class
.as_ref()
.map(|sc| settings.common.smb_storage_classes.contains(sc))
.unwrap_or(false);
let (is_blk_mount, is_smb_mount, smb_mount_options) = get_mount_info(storage_class, settings);

handle_persistent_volume_claim(
is_blk_mount,
Expand All @@ -443,6 +476,7 @@ fn get_ephemeral_mount(
p_mounts,
storages,
mount_options,
smb_mount_options,
);
}

Expand All @@ -453,6 +487,7 @@ pub fn handle_persistent_volume_claim(
p_mounts: &mut Vec<policy::KataMount>,
storages: &mut Vec<agent::Storage>,
mount_options: (&str, &str),
smb_mount_options: Option<Vec<String>>, // Pass SMB mount options
) {
if is_blk_mount || is_smb_mount {
let source = "$(spath)/$(b64-direct-vol-path)".to_string();
Expand All @@ -468,7 +503,15 @@ pub fn handle_persistent_volume_claim(
source: "$(direct-vol-path)".to_string(),
mount_point: source.to_string(),
fstype: "$(fs-type)".to_string(),
options: Vec::new(),
options: if is_smb_mount {
if let Some(mount_options) = smb_mount_options {
mount_options.clone()
} else {
Vec::new()
}
} else {
Vec::new()
},
});

let dest = yaml_mount.mountPath.clone();
Expand Down
9 changes: 8 additions & 1 deletion src/tools/genpolicy/src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,13 @@ pub struct RequestDefaults {
pub WriteStreamRequest: bool,
}

// SMB storage class settings from genpolicy-settings.json.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SmbStorageClass {
pub name: String,
pub mount_options: Vec<String>,
}

/// Struct used to read data from the settings file and copy that data into the policy.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CommonData {
Expand Down Expand Up @@ -379,7 +386,7 @@ pub struct CommonData {
pub virtio_blk_storage_classes: Vec<String>,

/// Storage classes which mounts should be handled as smb mounts
pub smb_storage_classes: Vec<String>,
pub smb_storage_classes: Vec<SmbStorageClass>,
}

/// Struct used to read data from the settings file and copy that data into the policy.
Expand Down
21 changes: 4 additions & 17 deletions src/tools/genpolicy/src/stateful_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,23 +212,9 @@ impl StatefulSet {
for claim in claims {
if let Some(claim_name) = &claim.metadata.name {
if claim_name.eq(&mount.name) {
// check if a storage class is set and if it is a virtio-blk storage class
let is_blk_mount = if let Some(storage_class) = &claim.spec.storageClassName
{
settings
.common
.virtio_blk_storage_classes
.contains(storage_class)
} else {
false
};
// check if a storage class is set and if it is a smb storage class
let is_smb_mount = if let Some(storage_class) = &claim.spec.storageClassName
{
settings.common.smb_storage_classes.contains(storage_class)
} else {
false
};
let storage_class = claim.spec.storageClassName.as_ref();
let (is_blk_mount, is_smb_mount, smb_mount_options) =
mount_and_storage::get_mount_info(storage_class, settings);

let propagation = match &mount.mountPropagation {
Some(p) if p == "Bidirectional" => "rshared",
Expand All @@ -249,6 +235,7 @@ impl StatefulSet {
policy_mounts,
storages,
mount_options,
smb_mount_options,
);
}
}
Expand Down
Loading