Skip to content

Commit 2036590

Browse files
committed
Add docstrings
1 parent 13d7261 commit 2036590

File tree

6 files changed

+37
-0
lines changed

6 files changed

+37
-0
lines changed

src/client/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use serde::de::DeserializeOwned;
66

77
use super::config::Configuration;
88

9+
/// APIClient requires `config::Configuration` includes client to connect with kubernetes cluster.
910
pub struct APIClient {
1011
configuration: Rc<Configuration>,
1112
}
@@ -16,6 +17,7 @@ impl APIClient {
1617
APIClient { configuration: rc }
1718
}
1819

20+
/// Returns kubernetes resources binded `Arnavion/k8s-openapi-codegen` APIs.
1921
pub fn request<T>(&self, request: http::Request<Vec<u8>>) -> Result<T, Error>
2022
where
2123
T: DeserializeOwned,

src/config/apis.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use serde_yaml;
66

77
use config::utils;
88

9+
/// Config stores information to connect remote kubernetes cluster.
910
#[derive(Clone, Debug, Serialize, Deserialize)]
1011
pub struct Config {
1112
pub kind: Option<String>,
@@ -21,24 +22,28 @@ pub struct Config {
2122
pub extensions: Option<Vec<NamedExtension>>,
2223
}
2324

25+
/// Preferences stores extensions for cli.
2426
#[derive(Clone, Debug, Serialize, Deserialize)]
2527
pub struct Preferences {
2628
pub colors: Option<bool>,
2729
pub extensions: Option<Vec<NamedExtension>>,
2830
}
2931

32+
/// NamedExtention associates name with extension.
3033
#[derive(Clone, Debug, Serialize, Deserialize)]
3134
pub struct NamedExtension {
3235
pub name: String,
3336
pub extension: String,
3437
}
3538

39+
/// NamedCluster associates name with cluster.
3640
#[derive(Clone, Debug, Serialize, Deserialize)]
3741
pub struct NamedCluster {
3842
pub name: String,
3943
pub cluster: Cluster,
4044
}
4145

46+
/// Cluster stores information to connect kubernetes cluster.
4247
#[derive(Clone, Debug, Serialize, Deserialize)]
4348
pub struct Cluster {
4449
pub server: String,
@@ -50,13 +55,15 @@ pub struct Cluster {
5055
pub certificate_authority_data: Option<String>,
5156
}
5257

58+
/// NamedAuthInfo associates name with authentication.
5359
#[derive(Clone, Debug, Serialize, Deserialize)]
5460
pub struct NamedAuthInfo {
5561
pub name: String,
5662
#[serde(rename = "user")]
5763
pub auth_info: AuthInfo,
5864
}
5965

66+
/// AuthInfo stores information to tell cluster who you are.
6067
#[derive(Clone, Debug, Serialize, Deserialize)]
6168
pub struct AuthInfo {
6269
pub username: Option<String>,
@@ -82,12 +89,14 @@ pub struct AuthInfo {
8289
pub impersonate_groups: Option<Vec<String>>,
8390
}
8491

92+
/// NamedContext associates name with context.
8593
#[derive(Clone, Debug, Serialize, Deserialize)]
8694
pub struct NamedContext {
8795
pub name: String,
8896
pub context: Context,
8997
}
9098

99+
/// Context stores tuple of cluster and user information.
91100
#[derive(Clone, Debug, Serialize, Deserialize)]
92101
pub struct Context {
93102
pub cluster: String,

src/config/incluster_config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub const SERVICE_PORTENV: &str = "KUBERNETES_SERVICE_PORT";
1010
const SERVICE_TOKENFILE: &str = "/var/run/secrets/kubernetes.io/serviceaccount/token";
1111
const SERVICE_CERTFILE: &str = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt";
1212

13+
/// Returns kubernetes address from specified environment variables.
1314
pub fn kube_server() -> Option<String> {
1415
let f = |(h, p)| format!("https://{}:{}", h, p);
1516
kube_host().and_then(|h| kube_port().map(|p| f((h, p))))
@@ -23,10 +24,12 @@ fn kube_port() -> Option<String> {
2324
env::var(SERVICE_PORTENV).ok()
2425
}
2526

27+
/// Returns token from specified path in cluster.
2628
pub fn load_token() -> Result<String, Error> {
2729
utils::data_or_file(&None, &Some(SERVICE_TOKENFILE.to_string()))
2830
}
2931

32+
/// Returns certification from specified path in cluster.
3033
pub fn load_cert() -> Result<X509, Error> {
3134
let ca = utils::data_or_file_with_base64(&None, &Some(SERVICE_CERTFILE.to_string()))?;
3235
X509::from_pem(&ca).map_err(Error::from)

src/config/kube_config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use openssl::x509::X509;
77

88
use config::apis::{AuthInfo, Cluster, Config, Context};
99

10+
/// KubeConfigLoader loads current context, cluster, and authentication information.
1011
#[derive(Debug)]
1112
pub struct KubeConfigLoader {
1213
pub current_context: Context,

src/config/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use reqwest::{header, Certificate, Client, Identity};
99

1010
use self::kube_config::KubeConfigLoader;
1111

12+
/// Configuration stores kubernetes path and client for requests.
1213
pub struct Configuration {
1314
pub base_path: String,
1415
pub client: Client,
@@ -23,6 +24,15 @@ impl Configuration {
2324
}
2425
}
2526

27+
/// Returns a config includes authentication and cluster infomation from kubeconfig file.
28+
///
29+
/// # Example
30+
/// ```
31+
/// use kubernetes::config;
32+
///
33+
/// let kubeconfig = config::load_kube_config()
34+
/// .expect("failed to load kubeconfig");
35+
/// ```
2636
pub fn load_kube_config() -> Result<Configuration, Error> {
2737
let kubeconfig = utils::kubeconfig_path()
2838
.or_else(utils::default_kube_path)
@@ -69,6 +79,16 @@ pub fn load_kube_config() -> Result<Configuration, Error> {
6979
))
7080
}
7181

82+
/// Returns a config which is used by clients within pods on kubernetes.
83+
/// It will return an error if called from out of kubernetes cluster.
84+
///
85+
/// # Example
86+
/// ```
87+
/// use kubernetes::config;
88+
///
89+
/// let kubeconfig = config::incluster_config()
90+
/// .expect("failed to load incluster config");
91+
/// ```
7292
pub fn incluster_config() -> Result<Configuration, Error> {
7393
let server = incluster_config::kube_server().ok_or(format_err!(
7494
"Unable to load incluster config, {} and {} must be defined",

src/config/utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ use failure::Error;
99

1010
const KUBECONFIG: &str = "KUBECONFIG";
1111

12+
/// Returns kubeconfig path from specified environment variable.
1213
pub fn kubeconfig_path() -> Option<PathBuf> {
1314
env::var_os(KUBECONFIG).map(PathBuf::from)
1415
}
1516

17+
/// Returns kubeconfig path from `$HOME/.kube/config`.
1618
pub fn default_kube_path() -> Option<PathBuf> {
1719
home_dir().map(|h| h.join(".kube").join("config"))
1820
}

0 commit comments

Comments
 (0)