mz_orchestrator_kubernetes/
util.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use std::time::Duration;
11
12use anyhow::bail;
13use kube::config::KubeConfigOptions;
14use kube::{Client, Config};
15
16/// Constructs a new Kubernetes client.
17///
18/// The `context` specifies the Kubernetes context to load. If loading from the
19/// context fails, the in-cluster configuration is attempted.
20///
21/// Returns the constructed client and the default namespace loaded from the
22/// configuration.
23pub async fn create_client(context: String) -> Result<(Client, String), anyhow::Error> {
24    let kubeconfig_options = KubeConfigOptions {
25        context: Some(context),
26        ..Default::default()
27    };
28    let mut kubeconfig = match Config::from_kubeconfig(&kubeconfig_options).await {
29        Ok(config) => config,
30        Err(kubeconfig_err) => match Config::incluster_env() {
31            Ok(config) => config,
32            Err(in_cluster_err) => {
33                bail!(
34                    "failed to infer config: in-cluster: ({in_cluster_err}), kubeconfig: ({kubeconfig_err})"
35                );
36            }
37        },
38    };
39
40    kubeconfig.connect_timeout = Some(Duration::from_secs(10));
41    kubeconfig.read_timeout = Some(Duration::from_secs(60));
42    kubeconfig.write_timeout = Some(Duration::from_secs(60));
43
44    let namespace = kubeconfig.default_namespace.clone();
45    let client = Client::try_from(kubeconfig)?;
46    Ok((client, namespace))
47}