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

Support custom group label #31

Merged
merged 5 commits into from
Aug 14, 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
22 changes: 22 additions & 0 deletions charts/pod-director/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
{{- $labelKeyRegex := "^([a-z0-9]([-a-z0-9]*[a-z0-9])?\\.)*([a-z0-9]([-a-z0-9]*[a-z0-9])?\\/)?[a-z0-9]([-a-z0-9]*[a-z0-9])?$" }}
{{- $labelValueRegex := "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$" }}
{{- $groupLabel := .Values.config.groupLabel }}
{{- if $groupLabel }}
{{- if gt (len $groupLabel) 63 }}
{{- fail (printf "The group label '%s' is longer than the maximum length 63 for labels" $groupLabel) }}
{{- end }}
{{- if not (mustRegexMatch $labelKeyRegex $groupLabel) }}
{{- fail (printf "The group label '%s' is not a valid label" $groupLabel) }}
{{- end }}
{{- end }}
{{- range $groupName := keys .Values.config.groups }}
{{- if gt (len $groupName) 63 }}
{{- fail (printf "The group name '%s' is longer than the maximum length 63 for label values" $groupName) }}
{{- end }}
{{- if not (mustRegexMatch $labelValueRegex $groupName) }}
{{- fail (printf "The group name '%s' is not a valid label value and thus cannot be a group name" $groupName) }}
{{- end }}
{{- end }}
apiVersion: v1
kind: ConfigMap
metadata:
Expand All @@ -14,3 +33,6 @@ data:
{{- toYaml .Values.config.server | nindent 6 }}
groups:
{{- toYaml .Values.config.groups | nindent 6 }}
{{- with $groupLabel }}
groupLabel: {{ . | quote }}
{{- end }}
63 changes: 63 additions & 0 deletions charts/pod-director/tests/configmap_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
suite: configmap tests

templates:
- configmap.yaml

tests:
- it: given no configured group label then it must not be provided in the configuration file
set:
config:
filename: test-file.yaml
groupLabel: ""
asserts:
- notMatchRegex:
path: .data["test-file.yaml"]
pattern: 'groupLabel:'

- it: given a valid configured group label then it must be provided in the configuration file
set:
config:
filename: test-file.yaml
groupLabel: "some-label/value"
asserts:
- matchRegex:
path: .data["test-file.yaml"]
pattern: 'groupLabel: "some-label/value"'

- it: given a group label with length greater than 63 chars then templating should fail
set:
config:
groupLabel: "0123456789012345678901234567890123456789012345678901234567890123"
asserts:
- failedTemplate:
errorMessage: "The group label '0123456789012345678901234567890123456789012345678901234567890123' is longer than the maximum length 63 for labels"

- it: given a group label with an invalid name then should fail template
set:
config:
groupLabel: "not-valid-@-label"
asserts:
- failedTemplate:
errorMessage: "The group label 'not-valid-@-label' is not a valid label"

- it: given a group name with length greater than 63 chars then templating should fail
set:
config:
groups:
"0123456789012345678901234567890123456789012345678901234567890123":
nodeSelector:
role: "test"
asserts:
- failedTemplate:
errorMessage: "The group name '0123456789012345678901234567890123456789012345678901234567890123' is longer than the maximum length 63 for label values"

- it: given a group name that is an invalid label value then templating should fail
set:
config:
groups:
"SomeGroup@Name":
nodeSelector:
role: "test"
asserts:
- failedTemplate:
errorMessage: "The group name 'SomeGroup@Name' is not a valid label value and thus cannot be a group name"
4 changes: 4 additions & 0 deletions charts/pod-director/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ config:
# nodeSelector:
# kubernetes.io/os: "windows"

# Changes the group label that must be assigned to namespaces for Pod director to watch them
# If not supplied, the default "pod-director/group" label is used
groupLabel: ""

# Server configs, generally they do not need to be changed unless you have very specific requirements
server: {}
# bind_addr: 0.0.0.0
Expand Down
22 changes: 17 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,25 @@ pub enum Conflict {
Reject,
}

#[derive(Deserialize, Serialize, Default, Debug)]
#[derive(Deserialize, Serialize, Debug)]
#[cfg_attr(test, derive(PartialEq))]
#[serde(rename_all = "camelCase")]
pub struct Config {
pub groups: HashMap<String, GroupConfig>,
pub group_label: String,
pub server: ServerConfig,
}

impl Default for Config {
fn default() -> Self {
Self {
groups: Default::default(),
group_label: "pod-director/group".to_string(),
server: Default::default(),
}
}
}

#[derive(Deserialize, Serialize, Debug)]
#[cfg_attr(test, derive(PartialEq))]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -182,7 +194,7 @@ mod tests {
on_conflict: Conflict::Override,
});

assert_eq!(config, Config { groups, server: Default::default() });
assert_eq!(config, Config { groups, group_label: "pod-director/group".into(), server: Default::default() });

Ok(())
});
Expand Down Expand Up @@ -215,7 +227,7 @@ mod tests {
on_conflict: Default::default(),
});

assert_eq!(config, Config { groups, server: Default::default() });
assert_eq!(config, Config { groups, group_label: "pod-director/group".into(), server: Default::default() });

Ok(())
});
Expand All @@ -241,7 +253,7 @@ mod tests {
on_conflict: Default::default(),
});

assert_eq!(config, Config { groups, server: Default::default() });
assert_eq!(config, Config { groups, group_label: "pod-director/group".into(), server: Default::default() });

Ok(())
});
Expand All @@ -262,7 +274,7 @@ mod tests {
on_conflict: Default::default(),
});

assert_eq!(config, Config { groups, server: Default::default() });
assert_eq!(config, Config { groups, group_label: "pod-director/group".into(), server: Default::default() });

Ok(())
});
Expand Down
2 changes: 1 addition & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub async fn serve(config: Arc<Config>) -> Result<()> {
let shutdown_handle = Handle::new();
tokio::spawn(shutdown::graceful_shutdown(shutdown_handle.clone()));

let kubernetes = StandardKubernetesService::new().await?;
let kubernetes = StandardKubernetesService::new(&config.group_label).await?;
let app_state = StandardAppState::new(config.clone(), kubernetes);
let service = build_app(app_state).into_make_service();

Expand Down
8 changes: 4 additions & 4 deletions src/service/kubernetes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use axum::async_trait;
use k8s_openapi::api::core::v1::Namespace;
use kube::{Api, Client, ResourceExt};

const LABEL: &str = "pod-director/group";

#[async_trait]
pub trait KubernetesService: Send + Sync + Clone {
async fn namespace_group<S: AsRef<str> + Send + Sync>(&self, namespace: S) -> Result<Option<String>, kube::error::Error>;
Expand All @@ -12,12 +10,14 @@ pub trait KubernetesService: Send + Sync + Clone {
#[derive(Clone)]
pub struct StandardKubernetesService {
api: Api<Namespace>,
group_label: String,
}

impl StandardKubernetesService {
pub async fn new() -> anyhow::Result<Self> {
pub async fn new<S: AsRef<str>>(group_label: S) -> anyhow::Result<Self> {
Ok(StandardKubernetesService {
api: Api::all(Client::try_default().await?),
group_label: group_label.as_ref().to_string(),
})
}
}
Expand All @@ -28,7 +28,7 @@ impl KubernetesService for StandardKubernetesService {
async fn namespace_group<S: AsRef<str> + Send + Sync>(&self, namespace: S) -> Result<Option<String>, kube::error::Error> {
let namespace = self.api.get(namespace.as_ref()).await?;

let result = match namespace.labels().get(LABEL) {
let result = match namespace.labels().get(&self.group_label) {
None => None,
Some(s) => Some(s.to_string())
};
Expand Down
Loading