Skip to content

Commit

Permalink
moved MirrordPolicy to a separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
Razz4780 committed Dec 10, 2024
1 parent 473b65b commit 21a9f4c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 49 deletions.
49 changes: 1 addition & 48 deletions mirrord/operator/src/crd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use schemars::JsonSchema;
use semver::Version;
use serde::{Deserialize, Serialize};

use self::label_selector::LabelSelector;
#[cfg(feature = "client")]
use crate::client::error::OperatorApiError;
use crate::types::LicenseInfoOwned;

pub mod kafka;
pub mod kube_target;
pub mod label_selector;
pub mod policy;

pub const TARGETLESS_TARGET_NAME: &str = "targetless";

Expand Down Expand Up @@ -353,53 +353,6 @@ pub struct CopyTargetStatus {
pub creator_session: Session,
}

/// Features and operations that can be blocked by a `MirrordPolicy`.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize, JsonSchema)]
#[serde(rename_all = "kebab-case")] // StealWithoutFilter -> steal-without-filter in yaml.
pub enum BlockedFeature {
/// Blocks stealing traffic in any way (without or without filter).
Steal,

/// Blocks stealing traffic without specifying (any) filter. Client can still specify a
/// filter that matches anything.
StealWithoutFilter,

/// Blocks mirroring traffic.
Mirror,

/// So that the operator is able to list all policies with [`kube::Api`],
/// even if it doesn't recognize blocked features used in some of them.
#[schemars(skip)]
#[serde(other, skip_serializing)]
Unknown,
}

/// Custom resource for policies that limit what mirrord features users can use.
#[derive(CustomResource, Clone, Debug, Deserialize, Serialize, JsonSchema)]
#[kube(
// The operator group is handled by the operator, we want policies to be handled by k8s.
group = "policies.mirrord.metalbear.co",
version = "v1alpha",
kind = "MirrordPolicy",
namespaced
)]
#[serde(rename_all = "camelCase")] // target_path -> targetPath in yaml.
pub struct MirrordPolicySpec {
/// Specify the targets for which this policy applies, in the pod/my-pod deploy/my-deploy
/// notation. Targets can be matched using `*` and `?` where `?` matches exactly one
/// occurrence of any character and `*` matches arbitrary many (including zero) occurrences
/// of any character. If not specified, this policy does not depend on the target's path.
pub target_path: Option<String>,

/// If specified in a policy, the policy will only apply to targets with labels that match all
/// of the selector's rules.
pub selector: Option<LabelSelector>,

// TODO: make the k8s list type be set/map to prevent duplicates.
/// List of features and operations blocked by this policy.
pub block: Vec<BlockedFeature>,
}

/// Set where the application reads the name of the queue from, so that mirrord can find that queue,
/// split it, and temporarily change the name there to the name of the branch queue when splitting.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, JsonSchema)]
Expand Down
52 changes: 52 additions & 0 deletions mirrord/operator/src/crd/policy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use kube::CustomResource;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use super::label_selector::LabelSelector;

/// Features and operations that can be blocked by a `MirrordPolicy`.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize, JsonSchema)]
#[serde(rename_all = "kebab-case")] // StealWithoutFilter -> steal-without-filter in yaml.
pub enum BlockedFeature {
/// Blocks stealing traffic in any way (without or without filter).
Steal,

/// Blocks stealing traffic without specifying (any) filter. Client can still specify a
/// filter that matches anything.
StealWithoutFilter,

/// Blocks mirroring traffic.
Mirror,

/// So that the operator is able to list all policies with [`kube::Api`],
/// even if it doesn't recognize blocked features used in some of them.
#[schemars(skip)]
#[serde(other, skip_serializing)]
Unknown,
}

/// Custom resource for policies that limit what mirrord features users can use.
#[derive(CustomResource, Clone, Debug, Deserialize, Serialize, JsonSchema)]
#[kube(
// The operator group is handled by the operator, we want policies to be handled by k8s.
group = "policies.mirrord.metalbear.co",
version = "v1alpha",
kind = "MirrordPolicy",
namespaced
)]
#[serde(rename_all = "camelCase")] // target_path -> targetPath in yaml.
pub struct MirrordPolicySpec {
/// Specify the targets for which this policy applies, in the pod/my-pod deploy/my-deploy
/// notation. Targets can be matched using `*` and `?` where `?` matches exactly one
/// occurrence of any character and `*` matches arbitrary many (including zero) occurrences
/// of any character. If not specified, this policy does not depend on the target's path.
pub target_path: Option<String>,

/// If specified in a policy, the policy will only apply to targets with labels that match all
/// of the selector's rules.
pub selector: Option<LabelSelector>,

// TODO: make the k8s list type be set/map to prevent duplicates.
/// List of features and operations blocked by this policy.
pub block: Vec<BlockedFeature>,
}
3 changes: 2 additions & 1 deletion mirrord/operator/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ use thiserror::Error;

use crate::crd::{
kafka::{MirrordKafkaClientConfig, MirrordKafkaEphemeralTopic, MirrordKafkaTopicsConsumer},
MirrordOperatorUser, MirrordPolicy, MirrordSqsSession, MirrordWorkloadQueueRegistry, TargetCrd,
policy::MirrordPolicy,
MirrordOperatorUser, MirrordSqsSession, MirrordWorkloadQueueRegistry, TargetCrd,
};

pub static OPERATOR_NAME: &str = "mirrord-operator";
Expand Down

0 comments on commit 21a9f4c

Please sign in to comment.