Skip to content

Commit

Permalink
Merge pull request #323 from Vincent056/starttime-end
Browse files Browse the repository at this point in the history
Add Start and End time to CRD
  • Loading branch information
openshift-merge-robot authored Jun 13, 2023
2 parents abe965b + 3ff6d38 commit a35521c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Versioning](https://semver.org/spec/v2.0.0.html).

### Enhancements

- Added start and end timestamp to the ComplianceScan CRD status.

- The operator can now be deployed on HyperShift HostedCluster using OLM with
a special subscription file in `config/catalog/subscriptions-hypershift.yaml`.
This can be used to deploy from both downstream and upstream source. See
Expand Down
8 changes: 8 additions & 0 deletions config/crd/bases/compliance.openshift.io_compliancescans.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ spec:
scans, this marks the amount that have been executed.
format: int64
type: integer
endTimestamp:
description: Is the time when the scan was finished
format: date-time
type: string
errormsg:
description: If there are issues on the scan, this will be filled
up with an error message.
Expand Down Expand Up @@ -369,6 +373,10 @@ spec:
description: 'Namespace of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/'
type: string
type: object
startTimestamp:
description: Is the time when the scan was started
format: date-time
type: string
warnings:
description: If there are warnings on the scan, this will be filled
up with warning messages.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,10 @@ spec:
multiple scans, this marks the amount that have been executed.
format: int64
type: integer
endTimestamp:
description: Is the time when the scan was finished
format: date-time
type: string
errormsg:
description: If there are issues on the scan, this will be filled
up with an error message.
Expand Down Expand Up @@ -469,6 +473,10 @@ spec:
description: 'Namespace of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/'
type: string
type: object
startTimestamp:
description: Is the time when the scan was started
format: date-time
type: string
warnings:
description: If there are warnings on the scan, this will be
filled up with warning messages.
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/compliance/v1alpha1/compliancescan_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ type ComplianceScanStatus struct {
Conditions Conditions `json:"conditions,omitempty"`
//Is the number of retries left for the scan on timeout
RemainingRetries int `json:"remainingRetries,omitempty"`
// Is the time when the scan was started
StartTimestamp *metav1.Time `json:"startTimestamp,omitempty"`
// Is the time when the scan was finished
EndTimestamp *metav1.Time `json:"endTimestamp,omitempty"`
}

// StorageReference stores a reference to where certain objects are being stored
Expand Down
8 changes: 8 additions & 0 deletions pkg/apis/compliance/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions pkg/controller/compliancescan/compliancescan_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -215,6 +216,7 @@ func (r *ReconcileComplianceScan) validate(instance *compv1alpha1.ComplianceScan
instanceCopy.Status.RemainingRetries = instance.Spec.MaxRetryOnTimeout
}
instanceCopy.Status.Phase = compv1alpha1.PhasePending
instanceCopy.Status.StartTimestamp = &metav1.Time{Time: time.Now()}
instanceCopy.Status.SetConditionPending()
updateErr := r.Client.Status().Update(context.TODO(), instanceCopy)
if updateErr != nil {
Expand All @@ -240,6 +242,7 @@ func (r *ReconcileComplianceScan) validate(instance *compv1alpha1.ComplianceScan
instanceCopy.Status.Result = compv1alpha1.ResultError
instanceCopy.Status.ErrorMessage = fmt.Sprintf("Scan type '%s' is not valid", instance.Spec.ScanType)
instanceCopy.Status.Phase = compv1alpha1.PhaseDone
instanceCopy.Status.EndTimestamp = &metav1.Time{Time: time.Now()}
instanceCopy.Status.SetConditionInvalid()
updateErr := r.Client.Status().Update(context.TODO(), instanceCopy)
if updateErr != nil {
Expand Down Expand Up @@ -270,6 +273,7 @@ func (r *ReconcileComplianceScan) validate(instance *compv1alpha1.ComplianceScan
instanceCopy.Status.ErrorMessage = fmt.Sprintf("Error parsing RawResultsStorageSize: %s", err)
instanceCopy.Status.Result = compv1alpha1.ResultError
instanceCopy.Status.Phase = compv1alpha1.PhaseDone
instanceCopy.Status.EndTimestamp = &metav1.Time{Time: time.Now()}
instanceCopy.Status.SetConditionInvalid()
err := r.Client.Status().Update(context.TODO(), instanceCopy)
if err != nil {
Expand All @@ -289,20 +293,24 @@ func (r *ReconcileComplianceScan) phasePendingHandler(instance *compv1alpha1.Com
if instance.NeedsRescan() {
instanceCopy := instance.DeepCopy()
delete(instanceCopy.Annotations, compv1alpha1.ComplianceScanRescanAnnotation)
delete(instanceCopy.Annotations, compv1alpha1.ComplianceScanTimeoutAnnotation)
err := r.Client.Update(context.TODO(), instanceCopy)
return reconcile.Result{}, err
}

if instance.NeedsTimeoutRescan() {
instanceCopy := instance.DeepCopy()
delete(instanceCopy.Annotations, compv1alpha1.ComplianceScanTimeoutAnnotation)
delete(instanceCopy.Annotations, compv1alpha1.ComplianceScanTimeoutAnnotation)
err := r.Client.Update(context.TODO(), instanceCopy)
return reconcile.Result{}, err
}

// Update the scan instance, the next phase is running
instance.Status.Phase = compv1alpha1.PhaseLaunching
instance.Status.Result = compv1alpha1.ResultNotAvailable
instance.Status.StartTimestamp = &metav1.Time{Time: time.Now()}
instance.Status.EndTimestamp = nil
err := r.Client.Status().Update(context.TODO(), instance)
if err != nil {
logger.Error(err, "Cannot update the status")
Expand Down Expand Up @@ -363,6 +371,7 @@ func (r *ReconcileComplianceScan) phaseLaunchingHandler(h scanTypeHandler, logge
scanCopy.Status.ErrorMessage = err.Error()
scanCopy.Status.Result = compv1alpha1.ResultError
scanCopy.Status.Phase = compv1alpha1.PhaseDone
scanCopy.Status.EndTimestamp = &metav1.Time{Time: time.Now()}
scanCopy.Status.SetConditionInvalid()
if updateerr := r.Client.Status().Update(context.TODO(), scanCopy); updateerr != nil {
logger.Error(updateerr, "Failed to update a scan")
Expand Down Expand Up @@ -398,8 +407,10 @@ func (r *ReconcileComplianceScan) phaseRunningHandler(h scanTypeHandler, logger
scan = scan.DeepCopy()

if scan.NeedsTimeoutRescan() {
// If we already have rescan annotation, we need to update the scan status
return r.updateScanStatusOnTimeout(scan, timeoutNodes, logger)
} else {
// If we don't have rescan annotation, we need to add them
return r.setAnnotationOnTimeout(scan, timeoutNodes, logger)
}
}
Expand All @@ -426,6 +437,7 @@ func (r *ReconcileComplianceScan) updateScanStatusOnTimeout(scan *compv1alpha1.C
var err error
// If we have retries left, let's retry the scan
scan.Status.Phase = compv1alpha1.PhaseDone
scan.Status.EndTimestamp = &metav1.Time{Time: time.Now()}
scan.Status.Result = compv1alpha1.ResultError
scan.Status.ErrorMessage = "Timeout while waiting for the scan pod to be finished."
scan.Status.SetConditionTimeout()
Expand Down Expand Up @@ -482,6 +494,7 @@ func (r *ReconcileComplianceScan) phaseAggregatingHandler(h scanTypeHandler, log

if err != nil {
instance.Status.Phase = compv1alpha1.PhaseDone
instance.Status.EndTimestamp = &metav1.Time{Time: time.Now()}
instance.Status.Result = compv1alpha1.ResultError
instance.Status.SetConditionInvalid()
instance.Status.ErrorMessage = err.Error()
Expand Down Expand Up @@ -543,6 +556,7 @@ func (r *ReconcileComplianceScan) phaseAggregatingHandler(h scanTypeHandler, log
}

instance.Status.Phase = compv1alpha1.PhaseDone
instance.Status.EndTimestamp = &metav1.Time{Time: time.Now()}
instance.Status.SetConditionReady()
err = r.updateStatusWithEvent(instance, logger)
if err != nil {
Expand Down Expand Up @@ -615,6 +629,7 @@ func (r *ReconcileComplianceScan) phaseDoneHandler(h scanTypeHandler, instance *
instanceCopy := instance.DeepCopy()
instanceCopy.Status.Phase = compv1alpha1.PhasePending
instanceCopy.Status.Result = compv1alpha1.ResultNotAvailable
instanceCopy.Status.StartTimestamp = &metav1.Time{Time: time.Now()}
if instance.Status.CurrentIndex == math.MaxInt64 {
instanceCopy.Status.CurrentIndex = 0
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package compliancescan
import (
"context"
"fmt"
"time"

"github.com/ComplianceAsCode/compliance-operator/pkg/controller/metrics"
"github.com/ComplianceAsCode/compliance-operator/pkg/controller/metrics/metricsfakes"
Expand Down Expand Up @@ -205,6 +206,7 @@ var _ = Describe("Testing compliancescan controller phases", func() {
BeforeEach(func() {
// Set state to RUNNING
compliancescaninstance.Status.Phase = compv1alpha1.PhaseLaunching
compliancescaninstance.Status.StartTimestamp = &metav1.Time{Time: time.Now()}
err := reconciler.Client.Status().Update(context.TODO(), compliancescaninstance)
Expect(err).To(BeNil())
})
Expand Down

0 comments on commit a35521c

Please sign in to comment.