Skip to content

Commit d0d5cc7

Browse files
committed
Collect OpenStackVersion CRs
Red Hat's openstack-operator in addition to the OpenStackControlPlane and OpenStackDataPlane CRs uses also additonal CR 'openstackversions' which reports version of the openstack(s) deployed in the OCP cluster. This patch adds support for gathering also this CRs from the cluster if they exists. Related: #OSPRH-5904
1 parent c6feae6 commit d0d5cc7

File tree

7 files changed

+410
-0
lines changed

7 files changed

+410
-0
lines changed

docs/gathered-data.md

+24
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,30 @@ None
16271627

16281628
### Changes
16291629
None
1630+
1631+
1632+
## Openstackversions
1633+
1634+
Collects `openstackversion.core.openstack.org`
1635+
resources from all namespaces
1636+
1637+
### API Reference
1638+
None
1639+
1640+
### Sample data
1641+
- [docs/insights-archive-sample/namespaces/openstack/core.openstack.org/openstackversions/openstack-galera-network-isolation.json](./insights-archive-sample/namespaces/openstack/core.openstack.org/openstackversions/openstack-galera-network-isolation.json)
1642+
1643+
### Location in archive
1644+
- `namespaces/{namespace}/core.openstack.org/openstackversions/{name}.json`
1645+
1646+
### Config ID
1647+
`clusterconfig/openstack_version`
1648+
1649+
### Released version
1650+
- 4.17
1651+
1652+
### Changes
1653+
None
16301654

16311655

16321656
## PodDefinition

docs/insights-archive-sample/insights-operator/gathers.json

+8
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@
153153
"warnings": null,
154154
"panic": null
155155
},
156+
{
157+
"name": "clusterconfig/openstack_version",
158+
"duration_in_ms": 25,
159+
"records_count": 1,
160+
"errors": null,
161+
"warnings": null,
162+
"panic": null
163+
},
156164
{
157165
"name": "clusterconfig/scheduler_logs",
158166
"duration_in_ms": 617,

docs/insights-archive-sample/namespaces/openstack/core.openstack.org/openstackversions/openstack-galera-network-isolation.json

+216
Large diffs are not rendered by default.

pkg/gatherers/clusterconfig/clusterconfig_gatherer.go

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ var gatheringFunctions = map[string]gathererFuncPtr{
6969
"openstack_controlplanes": (*Gatherer).GatherOpenstackControlplanes,
7070
"openstack_dataplanedeployments": (*Gatherer).GatherOpenstackDataplaneDeployments,
7171
"openstack_dataplanenodesets": (*Gatherer).GatherOpenstackDataplaneNodeSets,
72+
"openstack_version": (*Gatherer).GatherOpenstackVersions,
7273
"operators": (*Gatherer).GatherClusterOperators,
7374
"operators_pods_and_events": (*Gatherer).GatherClusterOperatorPodsAndEvents,
7475
"overlapping_namespace_uids": (*Gatherer).GatherNamespacesWithOverlappingUIDs,

pkg/gatherers/clusterconfig/const.go

+3
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ var (
8383
osdpnsGroupVersionResource = schema.GroupVersionResource{
8484
Group: "dataplane.openstack.org", Version: "v1beta1", Resource: "openstackdataplanenodesets",
8585
}
86+
osvGroupVersionResource = schema.GroupVersionResource{
87+
Group: "core.openstack.org", Version: "v1beta1", Resource: "openstackversions",
88+
}
8689
)
8790

8891
func init() { //nolint: gochecknoinits
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// nolint: dupl
2+
package clusterconfig
3+
4+
import (
5+
"context"
6+
"fmt"
7+
8+
"k8s.io/apimachinery/pkg/api/errors"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/client-go/dynamic"
11+
12+
"github.com/openshift/insights-operator/pkg/record"
13+
)
14+
15+
// GatherOpenstackVersions Collects `openstackversion.core.openstack.org`
16+
// resources from all namespaces
17+
//
18+
// ### API Reference
19+
// None
20+
//
21+
// ### Sample data
22+
// - docs/insights-archive-sample/namespaces/openstack/core.openstack.org/openstackversion/openstack-galera-network-isolation.json
23+
//
24+
// ### Location in archive
25+
// - `namespaces/{namespace}/core.openstack.org/openstackversion/{name}.json`
26+
//
27+
// ### Config ID
28+
// `clusterconfig/openstack_version`
29+
//
30+
// ### Released version
31+
// - 4.17
32+
//
33+
// ### Changes
34+
// None
35+
func (g *Gatherer) GatherOpenstackVersions(ctx context.Context) ([]record.Record, []error) {
36+
gatherDynamicClient, err := dynamic.NewForConfig(g.gatherKubeConfig)
37+
if err != nil {
38+
return nil, []error{err}
39+
}
40+
41+
return GatherOpenstackVersions(ctx, gatherDynamicClient)
42+
}
43+
44+
func GatherOpenstackVersions(ctx context.Context, dynamicClient dynamic.Interface) ([]record.Record, []error) {
45+
openstackversionsList, err := dynamicClient.Resource(osvGroupVersionResource).List(ctx, metav1.ListOptions{})
46+
if errors.IsNotFound(err) {
47+
return nil, nil
48+
}
49+
if err != nil {
50+
return nil, []error{err}
51+
}
52+
53+
var records []record.Record
54+
for i, osv := range openstackversionsList.Items {
55+
records = append(records, record.Record{
56+
Name: fmt.Sprintf("namespaces/%s/%s/%s/%s",
57+
osv.GetNamespace(),
58+
osvGroupVersionResource.Group,
59+
osvGroupVersionResource.Resource,
60+
osv.GetName()),
61+
Item: record.ResourceMarshaller{Resource: &openstackversionsList.Items[i]},
62+
})
63+
}
64+
65+
return records, nil
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package clusterconfig
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
10+
"k8s.io/apimachinery/pkg/runtime"
11+
"k8s.io/apimachinery/pkg/runtime/schema"
12+
"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
13+
dynamicfake "k8s.io/client-go/dynamic/fake"
14+
)
15+
16+
func Test_OpenStackVersions_Gather(t *testing.T) {
17+
tests := []struct {
18+
name string
19+
namespaces []string
20+
osvYAML []string
21+
exp []string
22+
}{
23+
{
24+
name: "Test OpenStacVersions CR not exists",
25+
namespaces: []string{"openstack"},
26+
osvYAML: []string{},
27+
exp: []string{},
28+
},
29+
{
30+
name: "Test single OpenStacVersions CR",
31+
namespaces: []string{"openstack"},
32+
osvYAML: []string{`
33+
apiVersion: core.openstack.org/v1beta1
34+
kind: OpenStackVersion
35+
metadata:
36+
name: test-cr
37+
namespace: openstack
38+
`},
39+
exp: []string{"namespaces/openstack/core.openstack.org/openstackversions/test-cr"},
40+
},
41+
{
42+
name: "Test Multiple OpenStackVersions CR",
43+
namespaces: []string{"openstack-1", "openstack-2"},
44+
osvYAML: []string{`
45+
apiVersion: core.openstack.org/v1beta1
46+
kind: OpenStackVersion
47+
metadata:
48+
name: test-cr-1
49+
namespace: openstack-1
50+
`, `
51+
apiVersion: core.openstack.org/v1beta1
52+
kind: OpenStackVersion
53+
metadata:
54+
name: test-cr-2
55+
namespace: openstack-2
56+
`},
57+
exp: []string{
58+
"namespaces/openstack-1/core.openstack.org/openstackversions/test-cr-1",
59+
"namespaces/openstack-2/core.openstack.org/openstackversions/test-cr-2",
60+
},
61+
},
62+
}
63+
for _, test := range tests {
64+
test := test
65+
t.Run(test.name, func(t *testing.T) {
66+
t.Parallel()
67+
dynamicClient := dynamicfake.NewSimpleDynamicClientWithCustomListKinds(runtime.NewScheme(), map[schema.GroupVersionResource]string{
68+
osvGroupVersionResource: "openstackversionsList",
69+
})
70+
decUnstructured := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
71+
testOsv := &unstructured.Unstructured{}
72+
ctx := context.Background()
73+
74+
for i := range test.osvYAML {
75+
_, _, err := decUnstructured.Decode([]byte(test.osvYAML[i]), nil, testOsv)
76+
assert.NoError(t, err)
77+
_, err = dynamicClient.Resource(osvGroupVersionResource).
78+
Namespace(test.namespaces[i]).
79+
Create(ctx, testOsv, metav1.CreateOptions{})
80+
assert.NoError(t, err)
81+
}
82+
83+
records, errs := GatherOpenstackVersions(ctx, dynamicClient)
84+
assert.Emptyf(t, errs, "Unexpected errors: %#v", errs)
85+
var recordNames []string
86+
for i := range records {
87+
recordNames = append(recordNames, records[i].Name)
88+
}
89+
assert.ElementsMatch(t, test.exp, recordNames)
90+
})
91+
}
92+
}

0 commit comments

Comments
 (0)