github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/store/sessions/object.go (about)

     1  package sessions
     2  
     3  import (
     4  	"os"
     5  	"time"
     6  
     7  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     8  
     9  	"github.com/tilt-dev/tilt/internal/store"
    10  	"github.com/tilt-dev/tilt/internal/tiltfile"
    11  	"github.com/tilt-dev/tilt/pkg/apis"
    12  	"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
    13  	"github.com/tilt-dev/tilt/pkg/model"
    14  )
    15  
    16  const DefaultSessionName = "Tiltfile"
    17  
    18  var processStartTime = time.Now()
    19  var processPID = int64(os.Getpid())
    20  
    21  func FromTiltfile(tf *v1alpha1.Tiltfile, tlr *tiltfile.TiltfileLoadResult, ciTimeoutFlag model.CITimeoutFlag, mode store.EngineMode) *v1alpha1.Session {
    22  	s := &v1alpha1.Session{
    23  		ObjectMeta: metav1.ObjectMeta{
    24  			Name: DefaultSessionName,
    25  		},
    26  		Spec: v1alpha1.SessionSpec{
    27  			TiltfilePath: tf.Spec.Path,
    28  		},
    29  		Status: v1alpha1.SessionStatus{
    30  			PID:       processPID,
    31  			StartTime: apis.NewMicroTime(processStartTime),
    32  		},
    33  	}
    34  
    35  	if mode == store.EngineModeCI {
    36  		s.Spec.CI = &v1alpha1.SessionCISpec{Timeout: &metav1.Duration{Duration: time.Duration(ciTimeoutFlag)}}
    37  	}
    38  
    39  	// TLR may be nil if the tiltfile hasn't finished loading yet.
    40  	if tlr != nil {
    41  		s.Spec.CI = tlr.CISettings
    42  	}
    43  
    44  	// currently, manual + CI are the only supported modes; the apiserver will validate this field and reject
    45  	// the object on creation if it doesn't conform, so there's no additional validation/error-handling here
    46  	switch mode {
    47  	case store.EngineModeUp:
    48  		s.Spec.ExitCondition = v1alpha1.ExitConditionManual
    49  	case store.EngineModeCI:
    50  		s.Spec.ExitCondition = v1alpha1.ExitConditionCI
    51  	}
    52  	return s
    53  }