Skip to content

Commit

Permalink
StorageClass improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sebt3 committed Nov 16, 2024
1 parent 7d39ba3 commit ee7c037
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 62 deletions.
24 changes: 12 additions & 12 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fmt = { cmd=[
]}
precommit = { cmd=[
"cargo update",
"cargo clippy --fix --allow-dirty",
"cargo clippy --fix --allow-dirty --allow-staged",
"cargo cmd generate",
"cargo +nightly fmt"
]}
Expand Down
53 changes: 4 additions & 49 deletions agent/scripts/lib/build_context.rhai
Original file line number Diff line number Diff line change
@@ -1,50 +1,5 @@
// TODO: implement more from: https://storageclass.info/csidrivers/
// https://github.com/storageclass/storageclass.github.io/blob/main/data/csidrivers.yaml
fn get_prefered_sc(scs) {
let pref = #{
"many_block": (),
"many_safe": (),
"many_cheap": (),
"many_only": (),
"only_block": (),
"only_performance": (),
"only_cheap": (),
"only_safe": (),
};
if scs.some(|s| s.provisioner == "smb.csi.k8s.io") {
let smb = scs.find(|s| s.provisioner == "smb.csi.k8s.io").name;
pref["many_safe"] = smb;
pref["many_cheap"] = smb;
}
if scs.some(|s| s.provisioner == "k8s-sigs.io/nfs-provisioner") {
let nfs = scs.find(|s| s.provisioner == "k8s-sigs.io/nfs-provisioner").name;
pref["many_safe"] = nfs;
pref["many_cheap"] = nfs;
}
if scs.some(|s| s.provisioner == "rancher.io/local-path") {
let lp = scs.find(|s| s.provisioner == "rancher.io/local-path").name;
pref["only_performance"] = lp;
pref["only_cheap"] = lp;
}
if scs.some(|s| s.provisioner.ends_with("nfs.csi.ceph.com")) {
let nfs = scs.find(|s| s.provisioner.ends_with("nfs.csi.ceph.com")).name;
pref["many_safe"] = nfs;
pref["many_cheap"] = nfs;
}
if scs.some(|s| s.provisioner.ends_with("cephfs.csi.ceph.com")) {
let cfs = scs.find(|s| s.provisioner.ends_with("cephfs.csi.ceph.com")).name;
pref["many_safe"] = cfs;
}
if scs.some(|s| s.provisioner.ends_with("rbd.csi.ceph.com")) {
let rbd = scs.find(|s| s.provisioner.ends_with("rbd.csi.ceph.com")).name;
pref["many_block"] = rbd;
pref["only_block"] = rbd;
pref["only_safe"] = rbd;
pref["many_only"] = rbd;
}
pref
}

import "storage_class_enrich" as sce;
fn appslug(pkg, inst) {
if pkg == inst {
inst
Expand Down Expand Up @@ -81,7 +36,7 @@ fn get_values(options, defaults) {

fn run(instance, args) {
let pkg = read_package_yaml(`${args.package_dir}/package.yaml`);
let scs = k8s_resource("StorageClass").list().items.map(|s| #{ name: s.metadata.name, provisioner: s.provisioner });
let scs = sce::classes_enrich(k8s_resource("StorageClass").list().items.map(|s| #{ name: s.metadata.name, provisioner: s.provisioner, is_default: s.metadata.annotations != () && s.metadata.annotations["storageclass.kubernetes.io/is-default-class"]=="true" }));
let current = "";
let controller = #{};
try {
Expand All @@ -96,8 +51,8 @@ fn run(instance, args) {
cluster_config["storage_classes"] = scs;
let crds = k8s_resource("CustomResourceDefinition").list_meta().items;
cluster_config["crds"] = crds.map(|c| c.metadata.name);
if ! ("prefered" in cluster_config) {
cluster_config["prefered"] = get_prefered_sc(scs);
if ! ("prefered_storage" in cluster_config) {
cluster_config["prefered_storage"] = sce::get_prefered_sc(scs);
}
if ! ("ha" in cluster_config) {
let nodes = k8s_resource("Nodes").list_meta().items.map(|c| c.metadata.name);
Expand Down
119 changes: 119 additions & 0 deletions agent/scripts/lib/storage_class_enrich.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// TODO: implement more from: https://storageclass.info/csidrivers/
// https://github.com/storageclass/storageclass.github.io/blob/main/data/csidrivers.yaml
const KNOWN_CLASS = [
#{
"driverClass": "cephfs.csi.ceph.com",
"accessModes": #{"readOnlyMany": true, "readWriteMany": true, "readWriteOnce": true, "readWriteOncePod": false},
"capabilities": #{"clone": true, "dynamic": true, "expansion": true, "raw": false, "snapshot": true, "topology": false, "tracking": false},
}, #{
"driverClass": "rbd.csi.ceph.com",
"accessModes": #{"readOnlyMany": true, "readWriteMany": true, "readWriteOnce": true, "readWriteOncePod": true},
"capabilities": #{"clone": true, "dynamic": true, "expansion": true, "raw": true, "snapshot": true, "topology": true, "tracking": false},
}, #{
"driverClass": "smb.csi.k8s.io",
"accessModes": #{"readOnlyMany": true, "readWriteMany": true, "readWriteOnce": true, "readWriteOncePod": false},
"capabilities": #{"dynamic": true},
}, #{
"driverClass": "nfs.csi.k8s.io",
"accessModes": #{"readOnlyMany": true, "readWriteMany": true, "readWriteOnce": true, "readWriteOncePod": false},
"capabilities": #{"dynamic": true},
}, #{
"driverClass": "k8s-sigs.io/nfs-provisioner",
"accessModes": #{"readOnlyMany": true, "readWriteMany": true, "readWriteOnce": true, "readWriteOncePod": false},
"capabilities": #{"dynamic": true},
}, #{
"driverClass": "rancher.io/local-path",
"accessModes": #{"readOnlyMany": false, "readWriteMany": false, "readWriteOnce": true, "readWriteOncePod": true},
"capabilities": #{"dynamic": true},
}, #{
"driverClass": "csi.scaleway.com",
"accessModes": #{"readOnlyMany": true, "readWriteMany": true, "readWriteOnce": true, "readWriteOncePod": true},
"capabilities": #{"clone": false, "dynamic": true, "expansion": true, "raw": true, "snapshot": true, "topology": true, "tracking": false},
}, #{
"driverClass": "filestore.csi.storage.gke.io",
"accessModes": #{"readOnlyMany": true, "readWriteMany": true, "readWriteOnce": true, "readWriteOncePod": false},
"capabilities": #{"dynamic": true, "file": true},
}, #{
"driverClass": "file.csi.azure.com",
"accessModes": #{"readOnlyMany": true, "readWriteMany": true, "readWriteOnce": true, "readWriteOncePod": false},
"capabilities": #{"clone": false, "dynamic": true, "expansion": true, "file": true, "raw": false, "snapshot": false, "topology": false, "tracking": false},
}, #{
"driverClass": "efs.csi.aws.com",
"accessModes": #{"readOnlyMany": true, "readWriteMany": true, "readWriteOnce": true, "readWriteOncePod": false},
"capabilities": #{"dynamic": true, "file": true},
}
];

fn get_default_access_modes_from_all(all) {
if all.contains("ReadWriteMany") {
["ReadWriteMany"]
} else if all.contains("ReadOnlyMany") && all.contains("ReadWriteOnce") {
["ReadOnlyMany", "ReadWriteOnce"]
} else if all.contains("ReadWriteOncePod") {
["ReadWriteOncePod"]
} else {
all
}
}

fn classes_enrich(scs) {
let enriched = [];
for i in scs {
i["volumeMode"] = "Filesystem";
let f = global::KNOWN_CLASS.find(|k| i.provisioner.contains(k.driverClass));
if f != () {
i["capabilities"] = f.capabilities;
let keys = f.accessModes.keys();
i["allAccessModes"] = keys.filter(|k| f.accessModes[k]).map(|s| {s.replace("read","Read");s});
if (f.capabilities.raw == true) {
let b = i;
b["volumeMode"] = "Block";
i["allAccessModes"] = keys.filter(|k| f.accessModes[k] && k != "ReadWriteMany");
b["accessModes"] = get_default_access_modes_from_all(b["allAccessModes"]);
enriched.push(b);
}
i["accessModes"] = get_default_access_modes_from_all(i["allAccessModes"]);
} else {
i["capabilities"] = #{};
i["allAccessModes"] = ["ReadWriteOnce"];
i["accessModes"] = ["ReadWriteOnce"];
}
enriched.push(i);
}
enriched
}

fn get_prefered_sc(scs) {
let pref = #{
"block_readWriteMany": (),
"block_readWriteOnce": (),
"fs_cheap_readWriteMany": (),
"fs_fast_readWriteMany": (),
"fs_readWriteMany": (),
"fs_cheap_readWriteOnce": (),
"fs_fast_readWriteOnce": (),
"fs_readWriteOnce": (),
};
let block = scs.filter(|s| s.volumeMode=="Block");
let fs_only = scs.filter(|s| s.volumeMode=="Filesystem" && [["ReadWriteOncePod"],["ReadWriteOnce"]].contains(s.accessModes));
let fs_many = scs.filter(|s| s.volumeMode=="Filesystem" && s.accessModes==["ReadWriteMany"]);
if block.len() > 0 {
let many = block.filter(|s| s.accessModes==["ReadWriteMany"]);
let others = block.filter(|s| s.accessModes!=["ReadWriteMany"]);
if many.len() > 0 {
pref["block_readWriteMany"] = many[0].name;
}
if others.len() > 0 {
pref["block_readWriteOnce"] = others[0].name;
} else {
pref["block_readWriteOnce"] = many[0].name;
}
}
if fs_many.len() > 0 {
pref["fs_readWriteMany"] = fs_many[0].name;
}
if fs_only.len() > 0 {
pref["fs_readWriteOnce"] = fs_only[0].name;
}
pref
}
48 changes: 48 additions & 0 deletions agent/scripts/lib/storage_class_selector.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
fn for_deployments(context, typed) {
let scs = context.cluster.storage_classes;
if typed=="fast" && context.cluster.prefered_storage.fs_fast_readWriteMany != () {
scs.find(|s| s.name == context.cluster.prefered_storage.fs_fast_readWriteMany && s.volumeMode == "Filesystem")
} else if typed=="cheap" && context.cluster.prefered_storage.fs_cheap_readWriteMany != () {
scs.find(|s| s.name == context.cluster.prefered_storage.fs_cheap_readWriteMany && s.volumeMode == "Filesystem")
} else if context.cluster.prefered_storage.fs_readWriteMany != () {
scs.find(|s| s.name == context.cluster.prefered_storage.fs_readWriteMany && s.volumeMode == "Filesystem")
} else if typed=="fast" && context.cluster.prefered_storage.fs_fast_readWriteOnce != () {
scs.find(|s| s.name == context.cluster.prefered_storage.fs_fast_readWriteOnce && s.volumeMode == "Filesystem")
} else if typed=="cheap" && context.cluster.prefered_storage.fs_cheap_readWriteOnce != () {
scs.find(|s| s.name == context.cluster.prefered_storage.fs_cheap_readWriteOnce && s.volumeMode == "Filesystem")
} else if context.cluster.prefered_storage.fs_readWriteOnce != () {
scs.find(|s| s.name == context.cluster.prefered_storage.fs_readWriteOnce && s.volumeMode == "Filesystem")
} else {
scs.find(|s| s.is_default)
}
}
fn for_deployments(context) {
for_deployments(context, ());
}

fn for_statefulsets(context, typed) {
let scs = context.cluster.storage_classes;
if typed=="fast" && context.cluster.prefered_storage.fs_fast_readWriteOnce != () {
scs.find(|s| s.name == context.cluster.prefered_storage.fs_fast_readWriteOnce && s.volumeMode == "Filesystem")
} else if typed=="cheap" && context.cluster.prefered_storage.fs_cheap_readWriteOnce != () {
scs.find(|s| s.name == context.cluster.prefered_storage.fs_cheap_readWriteOnce && s.volumeMode == "Filesystem")
} else if context.cluster.prefered_storage.fs_readWriteOnce != () {
scs.find(|s| s.name == context.cluster.prefered_storage.fs_readWriteOnce && s.volumeMode == "Filesystem")
} else {
scs.find(|s| s.is_default)
}
}
fn for_statefulsets(context) {
for_statefulsets(context, ());
}

fn for_vm(context) {
let scs = context.cluster.storage_classes;
if context.cluster.prefered_storage.block_readWriteMany != () {
scs.find(|s| s.name == context.cluster.prefered_storage.block_readWriteMany && s.volumeMode == "Block")
} else if context.cluster.prefered_storage.block_readWriteOnce != () {
scs.find(|s| s.name == context.cluster.prefered_storage.block_readWriteOnce && s.volumeMode == "Block")
} else {
scs.find(|s| s.is_default)
}
}

0 comments on commit ee7c037

Please sign in to comment.