Skip to content

Commit 732f801

Browse files
committed
crd testdata: add example files and description
1 parent 436f87f commit 732f801

File tree

4 files changed

+98
-4
lines changed

4 files changed

+98
-4
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Testdata for generator tests
2+
3+
The files in this directory are used for testing the `kube-state-metrics generate` command and to provide an example.
4+
5+
## foo-config.yaml
6+
7+
This file is used in the test at [generate_integration_test.go](../generate_integration_test.go) to verify that the resulting configuration does not change during changes in the codebase.
8+
9+
If there are intended changes this file needs to get regenerated to make the test succeed again.
10+
This could be done via:
11+
12+
```sh
13+
go run generate \
14+
./pkg/customresourcestate/generate/generator/testdata/ \
15+
> ./pkg/customresourcestate/generate/generator/testdata/foo-config.yaml
16+
```
17+
18+
Or by using the go:generate marker inside [foo_types.go](foo_types.go):
19+
20+
```sh
21+
go generate ./pkg/customresourcestate/generate/generator/testdata/
22+
```
23+
24+
## Example files: foo-cr-example.yaml and foo-cr-example-metrics.txt
25+
26+
There is also an example CR ([foo-cr-example.yaml](foo-cr-example.yaml)) and resulting example metrics ([foo-cr-example-metrics.txt](foo-cr-example-metrics.txt)).
27+
28+
The example metrics file got created by:
29+
30+
1. Generating a CustomResourceDefinition yaml by using [controller-gen](https://github.com/kubernetes-sigs/kubebuilder/blob/master/docs/book/src/reference/controller-gen.md):
31+
32+
```sh
33+
controller-gen crd paths=./pkg/customresourcestate/generate/generator/testdata/ output:dir=./pkg/customresourcestate/generate/generator/testdata/
34+
```
35+
36+
2. Creating a cluster using [kind](https://kind.sigs.k8s.io/)
37+
3. Applying the CRD and example CR to the cluster:
38+
39+
```sh
40+
kubectl apply -f /pkg/customresourcestate/generate/generator/testdata/bar.example.com_foos.yaml
41+
kubectl apply -f /pkg/customresourcestate/generate/generator/testdata/foo-cr-example.yaml
42+
```
43+
44+
4. Running kube-state-metrics with the provided configuration file:
45+
46+
```sh
47+
go run ./ --kubeconfig $HOME/.kube/config --custom-resource-state-only \
48+
--custom-resource-state-config-file pkg/customresourcestate/generate/generator/testdata/foo-config.yaml
49+
```
50+
51+
5. Querying the metrics endpoint in a second terminal:
52+
53+
```sh
54+
curl localhost:8080/metrics > ./pkg/customresourcestate/generate/generator/testdata/foo-cr-example-metrics.txt
55+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# HELP foo_created Unix creation timestamp.
2+
# TYPE foo_created gauge
3+
foo_created{customresource_group="bar.example.com",customresource_kind="Foo",customresource_version="foo",name="bar"} 1.697543739e+09
4+
# HELP foo_owner Owner references.
5+
# TYPE foo_owner info
6+
foo_owner{customresource_group="bar.example.com",customresource_kind="Foo",customresource_version="foo",name="bar",owner_is_controller="true",owner_kind="foo",owner_name="foo",owner_uid="someuid"} 1
7+
# HELP foo_status_condition The condition of a foo.
8+
# TYPE foo_status_condition stateset
9+
foo_status_condition{customresource_group="bar.example.com",customresource_kind="Foo",customresource_version="foo",name="bar",status="False",type="AnotherType"} 1
10+
foo_status_condition{customresource_group="bar.example.com",customresource_kind="Foo",customresource_version="foo",name="bar",status="False",type="SomeType"} 0
11+
foo_status_condition{customresource_group="bar.example.com",customresource_kind="Foo",customresource_version="foo",name="bar",status="True",type="AnotherType"} 0
12+
foo_status_condition{customresource_group="bar.example.com",customresource_kind="Foo",customresource_version="foo",name="bar",status="True",type="SomeType"} 1
13+
foo_status_condition{customresource_group="bar.example.com",customresource_kind="Foo",customresource_version="foo",name="bar",status="Unknown",type="AnotherType"} 0
14+
foo_status_condition{customresource_group="bar.example.com",customresource_kind="Foo",customresource_version="foo",name="bar",status="Unknown",type="SomeType"} 0
15+
# HELP foo_status_condition_last_transition_time The condition last transition time of a foo.
16+
# TYPE foo_status_condition_last_transition_time gauge
17+
foo_status_condition_last_transition_time{customresource_group="bar.example.com",customresource_kind="Foo",customresource_version="foo",name="bar",status="False",type="AnotherType"} 1.697119142e+09
18+
foo_status_condition_last_transition_time{customresource_group="bar.example.com",customresource_kind="Foo",customresource_version="foo",name="bar",status="True",type="SomeType"} 1.697119142e+09
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
apiVersion: bar.example.com/foo
3+
kind: Foo
4+
metadata:
5+
name: bar
6+
ownerReferences:
7+
- apiVersion: v1
8+
kind: foo
9+
controller: true
10+
name: foo
11+
uid: someuid
12+
spec:
13+
someString: test
14+
status:
15+
conditions:
16+
- lastTransitionTime: "2023-10-12T13:59:02Z"
17+
status: "True"
18+
type: SomeType
19+
- lastTransitionTime: "2023-10-12T13:59:02Z"
20+
status: "False"
21+
type: AnotherType

pkg/customresourcestate/generate/generator/testdata/foo_types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ limitations under the License.
2020
// the following command:
2121
// $ go generate ./pkg/customresourcestate/generate/generator/testdata
2222
//go:generate sh -c "go run ../../../../../ generate ./... > foo-config.yaml"
23+
//go:generate sh -c "controller-gen crd paths=./ output:stdout > foo-crd.yaml"
2324

2425
// +groupName=bar.example.com
2526
package foo
@@ -30,16 +31,15 @@ import (
3031

3132
// FooSpec is the spec of Foo.
3233
type FooSpec struct {
33-
// This tests that defaulted fields are stripped for v1beta1,
34-
// but not for v1
35-
DefaultedString string `json:"defaultedString"`
34+
// SomeString is a string.
35+
SomeString string `json:"someString"`
3636
}
3737

3838
// FooStatus is the status of Foo.
3939
type FooStatus struct {
4040
// +Metrics:stateset:name="status_condition",help="The condition of a foo.",labelName="status",JSONPath=".status",list={"True","False","Unknown"},labelsFromPath={"type":".type"}
4141
// +Metrics:gauge:name="status_condition_last_transition_time",help="The condition last transition time of a foo.",valueFrom=.lastTransitionTime,labelsFromPath={"type":".type","status":".status"}
42-
Conditions Condition `json:"conditions,omitempty"`
42+
Conditions []Condition `json:"conditions,omitempty"`
4343
}
4444

4545
// Foo is a test object.

0 commit comments

Comments
 (0)