017e36a45d1da98dcec9b3f4d7d0b594ed7c87fc
[oom/platform/cert-service.git] / certServiceK8sExternalProvider / src / cmpv2controller / cmpv2_issuer_status_updater.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 cmpv2controller
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/cmpv2api"
35 )
36
37 type CMPv2IssuerStatusUpdater struct {
38         *CMPv2IssuerController
39         issuer *cmpv2api.CMPv2Issuer
40         logger logr.Logger
41 }
42
43 func newStatusUpdater(controller *CMPv2IssuerController, issuer *cmpv2api.CMPv2Issuer, log logr.Logger) *CMPv2IssuerStatusUpdater {
44         return &CMPv2IssuerStatusUpdater{
45                 CMPv2IssuerController: controller,
46                 issuer:                issuer,
47                 logger:                log,
48         }
49 }
50
51 func (updater *CMPv2IssuerStatusUpdater) Update(ctx context.Context, status cmpv2api.ConditionStatus, reason, message string, args ...interface{}) error {
52         completeMessage := fmt.Sprintf(message, args...)
53         updater.setCondition(status, reason, completeMessage)
54
55         // Fire an Event to additionally inform users of the change
56         eventType := core.EventTypeNormal
57         if status == cmpv2api.ConditionFalse {
58                 eventType = core.EventTypeWarning
59         }
60         updater.Recorder.Event(updater.issuer, eventType, reason, completeMessage)
61
62         return updater.Client.Update(ctx, updater.issuer)
63 }
64
65 func (updater *CMPv2IssuerStatusUpdater) UpdateNoError(ctx context.Context, status cmpv2api.ConditionStatus, reason, message string, args ...interface{}) {
66         if err := updater.Update(ctx, status, reason, message, args...); err != nil {
67                 updater.logger.Error(err, "failed to update", "status", status, "reason", reason)
68         }
69 }
70
71 // setCondition will set a 'condition' on the given cmpv2api.CMPv2Issuer resource.
72 //
73 // - If no condition of the same type already exists, the condition will be
74 //   inserted with the LastTransitionTime set to the current time.
75 // - If a condition of the same type and state already exists, the condition
76 //   will be updated but the LastTransitionTime will not be modified.
77 // - If a condition of the same type and different state already exists, the
78 //   condition will be updated and the LastTransitionTime set to the current
79 //   time.
80 func (updater *CMPv2IssuerStatusUpdater) setCondition(status cmpv2api.ConditionStatus, reason, message string) {
81         now := meta.NewTime(updater.Clock.Now())
82         issuerCondition := cmpv2api.CMPv2IssuerCondition{
83                 Type:               cmpv2api.ConditionReady,
84                 Status:             status,
85                 Reason:             reason,
86                 Message:            message,
87                 LastTransitionTime: &now,
88         }
89
90         // Search through existing conditions
91         for i, condition := range updater.issuer.Status.Conditions {
92                 // Skip unrelated conditions
93                 if condition.Type != cmpv2api.ConditionReady {
94                         continue
95                 }
96
97                 // If this update doesn't contain a state transition, we don't update
98                 // the conditions LastTransitionTime to Now()
99                 if condition.Status == status {
100                         issuerCondition.LastTransitionTime = condition.LastTransitionTime
101                 } else {
102                         updater.logger.Info("found status change for CMPv2Issuer condition; setting lastTransitionTime", "condition", condition.Type, "old_status", condition.Status, "new_status", status, "time", now.Time)
103                 }
104
105                 // Overwrite the existing condition
106                 updater.issuer.Status.Conditions[i] = issuerCondition
107                 return
108         }
109
110         // If we've not found an existing condition of this type, we simply insert
111         // the new condition into the slice.
112         updater.issuer.Status.Conditions = append(updater.issuer.Status.Conditions, issuerCondition)
113         updater.logger.Info("setting lastTransitionTime for CMPv2Issuer condition", "condition", cmpv2api.ConditionReady, "time", now.Time)
114 }