Skip to content

Commit

Permalink
Fix deleting metric
Browse files Browse the repository at this point in the history
  • Loading branch information
bmangoen committed May 2, 2023
1 parent 746e91e commit 903c327
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
52 changes: 48 additions & 4 deletions pkg/controller/servicemesh/memberroll/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ func (r *MemberRollReconciler) Reconcile(request reconcile.Request) (reconcile.R
if err != nil {
if errors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
internalmetrics.ResetMeshMembers()
return reconcile.Result{}, nil
}
// Error reading the object
Expand All @@ -227,6 +226,26 @@ func (r *MemberRollReconciler) Reconcile(request reconcile.Request) (reconcile.R
// attempt will be triggered when the ServiceMeshMember object deletion event arrives.
return reconcile.Result{}, nil
}
// Fetch associated control plane object
// for deleting properly the ServiceMeshMemberRoll metric.
controlPlane, err := r.fetchControlPlane(ctx, object.Namespace)
if err != nil {
if errors.IsNotFound(err) {
// Request controlPlane object not found, could have been deleted after reconcile request.
return reconcile.Result{}, nil
}
// Error reading the controlPlane object
return reconcile.Result{}, err
}
if controlPlane != nil {
meshNamespace := controlPlane.GetNamespace()
meshVersion := controlPlane.Spec.Version
meshMode, err := getMeshMode(controlPlane)
if err != nil {
return reconcile.Result{}, err
}
internalmetrics.DeleteMeshMembersWithLabelsValues(meshNamespace, meshVersion, meshMode)
}
err = common.RemoveFinalizer(ctx, object, r.Client)
return reconcile.Result{}, err
}
Expand Down Expand Up @@ -262,10 +281,8 @@ func (r *MemberRollReconciler) reconcileObject(ctx context.Context, roll *maistr
switch len(meshList.Items) {
case 0:
mesh = nil
internalmetrics.ResetMeshMembers()
case 1:
mesh = &meshList.Items[0]
internalmetrics.ResetMeshMembers()
default: // more than 1 SMCP found
reason := maistrav1.ConditionReasonMultipleSMCP
message := "Multiple ServiceMeshControlPlane resources exist in the namespace"
Expand Down Expand Up @@ -422,11 +439,18 @@ func (r *MemberRollReconciler) reconcileObject(ctx context.Context, roll *maistr
return reconcile.Result{}, kialiErr
}
if mesh != nil {
meshNamespace := mesh.GetNamespace()
meshVersion := mesh.Spec.Version
meshMode, err := getMeshMode(mesh)
if err != nil {
return reconcile.Result{}, err
}
internalmetrics.GetMeshMembers(mesh.GetNamespace(), mesh.Spec.Version, meshMode).Set(float64(len(roll.Status.ConfiguredMembers)))
// Control plane mode would not be well set yet
if meshMode == "" {
return reconcile.Result{}, nil
}
internalmetrics.GetMeshMembers(meshNamespace, meshVersion, meshMode).
Set(float64(len(roll.Status.ConfiguredMembers)))
}
return reconcile.Result{}, nil
}
Expand Down Expand Up @@ -735,3 +759,23 @@ func getMeshMode(mesh *maistrav2.ServiceMeshControlPlane) (string, error) {
}
return internalmetrics.ControlPlaneModeValueMultiTenant, nil
}

// Fetchs the associated Service Mesh ControlPlane of the MemberRoll
// Used for the internal custom metrics
func (r *MemberRollReconciler) fetchControlPlane(ctx context.Context, controlPlaneNamespace string) (*maistrav2.ServiceMeshControlPlane, error) {
var mesh *maistrav2.ServiceMeshControlPlane
meshList := &maistrav2.ServiceMeshControlPlaneList{}
err := r.Client.List(ctx, meshList, client.InNamespace(controlPlaneNamespace))
if err != nil {
return nil, pkgerrors.Wrap(err, "Error retrieving ServiceMeshControlPlane resources")
}
switch len(meshList.Items) {
case 0:
mesh = nil
case 1:
mesh = &meshList.Items[0]
default: // more than 1 SMCP found
return nil, pkgerrors.Wrap(err, "Multiple ServiceMeshControlPlane resources exist in the namespace")
}
return mesh, nil
}
8 changes: 6 additions & 2 deletions pkg/internalmetrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func GetMeshMembers(smcpNamespace, smcpVersion, smcpMode string) prometheus.Gaug
})
}

func ResetMeshMembers() {
Metrics.MeshMembers.Reset()
func DeleteMeshMembersWithLabelsValues(smcpNamespace, smcpVersion, smcpMode string) bool {
return Metrics.MeshMembers.Delete(prometheus.Labels{
labelSMCPNamespace: smcpNamespace,
labelSMCPVersion: smcpVersion,
labelSMCPMode: smcpMode,
})
}

0 comments on commit 903c327

Please sign in to comment.