Skip to content

Commit

Permalink
add support for recording build time (#468)
Browse files Browse the repository at this point in the history
* add support for recording build time

Signed-off-by: wanjunlei <wanjunlei@kubesphere.io>

* change BuildTime to BuildDuration

Signed-off-by: wanjunlei <wanjunlei@kubesphere.io>

---------

Signed-off-by: wanjunlei <wanjunlei@kubesphere.io>
  • Loading branch information
wanjunlei committed Jun 25, 2023
1 parent 8e205f2 commit 5239261
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 19 deletions.
12 changes: 7 additions & 5 deletions apis/core/v1beta2/builder_types.go
Expand Up @@ -42,7 +42,7 @@ type Strategy struct {
Kind *string `json:"kind,omitempty"`
}

// The value type contains the properties for a value, this allows for an
// SingleValue is the value type contains the properties for a value, this allows for an
// easy extension in the future to support more kinds
type SingleValue struct {

Expand Down Expand Up @@ -156,10 +156,11 @@ type BuilderOutput struct {

// BuilderStatus defines the observed state of Builder
type BuilderStatus struct {
Phase string `json:"phase,omitempty"`
State string `json:"state,omitempty"`
Reason string `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
Phase string `json:"phase,omitempty"`
State string `json:"state,omitempty"`
Reason string `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
BuildDuration *metav1.Duration `json:"buildDuration,omitempty"`
// Associate resources.
ResourceRef map[string]string `json:"resourceRef,omitempty"`
// Output holds the results emitted from step definition of an output
Expand All @@ -181,6 +182,7 @@ type BuilderStatus struct {
//+kubebuilder:printcolumn:name="Phase",type=string,JSONPath=`.status.phase`
//+kubebuilder:printcolumn:name="State",type=string,JSONPath=`.status.state`
//+kubebuilder:printcolumn:name="Reason",type=string,JSONPath=`.status.reason`
//+kubebuilder:printcolumn:name="BuildDuration",type=string,JSONPath=`.status.buildDuration`
//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"

// Builder is the Schema for the builders API
Expand Down
15 changes: 8 additions & 7 deletions apis/core/v1beta2/function_types.go
Expand Up @@ -137,13 +137,14 @@ type FunctionSpec struct {
}

type Condition struct {
State string `json:"state,omitempty"`
Reason string `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
ResourceRef string `json:"resourceRef,omitempty"`
LastSuccessfulResourceRef string `json:"lastSuccessfulResourceRef,omitempty"`
ResourceHash string `json:"resourceHash,omitempty"`
Service string `json:"service,omitempty"`
State string `json:"state,omitempty"`
Reason string `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
ResourceRef string `json:"resourceRef,omitempty"`
LastSuccessfulResourceRef string `json:"lastSuccessfulResourceRef,omitempty"`
ResourceHash string `json:"resourceHash,omitempty"`
Service string `json:"service,omitempty"`
BuildDuration *metav1.Duration `json:"buildDuration,omitempty"`
}

type FunctionAddress struct {
Expand Down
14 changes: 12 additions & 2 deletions apis/core/v1beta2/zz_generated.deepcopy.go

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

9 changes: 9 additions & 0 deletions config/bundle.yaml
Expand Up @@ -188,6 +188,9 @@ spec:
- jsonPath: .status.reason
name: Reason
type: string
- jsonPath: .status.buildDuration
name: BuildDuration
type: string
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
Expand Down Expand Up @@ -343,6 +346,8 @@ spec:
type: object
status:
properties:
buildDuration:
type: string
message:
type: string
output:
Expand Down Expand Up @@ -9362,6 +9367,8 @@ spec:
type: array
build:
properties:
buildDuration:
type: string
lastSuccessfulResourceRef:
type: string
message:
Expand Down Expand Up @@ -9453,6 +9460,8 @@ spec:
type: object
serving:
properties:
buildDuration:
type: string
lastSuccessfulResourceRef:
type: string
message:
Expand Down
5 changes: 5 additions & 0 deletions config/crd/bases/core.openfunction.io_builders.yaml
Expand Up @@ -173,6 +173,9 @@ spec:
- jsonPath: .status.reason
name: Reason
type: string
- jsonPath: .status.buildDuration
name: BuildDuration
type: string
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
Expand Down Expand Up @@ -328,6 +331,8 @@ spec:
type: object
status:
properties:
buildDuration:
type: string
message:
type: string
output:
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/core.openfunction.io_functions.yaml
Expand Up @@ -8193,6 +8193,8 @@ spec:
type: array
build:
properties:
buildDuration:
type: string
lastSuccessfulResourceRef:
type: string
message:
Expand Down Expand Up @@ -8284,6 +8286,8 @@ spec:
type: object
serving:
properties:
buildDuration:
type: string
lastSuccessfulResourceRef:
type: string
message:
Expand Down
11 changes: 11 additions & 0 deletions controllers/core/builder_controller.go
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/go-logr/logr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -103,6 +104,9 @@ func (r *BuilderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
time.Since(builder.CreationTimestamp.Time) > builder.Spec.Timeout.Duration {
builder.Status.State = openfunction.Timeout
builder.Status.Reason = openfunction.Timeout
builder.Status.Message = openfunction.Timeout
builder.Status.BuildDuration = builder.Spec.Timeout

if err := r.Status().Update(r.ctx, builder); err != nil {
log.Error(err, "Failed to update builder status")
return ctrl.Result{}, err
Expand Down Expand Up @@ -174,6 +178,11 @@ func (r *BuilderReconciler) getBuilderResult(builder *openfunction.Builder, buil
builder.Status.State = res
builder.Status.Reason = reason
builder.Status.Message = message
if !builder.CreationTimestamp.IsZero() {
builder.Status.BuildDuration = &metav1.Duration{
Duration: metav1.Now().UTC().Sub(builder.CreationTimestamp.UTC()).Truncate(time.Second),
}
}
if err := r.Status().Update(r.ctx, builder); err != nil {
return err
}
Expand Down Expand Up @@ -247,6 +256,8 @@ func (r *BuilderReconciler) buildTimeout(builder *openfunction.Builder) error {
if !b.Status.IsCompleted() {
b.Status.State = openfunction.Timeout
b.Status.Reason = openfunction.Timeout
b.Status.Message = openfunction.Timeout
b.Status.BuildDuration = builder.Spec.Timeout
return r.Status().Update(r.ctx, b)
}

Expand Down
17 changes: 15 additions & 2 deletions controllers/core/function_controller.go
Expand Up @@ -153,6 +153,7 @@ func (r *FunctionReconciler) createBuilder(fn *openfunction.Function) error {
fn.Status.Build.State = ""
fn.Status.Build.Reason = ""
fn.Status.Build.Message = ""
fn.Status.Build.BuildDuration = nil
fn.Status.Build.ResourceRef = ""
if err := r.Status().Update(r.ctx, fn); err != nil {
log.Error(err, "Failed to reset function build status")
Expand Down Expand Up @@ -211,7 +212,7 @@ func (r *FunctionReconciler) createBuilder(fn *openfunction.Function) error {
fn.Status.Build = &openfunction.Condition{
State: openfunction.Created,
ResourceRef: builder.Name,
ResourceHash: util.Hash(builder.Spec),
ResourceHash: getBuilderHash(builder.Spec),
}
if err := r.Status().Update(r.ctx, fn); err != nil {
log.Error(err, "Failed to update function build status")
Expand Down Expand Up @@ -258,6 +259,7 @@ func (r *FunctionReconciler) updateFuncWithBuilderStatus(fn *openfunction.Functi
fn.Status.Build.State = builder.Status.State
fn.Status.Build.Reason = builder.Status.Reason
fn.Status.Build.Message = builder.Status.Message
fn.Status.Build.BuildDuration = builder.Status.BuildDuration
// If build had complete, update function serving status.
if builder.Status.State == openfunction.Succeeded {
if builder.Status.Output != nil {
Expand Down Expand Up @@ -596,7 +598,7 @@ func (r *FunctionReconciler) needToCreateBuilder(fn *openfunction.Function) bool
return true
}

newHash := util.Hash(r.createBuilderSpec(fn))
newHash := getBuilderHash(r.createBuilderSpec(fn))
// Builder changed, need to create.
if newHash != fn.Status.Build.ResourceHash {
log.V(1).Info("builder changed", "old", fn.Status.Build.ResourceHash, "new", newHash)
Expand All @@ -621,6 +623,17 @@ func (r *FunctionReconciler) needToCreateBuilder(fn *openfunction.Function) bool
return false
}

func getBuilderHash(spec openfunction.BuilderSpec) string {
newSpec := spec.DeepCopy()
newSpec.SuccessfulBuildsHistoryLimit = nil
newSpec.FailedBuildsHistoryLimit = nil
newSpec.BuilderMaxAge = nil
newSpec.Timeout = nil
newSpec.State = ""

return util.Hash(newSpec)
}

func (r *FunctionReconciler) needToCreateServing(fn *openfunction.Function) bool {

log := r.Log.WithName("NeedToCreateServing").
Expand Down
21 changes: 18 additions & 3 deletions pkg/core/builder/shipwright/builderrun.go
Expand Up @@ -135,9 +135,14 @@ func (r *builderRun) Result(builder *openfunction.Builder) (string, string, stri
log := r.log.WithName("Result").
WithValues("Builder", fmt.Sprintf("%s/%s", builder.Namespace, builder.Name))

shipwrightBuildName := getName(builder, shipwrightBuildName)
if shipwrightBuildName == "" {
return "", "", "", nil
}

shipwrightBuild := &shipwrightv1alpha1.Build{
ObjectMeta: metav1.ObjectMeta{
Name: getName(builder, shipwrightBuildName),
Name: shipwrightBuildName,
Namespace: builder.Namespace,
},
}
Expand All @@ -156,9 +161,14 @@ func (r *builderRun) Result(builder *openfunction.Builder) (string, string, stri
return "", "", "", nil
}

shipwrightBuildRunName := getName(builder, shipwrightBuildRunName)
if shipwrightBuildRunName == "" {
return "", "", "", nil
}

shipwrightBuildRun := &shipwrightv1alpha1.BuildRun{
ObjectMeta: metav1.ObjectMeta{
Name: getName(builder, shipwrightBuildRunName),
Name: shipwrightBuildRunName,
Namespace: builder.Namespace,
},
}
Expand Down Expand Up @@ -263,9 +273,14 @@ func (r *builderRun) Cancel(builder *openfunction.Builder) error {
log := r.log.WithName("Cancel").
WithValues("Builder", fmt.Sprintf("%s/%s", builder.Namespace, builder.Name))

shipwrightBuildRunName := getName(builder, shipwrightBuildRunName)
if shipwrightBuildRunName == "" {
return nil
}

shipwrightBuildRun := &shipwrightv1alpha1.BuildRun{
ObjectMeta: metav1.ObjectMeta{
Name: getName(builder, shipwrightBuildRunName),
Name: shipwrightBuildRunName,
Namespace: builder.Namespace,
},
}
Expand Down

0 comments on commit 5239261

Please sign in to comment.