All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.avito.k8s.KubernetesRequestMetadataProvider.kt Maven / Gradle / Ivy

Go to download

Collection of infrastructure libraries and gradle plugins of Avito Android project

There is a newer version: 2023.22
Show newest version
package com.avito.k8s

import com.avito.android.Result
import com.avito.http.internal.RequestMetadata
import com.avito.http.internal.RequestMetadataProvider
import okhttp3.Request

/**
 * API reference: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.21/#deployment-v1-apps
 */
internal class KubernetesRequestMetadataProvider : RequestMetadataProvider {

    override fun provide(request: Request): Result {
        val pathSegments = request.url().pathSegments()

        val serviceName = "kubernetes"

        try {
            when (pathSegments[0]) {
                "api" -> if (pathSegments[2] == "namespaces") {
                    if (pathSegments[4] == "pods") {
                        return Result.Success(
                            RequestMetadata(
                                serviceName = serviceName,
                                methodName = "pods_${request.method().lowercase()}"
                            )
                        )
                    }
                }
                "apis" -> {
                    val apiGroup = pathSegments[1]
                    if (apiGroup == "apps") {
                        if (pathSegments[3] == "namespaces") {
                            if (pathSegments[5] == "deployments") {
                                return Result.Success(
                                    RequestMetadata(
                                        serviceName = serviceName,
                                        methodName = "deployments_${request.method().lowercase()}"
                                    )
                                )
                            }
                        }
                    }
                }
            }
        } catch (e: Throwable) {
            return Result.Failure(IllegalArgumentException(getErrorMessage(request), e))
        }

        return Result.Failure(IllegalArgumentException(getErrorMessage(request)))
    }

    private fun getErrorMessage(request: Request): String {
        return buildString {
            appendLine("KubernetesRequestMetadataProvider: unknown k8s API method")
            appendLine("Original request was: ${request.method()} ${request.url()}")
            appendLine("To fix it just add required method handling")
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy