Skip to content

Commit 7638fce

Browse files
authored
Add function to create KubernetesClientConfig from string (#41)
1 parent 42ced33 commit 7638fce

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

Sources/SwiftkubeClient/Config/KubernetesClientConfig.swift

+77
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,30 @@ public extension KubernetesClientConfig {
114114

115115
return try? URLConfigLoader(url: url).load(timeout: timeout, redirectConfiguration: redirectConfiguration, logger: logger)
116116
}
117+
118+
/// Initializes a client configuration from a given String.
119+
///
120+
/// It is also possible to override the default values for the underlying `HTTPClient` timeout and redirect config.
121+
///
122+
/// - Parameters:
123+
/// - string: The string to load the configuration from.
124+
/// - timeout: The desired timeout configuration to apply. If not provided, then `connect` timeout will
125+
/// default to 10 seconds.
126+
/// - redirectConfiguration: Specifies redirect processing settings. If not provided, then it will default
127+
/// to a maximum of 5 follows w/o cycles.
128+
/// - logger: The logger to use for the underlying configuration loaders.
129+
/// - Returns: An instance of KubernetesClientConfig for the Swiftkube KubernetesClient
130+
static func createFromString(
131+
fromString string: String,
132+
timeout: HTTPClient.Configuration.Timeout? = nil,
133+
redirectConfiguration: HTTPClient.Configuration.RedirectConfiguration? = nil,
134+
logger: Logger = SwiftkubeClient.loggingDisabled
135+
) -> KubernetesClientConfig? {
136+
let timeout = timeout ?? .init()
137+
let redirectConfiguration = redirectConfiguration ?? .follow(max: 5, allowCycles: false)
138+
139+
return try? StringConfigLoader(contents: string).load(timeout: timeout, redirectConfiguration: redirectConfiguration, logger: logger)
140+
}
117141
}
118142

119143
// MARK: - KubernetesClientConfigLoader
@@ -133,6 +157,59 @@ extension KubernetesClientConfigLoader {
133157
}
134158
}
135159

160+
// MARK: - StringConfigLoader
161+
162+
internal struct StringConfigLoader: KubernetesClientConfigLoader {
163+
164+
let contents: String
165+
166+
internal func load(
167+
timeout: HTTPClient.Configuration.Timeout,
168+
redirectConfiguration: HTTPClient.Configuration.RedirectConfiguration,
169+
logger: Logger?
170+
) throws -> KubernetesClientConfig? {
171+
let decoder = YAMLDecoder()
172+
173+
guard let kubeConfig = try? decoder.decode(KubeConfig.self, from: contents) else {
174+
return nil
175+
}
176+
177+
guard let currentContext = kubeConfig.currentContext else {
178+
return nil
179+
}
180+
181+
guard let context = kubeConfig.contexts?.filter({ $0.name == currentContext }).map(\.context).first else {
182+
return nil
183+
}
184+
185+
guard let cluster = kubeConfig.clusters?.filter({ $0.name == context.cluster }).map(\.cluster).first else {
186+
return nil
187+
}
188+
189+
guard let masterURL = URL(string: cluster.server) else {
190+
return nil
191+
}
192+
193+
guard let authInfo = kubeConfig.users?.filter({ $0.name == context.user }).map(\.authInfo).first else {
194+
return nil
195+
}
196+
197+
guard let authentication = authInfo.authentication(logger: logger) else {
198+
return nil
199+
}
200+
201+
return KubernetesClientConfig(
202+
masterURL: masterURL,
203+
namespace: context.namespace ?? "default",
204+
authentication: authentication,
205+
trustRoots: cluster.trustRoots(logger: logger),
206+
insecureSkipTLSVerify: cluster.insecureSkipTLSVerify ?? true,
207+
timeout: timeout,
208+
redirectConfiguration: redirectConfiguration
209+
)
210+
}
211+
}
212+
136213
// MARK: - URLConfigLoader
137214

138215
internal struct URLConfigLoader: KubernetesClientConfigLoader {

0 commit comments

Comments
 (0)