CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-fabric8--kubernetes-server-mock

A JUnit 5 testing library that provides a Kubernetes mock server for testing Kubernetes client applications

Pending
Overview
Eval results
Files

attribute-extraction.mddocs/

Attribute Extraction

Core functionality for extracting resource attributes from Kubernetes API paths and resource definitions. The attribute extraction system is fundamental to the mock server's ability to match, filter, and categorize resources.

Capabilities

KubernetesAttributesExtractor

Main class responsible for parsing Kubernetes API paths and extracting resource metadata into structured attribute sets.

public class KubernetesAttributesExtractor implements AttributeExtractor {
    
    // Key constants for attribute identification
    public static final String KEY = "key";
    public static final String KIND = "kind";
    public static final String API = "api";
    public static final String VERSION = "version";
    public static final String NAME = "name";
    public static final String METADATA_NAME = "metadata.name";
    public static final String NAMESPACE = "namespace";
    public static final String METADATA_NAMESPACE = "metadata.namespace";
    public static final String VALUE = "value";
    public static final String PLURAL = "plural";
    public static final String UNKNOWN_KIND = "%unknown";
    
    /**
     * Set the custom resource definition processor for handling CRDs
     * @param customResourceDefinitionProcessor - Processor for CRD contexts
     */
    public void setCustomResourceDefinitionProcessor(
        CustomResourceDefinitionProcessor customResourceDefinitionProcessor);
    
    /**
     * Extract Kubernetes resource attributes from an API path
     * @param path - Kubernetes API path (e.g., "/api/v1/namespaces/default/pods/my-pod")
     * @return Map containing extracted attributes (name, namespace, api, version, plural, kind)
     */
    public Map<String, String> fromKubernetesPath(String path);
    
    /**
     * Extract attributes from path and query parameters into an AttributeSet
     * @param path - Complete path including query parameters
     * @return AttributeSet with extracted attributes including label and field selectors
     */
    @Override
    public AttributeSet fromPath(String path);
    
    /**
     * Extract attributes from a resource's JSON/YAML string representation
     * @param resourceString - Serialized Kubernetes resource
     * @return AttributeSet with resource metadata attributes
     */
    @Override
    public AttributeSet fromResource(String resourceString);
    
    /**
     * Extract attributes from a HasMetadata resource object
     * @param hasMetadata - Kubernetes resource object
     * @return AttributeSet with metadata attributes including labels
     */
    public AttributeSet extract(HasMetadata hasMetadata);
    
    /**
     * Convert JSON/YAML string to Kubernetes resource object
     * @param resourceString - Serialized resource content
     * @return HasMetadata resource object or null if parsing fails
     */
    static HasMetadata toKubernetesResource(String resourceString);
    
    /**
     * Generate a composite key for resource identification
     * @param api - API group
     * @param version - API version  
     * @param plural - Resource plural name
     * @return List containing the key components
     */
    static List<String> pluralKey(String api, String version, String plural);
}

Path Parsing Examples

import io.fabric8.kubernetes.client.server.mock.KubernetesAttributesExtractor;
import java.util.Map;

KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor();

// Parse core resource path
Map<String, String> attributes = extractor.fromKubernetesPath(
    "/api/v1/namespaces/default/pods/my-pod");
// Returns: {version=v1, namespace=default, plural=pods, name=my-pod}

// Parse custom resource path
attributes = extractor.fromKubernetesPath(
    "/apis/apps/v1/namespaces/production/deployments/web-server");
// Returns: {api=apps, version=v1, namespace=production, plural=deployments, name=web-server}

// Parse cluster-scoped resource path
attributes = extractor.fromKubernetesPath("/api/v1/nodes/worker-1");
// Returns: {version=v1, plural=nodes, name=worker-1}

Label and Field Selector Support

The extractor automatically parses label and field selectors from query parameters:

// Path with label selector
AttributeSet attrs = extractor.fromPath(
    "/api/v1/namespaces/default/pods?labelSelector=app=web,env!=test");

// Path with field selector  
attrs = extractor.fromPath(
    "/api/v1/namespaces/default/pods?fieldSelector=status.phase=Running");

Resource Attribute Extraction

Extract attributes from resource objects for matching and filtering:

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;

Pod pod = new PodBuilder()
    .withNewMetadata()
        .withName("test-pod")
        .withNamespace("default")
        .addToLabels("app", "web")
        .addToLabels("version", "1.0")
    .endMetadata()
    .build();

AttributeSet attributes = extractor.extract(pod);
// Contains: name=test-pod, namespace=default, labels:app=web, labels:version=1.0

Types

Core Attribute Constants

The following constants are used throughout the system for consistent attribute naming:

// Resource identification
public static final String KIND = "kind";           // Resource kind (Pod, Service, etc.)
public static final String API = "api";             // API group
public static final String VERSION = "version";     // API version
public static final String PLURAL = "plural";       // Plural resource name

// Resource naming and scoping
public static final String NAME = "name";                     // Resource name
public static final String METADATA_NAME = "metadata.name";   // Metadata name field
public static final String NAMESPACE = "namespace";           // Namespace name
public static final String METADATA_NAMESPACE = "metadata.namespace"; // Metadata namespace field

// Generic attribute handling
public static final String KEY = "key";             // Generic key identifier
public static final String VALUE = "value";         // Generic value identifier
public static final String UNKNOWN_KIND = "%unknown"; // Placeholder for unknown kinds

Pattern Matching

The extractor uses sophisticated regular expressions to parse Kubernetes API paths:

  • API Group Pattern: Matches /api (core) or /apis/group.name (non-core)
  • Version Pattern: Captures API version (v1, v1beta1, etc.)
  • Namespace Pattern: Optionally matches namespaced resources
  • Resource Pattern: Captures plural resource name
  • Name Pattern: Optionally captures individual resource name
  • Subresource Pattern: Handles status and scale subresources

Label Selector Parsing

Supports all Kubernetes label selector operators:

  • Equality: app=web or app==web
  • Inequality: env!=test
  • Existence: version (key exists)
  • Non-existence: !debug (key does not exist)

Multiple requirements can be combined with commas: app=web,env!=test,version

KubernetesResponseComposer

Utility class for composing Kubernetes-compatible list responses in JSON format.

public class KubernetesResponseComposer implements ResponseComposer {
    
    /**
     * Compose a Kubernetes List response from a collection of resource strings
     * @param collection - Collection of JSON resource strings
     * @return JSON string containing a Kubernetes List with the resources
     */
    @Override
    public String compose(Collection<String> collection);
    
    /**
     * Compose a Kubernetes List response with a specific resource version
     * @param collection - Collection of JSON resource strings
     * @param resourceVersion - Resource version to include in metadata
     * @return JSON string containing a Kubernetes List with metadata
     */
    public String compose(Collection<String> collection, String resourceVersion);
}

Response Format

The composer generates responses in the standard Kubernetes List format:

{
  "apiVersion": "v1",
  "kind": "List",
  "items": [
    // Individual resource objects
  ],
  "metadata": {
    "resourceVersion": "12345",
    "selfLink": ""
  }
}

Install with Tessl CLI

npx tessl i tessl/maven-io-fabric8--kubernetes-server-mock

docs

attribute-extraction.md

crud-handlers.md

crud-operations.md

custom-resources.md

index.md

junit-integration.md

mock-server-management.md

websocket-operations.md

tile.json