...

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

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

     1  package internal
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"edge-infra.dev/pkg/edge/rollouts"
     7  )
     8  
     9  func NewExampleInMemStore() *InMemStore {
    10  	graphStore, err := NewBannerGraphStore(ExampleBannerByteGraphStore)
    11  	fmt.Printf("graphStore address: %p\n", graphStore)
    12  	if err != nil {
    13  		panic(err)
    14  	}
    15  
    16  	fmt.Printf("tg1 address: %p\n", graphStore[ExampleBannerID][ExampleRolloutID].Nodes["tg1"])
    17  
    18  	planStore, err := NewBannerPlanStore(ExampleBannerBytePlanStore)
    19  	if err != nil {
    20  		panic(err)
    21  	}
    22  	return &InMemStore{
    23  		PlanStore:             planStore,
    24  		GraphStore:            graphStore,
    25  		LabelStore:            NewExampleLabelStore(),
    26  		TargetGroupStateStore: NewExampleTGStore(),
    27  		ApprovalGateStore:     NewExampleAGStore(),
    28  		ArtifactVersionStore:  NewExampleAVStore(),
    29  	}
    30  }
    31  
    32  type InMemStore struct {
    33  	GraphStore            BannerGraphStore
    34  	PlanStore             BannerPlanStore
    35  	LabelStore            LabelStore
    36  	TargetGroupStateStore TargetGroupStateStore
    37  	ApprovalGateStore     ApprovalGateStore
    38  	ArtifactVersionStore  ArtifactVersionStore
    39  }
    40  
    41  func (s *InMemStore) GetTargetGroupVersionStates(artifactName string, clusterMatches rollouts.MatchResult) ([]*VersionState, error) {
    42  	tgs := []*VersionState{}
    43  	for _, match := range clusterMatches.Matches {
    44  		if artifact, found := s.TargetGroupStateStore.clusterVersions[match][artifactName]; found {
    45  			fmt.Printf("version state address: %p\n", artifact)
    46  			tgs = append(tgs, artifact)
    47  		}
    48  	}
    49  	if len(tgs) == 0 {
    50  		return nil, fmt.Errorf("no matches found for clusters [%s]", clusterMatches.Matches)
    51  	}
    52  	return tgs, nil
    53  }
    54  
    55  func (s *InMemStore) GetClusterLabelMatches(selector string) (rollouts.MatchResult, error) {
    56  	result := rollouts.MatchResult{
    57  		Matches: []string{},
    58  	}
    59  
    60  	var clusterIDs []string
    61  	if foundClusterIDs, found := s.LabelStore[selector]; found {
    62  		clusterIDs = append(clusterIDs, foundClusterIDs...)
    63  		result.Matches = append(result.Matches, clusterIDs...)
    64  	}
    65  
    66  	return result, nil
    67  }
    68  
    69  func (s *InMemStore) ApplyClusterArtifactVersions(artifactName string, artifactVersion string, clusterMatches rollouts.MatchResult) error {
    70  	for _, match := range clusterMatches.Matches {
    71  		if state, found := s.TargetGroupStateStore.clusterVersions[match][artifactName]; !found {
    72  			fmt.Printf("didn't find artifact %s for %s. setting to pending\n", artifactName, match)
    73  			s.TargetGroupStateStore.clusterVersions[match][artifactName] = &VersionState{Version: artifactVersion}
    74  		} else {
    75  			fmt.Printf("found %s for %s. applying\n", artifactName, match)
    76  			// s.targetGroupStateStore.clusterVersions[match][artifactName] = &VersionState{Version: artifactVersion}
    77  			state.Version = artifactVersion
    78  		}
    79  	}
    80  	return nil
    81  }
    82  
    83  func (s *InMemStore) SetClusterArtifactReady(artifactName string, clusterMatches rollouts.MatchResult) {
    84  	for _, clusterID := range clusterMatches.Matches {
    85  		if clusterVersionState, clusterFound := s.TargetGroupStateStore.clusterVersions[clusterID]; clusterFound {
    86  			if versionState, artifactFound := clusterVersionState[artifactName]; artifactFound {
    87  				versionState.Ready = true
    88  			}
    89  		}
    90  	}
    91  }
    92  
    93  func (s *InMemStore) GetApprovalGateStatus(ag *rollouts.ApprovalGate) (rollouts.GateApproval, error) {
    94  	storeGateStatus, found := s.ApprovalGateStore[ag.GetKey()]
    95  	if !found {
    96  		return rollouts.GatePending, fmt.Errorf("gate %s not found", ag.GetKey())
    97  	}
    98  
    99  	return storeGateStatus, nil
   100  }
   101  
   102  func (s *InMemStore) OpenApprovalGate(agKey rollouts.NodeKey) error {
   103  	_, found := s.ApprovalGateStore[agKey]
   104  	if !found {
   105  		return fmt.Errorf("gate %s not found", agKey)
   106  	}
   107  	s.ApprovalGateStore[agKey] = rollouts.GateApproved
   108  
   109  	return nil
   110  }
   111  

View as plain text