Skip to content

Commit 6d9652d

Browse files
authored
Gather all CostManagementMericsConfig definitions. (#525)
This PR gathers all the CostManagementMetricsConfigs custom resources. - [ ] Bugfix - [x] Enhancement - [ ] Backporting - [ ] Others (CI, Infrastructure, Documentation) TBD Updated in docs/gathered-data.md Add in: pkg/gatherers/clusterconfig/cost_management_metrics_configs_test.go Yes. There are no sensitive data in the newly collected information. No https://issues.redhat.com/browse/CCXDEV-6197
1 parent 04360d3 commit 6d9652d

File tree

8 files changed

+259
-0
lines changed

8 files changed

+259
-0
lines changed

docs/gathered-data.md

+9
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,15 @@ Response see:
273273
* 4.7+
274274

275275

276+
## CostManagementMetricsConfigs
277+
278+
collects CostManagementMetricsConfigs definitions.
279+
* Location in archive: config/cost_management_metrics_configs/<name>.json
280+
* Id in config: cost_management_metrics_configs
281+
* Since versions:
282+
* 4.10+
283+
284+
276285
## HostSubnet
277286

278287
collects HostSubnet information
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"apiVersion": "costmanagement-metrics-cfg.openshift.io/v1beta1",
3+
"kind": "CostManagementMetricsConfig",
4+
"metadata": {
5+
"namespace": "costmanagement-metrics-operator",
6+
"name": "costmanagementmetricscfg-sample-token"
7+
},
8+
"spec": {
9+
"upload": {
10+
"ingress_path": "/api/ingress/v1/upload",
11+
"upload_cycle": 360,
12+
"upload_toggle": true,
13+
"validate_cert": true
14+
},
15+
"packaging": {
16+
"max_reports_to_store": 30,
17+
"max_size_MB": 100
18+
},
19+
"api_url": "https://cloud.redhat.com",
20+
"prometheus_config": {
21+
"service_address": "https://thanos-querier.openshift-monitoring.svc:9091",
22+
"skip_tls_verification": false
23+
},
24+
"authentication": {
25+
"type": "basic",
26+
"secret_name": "console_basic"
27+
},
28+
"source": {
29+
"check_cycle": 1440,
30+
"create_source": false,
31+
"name": "INSERT-SOURCE-NAME",
32+
"sources_path": "/api/sources/v1.0/"
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"apiVersion": "costmanagement-metrics-cfg.openshift.io/v1beta1",
3+
"kind": "CostManagementMetricsConfig",
4+
"metadata": {
5+
"namespace": "costmanagement-metrics-operator",
6+
"name": "costmanagementmetricscfg-sample-token"
7+
},
8+
"spec": {
9+
"upload": {
10+
"ingress_path": "/api/ingress/v1/upload",
11+
"upload_cycle": 360,
12+
"upload_toggle": true,
13+
"validate_cert": true
14+
},
15+
"packaging": {
16+
"max_reports_to_store": 30,
17+
"max_size_MB": 100
18+
},
19+
"api_url": "https://cloud.redhat.com",
20+
"prometheus_config": {
21+
"service_address": "https://thanos-querier.openshift-monitoring.svc:9091",
22+
"skip_tls_verification": false
23+
},
24+
"authentication": {
25+
"type": "token"
26+
},
27+
"source": {
28+
"check_cycle": 1440,
29+
"create_source": false,
30+
"name": "INSERT-SOURCE-NAME",
31+
"sources_path": "/api/sources/v1.0/"
32+
}
33+
}
34+
}

manifests/03-clusterrole.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,14 @@ rules:
224224
- jaegertracing.io
225225
resources:
226226
- jaegers
227+
verbs:
228+
- get
229+
- list
230+
- watch
231+
- apiGroups:
232+
- costmanagement-metrics-cfg.openshift.io
233+
resources:
234+
- costmanagementmetricsconfig
227235
verbs:
228236
- get
229237
- list

pkg/gatherers/clusterconfig/clusterconfig_gatherer.go

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ var gatheringFunctions = map[string]gatheringFunction{
9090
"jaegers": failableFunc((*Gatherer).GatherJaegerCR),
9191
"validating_webhook_configurations": failableFunc((*Gatherer).GatherValidatingWebhookConfigurations),
9292
"mutating_webhook_configurations": failableFunc((*Gatherer).GatherMutatingWebhookConfigurations),
93+
"cost_management_metrics_configs": failableFunc((*Gatherer).GatherCostManagementMetricsConfigs),
9394
}
9495

9596
func New(

pkg/gatherers/clusterconfig/const.go

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ var (
4242
jaegerResource = schema.GroupVersionResource{
4343
Group: "jaegertracing.io", Version: "v1", Resource: "jaegers",
4444
}
45+
46+
costManagementMetricsConfigResource = schema.GroupVersionResource{
47+
Group: "costmanagement-metrics-cfg.openshift.io", Version: "v1beta1", Resource: "costmanagementmetricsconfigs",
48+
}
4549
)
4650

4751
func init() { //nolint: gochecknoinits
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package clusterconfig
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"k8s.io/apimachinery/pkg/api/errors"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
"k8s.io/client-go/dynamic"
10+
11+
"github.com/openshift/insights-operator/pkg/record"
12+
)
13+
14+
// GatherCostManagementMetricsConfigs collects CostManagementMetricsConfigs definitions.
15+
// * Location in archive: config/cost_management_metrics_configs/<name>.json
16+
// * Id in config: cost_management_metrics_configs
17+
// * Since versions:
18+
// * 4.10+
19+
func (g *Gatherer) GatherCostManagementMetricsConfigs(ctx context.Context) ([]record.Record, []error) {
20+
gatherDynamicClient, err := dynamic.NewForConfig(g.gatherKubeConfig)
21+
if err != nil {
22+
return nil, []error{err}
23+
}
24+
25+
return gatherCostManagementMetricsConfigs(ctx, gatherDynamicClient)
26+
}
27+
28+
func gatherCostManagementMetricsConfigs(ctx context.Context, dynamicClient dynamic.Interface) ([]record.Record, []error) {
29+
mcList, err := dynamicClient.Resource(costManagementMetricsConfigResource).List(ctx, metav1.ListOptions{})
30+
if errors.IsNotFound(err) {
31+
return nil, nil
32+
}
33+
if err != nil {
34+
return nil, []error{err}
35+
}
36+
records := []record.Record{}
37+
var errs []error
38+
for i := range mcList.Items {
39+
mc := mcList.Items[i]
40+
records = append(records, record.Record{
41+
Name: fmt.Sprintf("config/cost_management_metrics_configs/%s", mc.GetName()),
42+
Item: record.ResourceMarshaller{Resource: &mc},
43+
})
44+
}
45+
if len(errs) > 0 {
46+
return records, errs
47+
}
48+
return records, nil
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package clusterconfig
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
9+
"k8s.io/apimachinery/pkg/runtime"
10+
"k8s.io/apimachinery/pkg/runtime/schema"
11+
"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
12+
"k8s.io/client-go/dynamic"
13+
dynamicfake "k8s.io/client-go/dynamic/fake"
14+
)
15+
16+
func createMockCostManagementMetricsConfig(t *testing.T, c dynamic.Interface, data string) {
17+
decUnstructured1 := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
18+
testCostManagementMetricsConfig := &unstructured.Unstructured{}
19+
_, _, err := decUnstructured1.Decode([]byte(data), nil, testCostManagementMetricsConfig)
20+
if err != nil {
21+
t.Fatal("unable to decode CostManagementMetricsConfig YAML", err)
22+
}
23+
24+
_, _ = c.
25+
Resource(costManagementMetricsConfigResource).
26+
Create(context.Background(), testCostManagementMetricsConfig, metav1.CreateOptions{})
27+
}
28+
29+
func Test_CostManagementMetricsConfigs(t *testing.T) {
30+
// Initialize the fake dynamic client.
31+
costMgmtMetricsConfigClient := dynamicfake.NewSimpleDynamicClientWithCustomListKinds(
32+
runtime.NewScheme(), map[schema.GroupVersionResource]string{
33+
costManagementMetricsConfigResource: "CostManagementMetricsConfigList",
34+
})
35+
36+
records, errs := gatherCostManagementMetricsConfigs(context.Background(), costMgmtMetricsConfigClient)
37+
if len(errs) > 0 {
38+
t.Fatalf("unexpected errors: %#v", errs)
39+
}
40+
// 0 records because there is no CostManagementMetricsConfigs yet.
41+
if len(records) != 0 {
42+
t.Fatalf("unexpected number or records in the first run: %d", len(records))
43+
}
44+
45+
// Create first CostManagementMetricsConfig resource.
46+
costMgmtMetricsConfigYAML1 := `apiVersion: costmanagement-metrics-cfg.openshift.io/v1beta1
47+
kind: CostManagementMetricsConfig
48+
metadata:
49+
name: costmanagementmetricscfg-sample-1
50+
`
51+
52+
createMockCostManagementMetricsConfig(t, costMgmtMetricsConfigClient, costMgmtMetricsConfigYAML1)
53+
records, errs = gatherCostManagementMetricsConfigs(context.Background(), costMgmtMetricsConfigClient)
54+
if len(errs) > 0 {
55+
t.Fatalf("unexpected errors: %#v", errs)
56+
}
57+
// 1 record because there is now 1 CostManagementMetricsConfig resource.
58+
if len(records) != 1 {
59+
t.Fatalf("unexpected number or records in the second run: %d", len(records))
60+
}
61+
62+
// Create second CostManagementMetricsConfig resource.
63+
costMgmtMetricsConfigYAML2 := `apiVersion: costmanagement-metrics-cfg.openshift.io/v1beta1
64+
kind: CostManagementMetricsConfig
65+
metadata:
66+
name: costmanagementmetricscfg-sample-2
67+
`
68+
69+
createMockCostManagementMetricsConfig(t, costMgmtMetricsConfigClient, costMgmtMetricsConfigYAML2)
70+
records, errs = gatherCostManagementMetricsConfigs(context.Background(), costMgmtMetricsConfigClient)
71+
if len(errs) > 0 {
72+
t.Fatalf("unexpected errors: %#v", errs)
73+
}
74+
// 2 record because there are now 2 CostManagementMetricsConfig resource.
75+
if len(records) != 2 {
76+
t.Fatalf("unexpected number or records in the third run: %d", len(records))
77+
}
78+
79+
// Create third CostManagementMetricsConfig resource.
80+
costMgmtMetricsConfigYAML3 := `apiVersion: costmanagement-metrics-cfg.openshift.io/v1beta1
81+
kind: CostManagementMetricsConfig
82+
metadata:
83+
name: costmanagementmetricscfg-sample-3
84+
spec:
85+
authentication:
86+
type: basic
87+
secret_name: console_basic_auth
88+
`
89+
90+
createMockCostManagementMetricsConfig(t, costMgmtMetricsConfigClient, costMgmtMetricsConfigYAML3)
91+
records, errs = gatherCostManagementMetricsConfigs(context.Background(), costMgmtMetricsConfigClient)
92+
if len(errs) > 0 {
93+
t.Fatalf("unexpected errors: %#v", errs)
94+
}
95+
// 3 record because there are now 3 CostManagementMetricsConfig resource.
96+
if len(records) != 3 {
97+
t.Fatalf("unexpected number or records in the fourth run: %d", len(records))
98+
}
99+
100+
// Create fourth CostManagementMetricsConfig resource.
101+
costMgmtMetricsConfigYAML4 := `apiVersion: costmanagement-metrics-cfg.openshift.io/v1beta1
102+
kind: CostManagementMetricsConfig
103+
metadata:
104+
name: costmanagementmetricscfg-sample-4
105+
spec:
106+
authentication:
107+
type: token
108+
`
109+
110+
createMockCostManagementMetricsConfig(t, costMgmtMetricsConfigClient, costMgmtMetricsConfigYAML4)
111+
records, errs = gatherCostManagementMetricsConfigs(context.Background(), costMgmtMetricsConfigClient)
112+
if len(errs) > 0 {
113+
t.Fatalf("unexpected errors: %#v", errs)
114+
}
115+
// 4 record because there are now 4 CostManagementMetricsConfig resource.
116+
if len(records) != 4 {
117+
t.Fatalf("unexpected number or records in the fifth run: %d", len(records))
118+
}
119+
}

0 commit comments

Comments
 (0)