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 // The read timeout bounds how long a hung Kubernetes call can block an orchestrator worker,
42 // which handles one command at a time.
43 //
44 // NOTE: The read timeout must exceed the connection pool's 90 s idle expiry. hyper-timeout
45 // starts the read timer while a pooled connection sits idle and does not reset it when a
46 // request is written. A request on a connection that idled for close to `read_timeout`
47 // therefore times out before the response arrives and fails with
48 // `client error (SendRequest)`.
49 //
50 // TODO(CPU-306): Return to 60 s once kube-client enables hyper-timeout's
51 // `reset_reader_on_write`.
52 kubeconfig.read_timeout = Some(Duration::from_secs(120));
53 kubeconfig.write_timeout = Some(Duration::from_secs(60));
54
55 let namespace = kubeconfig.default_namespace.clone();
56 let client = Client::try_from(kubeconfig)?;
57 Ok((client, namespace))
58}