-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
runtime/metrics: Delete metrics on object delete
Delete the object metrics when the object is deleted. This ensures that stale metrics about a deleted object is no longer exported. As a result, the `ConditionDelete` is no longer needed. Another reason to not have `ConditionDelete` is that a condition can only be one of True, False or Unknown. This introduces new delete methods in the low level metrics Recorder. In the high level controller metrics, a list of owned finalizers is introduced which is used to determine if an object is being deleted. The existing Record*() methods are updated to check if the given object is deleted, and call record or delete based on that. This helps make this change transparent. The user of this API has to pass in the finalizer they write on object they maintain to the metrics recorder and record the metrics at the very end of the reconciliation so that the final object state can be used to determine if the metrics can be deleted safely. Signed-off-by: Sunny <darkowlzz@protonmail.com>
- Loading branch information
Showing
5 changed files
with
195 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
Copyright 2023 The Flux authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package controller | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
. "github.com/onsi/gomega" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/fluxcd/pkg/runtime/conditions/testdata" | ||
) | ||
|
||
func TestMetrics_IsDelete(t *testing.T) { | ||
testFinalizers := []string{"finalizers.fluxcd.io", "finalizers.foo.bar"} | ||
timenow := metav1.NewTime(time.Now()) | ||
|
||
tests := []struct { | ||
name string | ||
finalizers []string | ||
deleteTimestamp *metav1.Time | ||
ownedFinalizers []string | ||
want bool | ||
}{ | ||
{"equal finalizers, no delete timestamp", testFinalizers, nil, testFinalizers, false}, | ||
{"partial finalizers, no delete timestamp", []string{"finalizers.fluxcd.io"}, nil, testFinalizers, false}, | ||
{"unknown finalizers, no delete timestamp", []string{"foo"}, nil, testFinalizers, false}, | ||
{"unknown finalizers, delete timestamp", []string{"foo"}, &timenow, testFinalizers, true}, | ||
{"no finalizers, no delete timestamp", []string{}, nil, testFinalizers, false}, | ||
{"no owned finalizers, no delete timestamp", []string{"foo"}, nil, nil, false}, | ||
{"no finalizers, delete timestamp", []string{}, &timenow, testFinalizers, true}, | ||
{"no finalizers, no delete timestamp", nil, nil, nil, false}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
metrics := Metrics{ownedFinalizers: tt.ownedFinalizers} | ||
obj := &testdata.Fake{} | ||
obj.SetFinalizers(tt.finalizers) | ||
obj.SetDeletionTimestamp(tt.deleteTimestamp) | ||
g.Expect(metrics.IsDelete(obj)).To(Equal(tt.want)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters