func()

in store/store.go [174:203]


func (s *ClusterStore) UpdateCluster(ctx context.Context, ns string, clusterInfo *Cluster) error {
	lock := s.getLock(ns, clusterInfo.Name)
	lock.Lock()
	defer lock.Unlock()

	oldCluster, err := s.getClusterWithoutLock(ctx, ns, clusterInfo.Name)
	if err != nil {
		return err
	}
	if oldCluster.Version.Load() > clusterInfo.Version.Load() {
		return fmt.Errorf("the cluster has been updated by others")
	}

	clusterInfo.Version.Add(1)
	clusterBytes, err := json.Marshal(clusterInfo)
	if err != nil {
		return fmt.Errorf("cluster: %w", err)
	}
	if err := s.e.Set(ctx, buildClusterKey(ns, clusterInfo.Name), clusterBytes); err != nil {
		return err
	}

	s.EmitEvent(EventPayload{
		Namespace: ns,
		Cluster:   clusterInfo.Name,
		Type:      EventCluster,
		Command:   CommandUpdate,
	})
	return nil
}