github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/sysadvisor/plugin/metacache/metacache.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 metacacheplugin 18 19 import ( 20 "context" 21 "time" 22 23 "k8s.io/apimachinery/pkg/util/wait" 24 "k8s.io/klog/v2" 25 26 "github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/metacache" 27 "github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin" 28 "github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/types" 29 "github.com/kubewharf/katalyst-core/pkg/config" 30 "github.com/kubewharf/katalyst-core/pkg/metaserver" 31 "github.com/kubewharf/katalyst-core/pkg/metrics" 32 metricspool "github.com/kubewharf/katalyst-core/pkg/metrics/metrics-pool" 33 "github.com/kubewharf/katalyst-core/pkg/util/general" 34 ) 35 36 const ( 37 // MetricsNamePlugMetaCacheHeartbeat is the heartbeat metrics of metacache plugin 38 MetricsNamePlugMetaCacheHeartbeat = "plugin_metacache_heartbeat" 39 ) 40 41 // MetaCachePlugin collects pod info from kubelet 42 type MetaCachePlugin struct { 43 name string 44 period time.Duration 45 46 emitter metrics.MetricEmitter 47 metaServer *metaserver.MetaServer 48 MetaWriter metacache.MetaWriter 49 } 50 51 // NewMetaCachePlugin creates a metacache plugin with the specified config 52 func NewMetaCachePlugin(pluginName string, conf *config.Configuration, _ interface{}, emitterPool metricspool.MetricsEmitterPool, 53 metaServer *metaserver.MetaServer, metaCache metacache.MetaCache, 54 ) (plugin.SysAdvisorPlugin, error) { 55 emitter := emitterPool.GetDefaultMetricsEmitter().WithTags("advisor-metacache") 56 57 mcp := &MetaCachePlugin{ 58 name: pluginName, 59 period: conf.SysAdvisorPluginsConfiguration.MetaCachePluginConfiguration.SyncPeriod, 60 emitter: emitter, 61 metaServer: metaServer, 62 MetaWriter: metaCache, 63 } 64 65 return mcp, nil 66 } 67 68 // Name returns the name of metacache 69 func (mcp *MetaCachePlugin) Name() string { 70 return mcp.name 71 } 72 73 // Init initializes the metacache plugin 74 func (mcp *MetaCachePlugin) Init() error { 75 return nil 76 } 77 78 // Run starts the metacache plugin 79 func (mcp *MetaCachePlugin) Run(ctx context.Context) { 80 general.RegisterHeartbeatCheck(mcp.name, 3*mcp.period, general.HealthzCheckStateNotReady, 3*mcp.period) 81 go wait.UntilWithContext(ctx, mcp.periodicWork, mcp.period) 82 } 83 84 func (mcp *MetaCachePlugin) periodicWork(_ context.Context) { 85 _ = mcp.emitter.StoreInt64(MetricsNamePlugMetaCacheHeartbeat, int64(mcp.period.Seconds()), metrics.MetricTypeNameCount) 86 87 // Fill missing container metadata from metaserver 88 f := func(podUID string, containerName string, ci *types.ContainerInfo) bool { 89 spec, err := mcp.metaServer.GetContainerSpec(podUID, containerName) 90 if err != nil { 91 klog.Errorf("[metacache] get container spec failed: %v, %v/%v", err, podUID, containerName) 92 return true 93 } 94 95 // For these containers do not belong to NumaExclusive, assign the actual value to CPURequest of them. 96 // Because CPURequest of containerInfo would be assigned as math.Ceil(Actual CPURequest). 97 // As for NumaExclusive containers, the "math.Ceil(Actual CPURequest)" is acceptable. 98 if ci.CPURequest <= 0 || !ci.IsNumaExclusive() { 99 ci.CPURequest = spec.Resources.Requests.Cpu().AsApproximateFloat64() 100 } 101 if ci.CPULimit <= 0 { 102 ci.CPULimit = spec.Resources.Limits.Cpu().AsApproximateFloat64() 103 } 104 if ci.MemoryRequest <= 0 { 105 ci.MemoryRequest = spec.Resources.Requests.Memory().AsApproximateFloat64() 106 } 107 if ci.MemoryLimit <= 0 { 108 ci.MemoryLimit = spec.Resources.Limits.Memory().AsApproximateFloat64() 109 } 110 return true 111 } 112 err := mcp.MetaWriter.RangeAndUpdateContainer(f) 113 _ = general.UpdateHealthzStateByError(mcp.name, err) 114 }