Skip to content

Commit

Permalink
feat(shulker-operator): support additional ports in overrides (#485)
Browse files Browse the repository at this point in the history
* feat(shulker-crds): add ports in pod overrides

* feat(shulker-operator): inject extra ports
  • Loading branch information
jeremylvln authored Mar 25, 2024
1 parent 0b46ed5 commit 39c5b81
Show file tree
Hide file tree
Showing 14 changed files with 204 additions and 17 deletions.
7 changes: 0 additions & 7 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,4 @@
node scripts/generate_codecov_config.cjs
git add codecov.yml scripts/upload_codecov_files.sh

files_modified=$(git diff --cached --name-only --diff-filter=ACM)
if [[ $files_modified == *"packages/shulker-crds/src"* ]]; then
echo "Regenerating CRDs because package was modified..."
cargo run --bin crdgen
git add "kube/helm/**/crds/*.yaml"
fi

npx --no -- lint-staged
27 changes: 27 additions & 0 deletions kube/helm/templates/crds/shulkermc.io_minecraftserverfleets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,33 @@ spec:
description: Node selector to be applied on created `Pod`
nullable: true
type: object
ports:
description: Extra ports to add to the created `Pod`'s main container
items:
description: ContainerPort represents a network port in a single container.
properties:
containerPort:
description: Number of port to expose on the pod's IP address. This must be a valid port number, 0 < x < 65536.
format: int32
type: integer
hostIP:
description: What host IP to bind the external port to.
type: string
hostPort:
description: Number of port to expose on the host. If specified, this must be a valid port number, 0 < x < 65536. If HostNetwork is specified, this must match ContainerPort. Most containers do not need this.
format: int32
type: integer
name:
description: If specified, this must be an IANA_SVC_NAME and unique within the pod. Each named port in a pod must have a unique name. Name for the port that can be referred to by services.
type: string
protocol:
description: Protocol for port. Must be UDP, TCP, or SCTP. Defaults to "TCP".
type: string
required:
- containerPort
type: object
nullable: true
type: array
resources:
description: The desired compute resource requirements of the created `Pod`
nullable: true
Expand Down
27 changes: 27 additions & 0 deletions kube/helm/templates/crds/shulkermc.io_minecraftservers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,33 @@ spec:
description: Node selector to be applied on created `Pod`
nullable: true
type: object
ports:
description: Extra ports to add to the created `Pod`'s main container
items:
description: ContainerPort represents a network port in a single container.
properties:
containerPort:
description: Number of port to expose on the pod's IP address. This must be a valid port number, 0 < x < 65536.
format: int32
type: integer
hostIP:
description: What host IP to bind the external port to.
type: string
hostPort:
description: Number of port to expose on the host. If specified, this must be a valid port number, 0 < x < 65536. If HostNetwork is specified, this must match ContainerPort. Most containers do not need this.
format: int32
type: integer
name:
description: If specified, this must be an IANA_SVC_NAME and unique within the pod. Each named port in a pod must have a unique name. Name for the port that can be referred to by services.
type: string
protocol:
description: Protocol for port. Must be UDP, TCP, or SCTP. Defaults to "TCP".
type: string
required:
- containerPort
type: object
nullable: true
type: array
resources:
description: The desired compute resource requirements of the created `Pod`
nullable: true
Expand Down
27 changes: 27 additions & 0 deletions kube/helm/templates/crds/shulkermc.io_proxyfleets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,33 @@ spec:
description: Node selector to be applied on created `Pod`
nullable: true
type: object
ports:
description: Extra ports to add to the created `Pod`'s main container
items:
description: ContainerPort represents a network port in a single container.
properties:
containerPort:
description: Number of port to expose on the pod's IP address. This must be a valid port number, 0 < x < 65536.
format: int32
type: integer
hostIP:
description: What host IP to bind the external port to.
type: string
hostPort:
description: Number of port to expose on the host. If specified, this must be a valid port number, 0 < x < 65536. If HostNetwork is specified, this must match ContainerPort. Most containers do not need this.
format: int32
type: integer
name:
description: If specified, this must be an IANA_SVC_NAME and unique within the pod. Each named port in a pod must have a unique name. Name for the port that can be referred to by services.
type: string
protocol:
description: Protocol for port. Must be UDP, TCP, or SCTP. Defaults to "TCP".
type: string
required:
- containerPort
type: object
nullable: true
type: array
resources:
description: The desired compute resource requirements of the created `Pod`
nullable: true
Expand Down
8 changes: 8 additions & 0 deletions lint-staged.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
'*': 'prettier --ignore-unknown --write',
'*.rs': () => 'cargo fmt',
'packages/shulker-crds/src/**/*.rs': () => [
'cargo run --bin crdgen',
'git add "kube/helm/**/crds/*.yaml"',
],
};
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,5 @@
"workspaces": [
"packages/*",
"docs"
],
"lint-staged": {
"*": "prettier --ignore-unknown --write"
}
]
}
6 changes: 6 additions & 0 deletions packages/shulker-crds/src/v1alpha1/minecraft_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,16 @@ pub struct MinecraftServerPodOverridesSpec {
pub service_account_name: Option<String>,

/// Extra volumesmounts to add to the created `Pod`
#[serde(skip_serializing_if = "Option::is_none")]
pub volume_mounts: Option<Vec<k8s_openapi::api::core::v1::VolumeMount>>,

/// Extra volumes to add to the created `Pod`
#[serde(skip_serializing_if = "Option::is_none")]
pub volumes: Option<Vec<k8s_openapi::api::core::v1::Volume>>,

/// Extra ports to add to the created `Pod`'s main container
#[serde(skip_serializing_if = "Option::is_none")]
pub ports: Option<Vec<k8s_openapi::api::core::v1::ContainerPort>>,
}

/// The status object of `MinecraftServer`
Expand Down
6 changes: 6 additions & 0 deletions packages/shulker-crds/src/v1alpha1/proxy_fleet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,16 @@ pub struct ProxyFleetTemplatePodOverridesSpec {
pub service_account_name: Option<String>,

/// Extra volumesmounts to add to the created `Pod`
#[serde(skip_serializing_if = "Option::is_none")]
pub volume_mounts: Option<Vec<k8s_openapi::api::core::v1::VolumeMount>>,

/// Extra volumes to add to the created `Pod`
#[serde(skip_serializing_if = "Option::is_none")]
pub volumes: Option<Vec<k8s_openapi::api::core::v1::Volume>>,

/// Extra ports to add to the created `Pod`'s main container
#[serde(skip_serializing_if = "Option::is_none")]
pub ports: Option<Vec<k8s_openapi::api::core::v1::ContainerPort>>,
}

#[derive(Deserialize, Serialize, Clone, Debug, Default, JsonSchema)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ impl<'a> ResourceBuilder<'a> for MinecraftServerRoleBuilder {
PolicyRule {
api_groups: Some(vec!["".to_string()]),
resources: Some(vec!["events".to_string()]),
verbs: vec![
"create".to_string(),
"patch".to_string()
],
verbs: vec!["create".to_string(), "patch".to_string()],
..PolicyRule::default()
},
PolicyRule {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ lazy_static! {
service_account_name: None,
volume_mounts: None,
volumes: None,
ports: None,
})
},
status: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ impl<'a> GameServerBuilder {
.unwrap()
.append(&mut volumes_override.clone());
}

if let Some(ports_overrides) = &pod_overrides.ports {
pod_spec.containers[0]
.ports
.as_mut()
.unwrap()
.append(&mut ports_overrides.clone());
}
}

let mut pod_labels = minecraft_server.labels().clone();
Expand Down Expand Up @@ -601,7 +609,7 @@ impl<'a> GameServerBuilder {
#[cfg(test)]
mod tests {
use k8s_openapi::api::core::v1::{
EmptyDirVolumeSource, LocalObjectReference, Volume, VolumeMount,
Container, ContainerPort, EmptyDirVolumeSource, LocalObjectReference, Volume, VolumeMount,
};
use shulker_crds::{resourceref::ResourceRefSpec, schemas::ImageOverrideSpec};
use shulker_kube_utils::reconcilers::builder::ResourceBuilder;
Expand Down Expand Up @@ -848,6 +856,44 @@ mod tests {
assert!(extra_volume.is_some());
}

#[tokio::test]
async fn get_pod_template_spec_contains_ports() {
// G
let client = create_client_mock();
let resourceref_resolver = ResourceRefResolver::new(client);
let mut server = TEST_SERVER.clone();
server.spec.pod_overrides.as_mut().unwrap().ports = Some(vec![ContainerPort {
name: Some("metrics".to_owned()),
container_port: 9090,
..ContainerPort::default()
}]);
let context = super::GameServerBuilderContext {
cluster: &TEST_CLUSTER,
agent_config: &AgentConfig {
maven_repository: constants::SHULKER_PLUGIN_REPOSITORY.to_string(),
version: constants::SHULKER_PLUGIN_VERSION.to_string(),
},
};

// W
let pod_template = super::GameServerBuilder::get_pod_template_spec(
&resourceref_resolver,
&context,
&server,
)
.await
.unwrap();

// T
let extra_port = pod_template.spec.as_ref().unwrap().containers[0]
.ports
.as_ref()
.unwrap()
.iter()
.find(|port| port.name == Some("metrics".to_owned()));
assert!(extra_port.is_some());
}

#[tokio::test]
async fn get_init_env_contains_world() {
// G
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ lazy_static! {
service_account_name: None,
volume_mounts: None,
volumes: None,
ports: None,
})
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ lazy_static! {
service_account_name: None,
volume_mounts: None,
volumes: None,
ports: None,
})
},
},
Expand Down
52 changes: 51 additions & 1 deletion packages/shulker-operator/src/reconcilers/proxy_fleet/fleet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,14 @@ impl<'a> FleetBuilder {
.unwrap()
.append(&mut volumes_override.clone());
}

if let Some(ports_overrides) = &pod_overrides.ports {
pod_spec.containers[0]
.ports
.as_mut()
.unwrap()
.append(&mut ports_overrides.clone());
}
}

let mut pod_labels =
Expand Down Expand Up @@ -619,7 +627,7 @@ impl<'a> FleetBuilder {
#[cfg(test)]
mod tests {
use k8s_openapi::api::core::v1::{
EmptyDirVolumeSource, LocalObjectReference, Volume, VolumeMount,
ContainerPort, EmptyDirVolumeSource, LocalObjectReference, Volume, VolumeMount,
};
use shulker_crds::schemas::ImageOverrideSpec;
use shulker_kube_utils::reconcilers::builder::ResourceBuilder;
Expand Down Expand Up @@ -980,6 +988,48 @@ mod tests {
assert!(extra_volume.is_some());
}

#[tokio::test]
async fn get_pod_template_spec_contains_ports() {
// G
let client = create_client_mock();
let builder = super::FleetBuilder::new(client);
let mut proxy_fleet = TEST_PROXY_FLEET.clone();
proxy_fleet
.spec
.template
.spec
.pod_overrides
.as_mut()
.unwrap()
.ports = Some(vec![ContainerPort {
name: Some("metrics".to_string()),
container_port: 9090,
..ContainerPort::default()
}]);
let context = super::FleetBuilderContext {
cluster: &TEST_CLUSTER,
agent_config: &AgentConfig {
maven_repository: constants::SHULKER_PLUGIN_REPOSITORY.to_string(),
version: constants::SHULKER_PLUGIN_VERSION.to_string(),
},
};

// W
let pod_template = builder
.get_pod_template_spec(&context, &proxy_fleet)
.await
.unwrap();

// T
let extra_port = pod_template.spec.as_ref().unwrap().containers[0]
.ports
.as_ref()
.unwrap()
.iter()
.find(|port| port.name == Some("metrics".to_owned()));
assert!(extra_port.is_some());
}

#[tokio::test]
async fn get_init_env_contains_plugins() {
// G
Expand Down

0 comments on commit 39c5b81

Please sign in to comment.