Skip to content

Commit

Permalink
feat(shulker-operator): support volume and volume mounts on proxy fle…
Browse files Browse the repository at this point in the history
…ets (#470)

* feat(shulker-crds): add volume and volume mounts overrides to proxy fleet

* feat(shulker-operator): inject volume and volume mounts overrides to proxy fleets

* fix(shulker-operator): properly use image overrides

* chore: fix version drift between protobuf and buf failing builds
  • Loading branch information
jeremylvln authored Mar 21, 2024
1 parent 90f15b5 commit 90b93a1
Show file tree
Hide file tree
Showing 17 changed files with 1,722 additions and 124 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,9 @@ jobs:

- name: Run clippy
run: cargo clippy --all-features --message-format=json -- --deny warnings | clippy-sarif | tee rust-clippy-results.sarif | sarif-fmt
continue-on-error: true

- name: Run detekt
run: ./gradlew detektAll
continue-on-error: true

- name: Upload Clippy analysis results to GitHub
uses: github/codeql-action/upload-sarif@v3
Expand Down
2 changes: 2 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#!/usr/bin/env bash

npx --no -- commitlint --edit ${1}
9 changes: 9 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
#!/usr/bin/env bash

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
137 changes: 106 additions & 31 deletions kube/helm/templates/crds/shulkermc.io_minecraftserverfleets.yaml

Large diffs are not rendered by default.

134 changes: 105 additions & 29 deletions kube/helm/templates/crds/shulkermc.io_minecraftservers.yaml

Large diffs are not rendered by default.

1,209 changes: 1,195 additions & 14 deletions kube/helm/templates/crds/shulkermc.io_proxyfleets.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"test": "nx run-many --target=test --all",
"format": "prettier --ignore-unknown --write '**/*'",
"lint": "nx run-many --target=lint --all",
"proto:build": "nx run-many --target=buf --all"
"proto:build": "nx run-many --target=buf --all --skipNxCache"
},
"devDependencies": {
"@bufbuild/buf": "1.29.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/google-agones-sdk/buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ plugins:
- extern_path=.google.protobuf=::pbjson_types
- server_mod_attribute=.=#[cfg(feature = "server")]
- client_mod_attribute=.=#[cfg(feature = "client")]
- plugin: buf.build/protocolbuffers/java
- plugin: buf.build/protocolbuffers/java:v26.0
out: bindings/java/src/generated/java
- plugin: buf.build/grpc/java:v1.59.0
- plugin: buf.build/grpc/java:v1.62.2
out: bindings/java/src/generated/grpc
4 changes: 2 additions & 2 deletions packages/google-open-match-sdk/buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ plugins:
- extern_path=.google.protobuf=::pbjson_types
- server_mod_attribute=.=#[cfg(feature = "server")]
- client_mod_attribute=.=#[cfg(feature = "client")]
- plugin: buf.build/protocolbuffers/java
- plugin: buf.build/protocolbuffers/java:v26.0
out: bindings/java/src/generated/java
- plugin: buf.build/grpc/java:v1.59.0
- plugin: buf.build/grpc/java:v1.62.2
out: bindings/java/src/generated/grpc
33 changes: 7 additions & 26 deletions packages/shulker-crds/src/schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,21 @@ pub struct TemplateSpec<T> {
pub spec: T,
}

#[derive(Deserialize, Serialize, Clone, Debug, JsonSchema)]
#[derive(Deserialize, Serialize, Clone, Debug, JsonSchema, Default)]
#[serde(rename_all = "camelCase")]
pub struct ImageOverrideSpec {
/// Complete name of the image, including the repository name
/// and tag
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,

/// Policy about when to pull the image
#[schemars(default = "ImageOverrideSpec::default_pull_policy")]
#[schemars(schema_with = "ImageOverrideSpec::schema_pull_policy")]
pub pull_policy: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub pull_policy: Option<String>,

/// A list of secrets to use to pull the image
pub image_pull_secrets: Vec<k8s_openapi::api::core::v1::LocalObjectReference>,
}

#[cfg(not(tarpaulin_include))]
impl ImageOverrideSpec {
fn default_pull_policy() -> String {
"IfNotPresent".to_string()
}

fn schema_pull_policy(_: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
schemars::schema::SchemaObject {
instance_type: Some(schemars::schema::InstanceType::String.into()),
enum_values: Some(vec![
serde_json::Value::String("Always".to_string()),
serde_json::Value::String("Never".to_string()),
serde_json::Value::String("IfNotPresent".to_string()),
]),
..Default::default()
}
.into()
}
#[serde(skip_serializing_if = "Option::is_none")]
pub image_pull_secrets: Option<Vec<k8s_openapi::api::core::v1::LocalObjectReference>>,
}

#[derive(Deserialize, Serialize, Clone, Debug, JsonSchema)]
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 @@ -198,6 +198,12 @@ pub struct ProxyFleetTemplatePodOverridesSpec {
/// Name of the ServiceAccount to use
#[serde(skip_serializing_if = "Option::is_none")]
pub service_account_name: Option<String>,

/// Extra volumesmounts to add to the created `Pod`
pub volume_mounts: Option<Vec<k8s_openapi::api::core::v1::VolumeMount>>,

/// Extra volumes to add to the created `Pod`
pub volumes: Option<Vec<k8s_openapi::api::core::v1::Volume>>,
}

#[derive(Deserialize, Serialize, Clone, Debug, Default, JsonSchema)]
Expand Down
2 changes: 1 addition & 1 deletion packages/shulker-kube-utils/src/lease.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl LeaseLock {
} = lease.spec.as_ref().unwrap();

let utc_now = time::now();
let lease_duration = Duration::seconds(lease_duration_seconds.unwrap() as i64);
let lease_duration = Duration::try_seconds(lease_duration_seconds.unwrap() as i64).unwrap();

if let Some(MicroTime(time)) = renew_time {
let renew_expire = time.checked_add_signed(lease_duration).unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,16 @@ impl<'a> GameServerBuilder {

if let Some(pod_overrides) = &minecraft_server.spec.pod_overrides {
if let Some(image_overrides) = &pod_overrides.image {
pod_spec.containers[0].image = Some(image_overrides.name.clone());
pod_spec.containers[0].image_pull_policy =
Some(image_overrides.pull_policy.clone());
if let Some(name) = image_overrides.name.as_ref() {
pod_spec.containers[0].image = Some(name.clone())
}

if let Some(pull_policy) = image_overrides.pull_policy.as_ref() {
pod_spec.containers[0].image_pull_policy = Some(pull_policy.clone())
}

if let Some(image_pull_secrets) = pod_spec.image_pull_secrets.as_mut() {
image_pull_secrets.append(&mut image_overrides.image_pull_secrets.clone());
if let Some(image_pull_secrets) = image_overrides.image_pull_secrets.as_ref() {
pod_spec.image_pull_secrets = Some(image_pull_secrets.clone())
}
}

Expand Down Expand Up @@ -594,8 +598,10 @@ impl<'a> GameServerBuilder {

#[cfg(test)]
mod tests {
use k8s_openapi::api::core::v1::{EmptyDirVolumeSource, Volume, VolumeMount};
use shulker_crds::resourceref::ResourceRefSpec;
use k8s_openapi::api::core::v1::{
EmptyDirVolumeSource, LocalObjectReference, Volume, VolumeMount,
};
use shulker_crds::{resourceref::ResourceRefSpec, schemas::ImageOverrideSpec};
use shulker_kube_utils::reconcilers::builder::ResourceBuilder;

use crate::{
Expand Down Expand Up @@ -689,6 +695,78 @@ mod tests {
)
}

#[tokio::test]
async fn get_pod_template_spec_contains_image_override() {
// 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().image = Some(ImageOverrideSpec {
name: Some("my_better_image".to_string()),
..ImageOverrideSpec::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
assert_eq!(
pod_template.spec.as_ref().unwrap().containers[0].image,
Some("my_better_image".to_string())
);
}

#[tokio::test]
async fn get_pod_template_spec_contains_image_pull_secrets() {
// 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().image = Some(ImageOverrideSpec {
image_pull_secrets: Some(vec![LocalObjectReference {
name: Some("my_pull_secret".to_string()),
}]),
..ImageOverrideSpec::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
assert_eq!(
pod_template.spec.as_ref().unwrap().image_pull_secrets,
Some(vec![LocalObjectReference {
name: Some("my_pull_secret".to_string())
}])
);
}

#[tokio::test]
async fn get_pod_template_spec_contains_volume_mounts() {
// G
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ lazy_static! {
)])),
tolerations: None,
service_account_name: None,
volume_mounts: None,
volumes: None,
})
},
},
Expand Down
Loading

0 comments on commit 90b93a1

Please sign in to comment.