github.com/polarismesh/polaris@v1.17.8/namespace/default.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  	"errors"
    23  	"sync"
    24  
    25  	"golang.org/x/sync/singleflight"
    26  
    27  	"github.com/polarismesh/polaris/auth"
    28  	"github.com/polarismesh/polaris/cache"
    29  	"github.com/polarismesh/polaris/plugin"
    30  	"github.com/polarismesh/polaris/store"
    31  )
    32  
    33  var (
    34  	server          NamespaceOperateServer
    35  	namespaceServer = &Server{}
    36  	once            sync.Once
    37  	finishInit      bool
    38  )
    39  
    40  type Config struct {
    41  	AutoCreate bool `yaml:"autoCreate"`
    42  }
    43  
    44  // Initialize 初始化
    45  func Initialize(ctx context.Context, nsOpt *Config, storage store.Store, cacheMgn *cache.CacheManager) error {
    46  	var err error
    47  	once.Do(func() {
    48  		err = initialize(ctx, nsOpt, storage, cacheMgn)
    49  	})
    50  
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	finishInit = true
    56  	return nil
    57  }
    58  
    59  func initialize(_ context.Context, nsOpt *Config, storage store.Store, cacheMgn *cache.CacheManager) error {
    60  	namespaceServer.caches = cacheMgn
    61  	namespaceServer.storage = storage
    62  	namespaceServer.cfg = *nsOpt
    63  	namespaceServer.createNamespaceSingle = &singleflight.Group{}
    64  
    65  	// 获取History插件,注意:插件的配置在bootstrap已经设置好
    66  	namespaceServer.history = plugin.GetHistory()
    67  	if namespaceServer.history == nil {
    68  		log.Warn("Not Found History Log Plugin")
    69  	}
    70  
    71  	userMgn, err := auth.GetUserServer()
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	strategyMgn, err := auth.GetStrategyServer()
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	server = newServerAuthAbility(namespaceServer, userMgn, strategyMgn)
    82  	return nil
    83  }
    84  
    85  // GetServer 获取已经初始化好的Server
    86  func GetServer() (NamespaceOperateServer, error) {
    87  	if !finishInit {
    88  		return nil, errors.New("NamespaceOperateServer has not done Initialize")
    89  	}
    90  
    91  	return server, nil
    92  }
    93  
    94  // GetOriginServer 获取已经初始化好的Server
    95  func GetOriginServer() (*Server, error) {
    96  	if !finishInit {
    97  		return nil, errors.New("NamespaceOperateServer has not done Initialize")
    98  	}
    99  
   100  	return namespaceServer, nil
   101  }