or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

attribute-extraction.mdcrud-handlers.mdcrud-operations.mdcustom-resources.mdindex.mdjunit-integration.mdmock-server-management.mdwebsocket-operations.md

attribute-extraction.mddocs/

0

# Attribute Extraction

1

2

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.

3

4

## Capabilities

5

6

### KubernetesAttributesExtractor

7

8

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

9

10

```java { .api }

11

public class KubernetesAttributesExtractor implements AttributeExtractor {

12

13

// Key constants for attribute identification

14

public static final String KEY = "key";

15

public static final String KIND = "kind";

16

public static final String API = "api";

17

public static final String VERSION = "version";

18

public static final String NAME = "name";

19

public static final String METADATA_NAME = "metadata.name";

20

public static final String NAMESPACE = "namespace";

21

public static final String METADATA_NAMESPACE = "metadata.namespace";

22

public static final String VALUE = "value";

23

public static final String PLURAL = "plural";

24

public static final String UNKNOWN_KIND = "%unknown";

25

26

/**

27

* Set the custom resource definition processor for handling CRDs

28

* @param customResourceDefinitionProcessor - Processor for CRD contexts

29

*/

30

public void setCustomResourceDefinitionProcessor(

31

CustomResourceDefinitionProcessor customResourceDefinitionProcessor);

32

33

/**

34

* Extract Kubernetes resource attributes from an API path

35

* @param path - Kubernetes API path (e.g., "/api/v1/namespaces/default/pods/my-pod")

36

* @return Map containing extracted attributes (name, namespace, api, version, plural, kind)

37

*/

38

public Map<String, String> fromKubernetesPath(String path);

39

40

/**

41

* Extract attributes from path and query parameters into an AttributeSet

42

* @param path - Complete path including query parameters

43

* @return AttributeSet with extracted attributes including label and field selectors

44

*/

45

@Override

46

public AttributeSet fromPath(String path);

47

48

/**

49

* Extract attributes from a resource's JSON/YAML string representation

50

* @param resourceString - Serialized Kubernetes resource

51

* @return AttributeSet with resource metadata attributes

52

*/

53

@Override

54

public AttributeSet fromResource(String resourceString);

55

56

/**

57

* Extract attributes from a HasMetadata resource object

58

* @param hasMetadata - Kubernetes resource object

59

* @return AttributeSet with metadata attributes including labels

60

*/

61

public AttributeSet extract(HasMetadata hasMetadata);

62

63

/**

64

* Convert JSON/YAML string to Kubernetes resource object

65

* @param resourceString - Serialized resource content

66

* @return HasMetadata resource object or null if parsing fails

67

*/

68

static HasMetadata toKubernetesResource(String resourceString);

69

70

/**

71

* Generate a composite key for resource identification

72

* @param api - API group

73

* @param version - API version

74

* @param plural - Resource plural name

75

* @return List containing the key components

76

*/

77

static List<String> pluralKey(String api, String version, String plural);

78

}

79

```

80

81

### Path Parsing Examples

82

83

```java

84

import io.fabric8.kubernetes.client.server.mock.KubernetesAttributesExtractor;

85

import java.util.Map;

86

87

KubernetesAttributesExtractor extractor = new KubernetesAttributesExtractor();

88

89

// Parse core resource path

90

Map<String, String> attributes = extractor.fromKubernetesPath(

91

"/api/v1/namespaces/default/pods/my-pod");

92

// Returns: {version=v1, namespace=default, plural=pods, name=my-pod}

93

94

// Parse custom resource path

95

attributes = extractor.fromKubernetesPath(

96

"/apis/apps/v1/namespaces/production/deployments/web-server");

97

// Returns: {api=apps, version=v1, namespace=production, plural=deployments, name=web-server}

98

99

// Parse cluster-scoped resource path

100

attributes = extractor.fromKubernetesPath("/api/v1/nodes/worker-1");

101

// Returns: {version=v1, plural=nodes, name=worker-1}

102

```

103

104

### Label and Field Selector Support

105

106

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

107

108

```java

109

// Path with label selector

110

AttributeSet attrs = extractor.fromPath(

111

"/api/v1/namespaces/default/pods?labelSelector=app=web,env!=test");

112

113

// Path with field selector

114

attrs = extractor.fromPath(

115

"/api/v1/namespaces/default/pods?fieldSelector=status.phase=Running");

116

```

117

118

### Resource Attribute Extraction

119

120

Extract attributes from resource objects for matching and filtering:

121

122

```java

123

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

124

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

125

126

Pod pod = new PodBuilder()

127

.withNewMetadata()

128

.withName("test-pod")

129

.withNamespace("default")

130

.addToLabels("app", "web")

131

.addToLabels("version", "1.0")

132

.endMetadata()

133

.build();

134

135

AttributeSet attributes = extractor.extract(pod);

136

// Contains: name=test-pod, namespace=default, labels:app=web, labels:version=1.0

137

```

138

139

## Types

140

141

### Core Attribute Constants

142

143

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

144

145

```java { .api }

146

// Resource identification

147

public static final String KIND = "kind"; // Resource kind (Pod, Service, etc.)

148

public static final String API = "api"; // API group

149

public static final String VERSION = "version"; // API version

150

public static final String PLURAL = "plural"; // Plural resource name

151

152

// Resource naming and scoping

153

public static final String NAME = "name"; // Resource name

154

public static final String METADATA_NAME = "metadata.name"; // Metadata name field

155

public static final String NAMESPACE = "namespace"; // Namespace name

156

public static final String METADATA_NAMESPACE = "metadata.namespace"; // Metadata namespace field

157

158

// Generic attribute handling

159

public static final String KEY = "key"; // Generic key identifier

160

public static final String VALUE = "value"; // Generic value identifier

161

public static final String UNKNOWN_KIND = "%unknown"; // Placeholder for unknown kinds

162

```

163

164

### Pattern Matching

165

166

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

167

168

- **API Group Pattern**: Matches `/api` (core) or `/apis/group.name` (non-core)

169

- **Version Pattern**: Captures API version (v1, v1beta1, etc.)

170

- **Namespace Pattern**: Optionally matches namespaced resources

171

- **Resource Pattern**: Captures plural resource name

172

- **Name Pattern**: Optionally captures individual resource name

173

- **Subresource Pattern**: Handles status and scale subresources

174

175

### Label Selector Parsing

176

177

Supports all Kubernetes label selector operators:

178

179

- **Equality**: `app=web` or `app==web`

180

- **Inequality**: `env!=test`

181

- **Existence**: `version` (key exists)

182

- **Non-existence**: `!debug` (key does not exist)

183

184

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

185

186

### KubernetesResponseComposer

187

188

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

189

190

```java { .api }

191

public class KubernetesResponseComposer implements ResponseComposer {

192

193

/**

194

* Compose a Kubernetes List response from a collection of resource strings

195

* @param collection - Collection of JSON resource strings

196

* @return JSON string containing a Kubernetes List with the resources

197

*/

198

@Override

199

public String compose(Collection<String> collection);

200

201

/**

202

* Compose a Kubernetes List response with a specific resource version

203

* @param collection - Collection of JSON resource strings

204

* @param resourceVersion - Resource version to include in metadata

205

* @return JSON string containing a Kubernetes List with metadata

206

*/

207

public String compose(Collection<String> collection, String resourceVersion);

208

}

209

```

210

211

### Response Format

212

213

The composer generates responses in the standard Kubernetes List format:

214

215

```json

216

{

217

"apiVersion": "v1",

218

"kind": "List",

219

"items": [

220

// Individual resource objects

221

],

222

"metadata": {

223

"resourceVersion": "12345",

224

"selfLink": ""

225

}

226

}

227

```