github.com/kubewharf/katalyst-core@v0.5.3/pkg/metaserver/metaserver.go (about) 1 /* 2 Copyright 2022 The Katalyst Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package metaserver is the package that contains those implementations to 18 // obtain metadata in the specific node, any other component wants to get 19 // those data should import this package rather than get directly. 20 package metaserver // import "github.com/kubewharf/katalyst-core/pkg/metaserver" 21 22 import ( 23 "context" 24 "fmt" 25 "os" 26 "sync" 27 28 "github.com/kubewharf/katalyst-core/pkg/client" 29 pkgconfig "github.com/kubewharf/katalyst-core/pkg/config" 30 "github.com/kubewharf/katalyst-core/pkg/metaserver/agent" 31 "github.com/kubewharf/katalyst-core/pkg/metaserver/external" 32 "github.com/kubewharf/katalyst-core/pkg/metaserver/kcc" 33 "github.com/kubewharf/katalyst-core/pkg/metaserver/spd" 34 "github.com/kubewharf/katalyst-core/pkg/metrics" 35 ) 36 37 // MetaServer is used to fetch metadata that other components may need to obtain, 38 // such as. dynamic configurations, pods or nodes running in agent, metrics info and so on. 39 type MetaServer struct { 40 start bool 41 sync.Mutex 42 43 *agent.MetaAgent 44 kcc.ConfigurationManager 45 spd.ServiceProfilingManager 46 external.ExternalManager 47 } 48 49 // NewMetaServer returns the instance of MetaServer. 50 func NewMetaServer(clientSet *client.GenericClientSet, emitter metrics.MetricEmitter, conf *pkgconfig.Configuration) (*MetaServer, error) { 51 metaAgent, err := agent.NewMetaAgent(conf, clientSet, emitter) 52 if err != nil { 53 return nil, err 54 } 55 56 // make sure meta server checkpoint directory already exist 57 err = os.MkdirAll(conf.CheckpointManagerDir, os.FileMode(0o755)) 58 if err != nil { 59 return nil, fmt.Errorf("initializes meta server checkpoint dir failed: %s", err) 60 } 61 62 var configurationManager kcc.ConfigurationManager 63 if !conf.ConfigDisableDynamic { 64 configurationManager, err = kcc.NewDynamicConfigManager(clientSet, emitter, 65 metaAgent.CNCFetcher, conf) 66 if err != nil { 67 return nil, err 68 } 69 } else { 70 configurationManager = &kcc.DummyConfigurationManager{} 71 } 72 73 spdFetcher, err := spd.NewSPDFetcher(clientSet, emitter, metaAgent.CNCFetcher, conf) 74 if err != nil { 75 return nil, fmt.Errorf("initializes spd fetcher failed: %s", err) 76 } 77 78 return &MetaServer{ 79 MetaAgent: metaAgent, 80 ConfigurationManager: configurationManager, 81 ServiceProfilingManager: spd.NewServiceProfilingManager(spdFetcher), 82 ExternalManager: external.InitExternalManager(metaAgent.PodFetcher), 83 }, nil 84 } 85 86 func (m *MetaServer) Run(ctx context.Context) { 87 m.Lock() 88 if m.start { 89 m.Unlock() 90 return 91 } 92 m.start = true 93 94 go m.MetaAgent.Run(ctx) 95 go m.ConfigurationManager.Run(ctx) 96 go m.ServiceProfilingManager.Run(ctx) 97 go m.ExternalManager.Run(ctx) 98 99 m.Unlock() 100 <-ctx.Done() 101 } 102 103 func (m *MetaServer) SetServiceProfilingManager(manager spd.ServiceProfilingManager) error { 104 m.Lock() 105 defer m.Unlock() 106 if m.start { 107 return fmt.Errorf("meta agent has already started, not allowed to set implementations") 108 } 109 110 m.ServiceProfilingManager = manager 111 return nil 112 }