Skip to content

Commit

Permalink
duration and count of deployments working
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 5c483aa commit f2c101b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 12 deletions.
20 changes: 18 additions & 2 deletions operator/api/v1alpha1/keptntask_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package v1alpha1

import (
"time"

"github.com/keptn-sandbox/lifecycle-controller/operator/api/v1alpha1/common"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -45,8 +47,10 @@ type SecureParameters struct {

// KeptnTaskStatus defines the observed state of KeptnTask
type KeptnTaskStatus struct {
JobName string `json:"jobName,omitempty"`
Status common.KeptnState `json:"status,omitempty"`
JobName string `json:"jobName,omitempty"`
Status common.KeptnState `json:"status,omitempty"`
StartTime time.Time `json:"startTime,omitempty"`
EndTime time.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 @@ -80,3 +84,15 @@ type KeptnTaskList struct {
func init() {
SchemeBuilder.Register(&KeptnTask{}, &KeptnTaskList{})
}

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

func (i KeptnTask) SetEndTime() {
if i.Status.EndTime.IsZero() {
i.Status.EndTime = time.Now().UTC()
}
}
30 changes: 30 additions & 0 deletions operator/api/v1alpha1/keptnworkloadinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package v1alpha1

import (
"time"

"github.com/keptn-sandbox/lifecycle-controller/operator/api/v1alpha1/common"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand All @@ -40,12 +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"`
}

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"`
}

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

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

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

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

func (i WorkloadTaskStatus) SetEndTime() {
if i.EndTime.IsZero() {
i.EndTime = time.Now().UTC()
}
}
10 changes: 0 additions & 10 deletions operator/controllers/keptnworkload/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"

"github.com/go-logr/logr"
"go.opentelemetry.io/otel/attribute"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -129,14 +128,5 @@ func (r *KeptnWorkloadReconciler) createWorkloadInstance(ctx context.Context, wo
r.Log.Error(err, "could not set controller reference for WorkloadInstance: "+workloadInstance.Name)
}

// metrics: increment deployment counter
attrs := []attribute.KeyValue{
attribute.Key("KeptnApp").String(workloadInstance.Spec.AppName),
attribute.Key("KeptnWorkload").String(workloadInstance.Spec.WorkloadName),
attribute.Key("KeptnVersion").String(workloadInstance.Spec.Version),
attribute.Key("Namespace").String(workloadInstance.Namespace),
}
r.Meters.DeploymentCount.Add(ctx, 1, attrs...)

return workloadInstance, err
}
19 changes: 19 additions & 0 deletions operator/controllers/keptnworkloadinstance/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr
return reconcile.Result{}, fmt.Errorf("could not fetch KeptnWorkloadInstance: %+v", err)
}

workloadInstance.SetStartTime()

if !workloadInstance.IsPreDeploymentCompleted() {
r.Log.Info("Pre deployment checks not finished")
err := r.reconcilePreDeployment(ctx, req, workloadInstance)
Expand Down Expand Up @@ -121,6 +123,23 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr
return ctrl.Result{Requeue: true, RequeueAfter: 5 * time.Second}, nil
}

workloadInstance.SetEndTime()

attrs := []attribute.KeyValue{
attribute.Key("KeptnApp").String(workloadInstance.Spec.AppName),
attribute.Key("KeptnWorkload").String(workloadInstance.Spec.WorkloadName),
attribute.Key("KeptnVersion").String(workloadInstance.Spec.Version),
attribute.Key("Namespace").String(workloadInstance.Namespace),
attribute.Key("Status").String(string(workloadInstance.Status.PostDeploymentStatus)),
}

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

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

return ctrl.Result{}, nil
}

Expand Down

0 comments on commit f2c101b

Please sign in to comment.