-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade to zio-config 4.0.0-RC16 (#519)
* Upgrade to zio-config 4.0.0-RC16 * Updates to correct compile errors in mdoc --------- Co-authored-by: ggavares <grant.gavares@oracle.com>
- Loading branch information
Showing
8 changed files
with
91 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 61 additions & 110 deletions
171
zio-k8s-client/src/main/scala/com/coralogix/zio/k8s/client/config/Descriptors.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,130 +1,81 @@ | ||
package com.coralogix.zio.k8s.client.config | ||
|
||
import sttp.model.Uri | ||
import zio.config.ConfigDescriptor._ | ||
import zio.config._ | ||
import zio.Config.boolean | ||
import zio.nio.file.Path | ||
import zio.{ Chunk, Config } | ||
|
||
/** Defines ZIO Config descriptors for all the configuration data types of zio-k8s | ||
*/ | ||
trait Descriptors { | ||
private val uri: ConfigDescriptor[Uri] = | ||
string.transformOrFail( | ||
s => Uri.parse(s), | ||
(uri: Uri) => Right(uri.toString) | ||
) | ||
|
||
private val path: ConfigDescriptor[Path] = | ||
string.transform( | ||
s => Path(s), | ||
(path: Path) => path.toString() | ||
) | ||
|
||
private val keySourceFromFile: ConfigDescriptor[KeySource] = | ||
nested("path")(path).transformOrFailRight( | ||
(s: Path) => KeySource.FromFile(s), | ||
{ | ||
case KeySource.FromFile(path) => Right(path) | ||
case _ => Left("Not a KeySource.FromFile") | ||
} | ||
) | ||
|
||
private val keySourceFromBase64: ConfigDescriptor[KeySource] = | ||
nested("base64")(string).transformOrFailRight( | ||
(s: String) => KeySource.FromBase64(s), | ||
{ | ||
case KeySource.FromBase64(base64) => Right(base64) | ||
case _ => Left("Not a KeySource.FromBase64") | ||
} | ||
) | ||
|
||
private val keySourceFromString: ConfigDescriptor[KeySource] = | ||
nested("value")(string).transformOrFailRight( | ||
(s: String) => KeySource.FromString(s), | ||
{ | ||
case KeySource.FromString(value) => Right(value) | ||
case _ => Left("Not a KeySource.FromString") | ||
} | ||
) | ||
private val uri: Config[Uri] = | ||
Config.string.mapOrFail { s => | ||
Uri.parse(s).left.map(err => Config.Error.InvalidData(Chunk.empty, err)) | ||
} | ||
|
||
private val keySource: ConfigDescriptor[KeySource] = | ||
keySourceFromFile <> keySourceFromString <> keySourceFromBase64 | ||
private val path: Config[Path] = Config.string.map(Path(_)) | ||
|
||
private val serviceAccountToken: ConfigDescriptor[K8sAuthentication] = | ||
nested("serviceAccountToken")(keySource).transformOrFailRight( | ||
(s: KeySource) => K8sAuthentication.ServiceAccountToken(s), | ||
{ | ||
case K8sAuthentication.ServiceAccountToken(token) => Right(token) | ||
case _ => Left("Not a K8sAuthentication.ServiceAccountToken") | ||
} | ||
) | ||
|
||
private val basicAuth: ConfigDescriptor[K8sAuthentication] = | ||
nested("basicAuth")( | ||
string("username") zip string("password") | ||
).transformOrFailRight( | ||
{ case (username, password) => K8sAuthentication.BasicAuth(username, password) }, | ||
{ | ||
case K8sAuthentication.BasicAuth(username, password) => Right((username, password)) | ||
case _ => Left("Not a K8sAuthentication.BasicAuth") | ||
} | ||
) | ||
|
||
private val clientCertificates: ConfigDescriptor[K8sAuthentication] = | ||
nested("clientCertificates")( | ||
nested("certificate")(keySource) zip nested("key")(keySource) zip string( | ||
"password" | ||
).optional | ||
).transformOrFailRight( | ||
{ case (certificate, key, password) => | ||
K8sAuthentication.ClientCertificates(certificate, key, password) | ||
}, | ||
{ | ||
case K8sAuthentication.ClientCertificates(certificate, key, password) => | ||
Right((certificate, key, password)) | ||
case _ => Left("Not a K8sAuthentication.ClientCertificates") | ||
} | ||
) | ||
|
||
private val k8sAuthentication: ConfigDescriptor[K8sAuthentication] = | ||
serviceAccountToken <> basicAuth <> clientCertificates | ||
|
||
private val insecureServerCertificate: ConfigDescriptor[K8sServerCertificate] = | ||
boolean("insecure").transformOrFail( | ||
{ | ||
case true => Right(K8sServerCertificate.Insecure) | ||
case false => Left("Use insecure: true or secure") | ||
}, | ||
{ | ||
case K8sServerCertificate.Insecure => Right(true) | ||
case _ => Left("Not a K8sServerCertificate.Insecure") | ||
private val keySourceFromFile: Config[KeySource] = | ||
path.nested("path").map(p => KeySource.FromFile(p)) | ||
|
||
private val keySourceFromBase64: Config[KeySource] = | ||
Config.string.nested("base64").map(s => KeySource.FromBase64(s)) | ||
|
||
private val keySourceFromString: Config[KeySource] = | ||
Config.string.nested("value").map(s => KeySource.FromString(s)) | ||
|
||
private val keySource: Config[KeySource] = | ||
keySourceFromFile orElse keySourceFromString orElse keySourceFromBase64 | ||
|
||
private val serviceAccountToken: Config[K8sAuthentication] = | ||
keySource.nested("serviceAccountToken").map(ks => K8sAuthentication.ServiceAccountToken(ks)) | ||
|
||
private val basicAuth: Config[K8sAuthentication] = | ||
(Config.string("username") zip Config.string("password")).nested("basicAuth") map { | ||
case (username, password) => K8sAuthentication.BasicAuth(username, password) | ||
} | ||
|
||
private val clientCertificates: Config[K8sAuthentication] = | ||
(keySource.nested("certificate") zip | ||
keySource.nested("key") zip | ||
Config.string("password").optional) | ||
.nested("clientCertificates") | ||
.map { case (cert, key, password) => | ||
K8sAuthentication.ClientCertificates(cert, key, password) | ||
} | ||
) | ||
|
||
private val secureServerCertificate: ConfigDescriptor[K8sServerCertificate] = | ||
nested("secure")( | ||
nested("certificate")(keySource) zip boolean("disableHostnameVerification") | ||
).transformOrFailRight( | ||
{ case (cert, disableHostnameVerification) => | ||
private val k8sAuthentication: Config[K8sAuthentication] = | ||
serviceAccountToken orElse basicAuth orElse clientCertificates | ||
|
||
private val insecureServerCertificate: Config[K8sServerCertificate] = | ||
boolean("insecure").mapOrFail { | ||
case true => Right(K8sServerCertificate.Insecure) | ||
case false => Left(Config.Error.InvalidData(Chunk.empty, "Use insecure: true or secure")) | ||
} | ||
|
||
private val secureServerCertificate: Config[K8sServerCertificate] = | ||
(keySource.nested("certificate") zip Config.boolean("disableHostnameVerification")) | ||
.nested("secure") | ||
.map { case (cert, disableHostnameVerification) => | ||
K8sServerCertificate.Secure(cert, disableHostnameVerification) | ||
}, | ||
{ | ||
case K8sServerCertificate.Secure(cert, disableHostnameVerification) => | ||
Right((cert, disableHostnameVerification)) | ||
case K8sServerCertificate.Insecure => Left("Not a K8sServerCertificate.Secure") | ||
} | ||
) | ||
|
||
private val serverCertificate: ConfigDescriptor[K8sServerCertificate] = | ||
insecureServerCertificate <> secureServerCertificate | ||
private val serverCertificate: Config[K8sServerCertificate] = | ||
insecureServerCertificate orElse secureServerCertificate | ||
|
||
private val clientConfig: ConfigDescriptor[K8sClientConfig] = | ||
(boolean("debug") zip serverCertificate).to[K8sClientConfig] | ||
private val clientConfig: Config[K8sClientConfig] = | ||
(boolean("debug") zip serverCertificate).map { case (debug, cert) => | ||
K8sClientConfig(debug, cert) | ||
} | ||
|
||
/** ZIO Config descriptor for [[K8sClusterConfig]] | ||
*/ | ||
val clusterConfigDescriptor: ConfigDescriptor[K8sClusterConfig] = | ||
(nested("host")(uri) zip nested("authentication")(k8sAuthentication) zip nested("client")( | ||
clientConfig | ||
)).to[K8sClusterConfig] | ||
val clusterConfigDescriptor: Config[K8sClusterConfig] = | ||
(uri.nested("host") zip | ||
k8sAuthentication.nested("authentication") zip | ||
clientConfig.nested("client")) | ||
.map { case (uri, auth, client) => | ||
K8sClusterConfig(uri, auth, client) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters