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

io.opentelemetry.javaagent.instrumentation.kubernetesclient.KubernetesResource Maven / Gradle / Ivy

/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.javaagent.instrumentation.kubernetesclient;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

class KubernetesResource {

  public static final Pattern CORE_RESOURCE_URL_PATH_PATTERN =
      Pattern.compile(
          "^/api/v1(/namespaces/(?[\\w-]+))?/(?[\\w-]+)(/(?[\\w-]+))?(/(?[\\w-]+))?");

  public static final Pattern REGULAR_RESOURCE_URL_PATH_PATTERN =
      Pattern.compile(
          "^/apis/(?\\S+?)/(?\\S+?)(/namespaces/(?[\\w-]+))?/(?[\\w-]+)(/(?[\\w-]+))?(/(?[\\w-]+))?");

  public static KubernetesResource parseCoreResource(String urlPath)
      throws ParseKubernetesResourceException {
    Matcher matcher = CORE_RESOURCE_URL_PATH_PATTERN.matcher(urlPath);
    if (!matcher.matches()) {
      throw new ParseKubernetesResourceException();
    }
    return new KubernetesResource(
        "",
        "v1",
        matcher.group("resource"),
        matcher.group("subresource"),
        matcher.group("namespace"),
        matcher.group("name"));
  }

  public static KubernetesResource parseRegularResource(String urlPath)
      throws ParseKubernetesResourceException {
    Matcher matcher = REGULAR_RESOURCE_URL_PATH_PATTERN.matcher(urlPath);
    if (!matcher.matches()) {
      throw new ParseKubernetesResourceException();
    }
    return new KubernetesResource(
        matcher.group("group"),
        matcher.group("version"),
        matcher.group("resource"),
        matcher.group("subresource"),
        matcher.group("namespace"),
        matcher.group("name"));
  }

  KubernetesResource(
      String apiGroup,
      String apiVersion,
      String resource,
      String subResource,
      String namespace,
      String name) {
    this.apiGroup = apiGroup;
    this.apiVersion = apiVersion;
    this.resource = resource;
    this.subResource = subResource;
    this.namespace = namespace;
    this.name = name;
  }

  private final String apiGroup;
  private final String apiVersion;
  private final String resource;
  private final String subResource;

  private final String namespace;
  private final String name;

  public String getApiGroup() {
    return apiGroup;
  }

  public String getApiVersion() {
    return apiVersion;
  }

  public String getResource() {
    return resource;
  }

  public String getSubResource() {
    return subResource;
  }

  public String getNamespace() {
    return namespace;
  }

  public String getName() {
    return name;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy