cert-manager/pkg/controller/certificaterequests/util/reporter.go
JoshVanL 0eb4ef385b Change CR reporter to be a long lived struct
Signed-off-by: JoshVanL <vleeuwenjoshua@gmail.com>
2019-08-13 11:36:53 +01:00

62 lines
2.0 KiB
Go

/*
Copyright 2019 The Jetstack cert-manager contributors.
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 util
import (
"fmt"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"
"k8s.io/utils/clock"
apiutil "github.com/jetstack/cert-manager/pkg/api/util"
"github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha1"
)
type Reporter struct {
clock clock.Clock
recorder record.EventRecorder
}
func NewReporter(clock clock.Clock, recorder record.EventRecorder) *Reporter {
return &Reporter{
clock: clock,
recorder: recorder,
}
}
func (r *Reporter) Failed(cr *v1alpha1.CertificateRequest, err error, reason, message string) {
// Set the FailureTime to c.clock.Now(), only if it has not been already set.
if cr.Status.FailureTime == nil {
nowTime := metav1.NewTime(r.clock.Now())
cr.Status.FailureTime = &nowTime
}
message = fmt.Sprintf("%s: %v", message, err)
r.recorder.Event(cr, corev1.EventTypeWarning, reason, message)
apiutil.SetCertificateRequestCondition(cr, v1alpha1.CertificateRequestConditionReady,
v1alpha1.ConditionFalse, v1alpha1.CertificateRequestReasonFailed, message)
}
func (r *Reporter) Pending(cr *v1alpha1.CertificateRequest, err error, reason, message string) {
message = fmt.Sprintf("%s: %v", message, err)
r.recorder.Event(cr, corev1.EventTypeNormal, reason, message)
apiutil.SetCertificateRequestCondition(cr, v1alpha1.CertificateRequestConditionReady,
v1alpha1.ConditionFalse, v1alpha1.CertificateRequestReasonPending, message)
}