github.com/polarismesh/polaris@v1.17.8/namespace/server.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package namespace
    19  
    20  import (
    21  	"context"
    22  
    23  	apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
    24  	"golang.org/x/sync/singleflight"
    25  
    26  	"github.com/polarismesh/polaris/cache"
    27  	"github.com/polarismesh/polaris/common/model"
    28  	"github.com/polarismesh/polaris/plugin"
    29  	"github.com/polarismesh/polaris/store"
    30  )
    31  
    32  var _ NamespaceOperateServer = (*Server)(nil)
    33  
    34  type Server struct {
    35  	storage               store.Store
    36  	caches                *cache.CacheManager
    37  	createNamespaceSingle *singleflight.Group
    38  	cfg                   Config
    39  	history               plugin.History
    40  	hooks                 []ResourceHook
    41  }
    42  
    43  func (s *Server) afterNamespaceResource(ctx context.Context, req *apimodel.Namespace, save *model.Namespace,
    44  	remove bool) error {
    45  	event := &ResourceEvent{
    46  		ReqNamespace: req,
    47  		Namespace:    save,
    48  		IsRemove:     remove,
    49  	}
    50  
    51  	for index := range s.hooks {
    52  		hook := s.hooks[index]
    53  		if err := hook.After(ctx, model.RNamespace, event); err != nil {
    54  			return err
    55  		}
    56  	}
    57  
    58  	return nil
    59  }
    60  
    61  // RecordHistory server对外提供history插件的简单封装
    62  func (s *Server) RecordHistory(entry *model.RecordEntry) {
    63  	// 如果插件没有初始化,那么不记录history
    64  	if s.history == nil {
    65  		return
    66  	}
    67  	// 如果数据为空,则不需要打印了
    68  	if entry == nil {
    69  		return
    70  	}
    71  
    72  	// 调用插件记录history
    73  	s.history.Record(entry)
    74  }
    75  
    76  // SetResourceHooks 返回Cache
    77  func (s *Server) SetResourceHooks(hooks ...ResourceHook) {
    78  	s.hooks = hooks
    79  }