github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/dataprotection/action/builder_status.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package action
    21  
    22  import (
    23  	corev1 "k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  
    26  	dpv1alpha1 "github.com/1aal/kubeblocks/apis/dataprotection/v1alpha1"
    27  )
    28  
    29  type statusBuilder struct {
    30  	status *dpv1alpha1.ActionStatus
    31  }
    32  
    33  func newStatusBuilder(a Action) *statusBuilder {
    34  	sb := &statusBuilder{
    35  		status: &dpv1alpha1.ActionStatus{
    36  			Name:       a.GetName(),
    37  			ActionType: a.Type(),
    38  			Phase:      dpv1alpha1.ActionPhaseRunning,
    39  		},
    40  	}
    41  	return sb.startTimestamp(nil)
    42  }
    43  
    44  func (b *statusBuilder) phase(phase dpv1alpha1.ActionPhase) *statusBuilder {
    45  	b.status.Phase = phase
    46  	return b
    47  }
    48  
    49  func (b *statusBuilder) reason(reason string) *statusBuilder {
    50  	b.status.FailureReason = reason
    51  	return b
    52  }
    53  
    54  func (b *statusBuilder) startTimestamp(timestamp *metav1.Time) *statusBuilder {
    55  	t := timestamp
    56  	if t == nil {
    57  		t = &metav1.Time{
    58  			Time: metav1.Now().UTC(),
    59  		}
    60  	}
    61  	b.status.StartTimestamp = t
    62  	return b
    63  }
    64  
    65  func (b *statusBuilder) completionTimestamp(timestamp *metav1.Time) *statusBuilder {
    66  	t := timestamp
    67  	if t == nil {
    68  		t = &metav1.Time{
    69  			Time: metav1.Now().UTC(),
    70  		}
    71  	}
    72  	b.status.CompletionTimestamp = t
    73  	return b
    74  }
    75  
    76  func (b *statusBuilder) objectRef(objectRef *corev1.ObjectReference) *statusBuilder {
    77  	b.status.ObjectRef = objectRef
    78  	return b
    79  }
    80  
    81  func (b *statusBuilder) withErr(err error) *statusBuilder {
    82  	if err == nil {
    83  		return b
    84  	}
    85  	b.status.FailureReason = err.Error()
    86  	b.status.Phase = dpv1alpha1.ActionPhaseFailed
    87  	return b
    88  }
    89  
    90  func (b *statusBuilder) totalSize(size string) *statusBuilder {
    91  	b.status.TotalSize = size
    92  	return b
    93  }
    94  
    95  func (b *statusBuilder) timeRange(start, end *metav1.Time) *statusBuilder {
    96  	b.status.TimeRange = &dpv1alpha1.BackupTimeRange{
    97  		Start: start,
    98  		End:   end,
    99  	}
   100  	return b
   101  }
   102  
   103  func (b *statusBuilder) build() *dpv1alpha1.ActionStatus {
   104  	return b.status
   105  }