Skip to content

Commit

Permalink
Update package
Browse files Browse the repository at this point in the history
  • Loading branch information
sebt3 committed Nov 14, 2024
1 parent 537d1cc commit 58814d7
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 144 deletions.
2 changes: 1 addition & 1 deletion agent/scripts/lib/gen_package.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn gen_system(path, docs) {
let yaml = yaml_decode(file_read(path+"/package.yaml"));
let name = if type_of(yaml["metadata"]) == "map" {yaml["metadata"]["name"]} else {()};
if name != () {
for doc in docs {
for doc in docs.filter(|obj| type_of(obj) == "map" && obj.keys().contains("kind") && obj.keys().contains("metadata") && type_of(obj.metadata) == "map") {
if doc.kind == "CustomResourceDefinition" {
create_dir(`${path}/get_crds`);
if "conversion" in doc.spec && doc.spec.conversion != () {
Expand Down
259 changes: 156 additions & 103 deletions agent/scripts/lib/install_from_dir.rhai
Original file line number Diff line number Diff line change
@@ -1,120 +1,173 @@
fn install(instance, context, dir, allow_cluster, force_ns) {
let hbs = new_hbs();
hbs.register_partial_dir(`${context.package_dir}/handlebars/partials`);
hbs.register_helper_dir(`${context.package_dir}/handlebars/helpers`);
let applied_objs = [];
for file in read_dir(dir) {
log_info(`Applying ${file}`);
let objects = if file.ends_with(".yaml") || file.ends_with(".yml") {
yaml_decode_multi(file_read(file))
} else if file.ends_with(".yaml.hbs") || file.ends_with(".yml.hbs") {
yaml_decode_multi(hbs.render_from(file_read(file), context))
} else {[]};
for obj in objects {
if obj.kind != () && obj.metadata != () {
let ns = if (force_ns) {
instance.metadata.namespace
} else {
if obj.metadata.namespace != () {obj.metadata.namespace} else {instance.metadata.namespace}
};
let api = k8s_resource(obj.kind, ns);
if api.exist() {
if force_ns && api.scope != "cluster" {
obj.metadata.namespace = ns;
}
if api.scope == "cluster" {
if allow_cluster {
api.apply(obj.metadata.name, obj);
applied_objs.push(#{
kind: obj.kind,
name: obj.metadata.name
});
} else {
log_warn(`Ignoring ${obj.kind} ${obj.metadata.name} as it is a clusterwide resource type.`);
}
} else {
api.apply(obj.metadata.name, obj);
applied_objs.push(#{
kind: obj.kind,
name: obj.metadata.name,
namespace: ns
});
}
} else {
log_debug(yaml_encode(obj));
throw `kind: ${obj.kind} is unknown from the apiServer.`;
}
} else {
const FIRST = [
"Namespace",
"ServiceAccount",
"ClusterRole",
"ClusterRoleBinding",
"Role",
"RoleBinding",
"Secret",
"ConfigMap",
"PersistentVolume",
"PersistentVolumeClaim",
];
const LAST = [
"Pod",
"Job",
"CronJob",
"DaemonSet",
"Deployment",
"ReplicaSet",
"StatefulSet",
"Service",
"Ingress",
"APIService",
"MutatingWebhookConfiguration",
"ValidatingAdmissionPolicy",
"ValidatingAdmissionPolicyBinding",
"ValidatingWebhookConfiguration",
];

fn get_objects(context, dir) {
let hbs = new_hbs();
hbs.register_partial_dir(`${context.package_dir}/handlebars/partials`);
hbs.register_helper_dir(`${context.package_dir}/handlebars/helpers`);
let ret = [];
for file in read_dir(dir) {
let objects = if file.ends_with(".yaml") || file.ends_with(".yml") {
yaml_decode_multi(file_read(file))
} else if file.ends_with(".yaml.hbs") || file.ends_with(".yml.hbs") {
yaml_decode_multi(hbs.render_from(file_read(file), context))
} else {[]};
for obj in objects.filter(|obj| type_of(obj) != "map" || ! obj.keys().contains("kind") || ! obj.keys().contains("metadata") || type_of(obj.metadata) != "map") {
log_warn(`No kind for an object in file ${file}`);
log_debug(yaml_encode(obj));
}
ret += objects.filter(|obj| type_of(obj) == "map" && obj.keys().contains("kind") && obj.keys().contains("metadata") && type_of(obj.metadata) == "map");
}
ret
}
applied_objs
}

fn install_allowed(instance, context, dir, allowed) {
let hbs = new_hbs();
hbs.register_partial_dir(`${context.package_dir}/handlebars/partials`);
hbs.register_helper_dir(`${context.package_dir}/handlebars/helpers`);
let applied_objs = [];
for file in read_dir(dir) {
log_info(`Applying ${file}`);
let objects = if file.ends_with(".yaml") || file.ends_with(".yml") {
yaml_decode_multi(file_read(file))
} else if file.ends_with(".yaml.hbs") || file.ends_with(".yml.hbs") {
yaml_decode_multi(hbs.render_from(file_read(file), context))
} else {[]};
for obj in objects {
if obj.kind != () && obj.metadata != () {
let ns = if (obj.metadata.namespace != () && obj.metadata.namespace in allowed) {
obj.metadata.namespace
} else {
instance.metadata.namespace
};
let api = k8s_resource(obj.kind, ns);
if api.exist() {
if api.scope != "cluster" {
obj.metadata.namespace = ns;
api.apply(obj.metadata.name, obj);
applied_objs.push(#{
kind: obj.kind,
name: obj.metadata.name,
namespace: ns
});
} else {
log_warn(`Ignoring ${obj.kind} ${obj.metadata.name} as it is a clusterwide resource type.`);
}
fn install_obj(obj, allow_cluster, namespace, force_ns) {
let applied = [];
let ns = if (force_ns) {
namespace
} else {
if obj.metadata.namespace != () {obj.metadata.namespace} else {namespace}
};
let api = k8s_resource(obj.kind, ns);
if api.exist() {
if force_ns && api.scope != "cluster" {
obj.metadata.namespace = ns;
}
if api.scope == "cluster" {
if allow_cluster {
log_info(`Applying ${obj.kind} ${obj.metadata.name}`);
api.apply(obj.metadata.name, obj);
applied.push(#{
kind: obj.kind,
name: obj.metadata.name
});
} else {
log_debug(yaml_encode(obj));
throw `kind: ${obj.kind} is unknown from the apiServer.`;
log_warn(`Ignoring ${obj.kind} ${obj.metadata.name} as it is a clusterwide resource type.`);
}
} else {
log_warn(`No kind for an object in file ${file}`);
log_debug(yaml_encode(obj));
log_info(`Applying ${obj.kind} ${ns}/${obj.metadata.name}`);
api.apply(obj.metadata.name, obj);
applied.push(#{
kind: obj.kind,
name: obj.metadata.name,
namespace: ns
});
}
} else {
log_debug(yaml_encode(obj));
throw `kind: ${obj.kind} is unknown from the apiServer.`;
}
applied
}
fn install(instance, context, dir, allow_cluster, force_ns) {
let all = get_objects(context, dir);
let applied_objs = [];
for k in global::FIRST {
for obj in all.filter(|o| o.kind==k) {
applied_objs += install_obj(obj, allow_cluster, instance.metadata.namespace, force_ns);
}
}
for obj in all.filter(|o| !global::FIRST.contains(o.kind) && !global::LAST.contains(o.kind)) {
applied_objs += install_obj(obj, allow_cluster, instance.metadata.namespace, force_ns);
}
for k in global::LAST {
for obj in all.filter(|o| o.kind==k) {
applied_objs += install_obj(obj, allow_cluster, instance.metadata.namespace, force_ns);
}
}
applied_objs
}
applied_objs
}

fn remove(applied, previous, default_ns) {
let deleted = [];
for old in previous {
if ! applied.some(|n| n.kind == old.kind && n.namespace == old.namespace && n.name == old.name) {
let ns = if old.namespace != () {old.namespace} else {default_ns};
let api = k8s_resource(old.kind, ns);
if api.exist() {
try {
let o = api.get_obj(old.name);
o.delete();
deleted.push(o);
} catch(e) {}
fn install_allowed_obj(obj, allowed, namespace) {
let applied = [];
let ns = if (obj.metadata.namespace != () && obj.metadata.namespace in allowed) {
obj.metadata.namespace
} else {
namespace
};
let api = k8s_resource(obj.kind, ns);
if api.exist() {
if api.scope != "cluster" {
obj.metadata.namespace = ns;
log_info(`Applying ${obj.kind} ${ns}/${obj.metadata.name}`);
api.apply(obj.metadata.name, obj);
applied.push(#{
kind: obj.kind,
name: obj.metadata.name,
namespace: ns
});
} else {
log_warn(`Ignoring ${obj.kind} ${obj.metadata.name} as it is a clusterwide resource type.`);
}
} else {
log_debug(yaml_encode(obj));
throw `kind: ${obj.kind} is unknown from the apiServer.`;
}
applied
}
for d in deleted {
log_info(`Waiting for ${d.metadata.name} to be deleted`);
d.wait_deleted(60*5);

fn install_allowed(instance, context, dir, allowed) {
let all = get_objects(context, dir);
let applied_objs = [];
for k in global::FIRST {
for obj in all.filter(|o| o.kind==k) {
applied_objs += install_allowed_obj(obj, allowed, instance.metadata.namespace);
}
}
for obj in all.filter(|o| !global::FIRST.contains(o.kind) && !global::LAST.contains(o.kind)) {
applied_objs += install_allowed_obj(obj, allowed, instance.metadata.namespace);
}
for k in global::LAST {
for obj in all.filter(|o| o.kind==k) {
applied_objs += install_allowed_obj(obj, allowed, instance.metadata.namespace);
}
}
applied_objs
}
}

fn remove(applied, previous, default_ns) {
let deleted = [];
for old in previous {
if ! applied.some(|n| n.kind == old.kind && n.namespace == old.namespace && n.name == old.name) {
let ns = if old.namespace != () {old.namespace} else {default_ns};
let api = k8s_resource(old.kind, ns);
if api.exist() {
try {
let o = api.get_obj(old.name);
o.delete();
deleted.push(o);
} catch(e) {}
}
}
}
for d in deleted {
log_info(`Waiting for ${d.metadata.name} to be deleted`);
d.wait_deleted(60*5);
}
}
2 changes: 1 addition & 1 deletion box/vynil/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
apiVersion: vinyl.solidite.fr/v1beta1
kind: Package
metadata:
type: system
name: vynil
category: core
description: Vynil controller to manage vynil packages installations
type: system
features:
- upgrade
- auto_config
Expand Down
4 changes: 0 additions & 4 deletions box/vynil/systems/cm.yaml

This file was deleted.

Loading

0 comments on commit 58814d7

Please sign in to comment.