Skip to content

Commit db55261

Browse files
committed
fix: hardened mcpusage update to tackle race condition errors
1 parent eb26b4f commit db55261

File tree

7 files changed

+197
-106
lines changed

7 files changed

+197
-106
lines changed

api/crds/manifests/usage.openmcp.cloud_mcpusages.yaml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,22 @@ spec:
1313
kind: MCPUsage
1414
listKind: MCPUsageList
1515
plural: mcpusages
16+
shortNames:
17+
- mcpu
1618
singular: mcpusage
17-
scope: Namespaced
19+
scope: Cluster
1820
versions:
19-
- name: v1
21+
- additionalPrinterColumns:
22+
- jsonPath: .spec.project
23+
name: Project
24+
type: string
25+
- jsonPath: .spec.workspace
26+
name: Workspace
27+
type: string
28+
- jsonPath: .spec.mcp
29+
name: MCP
30+
type: string
31+
name: v1
2032
schema:
2133
openAPIV3Schema:
2234
description: MCPUsage is the Schema for the mcpdailies API.

api/usage/v1/mcpusage_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ func NewDailyUsage(date time.Time, hours int) (DailyUsage, error) {
7474

7575
// +kubebuilder:object:root=true
7676
// +kubebuilder:subresource:status
77+
// +kubebuilder:resource:scope=Cluster,shortName=mcpu
7778
// +kubebuilder:metadata:labels="openmcp.cloud/cluster=onboarding"
79+
// +kubebuilder:printcolumn:name="Project",type=string,JSONPath=`.spec.project`
80+
// +kubebuilder:printcolumn:name="Workspace",type=string,JSONPath=`.spec.workspace`
81+
// +kubebuilder:printcolumn:name="MCP",type=string,JSONPath=`.spec.mcp`
7882

7983
// MCPUsage is the Schema for the mcpdailies API.
8084
type MCPUsage struct {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/openmcp-project/usage-operator
33
go 1.24.4
44

55
require (
6+
github.com/google/uuid v1.6.0
67
github.com/onsi/ginkgo/v2 v2.23.4
78
github.com/onsi/gomega v1.37.0
89
github.com/openmcp-project/controller-utils v0.10.0
@@ -45,7 +46,6 @@ require (
4546
github.com/google/gnostic-models v0.6.9 // indirect
4647
github.com/google/go-cmp v0.7.0 // indirect
4748
github.com/google/pprof v0.0.0-20250403155104-27863c87afa6 // indirect
48-
github.com/google/uuid v1.6.0 // indirect
4949
github.com/grpc-ecosystem/grpc-gateway/v2 v2.24.0 // indirect
5050
github.com/inconshreveable/mousetrap v1.1.0 // indirect
5151
github.com/josharian/intern v1.0.0 // indirect

internal/controller/managedcontrolplane_controller.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,6 @@ func (r *ManagedControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.
9393
return ctrl.Result{}, client.IgnoreNotFound(err)
9494
}
9595

96-
// if mcp.Status.ObservedGeneration == 0 {
97-
// log.Info("mcp '" + mcp.Name + "' was created.")
98-
// return ctrl.Result{}, nil
99-
// }
100-
101-
// if mcp.Status.ObservedGeneration != mcp.Generation {
102-
// log.Info("mcp '" + mcp.Name + "' was updated.")
103-
// return ctrl.Result{}, nil
104-
// }
105-
106-
// log.Info("mcp '" + mcp.Name + "' got just a status update.")
107-
10896
return ctrl.Result{}, nil
10997
}
11098

internal/helper/clusteraccess.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,18 @@ func GetOnboardingCluster(ctx context.Context, log logging.Logger, client client
4949
Rules: []rbacv1.PolicyRule{
5050
{
5151
APIGroups: []string{"core.openmcp.cloud"},
52-
Resources: []string{"managedcontrolplanes", "managedcontrolplanes/status"},
53-
Verbs: []string{"get", "list"},
52+
Resources: []string{
53+
"managedcontrolplanes", "managedcontrolplanes/status",
54+
"projects", "projects/status",
55+
"workspaces", "workspaces/status",
56+
},
57+
Verbs: []string{"get", "list", "watch"},
5458
},
5559
{
5660
APIGroups: []string{"apiextensions.k8s.io"},
5761
Resources: []string{"customresourcedefinitions"},
58-
Verbs: []string{"create", "update", "delete"},
59-
ResourceNames: []string{"mcpusage"},
62+
Verbs: []string{"get", "patch", "create", "update", "delete"},
63+
ResourceNames: []string{"mcpusages.usage.openmcp.cloud"},
6064
},
6165
{
6266
APIGroups: []string{"usage.openmcp.cloud"},

internal/usage/helper.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package usage
22

33
import (
4+
"errors"
45
"sort"
56
"time"
67

78
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
89
"sigs.k8s.io/controller-runtime/pkg/client"
910

11+
"github.com/google/uuid"
12+
1013
v1 "github.com/openmcp-project/usage-operator/api/usage/v1"
1114
)
1215

@@ -52,11 +55,17 @@ func GetNamespacedName(project, workspace string) string {
5255
return "project-" + project + "--ws-" + workspace
5356
}
5457

55-
func GetObjectKey(project, workspace, mcp string) client.ObjectKey {
56-
return client.ObjectKey{
57-
Name: mcp,
58-
Namespace: GetNamespacedName(project, workspace),
58+
func GetObjectKey(project, workspace, mcp string) (client.ObjectKey, error) {
59+
name := GetNamespacedName(project, workspace) + "-" + mcp
60+
id := uuid.NewSHA1(uuid.Nil, []byte(name))
61+
62+
if id.String() == "" {
63+
return client.ObjectKey{}, errors.New("can't generate uuid from input")
5964
}
65+
66+
return client.ObjectKey{
67+
Name: id.String(),
68+
}, nil
6069
}
6170

6271
// merges two DailyUsages where no Date is double

0 commit comments

Comments
 (0)