[OOM-K8S-CERT-EXTERNAL-PROVIDER] Refactoring & code improvements
[oom/platform/cert-service.git] / certServiceK8sExternalProvider / src / cmpv2controller / updater / certificate_request_status_updater.go
1 /*
2  * ============LICENSE_START=======================================================
3  * oom-certservice-k8s-external-provider
4  * ================================================================================
5  * Copyright (C) 2020 Nokia. All rights reserved.
6  * ================================================================================
7  * This source code was copied from the following git repository:
8  * https://github.com/smallstep/step-issuer
9  * The source code was modified for usage in the ONAP project.
10  * ================================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ============LICENSE_END=========================================================
23  */
24
25 package updater
26
27 import (
28         "context"
29         "fmt"
30
31         apiutil "github.com/jetstack/cert-manager/pkg/api/util"
32         cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
33         cmmeta "github.com/jetstack/cert-manager/pkg/apis/meta/v1"
34         "k8s.io/client-go/tools/record"
35         "sigs.k8s.io/controller-runtime/pkg/client"
36
37         "onap.org/oom-certservice/k8s-external-provider/src/leveledlogger"
38 )
39
40 type CertificateRequestStatusUpdater struct {
41         client             client.Client
42         recorder           record.EventRecorder
43         logger             leveledlogger.Logger
44         ctx                context.Context
45         certificateRequest *cmapi.CertificateRequest
46 }
47
48 func NewCertificateRequestUpdater(client client.Client,
49         recorder record.EventRecorder, certificateRequest *cmapi.CertificateRequest, ctx context.Context, log leveledlogger.Logger) *CertificateRequestStatusUpdater {
50         return &CertificateRequestStatusUpdater{
51                 client:             client,
52                 recorder:           recorder,
53                 logger:             log,
54                 ctx:                ctx,
55                 certificateRequest: certificateRequest,
56         }
57 }
58
59 func (instance *CertificateRequestStatusUpdater) UpdateStatusWithEventTypeWarning(reason string, message string, args ...interface{}) error {
60         return instance.updateStatus(cmmeta.ConditionFalse, reason, message, args...)
61 }
62
63 func (instance *CertificateRequestStatusUpdater) UpdateCertificateRequestWithSignedCertificates() error {
64         return instance.updateStatus(cmmeta.ConditionTrue, cmapi.CertificateRequestReasonIssued, "Certificate issued")
65 }
66
67 func (instance *CertificateRequestStatusUpdater) updateStatus(status cmmeta.ConditionStatus, reason string, message string, args ...interface{}) error {
68         completeMessage := fmt.Sprintf(message, args...)
69         apiutil.SetCertificateRequestCondition(instance.certificateRequest, cmapi.CertificateRequestConditionReady, status, reason, completeMessage)
70
71         FireEventCert(instance.recorder, instance.certificateRequest, status, reason, completeMessage)
72
73         return instance.client.Status().Update(instance.ctx, instance.certificateRequest)
74 }