github.com/grahambrereton-form3/tilt@v0.10.18/internal/store/actions.go (about)

     1  package store
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/windmilleng/wmclient/pkg/analytics"
     8  	v1 "k8s.io/api/core/v1"
     9  
    10  	"github.com/windmilleng/tilt/internal/k8s"
    11  	"github.com/windmilleng/tilt/pkg/model"
    12  )
    13  
    14  type ErrorAction struct {
    15  	Error error
    16  }
    17  
    18  func (ErrorAction) Action() {}
    19  
    20  func NewErrorAction(err error) ErrorAction {
    21  	return ErrorAction{Error: err}
    22  }
    23  
    24  type LogAction interface {
    25  	Action
    26  	model.LogEvent
    27  
    28  	// Ideally, all logs should be associated with a source.
    29  	//
    30  	// In practice, not all logs have an obvious source identifier,
    31  	// so this might be empty.
    32  	//
    33  	// Right now, that source is a ManifestName. But in the future,
    34  	// this might make more sense as another kind of identifier.
    35  	//
    36  	// (As of this writing, we have TargetID as an abstract build-time
    37  	// source identifier, but no generic run-time source identifier)
    38  	Source() model.ManifestName
    39  }
    40  
    41  type LogEvent struct {
    42  	mn        model.ManifestName
    43  	timestamp time.Time
    44  	msg       []byte
    45  }
    46  
    47  func (LogEvent) Action() {}
    48  
    49  func (le LogEvent) Source() model.ManifestName {
    50  	return le.mn
    51  }
    52  
    53  func (le LogEvent) Time() time.Time {
    54  	return le.timestamp
    55  }
    56  
    57  func (le LogEvent) Message() []byte {
    58  	return le.msg
    59  }
    60  
    61  func (le *LogEvent) ScrubSecret(secret model.Secret) {
    62  	le.msg = secret.Scrub(le.msg)
    63  }
    64  
    65  func (le *LogEvent) ScrubSecretSet(secrets model.SecretSet) {
    66  	for _, s := range secrets {
    67  		le.ScrubSecret(s)
    68  	}
    69  }
    70  
    71  func NewLogEvent(mn model.ManifestName, b []byte) LogEvent {
    72  	return LogEvent{
    73  		mn:        mn,
    74  		timestamp: time.Now(),
    75  		msg:       append([]byte{}, b...),
    76  	}
    77  }
    78  
    79  func NewGlobalLogEvent(b []byte) LogEvent {
    80  	return LogEvent{
    81  		mn:        "",
    82  		timestamp: time.Now(),
    83  		msg:       append([]byte{}, b...),
    84  	}
    85  }
    86  
    87  type K8sEventAction struct {
    88  	Event        *v1.Event
    89  	ManifestName model.ManifestName
    90  }
    91  
    92  func (K8sEventAction) Action() {}
    93  
    94  func NewK8sEventAction(event *v1.Event, manifestName model.ManifestName) K8sEventAction {
    95  	return K8sEventAction{event, manifestName}
    96  }
    97  
    98  func (kEvt K8sEventAction) ToLogAction(mn model.ManifestName) LogAction {
    99  	msg := fmt.Sprintf("[K8s EVENT: %s] %s\n",
   100  		objRefHumanReadable(kEvt.Event.InvolvedObject), kEvt.Event.Message)
   101  
   102  	return LogEvent{
   103  		mn:        mn,
   104  		timestamp: kEvt.Event.LastTimestamp.Time,
   105  		msg:       []byte(msg),
   106  	}
   107  }
   108  
   109  func objRefHumanReadable(obj v1.ObjectReference) string {
   110  	s := fmt.Sprintf("%s %s", obj.Kind, obj.Name)
   111  	if obj.Namespace != "" {
   112  		s += fmt.Sprintf(" (ns: %s)", obj.Namespace)
   113  	}
   114  	return s
   115  }
   116  
   117  type AnalyticsUserOptAction struct {
   118  	Opt analytics.Opt
   119  }
   120  
   121  func (AnalyticsUserOptAction) Action() {}
   122  
   123  type AnalyticsNudgeSurfacedAction struct{}
   124  
   125  func (AnalyticsNudgeSurfacedAction) Action() {}
   126  
   127  type TiltCloudUserLookedUpAction struct {
   128  	Found                    bool
   129  	Username                 string
   130  	IsPostRegistrationLookup bool
   131  }
   132  
   133  func (TiltCloudUserLookedUpAction) Action() {}
   134  
   135  type UserStartedTiltCloudRegistrationAction struct{}
   136  
   137  func (UserStartedTiltCloudRegistrationAction) Action() {}
   138  
   139  // The user can indicate "yes, I know the pod restarted N times, stop showing me"
   140  type PodResetRestartsAction struct {
   141  	PodID           k8s.PodID
   142  	ManifestName    model.ManifestName
   143  	VisibleRestarts int
   144  }
   145  
   146  func NewPodResetRestartsAction(podID k8s.PodID, mn model.ManifestName, visibleRestarts int) PodResetRestartsAction {
   147  	return PodResetRestartsAction{
   148  		PodID:           podID,
   149  		ManifestName:    mn,
   150  		VisibleRestarts: visibleRestarts,
   151  	}
   152  }
   153  
   154  func (PodResetRestartsAction) Action() {}