...

Source file src/edge-infra.dev/pkg/edge/rollouts/examples/examples.go

Documentation: edge-infra.dev/pkg/edge/rollouts/examples

     1  package examples
     2  
     3  import (
     4  	_ "embed"
     5  	"time"
     6  
     7  	"edge-infra.dev/pkg/edge/rollouts"
     8  )
     9  
    10  const (
    11  	samplePlanID  = "abcde-12345"
    12  	sampleGraphID = "fghij-67890"
    13  )
    14  
    15  //go:embed simple-graph.json
    16  var SampleGraphJSON []byte
    17  
    18  //go:embed simple-plan.json
    19  var SamplePlanJSON []byte
    20  
    21  type sampleTimer struct {
    22  	fakeTime time.Time
    23  }
    24  
    25  func (t *sampleTimer) Now() time.Time {
    26  	return t.fakeTime
    27  }
    28  
    29  var timer = &sampleTimer{fakeTime: time.Now()}
    30  
    31  func GetSampleGraphFromJSON() *rollouts.RolloutGraph {
    32  	graph, _ := rollouts.NewRolloutGraphFromJSON(SampleGraphJSON)
    33  	return graph
    34  }
    35  
    36  func GetSamplePlanFromJSON() rollouts.RolloutPlan {
    37  	plan, _ := rollouts.NewRolloutPlanFromJSON(SamplePlanJSON)
    38  	return plan
    39  }
    40  
    41  // TODO(dk185217): Either move to _test.go or remove. Examples for human use and developers wont
    42  // be creating graph entity structs etc etc
    43  func CreateSamplePlan() rollouts.RolloutPlan {
    44  	plan := rollouts.RolloutPlan{
    45  		ID:          samplePlanID,
    46  		Name:        "A sample plan",
    47  		Description: "A sample rollout plan that deploys store version 0.21 to pilot stores, then stage east and west, with a final approval to general us stores",
    48  		Nodes:       rollouts.NodeMap{},
    49  	}
    50  
    51  	targetGroup1 := rollouts.NewTargetGroup(
    52  		"targetGroup1", // the key of the node
    53  		"pilot",        // selector label that will match cluster IDs
    54  		"store",        // artifact name
    55  		"0.21",         // the desired version of the artifact
    56  	)
    57  	plan.Nodes.AddNode(targetGroup1)
    58  
    59  	timerGate1 := rollouts.NewTimerGate(
    60  		"timerGate1",
    61  		time.Now(),      // the start time of the timer
    62  		time.Second*600, // the duration of the timer
    63  		timer,           // a TimeSource
    64  	)
    65  	plan.Nodes.AddNode(timerGate1)
    66  
    67  	targetGroup2 := rollouts.NewTargetGroup(
    68  		"targetGroup2",
    69  		"stage:east",
    70  		"store",
    71  		"0.21",
    72  	)
    73  	plan.Nodes.AddNode(targetGroup2)
    74  
    75  	targetGroup3 := rollouts.NewTargetGroup(
    76  		"targetGroup3", // the key of the node
    77  		"stage:west",   // selector
    78  		"store",        // artifact name
    79  		"0.21",         // the desired version of the artifact
    80  	)
    81  	plan.Nodes.AddNode(targetGroup3)
    82  
    83  	approvalGate1 := rollouts.NewApprovalGate("approvalGate1")
    84  	plan.Nodes.AddNode(approvalGate1)
    85  
    86  	targetGroup4 := rollouts.NewTargetGroup(
    87  		"targetGroup4", // the key of the node
    88  		"general:us",   // selector
    89  		"store",        // artifact name
    90  		"0.21",         // the desired version of the artifact
    91  	)
    92  
    93  	// connect edges
    94  	//
    95  	//				targetGroup2
    96  	// targetGroup1 -> timerGate1 ->	    -> approvalGate1 -> targetGroup4
    97  	//				targetGroup3
    98  	plan.Edges = rollouts.ConnectEdge(targetGroup1, timerGate1, plan.Edges)
    99  	plan.Edges = rollouts.ConnectEdge(timerGate1, targetGroup2, plan.Edges)
   100  	plan.Edges = rollouts.ConnectEdge(timerGate1, targetGroup3, plan.Edges)
   101  	plan.Edges = rollouts.ConnectEdge(targetGroup2, approvalGate1, plan.Edges)
   102  	plan.Edges = rollouts.ConnectEdge(targetGroup3, approvalGate1, plan.Edges)
   103  	plan.Edges = rollouts.ConnectEdge(approvalGate1, targetGroup4, plan.Edges)
   104  
   105  	return plan
   106  }
   107  
   108  func CreateSampleGraph(plan rollouts.RolloutPlan) *rollouts.RolloutGraph {
   109  	// add a custom time source to the graph
   110  	customTimeSource := &sampleTimer{}
   111  	opts := []rollouts.Option{rollouts.WithTimeSource(customTimeSource)}
   112  
   113  	return rollouts.NewRolloutGraph(plan, opts...)
   114  }
   115  

View as plain text