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

Add scope to the Resource trait #25

Merged
merged 1 commit into from
Dec 19, 2023
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
54 changes: 51 additions & 3 deletions k8s-pb-codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,54 @@ fn main() -> Result<()> {

fn append_trait_def(lib_rs: &mut File) {
let tokens = quote! {
/// The scope of a [`Resource`].
pub trait ResourceScope {}

/// Indicates that a [`Resource`] is cluster-scoped.
pub struct ClusterResourceScope {}
impl ResourceScope for ClusterResourceScope {}

/// Indicates that a [`Resource`] is namespace-scoped.
pub struct NamespaceResourceScope {}
impl ResourceScope for NamespaceResourceScope {}

/// Indicates that a [`Resource`] is neither cluster-scoped nor namespace-scoped.
pub struct SubResourceScope {}
impl ResourceScope for SubResourceScope {}

/// A trait applied to all Kubernetes resources.
pub trait Resource {
/// The API version of the resource. This is a composite of [`Resource::GROUP`] and [`Resource::VERSION`] (eg `"apiextensions.k8s.io/v1beta1"`)
/// or just the version for resources without a group (eg `"v1"`).
///
/// This is the string used in the `apiVersion` field of the resource's serialized form.
const API_VERSION: &'static str;

/// The group of the resource, or the empty string if the resource doesn't have a group.
const GROUP: &'static str;
const VERSION: &'static str;

/// The kind of the resource.
///
/// This is the string used in the `kind` field of the resource's serialized form.
const KIND: &'static str;
const NAME: &'static str;
/// The version of the resource.
const VERSION: &'static str;

/// The URL path segment used to construct URLs related to this resource.
///
/// For cluster- and namespaced-scoped resources, this is the plural name of the resource that is followed by the resource name.
/// For example, [`api::core::v1::Pod`](crate::api::core::v1::Pod)'s value is `"pods"` and its URLs look like `.../pods/{name}`.
///
/// For subresources, this is the subresource name that comes after the parent resource's name.
/// For example, [`api::authentication::v1::TokenRequest`](crate::api::authentication::v1::TokenRequest)'s value is `"token"`,
/// and its URLs look like `.../serviceaccounts/{name}/token`.
const URL_PATH_SEGMENT: &'static str;

/// Indicates whether the resource is namespace-scoped or cluster-scoped or a subresource.
///
/// If you need to restrict some generic code to resources of a specific scope, use this associated type to create a bound on the generic.
/// For example, `fn foo<T: k8s_openapi::Resource<Scope = k8s_openapi::ClusterResourceScope>>() { }` can only be called with cluster-scoped resources.
type Scope: ResourceScope;
}

pub trait HasMetadata {
Expand Down Expand Up @@ -165,13 +207,19 @@ fn append_trait_impl(pkg_rs: &mut File, message_name: &str, resource: &Resource)
let kind = &resource.kind;
let version = &resource.version;
let name = &resource.name;
let scope = if resource.namespaced {
format_ident!("NamespaceResourceScope")
} else {
format_ident!("ClusterResourceScope")
};
let tokens = quote! {
impl crate::Resource for #type_name {
const API_VERSION: &'static str = #api_version;
const GROUP: &'static str = #group;
const VERSION: &'static str = #version;
const KIND: &'static str = #kind;
const NAME: &'static str = #name;
const URL_PATH_SEGMENT: &'static str = #name;
type Scope = crate::#scope;
}
};
let tokens = if let Some(metadata) = &resource.metadata {
Expand Down
6 changes: 4 additions & 2 deletions k8s-pb/src/api/admissionregistration/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,8 @@ impl crate::Resource for MutatingWebhookConfiguration {
const GROUP: &'static str = "admissionregistration.k8s.io";
const VERSION: &'static str = "v1";
const KIND: &'static str = "MutatingWebhookConfiguration";
const NAME: &'static str = "mutatingwebhookconfigurations";
const URL_PATH_SEGMENT: &'static str = "mutatingwebhookconfigurations";
type Scope = crate::ClusterResourceScope;
}
impl crate::HasMetadata for MutatingWebhookConfiguration {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand All @@ -590,7 +591,8 @@ impl crate::Resource for ValidatingWebhookConfiguration {
const GROUP: &'static str = "admissionregistration.k8s.io";
const VERSION: &'static str = "v1";
const KIND: &'static str = "ValidatingWebhookConfiguration";
const NAME: &'static str = "validatingwebhookconfigurations";
const URL_PATH_SEGMENT: &'static str = "validatingwebhookconfigurations";
type Scope = crate::ClusterResourceScope;
}
impl crate::HasMetadata for ValidatingWebhookConfiguration {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down
6 changes: 4 additions & 2 deletions k8s-pb/src/api/admissionregistration/v1alpha1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,8 @@ impl crate::Resource for ValidatingAdmissionPolicy {
const GROUP: &'static str = "admissionregistration.k8s.io";
const VERSION: &'static str = "v1alpha1";
const KIND: &'static str = "ValidatingAdmissionPolicy";
const NAME: &'static str = "validatingadmissionpolicies";
const URL_PATH_SEGMENT: &'static str = "validatingadmissionpolicies";
type Scope = crate::ClusterResourceScope;
}
impl crate::HasMetadata for ValidatingAdmissionPolicy {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down Expand Up @@ -681,7 +682,8 @@ impl crate::Resource for ValidatingAdmissionPolicyBinding {
const GROUP: &'static str = "admissionregistration.k8s.io";
const VERSION: &'static str = "v1alpha1";
const KIND: &'static str = "ValidatingAdmissionPolicyBinding";
const NAME: &'static str = "validatingadmissionpolicybindings";
const URL_PATH_SEGMENT: &'static str = "validatingadmissionpolicybindings";
type Scope = crate::ClusterResourceScope;
}
impl crate::HasMetadata for ValidatingAdmissionPolicyBinding {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down
6 changes: 4 additions & 2 deletions k8s-pb/src/api/admissionregistration/v1beta1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,8 @@ impl crate::Resource for ValidatingAdmissionPolicy {
const GROUP: &'static str = "admissionregistration.k8s.io";
const VERSION: &'static str = "v1beta1";
const KIND: &'static str = "ValidatingAdmissionPolicy";
const NAME: &'static str = "validatingadmissionpolicies";
const URL_PATH_SEGMENT: &'static str = "validatingadmissionpolicies";
type Scope = crate::ClusterResourceScope;
}
impl crate::HasMetadata for ValidatingAdmissionPolicy {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down Expand Up @@ -1172,7 +1173,8 @@ impl crate::Resource for ValidatingAdmissionPolicyBinding {
const GROUP: &'static str = "admissionregistration.k8s.io";
const VERSION: &'static str = "v1beta1";
const KIND: &'static str = "ValidatingAdmissionPolicyBinding";
const NAME: &'static str = "validatingadmissionpolicybindings";
const URL_PATH_SEGMENT: &'static str = "validatingadmissionpolicybindings";
type Scope = crate::ClusterResourceScope;
}
impl crate::HasMetadata for ValidatingAdmissionPolicyBinding {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down
3 changes: 2 additions & 1 deletion k8s-pb/src/api/apiserverinternal/v1alpha1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ impl crate::Resource for StorageVersion {
const GROUP: &'static str = "internal.apiserver.k8s.io";
const VERSION: &'static str = "v1alpha1";
const KIND: &'static str = "StorageVersion";
const NAME: &'static str = "storageversions";
const URL_PATH_SEGMENT: &'static str = "storageversions";
type Scope = crate::ClusterResourceScope;
}
impl crate::HasMetadata for StorageVersion {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down
15 changes: 10 additions & 5 deletions k8s-pb/src/api/apps/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,8 @@ impl crate::Resource for ControllerRevision {
const GROUP: &'static str = "apps";
const VERSION: &'static str = "v1";
const KIND: &'static str = "ControllerRevision";
const NAME: &'static str = "controllerrevisions";
const URL_PATH_SEGMENT: &'static str = "controllerrevisions";
type Scope = crate::NamespaceResourceScope;
}
impl crate::HasMetadata for ControllerRevision {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand All @@ -909,7 +910,8 @@ impl crate::Resource for DaemonSet {
const GROUP: &'static str = "apps";
const VERSION: &'static str = "v1";
const KIND: &'static str = "DaemonSet";
const NAME: &'static str = "daemonsets";
const URL_PATH_SEGMENT: &'static str = "daemonsets";
type Scope = crate::NamespaceResourceScope;
}
impl crate::HasMetadata for DaemonSet {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down Expand Up @@ -956,7 +958,8 @@ impl crate::Resource for Deployment {
const GROUP: &'static str = "apps";
const VERSION: &'static str = "v1";
const KIND: &'static str = "Deployment";
const NAME: &'static str = "deployments";
const URL_PATH_SEGMENT: &'static str = "deployments";
type Scope = crate::NamespaceResourceScope;
}
impl crate::HasMetadata for Deployment {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down Expand Up @@ -1003,7 +1006,8 @@ impl crate::Resource for ReplicaSet {
const GROUP: &'static str = "apps";
const VERSION: &'static str = "v1";
const KIND: &'static str = "ReplicaSet";
const NAME: &'static str = "replicasets";
const URL_PATH_SEGMENT: &'static str = "replicasets";
type Scope = crate::NamespaceResourceScope;
}
impl crate::HasMetadata for ReplicaSet {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down Expand Up @@ -1050,7 +1054,8 @@ impl crate::Resource for StatefulSet {
const GROUP: &'static str = "apps";
const VERSION: &'static str = "v1";
const KIND: &'static str = "StatefulSet";
const NAME: &'static str = "statefulsets";
const URL_PATH_SEGMENT: &'static str = "statefulsets";
type Scope = crate::NamespaceResourceScope;
}
impl crate::HasMetadata for StatefulSet {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down
6 changes: 4 additions & 2 deletions k8s-pb/src/api/authentication/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ impl crate::Resource for SelfSubjectReview {
const GROUP: &'static str = "authentication.k8s.io";
const VERSION: &'static str = "v1";
const KIND: &'static str = "SelfSubjectReview";
const NAME: &'static str = "selfsubjectreviews";
const URL_PATH_SEGMENT: &'static str = "selfsubjectreviews";
type Scope = crate::ClusterResourceScope;
}
impl crate::HasMetadata for SelfSubjectReview {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand All @@ -239,7 +240,8 @@ impl crate::Resource for TokenReview {
const GROUP: &'static str = "authentication.k8s.io";
const VERSION: &'static str = "v1";
const KIND: &'static str = "TokenReview";
const NAME: &'static str = "tokenreviews";
const URL_PATH_SEGMENT: &'static str = "tokenreviews";
type Scope = crate::ClusterResourceScope;
}
impl crate::HasMetadata for TokenReview {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down
3 changes: 2 additions & 1 deletion k8s-pb/src/api/authentication/v1alpha1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ impl crate::Resource for SelfSubjectReview {
const GROUP: &'static str = "authentication.k8s.io";
const VERSION: &'static str = "v1alpha1";
const KIND: &'static str = "SelfSubjectReview";
const NAME: &'static str = "selfsubjectreviews";
const URL_PATH_SEGMENT: &'static str = "selfsubjectreviews";
type Scope = crate::ClusterResourceScope;
}
impl crate::HasMetadata for SelfSubjectReview {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down
3 changes: 2 additions & 1 deletion k8s-pb/src/api/authentication/v1beta1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ impl crate::Resource for SelfSubjectReview {
const GROUP: &'static str = "authentication.k8s.io";
const VERSION: &'static str = "v1beta1";
const KIND: &'static str = "SelfSubjectReview";
const NAME: &'static str = "selfsubjectreviews";
const URL_PATH_SEGMENT: &'static str = "selfsubjectreviews";
type Scope = crate::ClusterResourceScope;
}
impl crate::HasMetadata for SelfSubjectReview {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down
12 changes: 8 additions & 4 deletions k8s-pb/src/api/authorization/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ impl crate::Resource for LocalSubjectAccessReview {
const GROUP: &'static str = "authorization.k8s.io";
const VERSION: &'static str = "v1";
const KIND: &'static str = "LocalSubjectAccessReview";
const NAME: &'static str = "localsubjectaccessreviews";
const URL_PATH_SEGMENT: &'static str = "localsubjectaccessreviews";
type Scope = crate::NamespaceResourceScope;
}
impl crate::HasMetadata for LocalSubjectAccessReview {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down Expand Up @@ -327,7 +328,8 @@ impl crate::Resource for SelfSubjectAccessReview {
const GROUP: &'static str = "authorization.k8s.io";
const VERSION: &'static str = "v1";
const KIND: &'static str = "SelfSubjectAccessReview";
const NAME: &'static str = "selfsubjectaccessreviews";
const URL_PATH_SEGMENT: &'static str = "selfsubjectaccessreviews";
type Scope = crate::ClusterResourceScope;
}
impl crate::HasMetadata for SelfSubjectAccessReview {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down Expand Up @@ -363,7 +365,8 @@ impl crate::Resource for SelfSubjectRulesReview {
const GROUP: &'static str = "authorization.k8s.io";
const VERSION: &'static str = "v1";
const KIND: &'static str = "SelfSubjectRulesReview";
const NAME: &'static str = "selfsubjectrulesreviews";
const URL_PATH_SEGMENT: &'static str = "selfsubjectrulesreviews";
type Scope = crate::ClusterResourceScope;
}
impl crate::HasMetadata for SelfSubjectRulesReview {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down Expand Up @@ -399,7 +402,8 @@ impl crate::Resource for SubjectAccessReview {
const GROUP: &'static str = "authorization.k8s.io";
const VERSION: &'static str = "v1";
const KIND: &'static str = "SubjectAccessReview";
const NAME: &'static str = "subjectaccessreviews";
const URL_PATH_SEGMENT: &'static str = "subjectaccessreviews";
type Scope = crate::ClusterResourceScope;
}
impl crate::HasMetadata for SubjectAccessReview {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down
3 changes: 2 additions & 1 deletion k8s-pb/src/api/autoscaling/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,8 @@ impl crate::Resource for HorizontalPodAutoscaler {
const GROUP: &'static str = "autoscaling";
const VERSION: &'static str = "v1";
const KIND: &'static str = "HorizontalPodAutoscaler";
const NAME: &'static str = "horizontalpodautoscalers";
const URL_PATH_SEGMENT: &'static str = "horizontalpodautoscalers";
type Scope = crate::NamespaceResourceScope;
}
impl crate::HasMetadata for HorizontalPodAutoscaler {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down
3 changes: 2 additions & 1 deletion k8s-pb/src/api/autoscaling/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,8 @@ impl crate::Resource for HorizontalPodAutoscaler {
const GROUP: &'static str = "autoscaling";
const VERSION: &'static str = "v2";
const KIND: &'static str = "HorizontalPodAutoscaler";
const NAME: &'static str = "horizontalpodautoscalers";
const URL_PATH_SEGMENT: &'static str = "horizontalpodautoscalers";
type Scope = crate::NamespaceResourceScope;
}
impl crate::HasMetadata for HorizontalPodAutoscaler {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down
6 changes: 4 additions & 2 deletions k8s-pb/src/api/batch/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,8 @@ impl crate::Resource for CronJob {
const GROUP: &'static str = "batch";
const VERSION: &'static str = "v1";
const KIND: &'static str = "CronJob";
const NAME: &'static str = "cronjobs";
const URL_PATH_SEGMENT: &'static str = "cronjobs";
type Scope = crate::NamespaceResourceScope;
}
impl crate::HasMetadata for CronJob {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down Expand Up @@ -613,7 +614,8 @@ impl crate::Resource for Job {
const GROUP: &'static str = "batch";
const VERSION: &'static str = "v1";
const KIND: &'static str = "Job";
const NAME: &'static str = "jobs";
const URL_PATH_SEGMENT: &'static str = "jobs";
type Scope = crate::NamespaceResourceScope;
}
impl crate::HasMetadata for Job {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down
3 changes: 2 additions & 1 deletion k8s-pb/src/api/certificates/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ impl crate::Resource for CertificateSigningRequest {
const GROUP: &'static str = "certificates.k8s.io";
const VERSION: &'static str = "v1";
const KIND: &'static str = "CertificateSigningRequest";
const NAME: &'static str = "certificatesigningrequests";
const URL_PATH_SEGMENT: &'static str = "certificatesigningrequests";
type Scope = crate::ClusterResourceScope;
}
impl crate::HasMetadata for CertificateSigningRequest {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down
3 changes: 2 additions & 1 deletion k8s-pb/src/api/certificates/v1alpha1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ impl crate::Resource for ClusterTrustBundle {
const GROUP: &'static str = "certificates.k8s.io";
const VERSION: &'static str = "v1alpha1";
const KIND: &'static str = "ClusterTrustBundle";
const NAME: &'static str = "clustertrustbundles";
const URL_PATH_SEGMENT: &'static str = "clustertrustbundles";
type Scope = crate::ClusterResourceScope;
}
impl crate::HasMetadata for ClusterTrustBundle {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down
3 changes: 2 additions & 1 deletion k8s-pb/src/api/coordination/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ impl crate::Resource for Lease {
const GROUP: &'static str = "coordination.k8s.io";
const VERSION: &'static str = "v1";
const KIND: &'static str = "Lease";
const NAME: &'static str = "leases";
const URL_PATH_SEGMENT: &'static str = "leases";
type Scope = crate::NamespaceResourceScope;
}
impl crate::HasMetadata for Lease {
type Metadata = crate::apimachinery::pkg::apis::meta::v1::ObjectMeta;
Expand Down
Loading