[OOM-K8S-CERT-EXTERNAL-PROVIDER] Mock implementaion enhanced
[oom/platform/cert-service.git] / certServiceK8sExternalProvider / src / certservice-controller / certservice_issuer_status_reconciler.go
1 /*
2  * ============LICENSE_START=======================================================
3  * oom-certservice-k8s-external-provider
4  * ================================================================================
5  * Copyright (c) 2019 Smallstep Labs, Inc.
6  * Modifications copyright (C) 2020 Nokia. All rights reserved.
7  * ================================================================================
8  * This source code was copied from the following git repository:
9  * https://github.com/smallstep/step-issuer
10  * The source code was modified for usage in the ONAP project.
11  * ================================================================================
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *      http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  * ============LICENSE_END=========================================================
24  */
25
26 package certservice_controller
27
28 import (
29         "context"
30         "fmt"
31         "github.com/go-logr/logr"
32         core "k8s.io/api/core/v1"
33         meta "k8s.io/apimachinery/pkg/apis/meta/v1"
34         "onap.org/oom-certservice/k8s-external-provider/src/api"
35 )
36
37 type certServiceIssuerStatusReconciler struct {
38         *CertServiceIssuerReconciler
39         issuer *api.CertServiceIssuer
40         logger logr.Logger
41 }
42
43 func newStatusReconciler(reconciler *CertServiceIssuerReconciler, issuer *api.CertServiceIssuer, log logr.Logger) *certServiceIssuerStatusReconciler {
44         return &certServiceIssuerStatusReconciler{
45                 CertServiceIssuerReconciler: reconciler,
46                 issuer:                      issuer,
47                 logger:                      log,
48         }
49 }
50
51 func (reconciler *certServiceIssuerStatusReconciler) Update(ctx context.Context, status api.ConditionStatus, reason, message string, args ...interface{}) error {
52         completeMessage := fmt.Sprintf(message, args...)
53         reconciler.setCondition(status, reason, completeMessage)
54
55         // Fire an Event to additionally inform users of the change
56         eventType := core.EventTypeNormal
57         if status == api.ConditionFalse {
58                 eventType = core.EventTypeWarning
59         }
60         reconciler.logger.Info("Firing event: ", "issuer", reconciler.issuer, "eventtype", eventType, "reason", reason, "message", completeMessage)
61         reconciler.Recorder.Event(reconciler.issuer, eventType, reason, completeMessage)
62
63         reconciler.logger.Info("Updating issuer... ")
64         return reconciler.Client.Update(ctx, reconciler.issuer)
65 }
66
67 func (reconciler *certServiceIssuerStatusReconciler) UpdateNoError(ctx context.Context, status api.ConditionStatus, reason, message string, args ...interface{}) {
68         if err := reconciler.Update(ctx, status, reason, message, args...); err != nil {
69                 reconciler.logger.Error(err, "failed to update", "status", status, "reason", reason)
70         }
71 }
72
73 // setCondition will set a 'condition' on the given api.CertServiceIssuer resource.
74 //
75 // - If no condition of the same type already exists, the condition will be
76 //   inserted with the LastTransitionTime set to the current time.
77 // - If a condition of the same type and state already exists, the condition
78 //   will be updated but the LastTransitionTime will not be modified.
79 // - If a condition of the same type and different state already exists, the
80 //   condition will be updated and the LastTransitionTime set to the current
81 //   time.
82 func (reconciler *certServiceIssuerStatusReconciler) setCondition(status api.ConditionStatus, reason, message string) {
83         now := meta.NewTime(reconciler.Clock.Now())
84         issuerCondition := api.CertServiceIssuerCondition{
85                 Type:               api.ConditionReady,
86                 Status:             status,
87                 Reason:             reason,
88                 Message:            message,
89                 LastTransitionTime: &now,
90         }
91
92         // Search through existing conditions
93         for i, condition := range reconciler.issuer.Status.Conditions {
94                 // Skip unrelated conditions
95                 if condition.Type != api.ConditionReady {
96                         continue
97                 }
98
99                 // If this update doesn't contain a state transition, we don't update
100                 // the conditions LastTransitionTime to Now()
101                 if condition.Status == status {
102                         issuerCondition.LastTransitionTime = condition.LastTransitionTime
103                 } else {
104                         reconciler.logger.Info("found status change for CertServiceIssuer condition; setting lastTransitionTime", "condition", condition.Type, "old_status", condition.Status, "new_status", status, "time", now.Time)
105                 }
106
107                 // Overwrite the existing condition
108                 reconciler.issuer.Status.Conditions[i] = issuerCondition
109                 return
110         }
111
112         // If we've not found an existing condition of this type, we simply insert
113         // the new condition into the slice.
114         reconciler.issuer.Status.Conditions = append(reconciler.issuer.Status.Conditions, issuerCondition)
115         reconciler.logger.Info("setting lastTransitionTime for CertServiceIssuer condition", "condition", api.ConditionReady, "time", now.Time)
116 }