Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions pkg/resourceinterpreter/default/thirdparty/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Thirdparty Resource Interpreter

This directory contains third-party resource interpreters for Karmada. These interpreters define how Karmada should handle custom resources from various third-party applications and operators.

## Files

- `thirdparty.go` - Main implementation of the third-party resource interpreter
- `thirdparty_test.go` - Test suite for validating resource interpreter customizations
- `resourcecustomizations/` - Directory containing resource customization definitions organized by API version and kind

## Directory Structure

The resource customizations are organized in the following structure:

```
resourcecustomizations/
├── <group>/
│ └── <version>/
│ └── <kind>/
│ ├── customizations.yaml # Resource interpreter customization rules
│ └── testdata/ # Test input and expected output files
│ ├── aggregatestatus-test.yaml # test case for AggregateStatus operation
│ ├── interpretcomponent-test.yaml # test case for InterpretComponent operation
│ ├── interprethealth-test.yaml # test case for InterpretHealth operation
│ ├── ...
```

## How to test

### Running Tests

To run all third-party resource interpreter tests:

```bash
cd pkg/resourceinterpreter/default/thirdparty
go test -v
```

### Creating Test Cases

#### 1. Create Test Structure

For a new resource type, create the directory structure:

```bash
mkdir -p resourcecustomizations/<group>/<version>/<kind>/testdata
```

#### 2. Create Test Data Files

Test data files are divided by operation type. Each file contains different test cases for the corresponding operation. The naming convention for test data files is `<operation>-test.yaml`.

For example:
```markdown
aggregatestatus-test.yaml # test case for AggregateStatus operation
interpretcomponent-test.yaml # test case for InterpretComponent operation
interprethealth-test.yaml # test case for InterpretHealth operation
```

#### 3. Add Test Cases

The test case structure is as follows:
```go
type IndividualTest struct {
Name string `yaml:"name"` // the name of individual test
Description string `yaml:"description,omitempty"` // the description of individual test
DesiredObj *unstructured.Unstructured `yaml:"desiredObj,omitempty"` // the desired object
ObservedObj *unstructured.Unstructured `yaml:"observedObj,omitempty"` // the observed object
StatusItems []workv1alpha2.AggregatedStatusItem `yaml:"statusItems,omitempty"` // the status items of aggregated status
InputReplicas int64 `yaml:"inputReplicas,omitempty"` // the input replicas for revise operation
Operation string `yaml:"operation"` // the operation of resource interpreter
Output map[string]interface{} `yaml:"output,omitempty"` // the expected output results
}
```

Where:
- `Output` The output are key-value mapping where the key is the field name of the expected result and the value is the expected result. The keys in output for different operations correspond to the Name field of the results returned by the corresponding resource interpreter operation `RuleResult.Results`.

For example:
```go
func (h *healthInterpretationRule) Run(interpreter *declarative.ConfigurableInterpreter, args RuleArgs) *RuleResult {
obj, err := args.getObjectOrError()
if err != nil {
return newRuleResultWithError(err)
}
healthy, enabled, err := interpreter.InterpretHealth(obj)
if err != nil {
return newRuleResultWithError(err)
}
if !enabled {
return newRuleResultWithError(fmt.Errorf("rule is not enabled"))
}
return newRuleResult().add("healthy", healthy)
}
```

The output for operation `InterpretHealth` should contain the `healthy` key.

For more examples of test cases, refer to the existing test data files in this directory.

### Supported Operations

The test framework supports the following operations:

- `InterpretReplica` - Extract replica count from resource
- `InterpretComponent` - Extract component information from resource
- `ReviseReplica` - Modify replica count in resource
- `InterpretStatus` - Extract status information
- `InterpretHealth` - Determine resource health status
- `InterpretDependency` - Extract resource dependencies
- `AggregateStatus` - Aggregate status from multiple clusters
- `Retain` - Retain the desired resource template.

> NOTE: Not all operations need to be implemented for every resource type. Implement only the operations relevant to your resource.

For more information about resource interpreter customizations, see the [Karmada documentation](https://karmada.io/docs/userguide/globalview/customizing-resource-interpreter/).

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# test case for aggregating status of AdvancedCronJob
# case1. AdvancedCronJob with two status items

name: "AdvancedCronJob with two status items"
description: "Test aggregating status of AdvancedCronJob with two status items"
desiredObj:
apiVersion: apps.kruise.io/v1alpha1
kind: AdvancedCronJob
metadata:
labels:
app: sample
name: sample
namespace: test-acj
spec:
schedule: "*/2 * * * *"
template:
broadcastJobTemplate:
spec:
template:
metadata:
labels:
app: sample
spec:
restartPolicy: Never
volumes:
- name: configmap
configMap:
name: my-sample-config
containers:
- name: nginx
image: nginx:alpine
env:
- name: logData
valueFrom:
configMapKeyRef:
name: mysql-config
key: log
- name: lowerData
valueFrom:
configMapKeyRef:
name: mysql-config
key: lower
completionPolicy:
type: Never
statusItems:
- applied: true
clusterName: member1
health: Healthy
status:
active:
- apiVersion: apps.kruise.io/v1alpha1
kind: BroadcastJob
name: sample-1681378080
namespace: test-acj
resourceVersion: "3636404"
uid: f96013ef-2869-49d7-adf3-8f7231cc5e2a
lastScheduleTime: "2023-04-13T09:30:00Z"
type: BroadcastJob
- applied: true
clusterName: member3
health: Healthy
status:
active:
- apiVersion: apps.kruise.io/v1alpha1
kind: BroadcastJob
name: sample-1681378080
namespace: test-acj
resourceVersion: "3635081"
uid: d1f3c194-d650-4cce-b23d-307a445bb92e
lastScheduleTime: "2023-04-13T09:30:00Z"
type: BroadcastJob
operation: AggregateStatus
output:

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# test case for interpreting dependency of AdvancedCronJob
# case1. AdvancedCronJob with configmap dependency

name: "AdvancedCronJob with configmap dependency"
description: "Test interpreting dependency of AdvancedCronJob with configmap dependency"
desiredObj:
apiVersion: apps.kruise.io/v1alpha1
kind: AdvancedCronJob
metadata:
labels:
app: sample
name: sample
namespace: test-acj
spec:
schedule: "*/2 * * * *"
template:
broadcastJobTemplate:
spec:
template:
metadata:
labels:
app: sample
spec:
restartPolicy: Never
volumes:
- name: configmap
configMap:
name: my-sample-config
containers:
- name: nginx
image: nginx:alpine
env:
- name: logData
valueFrom:
configMapKeyRef:
name: mysql-config
key: log
- name: lowerData
valueFrom:
configMapKeyRef:
name: mysql-config
key: lower
completionPolicy:
type: Never
operation: InterpretDependency
output:

This file was deleted.

This file was deleted.

This file was deleted.

Loading