k8s_openapi/resource.rs
1/// A trait applied to all Kubernetes resources.
2pub trait Resource {
3 /// The API version of the resource. This is a composite of [`Resource::GROUP`] and [`Resource::VERSION`] (eg `"apiextensions.k8s.io/v1beta1"`)
4 /// or just the version for resources without a group (eg `"v1"`).
5 ///
6 /// This is the string used in the `apiVersion` field of the resource's serialized form.
7 const API_VERSION: &'static str;
8
9 /// The group of the resource, or the empty string if the resource doesn't have a group.
10 const GROUP: &'static str;
11
12 /// The kind of the resource.
13 ///
14 /// This is the string used in the `kind` field of the resource's serialized form.
15 const KIND: &'static str;
16
17 /// The version of the resource.
18 const VERSION: &'static str;
19
20 /// The URL path segment used to construct URLs related to this resource.
21 ///
22 /// For cluster- and namespaced-scoped resources, this is the plural name of the resource that is followed by the resource name.
23 /// For example, [`api::core::v1::Pod`](crate::api::core::v1::Pod)'s value is `"pods"` and its URLs look like `.../pods/{name}`.
24 ///
25 /// For subresources, this is the subresource name that comes after the parent resource's name.
26 /// For example, [`api::authentication::v1::TokenRequest`](crate::api::authentication::v1::TokenRequest)'s value is `"token"`,
27 /// and its URLs look like `.../serviceaccounts/{name}/token`.
28 const URL_PATH_SEGMENT: &'static str;
29
30 /// Indicates whether the resource is namespace-scoped or cluster-scoped or a subresource.
31 ///
32 /// 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.
33 /// For example, `fn foo<T: k8s_openapi::Resource<Scope = k8s_openapi::ClusterResourceScope>>() { }` can only be called with cluster-scoped resources.
34 type Scope: ResourceScope;
35}
36
37/// The scope of a [`Resource`].
38pub trait ResourceScope {}
39
40/// Indicates that a [`Resource`] is cluster-scoped.
41pub struct ClusterResourceScope {}
42impl ResourceScope for ClusterResourceScope {}
43
44/// Indicates that a [`Resource`] is namespace-scoped.
45pub struct NamespaceResourceScope {}
46impl ResourceScope for NamespaceResourceScope {}
47
48/// Indicates that a [`Resource`] is neither cluster-scoped nor namespace-scoped.
49pub struct SubResourceScope {}
50impl ResourceScope for SubResourceScope {}
51
52/// A trait applied to all Kubernetes resources that can be part of a corresponding list.
53pub trait ListableResource: Resource {
54 /// The kind of the list type of the resource.
55 ///
56 /// This is the string used in the `kind` field of the list type's serialized form.
57 const LIST_KIND: &'static str;
58}
59
60/// A trait applied to all Kubernetes resources that have metadata.
61pub trait Metadata: Resource {
62 /// The type of the metadata object.
63 type Ty;
64
65 /// Gets a reference to the metadata of this resource value.
66 fn metadata(&self) -> &<Self as Metadata>::Ty;
67
68 /// Gets a mutable reference to the metadata of this resource value.
69 fn metadata_mut(&mut self) -> &mut<Self as Metadata>::Ty;
70}
71
72/// Extracts the API version of the given resource value.
73///
74/// This just returns the [`Resource::API_VERSION`] value for the argument's type, but is useful when you already have a value
75/// and don't want to explicitly write its type.
76pub fn api_version<T>(_: &T) -> &'static str where T: Resource {
77 <T as Resource>::API_VERSION
78}
79
80/// Extracts the group of the given resource value.
81///
82/// This just returns the [`Resource::GROUP`] value for the argument's type, but is useful when you already have a value
83/// and don't want to explicitly write its type.
84pub fn group<T>(_: &T) -> &'static str where T: Resource {
85 <T as Resource>::GROUP
86}
87
88/// Extracts the kind of the given resource value.
89///
90/// This just returns the [`Resource::KIND`] value for the argument's type, but is useful when you already have a value
91/// and don't want to explicitly write its type.
92pub fn kind<T>(_: &T) -> &'static str where T: Resource {
93 <T as Resource>::KIND
94}
95
96/// Extracts the version of the given resource value.
97///
98/// This just returns the [`Resource::VERSION`] value for the argument's type, but is useful when you already have a value
99/// and don't want to explicitly write its type.
100pub fn version<T>(_: &T) -> &'static str where T: Resource {
101 <T as Resource>::VERSION
102}