Skip to content

Commit

Permalink
working version of deployment counter and duration metrics
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT committed Oct 5, 2022
1 parent 0ac082b commit 210ec2d
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 22 deletions.
8 changes: 4 additions & 4 deletions operator/api/v1alpha1/keptntask_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ type SecureParameters struct {
type KeptnTaskStatus struct {
JobName string `json:"jobName,omitempty"`
Status common.KeptnState `json:"status,omitempty"`
StartTime time.Time `json:"startTime,omitempty"`
EndTime time.Time `json:"endTime,omitempty"`
StartTime metav1.Time `json:"startTime,omitempty"`
EndTime metav1.Time `json:"endTime,omitempty"`
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}
Expand Down Expand Up @@ -87,12 +87,12 @@ func init() {

func (i KeptnTask) SetStartTime() {
if i.Status.StartTime.IsZero() {
i.Status.StartTime = time.Now().UTC()
i.Status.StartTime = metav1.NewTime(time.Now().UTC())
}
}

func (i KeptnTask) SetEndTime() {
if i.Status.EndTime.IsZero() {
i.Status.EndTime = time.Now().UTC()
i.Status.EndTime = metav1.NewTime(time.Now().UTC())
}
}
24 changes: 12 additions & 12 deletions operator/api/v1alpha1/keptnworkloadinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ type KeptnWorkloadInstanceStatus struct {
PostDeploymentStatus common.KeptnState `json:"postDeploymentStatus,omitempty"`
PreDeploymentTaskStatus []WorkloadTaskStatus `json:"preDeploymentTaskStatus,omitempty"`
PostDeploymentTaskStatus []WorkloadTaskStatus `json:"postDeploymentTaskStatus,omitempty"`
StartTime time.Time `json:"startTime,omitempty"`
EndTime time.Time `json:"endTime,omitempty"`
StartTime metav1.Time `json:"startTime,omitempty"`
EndTime metav1.Time `json:"endTime,omitempty"`
}

type WorkloadTaskStatus struct {
TaskDefinitionName string `json:"TaskDefinitionName,omitempty"`
Status common.KeptnState `json:"status,omitempty"`
TaskName string `json:"taskName,omitempty"`
StartTime time.Time `json:"startTime,omitempty"`
EndTime time.Time `json:"endTime,omitempty"`
StartTime metav1.Time `json:"startTime,omitempty"`
EndTime metav1.Time `json:"endTime,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down Expand Up @@ -97,26 +97,26 @@ func (i KeptnWorkloadInstance) IsDeploymentCompleted() bool {
return i.Status.DeploymentStatus.IsCompleted()
}

func (i KeptnWorkloadInstance) SetStartTime() {
func (i *KeptnWorkloadInstance) SetStartTime() {
if i.Status.StartTime.IsZero() {
i.Status.StartTime = time.Now().UTC()
i.Status.StartTime = metav1.NewTime(time.Now().UTC())
}
}

func (i KeptnWorkloadInstance) SetEndTime() {
func (i *KeptnWorkloadInstance) SetEndTime() {
if i.Status.EndTime.IsZero() {
i.Status.EndTime = time.Now().UTC()
i.Status.EndTime = metav1.NewTime(time.Now().UTC())
}
}

func (i WorkloadTaskStatus) SetStartTime() {
func (i *WorkloadTaskStatus) SetStartTime() {
if i.StartTime.IsZero() {
i.StartTime = time.Now().UTC()
i.StartTime = metav1.NewTime(time.Now().UTC())
}
}

func (i WorkloadTaskStatus) SetEndTime() {
func (i *WorkloadTaskStatus) SetEndTime() {
if i.EndTime.IsZero() {
i.EndTime = time.Now().UTC()
i.EndTime = metav1.NewTime(time.Now().UTC())
}
}
16 changes: 13 additions & 3 deletions operator/api/v1alpha1/zz_generated.deepcopy.go

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

6 changes: 6 additions & 0 deletions operator/config/crd/bases/lifecycle.keptn.sh_keptntasks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,14 @@ spec:
status:
description: KeptnTaskStatus defines the observed state of KeptnTask
properties:
endTime:
format: date-time
type: string
jobName:
type: string
startTime:
format: date-time
type: string
status:
type: string
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ spec:
deploymentStatus:
default: Pending
type: string
endTime:
format: date-time
type: string
postDeploymentStatus:
default: Pending
type: string
Expand All @@ -112,6 +115,12 @@ spec:
properties:
TaskDefinitionName:
type: string
endTime:
format: date-time
type: string
startTime:
format: date-time
type: string
status:
type: string
taskName:
Expand All @@ -126,12 +135,21 @@ spec:
properties:
TaskDefinitionName:
type: string
endTime:
format: date-time
type: string
startTime:
format: date-time
type: string
status:
type: string
taskName:
type: string
type: object
type: array
startTime:
format: date-time
type: string
type: object
type: object
served: true
Expand Down
2 changes: 1 addition & 1 deletion operator/config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ kind: Kustomization
images:
- name: controller
newName: docker.io/odubajdt/keptn-lifecycle-operator
newTag: metrics
newTag: metrics4
16 changes: 14 additions & 2 deletions operator/controllers/keptnworkloadinstance/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/keptn-sandbox/lifecycle-controller/operator/api/v1alpha1"
Expand Down Expand Up @@ -123,8 +125,15 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr
return ctrl.Result{Requeue: true, RequeueAfter: 5 * time.Second}, nil
}

// WorkloadInstance is completed at this place

workloadInstance.SetEndTime()

err = r.Client.Status().Update(ctx, workloadInstance)
if err != nil {
return ctrl.Result{Requeue: true}, err
}

attrs := []attribute.KeyValue{
attribute.Key("KeptnApp").String(workloadInstance.Spec.AppName),
attribute.Key("KeptnWorkload").String(workloadInstance.Spec.WorkloadName),
Expand All @@ -133,11 +142,13 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr
attribute.Key("Status").String(string(workloadInstance.Status.PostDeploymentStatus)),
}

r.Log.Info("Increasing deployment count")

// metrics: increment deployment counter
r.Meters.DeploymentCount.Add(ctx, 1, attrs...)

// metrics: add deployment duration
duration := workloadInstance.Status.EndTime.Sub(workloadInstance.Status.StartTime)
duration := workloadInstance.Status.EndTime.Time.Sub(workloadInstance.Status.StartTime.Time)
r.Meters.DeploymentDuration.Record(ctx, duration.Seconds(), attrs...)

return ctrl.Result{}, nil
Expand All @@ -146,7 +157,8 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr
// SetupWithManager sets up the controller with the Manager.
func (r *KeptnWorkloadInstanceReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&klcv1alpha1.KeptnWorkloadInstance{}).
// predicate disabling the auto reconciliation after updating the object status
For(&klcv1alpha1.KeptnWorkloadInstance{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Complete(r)
}

Expand Down

0 comments on commit 210ec2d

Please sign in to comment.