|
| 1 | +use std::collections::BTreeMap; |
| 2 | + |
| 3 | +use k8s_openapi::{ |
| 4 | + api::{ |
| 5 | + apps::v1::{DaemonSet, DaemonSetSpec}, |
| 6 | + core::v1::{ |
| 7 | + Capabilities, ConfigMap, ConfigMapVolumeSource, Container, ContainerPort, EnvVar, |
| 8 | + HostPathVolumeSource, PodSpec, PodTemplateSpec, SecurityContext, Volume, VolumeMount, |
| 9 | + }, |
| 10 | + }, |
| 11 | + apimachinery::pkg::apis::meta::v1::LabelSelector, |
| 12 | +}; |
| 13 | +use kube::{CustomResource, Resource, ResourceExt, api::ObjectMeta}; |
| 14 | +use schemars::JsonSchema; |
| 15 | +use serde::{Deserialize, Serialize}; |
| 16 | + |
| 17 | +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, CustomResource)] |
| 18 | +#[serde(rename_all = "camelCase")] |
| 19 | +#[kube( |
| 20 | + group = "fact.stackrox.io", |
| 21 | + version = "v1alpha1", |
| 22 | + kind = "Fact", |
| 23 | + namespaced |
| 24 | +)] |
| 25 | +pub(crate) struct FactSpec { |
| 26 | + pub image: String, |
| 27 | + #[serde(default = "default_log_level")] |
| 28 | + pub log_level: String, |
| 29 | + #[serde(default = "default_rate_limit")] |
| 30 | + pub rate_limit: u64, |
| 31 | +} |
| 32 | + |
| 33 | +fn default_log_level() -> String { |
| 34 | + String::from("info") |
| 35 | +} |
| 36 | + |
| 37 | +fn default_rate_limit() -> u64 { |
| 38 | + 0 |
| 39 | +} |
| 40 | + |
| 41 | +pub(crate) fn build_configmap(fact: &Fact) -> ConfigMap { |
| 42 | + let data = BTreeMap::from([( |
| 43 | + "fact.yml".into(), |
| 44 | + format!("rate_limit: {}", fact.spec.rate_limit), |
| 45 | + )]); |
| 46 | + ConfigMap { |
| 47 | + data: Some(data), |
| 48 | + metadata: ObjectMeta { |
| 49 | + name: Some(format!("{}-config", fact.name_any())), |
| 50 | + namespace: fact.namespace(), |
| 51 | + owner_references: fact.controller_owner_ref(&()).map(|r| vec![r]), |
| 52 | + ..Default::default() |
| 53 | + }, |
| 54 | + ..Default::default() |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +pub(crate) fn build_daemonset(fact: &Fact) -> DaemonSet { |
| 59 | + let spec = &fact.spec; |
| 60 | + let name = fact.name_any(); |
| 61 | + let namespace = fact.namespace(); |
| 62 | + let labels = BTreeMap::from([("app".into(), "fact".into())]); |
| 63 | + |
| 64 | + let metadata = ObjectMeta { |
| 65 | + labels: Some(labels.clone()), |
| 66 | + name: Some(name), |
| 67 | + namespace, |
| 68 | + owner_references: fact.controller_owner_ref(&()).map(|r| vec![r]), |
| 69 | + ..Default::default() |
| 70 | + }; |
| 71 | + |
| 72 | + let container = Container { |
| 73 | + name: "fact".into(), |
| 74 | + image: Some(spec.image.clone()), |
| 75 | + image_pull_policy: Some("IfNotPresent".into()), |
| 76 | + args: Some(vec![ |
| 77 | + "--paths".into(), |
| 78 | + "/etc:/bin:/sbin:/usr/bin:/usr/sbin".into(), |
| 79 | + ]), |
| 80 | + ports: Some(vec![ContainerPort { |
| 81 | + container_port: 9000, |
| 82 | + name: Some("monitoring".into()), |
| 83 | + ..Default::default() |
| 84 | + }]), |
| 85 | + env: Some(vec![ |
| 86 | + EnvVar { |
| 87 | + name: "FACT_LOGLEVEL".into(), |
| 88 | + value: Some(spec.log_level.clone()), |
| 89 | + ..Default::default() |
| 90 | + }, |
| 91 | + EnvVar { |
| 92 | + name: "FACT_HOST_MOUNT".into(), |
| 93 | + value: Some("/host".into()), |
| 94 | + ..Default::default() |
| 95 | + }, |
| 96 | + ]), |
| 97 | + security_context: Some(SecurityContext { |
| 98 | + capabilities: Some(Capabilities { |
| 99 | + drop: Some(vec!["NET_RAW".into()]), |
| 100 | + ..Default::default() |
| 101 | + }), |
| 102 | + privileged: Some(true), |
| 103 | + read_only_root_filesystem: Some(true), |
| 104 | + ..Default::default() |
| 105 | + }), |
| 106 | + volume_mounts: Some(vec![ |
| 107 | + VolumeMount { |
| 108 | + name: "root-ro".into(), |
| 109 | + mount_path: "/host".into(), |
| 110 | + read_only: Some(true), |
| 111 | + mount_propagation: Some("HostToContainer".into()), |
| 112 | + ..Default::default() |
| 113 | + }, |
| 114 | + VolumeMount { |
| 115 | + mount_path: "/etc/stackrox".into(), |
| 116 | + name: "fact-config".into(), |
| 117 | + read_only: Some(true), |
| 118 | + ..Default::default() |
| 119 | + }, |
| 120 | + ]), |
| 121 | + ..Default::default() |
| 122 | + }; |
| 123 | + |
| 124 | + let volumes = vec![ |
| 125 | + Volume { |
| 126 | + host_path: Some(HostPathVolumeSource { |
| 127 | + path: "/".into(), |
| 128 | + ..Default::default() |
| 129 | + }), |
| 130 | + name: "root-ro".into(), |
| 131 | + ..Default::default() |
| 132 | + }, |
| 133 | + Volume { |
| 134 | + name: "fact-config".into(), |
| 135 | + config_map: Some(ConfigMapVolumeSource { |
| 136 | + name: format!("{}-config", fact.name_any()), |
| 137 | + ..Default::default() |
| 138 | + }), |
| 139 | + ..Default::default() |
| 140 | + }, |
| 141 | + ]; |
| 142 | + |
| 143 | + let spec = DaemonSetSpec { |
| 144 | + selector: LabelSelector { |
| 145 | + match_labels: Some(labels.clone()), |
| 146 | + ..Default::default() |
| 147 | + }, |
| 148 | + template: PodTemplateSpec { |
| 149 | + metadata: Some(ObjectMeta { |
| 150 | + labels: Some(labels), |
| 151 | + ..Default::default() |
| 152 | + }), |
| 153 | + spec: Some(PodSpec { |
| 154 | + containers: vec![container], |
| 155 | + volumes: Some(volumes), |
| 156 | + ..Default::default() |
| 157 | + }), |
| 158 | + }, |
| 159 | + ..Default::default() |
| 160 | + }; |
| 161 | + |
| 162 | + DaemonSet { |
| 163 | + metadata, |
| 164 | + spec: Some(spec), |
| 165 | + ..Default::default() |
| 166 | + } |
| 167 | +} |
0 commit comments