-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat(metrics): adding certmanager_certificate_challenge_status
metric
#7736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hjoshi123
wants to merge
7
commits into
cert-manager:master
Choose a base branch
from
hjoshi123:feat/cert-challenge-status-metrics
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+255
−1
Open
Changes from all commits
Commits
Show all changes
10000
7 commits
Select commit
Hold shift + click to select a range
dd5c269
adding unit test and controller for cert challenge
hjoshi123 501a401
added uid for deletion and removed value
hjoshi123 2e50b3d
added license to acme_test
hjoshi123 d9b1594
fixing license issues
hjoshi123 d9a0284
added better tests and value change on valid status
hjoshi123 23dd8d7
added all possible states
hjoshi123 0b471f8
added acme metrics controller to allcontrollers
hjoshi123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,113 @@ | ||
/* | ||
Copyright 2025 The cert-manager 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 metrics | ||
|
||
impo 10000 rt ( | ||
"context" | ||
"fmt" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/tools/cache" | ||
"k8s.io/client-go/util/workqueue" | ||
|
||
cmacmelisters "github.com/cert-manager/cert-manager/pkg/client/listers/acme/v1" | ||
controllerpkg "github.com/cert-manager/cert-manager/pkg/controller" | ||
"github.com/cert-manager/cert-manager/pkg/metrics" | ||
) | ||
|
||
const ( | ||
// ControllerName is the string used to refer to this controller | ||
// when enabling or disabling it from command line flags. | ||
ControllerName = "certificate-challenges-metrics" | ||
) | ||
|
||
// controllerWrapper wraps the `controller` structure to make it implement | ||
// the controllerpkg.queueingController interface | ||
type controllerWrapper struct { | ||
*controller | ||
} | ||
|
||
// This controller is synced on all Certificate 'create', 'update', and | ||
// 'delete' events which will update the metrics for that Certificate. | ||
type controller struct { | ||
certificateChallengeListers cmacmelisters.ChallengeLister | ||
|
||
metrics *metrics.Metrics | ||
} | ||
|
||
func NewController(ctx *controllerpkg.Context) (*controller, workqueue.TypedRateLimitingInterface[types.NamespacedName], []cache.InformerSynced, error) { | ||
// create a queue used to queue up items to be processed | ||
queue := workqueue.NewTypedRateLimitingQueueWithConfig( | ||
controllerpkg.DefaultCertificateRateLimiter(), | ||
workqueue.TypedRateLimitingQueueConfig[types.NamespacedName]{ | ||
Name: ControllerName, | ||
}, | ||
) | ||
|
||
certificateChallengeInformer := ctx.SharedInformerFactory.Acme().V1().Challenges() | ||
|
||
// handle all events when challenge is created, updated, or deleted. Delete shouldn't matter for challenges | ||
// but leaving the behavior of the default queueing event handler. | ||
if _, err := certificateChallengeInformer.Informer().AddEventHandler(&controllerpkg.QueuingEventHandler{ | ||
Queue: queue, | ||
}); err != nil { | ||
return nil, nil, nil, fmt.Errorf("error setting up event handler: %v", err) | ||
} | ||
|
||
// build a list of InformerSynced functions that will be returned by the | ||
// Register method. the controller will only begin processing items once all | ||
// of these informers have synced. | ||
mustSync := []cache.InformerSynced{ | ||
certificateChallengeInformer.Informer().HasSynced, | ||
} | ||
|
||
return &controller{ | ||
certificateChallengeListers: certificateChallengeInformer.Lister(), | ||
metrics: ctx.Metrics, | ||
}, queue, mustSync, nil | ||
} | ||
|
||
func (c *controller) ProcessItem(ctx context.Context, namespace types.NamespacedName) error { | ||
ns, name := namespace.Namespace, namespace.Name | ||
|
||
challenge, err := c.certificateChallengeListers.Challenges(ns).Get(name) | ||
if apierrors.IsNotFound(err) { | ||
c.metrics.RemoveChallengeStatus(challenge) | ||
return nil | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.metrics.UpdateChallengeStatus(challenge) | ||
return nil | ||
} | ||
|
||
func (c *controllerWrapper) Register(ctx *controllerpkg.Context) (workqueue.TypedRateLimitingInterface[types.NamespacedName], []cache.InformerSynced, error) { | ||
ctrl, queue, mustSync, err := NewController(ctx) | ||
c.controller = ctrl | ||
return queue, mustSync, err | ||
} | ||
|
||
func init() { | ||
controllerpkg.Register(ControllerName, func(ctx *controllerpkg.ContextFactory) (controllerpkg.Interface, error) { | ||
return controllerpkg.NewBuilder(ctx, ControllerName). | ||
For(&controllerWrapper{}). | ||
Complete() | ||
}) | ||
} |
This file contains hidden or 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 hidden or 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,88 @@ | ||
/* | ||
Copyright 2025 The cert-manager 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 metrics | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/go-logr/logr/testr" | ||
"github.com/prometheus/client_golang/prometheus/testutil" | ||
"k8s.io/utils/clock" | ||
|
||
cmacme "github.com/cert-manager/cert-manager/pkg/apis/acme/v1" | ||
"github.com/cert-manager/cert-manager/test/unit/gen" | ||
) | ||
|
||
const certificateChallengeStatusMetadata = ` | ||
# HELP certmanager_certificate_challenge_status The status of certificate challenges. | ||
# TYPE certmanager_certificate_challenge_status gauge | ||
` | ||
|
||
func TestCertificateChallengeStatusMetrics(t *testing.T) { | ||
type TestChallenge struct { | ||
challenges []*cmacme.Challenge | ||
expectedMetric string | ||
} | ||
|
||
pendingToValidChallenges := make([]*cmacme.Challenge, 0) | ||
pendingToValidChallenges = append(pendingToValidChallenges, gen.Challenge("test-challenge-status", | ||
gen.SetChallengeDNSName("example.com"), | ||
gen.SetChallengeProcessing(false), | ||
gen.SetChallengeType(cmacme.ACMEChallengeTypeDNS01), | ||
gen.SetChallengeState(cmacme.Pending), | ||
gen.SetChallengeUID("test-challenge-uid"), | ||
), gen.Challenge("test-challenge-status-2", | ||
gen.SetChallengeDNSName("example.com"), | ||
gen.SetChallengeProcessing(false), | ||
gen.SetChallengeType(cmacme.ACMEChallengeTypeDNS01), | ||
gen.SetChallengeState(cmacme.Ready), | ||
gen.SetChallengeUID("test-challenge-uid"), | ||
)) | ||
testCases := map[string]TestChallenge{ | ||
"challenge-metric-active-state-valid": { | ||
challenges: pendingToValidChallenges, | ||
expectedMetric: ` | ||
certmanager_certificate_challenge_status{domain="example.com",id="test-challenge-uid",processing="false",reason="",status="",type="DNS-01"} 0 | ||
certmanager_certificate_challenge_status{domain="example.com",id="test-challenge-uid",processing="false",reason="",status="errored",type="DNS-01"} 0 | ||
certmanager_certificate_challenge_status{domain="example.com",id="test-challenge-uid",processing="false",reason="",status="expired",type="DNS-01"} 0 | ||
certmanager_certificate_challenge_status{domain="example.com",id="test-challenge-uid",processing="false",reason="",status="invalid",type="DNS-01"} 0 | ||
certmanager_certificate_challenge_status{domain="example.com",id="test-challenge-uid",processing="false",reason="",status="processing",type="DNS-01"} 0 | ||
certmanager_certificate_challenge_status{domain="example.com",id="test-challenge-uid",processing="false",reason="",status="pending",type="DNS-01"} 0 | ||
certmanager_certificate_challenge_status{domain="example.com",id="test-challenge-uid",processing="false",reason="",status="ready",type="DNS-01"} 1 | ||
certmanager_certificate_challenge_status{domain="example.com",id="test-challenge-uid",processing="false",reason="",status="valid",type="DNS-01"} 0 | ||
`, | ||
}, | ||
} | ||
|
||
for testName, test := range testCases { | ||
t.Run(testName, func(t *testing.T) { | ||
m := New(testr.New(t), clock.RealClock{}) | ||
|
||
for _, challenge := range test.challenges { | ||
m.UpdateChallengeStatus(challenge) | ||
} | ||
|
||
if err := testutil.CollectAndCompare(m.certificateChallengeStatus, | ||
strings.NewReader(certificateChallengeStatusMetadata+test.expectedMetric), | ||
"certmanager_certificate_challenge_status", | ||
); err != nil { | ||
t.Errorf("unexpected collecting result:\n%s", err) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs adding to AllControllers as well