|
| 1 | +// Copyright 2025 Ant Group Co., Ltd. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package bandwidthfilter |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "strings" |
| 20 | + |
| 21 | + corev1 "k8s.io/api/core/v1" |
| 22 | + |
| 23 | + "github.com/secretflow/kuscia/pkg/agent/config" |
| 24 | + "github.com/secretflow/kuscia/pkg/agent/middleware/hook" |
| 25 | + "github.com/secretflow/kuscia/pkg/agent/middleware/plugin" |
| 26 | + "github.com/secretflow/kuscia/pkg/common" |
| 27 | + "github.com/secretflow/kuscia/pkg/utils/nlog" |
| 28 | +) |
| 29 | + |
| 30 | +func Register() { |
| 31 | + plugin.Register(common.PluginNameBandwidthFilter, &bandwidthFilter{}) |
| 32 | +} |
| 33 | + |
| 34 | +type bandwidthFilterConfig struct { |
| 35 | + // Whether to strip bandwidth resources when running in runk; defaults to true if not configured (nil). |
| 36 | + StripOnRunk *bool `json:"stripOnRunk" yaml:"stripOnRunk"` |
| 37 | +} |
| 38 | + |
| 39 | +type bandwidthFilter struct { |
| 40 | + initialized bool |
| 41 | + stripOnRunk bool |
| 42 | + isRunk bool |
| 43 | +} |
| 44 | + |
| 45 | +// Type implements plugin.Plugin. |
| 46 | +func (bf *bandwidthFilter) Type() string { |
| 47 | + return hook.PluginType |
| 48 | +} |
| 49 | + |
| 50 | +// Init implements plugin.Plugin. |
| 51 | +// Only registers to the hook system when runtime == runk and stripOnRunk is set to true. |
| 52 | +func (bf *bandwidthFilter) Init(ctx context.Context, deps *plugin.Dependencies, cfg *config.PluginCfg) error { |
| 53 | + // Default: On |
| 54 | + bf.stripOnRunk = true |
| 55 | + |
| 56 | + if cfg != nil { |
| 57 | + var c bandwidthFilterConfig |
| 58 | + if err := cfg.Config.Decode(&c); err == nil && c.StripOnRunk != nil { |
| 59 | + bf.stripOnRunk = *c.StripOnRunk |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + rt := "" |
| 64 | + if deps != nil && deps.AgentConfig != nil { |
| 65 | + rt = strings.ToLower(strings.TrimSpace(deps.AgentConfig.Provider.Runtime)) |
| 66 | + } |
| 67 | + bf.isRunk = rt == config.K8sRuntime |
| 68 | + |
| 69 | + nlog.Infof("[bandwidthfilter] Init: detected runtime=%q (raw=%q), stripOnRunk=%v", |
| 70 | + rt, deps.AgentConfig.Provider.Runtime, bf.stripOnRunk) |
| 71 | + |
| 72 | + // If the runtime is not runk, or the configuration disables it, the plugin will not be registered. |
| 73 | + if !bf.isRunk || !bf.stripOnRunk { |
| 74 | + nlog.Infof("Plugin %s NOT registered (runtime=%q, stripOnRunk=%v)", |
| 75 | + common.PluginNameBandwidthFilter, rt, bf.stripOnRunk) |
| 76 | + return nil |
| 77 | + } |
| 78 | + |
| 79 | + bf.initialized = true |
| 80 | + hook.Register(common.PluginNameBandwidthFilter, bf) |
| 81 | + nlog.Infof("Plugin %s registered (runtime=%q)", common.PluginNameBandwidthFilter, rt) |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +// Only executed at the stage “before syncing to the external Kubernetes”. |
| 86 | +func (bf *bandwidthFilter) CanExec(ctx hook.Context) bool { |
| 87 | + if !bf.initialized { |
| 88 | + return false |
| 89 | + } |
| 90 | + switch ctx.Point() { |
| 91 | + case hook.PointK8sProviderSyncPod: |
| 92 | + _, ok := ctx.(*hook.K8sProviderSyncPodContext) |
| 93 | + return ok |
| 94 | + default: |
| 95 | + return false |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// ExecHook: strips the bandwidth extended resource from the BkPod before it is dispatched. |
| 100 | +func (bf *bandwidthFilter) ExecHook(ctx hook.Context) (*hook.Result, error) { |
| 101 | + syncCtx, ok := ctx.(*hook.K8sProviderSyncPodContext) |
| 102 | + if !ok || syncCtx.BkPod == nil { |
| 103 | + return &hook.Result{}, nil |
| 104 | + } |
| 105 | + |
| 106 | + scrub := func(rr *corev1.ResourceRequirements) { |
| 107 | + if rr == nil { |
| 108 | + return |
| 109 | + } |
| 110 | + delete(rr.Limits, common.ResourceBandwidth) |
| 111 | + delete(rr.Requests, common.ResourceBandwidth) |
| 112 | + } |
| 113 | + |
| 114 | + for i := range syncCtx.BkPod.Spec.Containers { |
| 115 | + scrub(&syncCtx.BkPod.Spec.Containers[i].Resources) |
| 116 | + } |
| 117 | + for i := range syncCtx.BkPod.Spec.InitContainers { |
| 118 | + scrub(&syncCtx.BkPod.Spec.InitContainers[i].Resources) |
| 119 | + } |
| 120 | + for i := range syncCtx.BkPod.Spec.EphemeralContainers { |
| 121 | + scrub(&syncCtx.BkPod.Spec.EphemeralContainers[i].Resources) |
| 122 | + } |
| 123 | + if syncCtx.BkPod.Spec.Overhead != nil { |
| 124 | + delete(syncCtx.BkPod.Spec.Overhead, common.ResourceBandwidth) |
| 125 | + } |
| 126 | + |
| 127 | + return &hook.Result{}, nil |
| 128 | +} |
0 commit comments