|
| 1 | +/* |
| 2 | +Copyright 2026 The Kubernetes 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 models |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + "time" |
| 24 | + |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 26 | + |
| 27 | + "github.com/llm-d/llm-d-router/pkg/common/observability/logging" |
| 28 | + fwkdl "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/datalayer" |
| 29 | + fwkplugin "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/plugin" |
| 30 | + attrmodels "github.com/llm-d/llm-d-router/pkg/epp/framework/plugins/datalayer/attribute/models" |
| 31 | + srchttp "github.com/llm-d/llm-d-router/pkg/epp/framework/plugins/datalayer/source/http" |
| 32 | + srcnotifications "github.com/llm-d/llm-d-router/pkg/epp/framework/plugins/datalayer/source/notifications" |
| 33 | +) |
| 34 | + |
| 35 | +// ModelsEndpointExtractorType identifies the extractor that fetches /v1/models |
| 36 | +// once per endpoint, as opposed to the polling models-data-extractor. |
| 37 | +const ModelsEndpointExtractorType = "models-endpoint-extractor" |
| 38 | + |
| 39 | +// fetchTimeout bounds a single /v1/models fetch so a slow or unresponsive model |
| 40 | +// server cannot keep the background fetch goroutine alive indefinitely. |
| 41 | +const fetchTimeout = 10 * time.Second |
| 42 | + |
| 43 | +// Model-server connection defaults, matching the polling models source so both |
| 44 | +// reach the same endpoint with the same TLS handling. |
| 45 | +const ( |
| 46 | + defaultModelsScheme = "http" |
| 47 | + defaultModelsPath = "/v1/models" |
| 48 | + defaultModelsInsecureSkipVerify = true |
| 49 | +) |
| 50 | + |
| 51 | +// Assert the extractor produces an attribute, runs on endpoint lifecycle events, |
| 52 | +// and self-wires its source dependency. |
| 53 | +var ( |
| 54 | + _ fwkplugin.ProducerPlugin = (*ModelEndpointExtractor)(nil) |
| 55 | + _ fwkdl.EndpointExtractor = (*ModelEndpointExtractor)(nil) |
| 56 | + _ fwkdl.Registrant = (*ModelEndpointExtractor)(nil) |
| 57 | +) |
| 58 | + |
| 59 | +// modelsEndpointExtractorParams configures the model-server endpoint and TLS |
| 60 | +// handling. It mirrors the polling models source so operators can point the |
| 61 | +// extractor at an https model server. |
| 62 | +type modelsEndpointExtractorParams struct { |
| 63 | + Scheme string `json:"scheme"` |
| 64 | + Path string `json:"path"` |
| 65 | + InsecureSkipVerify bool `json:"insecureSkipVerify"` |
| 66 | +} |
| 67 | + |
| 68 | +// ModelEndpointExtractor fetches /v1/models once when an endpoint is added and |
| 69 | +// stores the parsed model list (including max_model_len) as an endpoint |
| 70 | +// attribute. The model list is fixed for an endpoint's lifetime, so it does not |
| 71 | +// re-fetch on a timer the way the polling extractor does. |
| 72 | +type ModelEndpointExtractor struct { |
| 73 | + typedName fwkplugin.TypedName |
| 74 | + dk fwkplugin.DataKey |
| 75 | + // fetcher performs the one-shot GET and JSON parse, reusing the HTTP source's |
| 76 | + // scheme/TLS handling. Only its Poll method is used; the polling Dispatch loop |
| 77 | + // is never driven. |
| 78 | + fetcher *srchttp.HTTPDataSource[*ModelResponse] |
| 79 | +} |
| 80 | + |
| 81 | +// NewModelEndpointExtractor builds an extractor that fetches from |
| 82 | +// scheme://<endpoint>/path, verifying the server certificate unless insecure is set. |
| 83 | +func NewModelEndpointExtractor(name, scheme, path string, insecure bool) (*ModelEndpointExtractor, error) { |
| 84 | + fetcher, err := srchttp.NewHTTPDataSource(scheme, path, insecure, ModelsEndpointExtractorType, name, ParseModels) |
| 85 | + if err != nil { |
| 86 | + return nil, fmt.Errorf("failed to create models fetcher: %w", err) |
| 87 | + } |
| 88 | + return &ModelEndpointExtractor{ |
| 89 | + typedName: fwkplugin.TypedName{Type: ModelsEndpointExtractorType, Name: name}, |
| 90 | + dk: attrmodels.ModelsAttributeKey, |
| 91 | + fetcher: fetcher, |
| 92 | + }, nil |
| 93 | +} |
| 94 | + |
| 95 | +// TypedName returns the plugin type and name. |
| 96 | +func (me *ModelEndpointExtractor) TypedName() fwkplugin.TypedName { return me.typedName } |
| 97 | + |
| 98 | +// Produces declares the /v1/models attribute this extractor populates so the |
| 99 | +// framework can wire it to consumers of that attribute. |
| 100 | +func (me *ModelEndpointExtractor) Produces() map[fwkplugin.DataKey]any { |
| 101 | + return map[fwkplugin.DataKey]any{me.dk: attrmodels.ModelDataCollection{}} |
| 102 | +} |
| 103 | + |
| 104 | +// Extract records the model list for an added endpoint. The fetch runs in the |
| 105 | +// background and returns immediately: endpoint add/update is processed serially, |
| 106 | +// so a slow or unreachable model server must not stall endpoint provisioning. |
| 107 | +// Until the fetch completes the attribute stays unset and the consumer falls |
| 108 | +// back to its default. Delete events need no work; the attribute leaves with the |
| 109 | +// endpoint. |
| 110 | +func (me *ModelEndpointExtractor) Extract(ctx context.Context, event fwkdl.EndpointEvent) error { |
| 111 | + // Only an add/update carries model data to extract; deletes carry none. |
| 112 | + if event.Type != fwkdl.EventAddOrUpdate { |
| 113 | + return nil |
| 114 | + } |
| 115 | + ep := event.Endpoint |
| 116 | + if ep == nil || ep.GetMetadata() == nil { |
| 117 | + return nil |
| 118 | + } |
| 119 | + // The model list is fixed for an endpoint's lifetime, so fetch only once even |
| 120 | + // though add/update may fire repeatedly for the same endpoint. |
| 121 | + if _, ok := ep.GetAttributes().Get(me.dk.String()); ok { |
| 122 | + return nil |
| 123 | + } |
| 124 | + go me.fetchAndStore(ctx, ep) |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +// fetchAndStore fetches /v1/models once, under a bounded timeout, and records the |
| 129 | +// model list (including max_model_len) as an endpoint attribute. A failed fetch |
| 130 | +// is logged and dropped, leaving the attribute unset so the consumer falls back |
| 131 | +// to its default. |
| 132 | +func (me *ModelEndpointExtractor) fetchAndStore(ctx context.Context, ep fwkdl.Endpoint) { |
| 133 | + ctx, cancel := context.WithTimeout(ctx, fetchTimeout) |
| 134 | + defer cancel() |
| 135 | + resp, err := me.fetcher.Poll(ctx, ep) |
| 136 | + if err != nil { |
| 137 | + log.FromContext(ctx).V(logging.DEBUG).Info("failed to fetch /v1/models", |
| 138 | + "endpoint", ep.GetMetadata().NamespacedName, "err", err) |
| 139 | + return |
| 140 | + } |
| 141 | + ep.GetAttributes().Put(me.dk.String(), attrmodels.ModelDataCollection(resp.Data)) |
| 142 | +} |
| 143 | + |
| 144 | +// RegisterDependencies binds this extractor to an endpoint-notification-source, |
| 145 | +// auto-creating that source when the configuration does not already define one. |
| 146 | +// This is what makes the extractor wire itself in as a default source. |
| 147 | +func (me *ModelEndpointExtractor) RegisterDependencies(r fwkdl.Registrar) error { |
| 148 | + return r.Register(fwkdl.PendingRegistration{ |
| 149 | + Owner: me.TypedName(), |
| 150 | + SourceType: srcnotifications.EndpointNotificationSourceType, |
| 151 | + Extractor: me, |
| 152 | + DefaultSource: srcnotifications.NewEndpointDataSource( |
| 153 | + srcnotifications.EndpointNotificationSourceType, srcnotifications.EndpointNotificationSourceType), |
| 154 | + }) |
| 155 | +} |
| 156 | + |
| 157 | +// ModelEndpointExtractorFactory instantiates the extractor from optional JSON |
| 158 | +// parameters, defaulting to an http model server with TLS verification skipped. |
| 159 | +func ModelEndpointExtractorFactory(name string, parameters *json.Decoder, _ fwkplugin.Handle) (fwkplugin.Plugin, error) { |
| 160 | + params := modelsEndpointExtractorParams{ |
| 161 | + Scheme: defaultModelsScheme, |
| 162 | + Path: defaultModelsPath, |
| 163 | + InsecureSkipVerify: defaultModelsInsecureSkipVerify, |
| 164 | + } |
| 165 | + // Overlay the defaults with any configured values. |
| 166 | + if parameters != nil { |
| 167 | + if err := parameters.Decode(¶ms); err != nil { |
| 168 | + return nil, err |
| 169 | + } |
| 170 | + } |
| 171 | + return NewModelEndpointExtractor(name, params.Scheme, params.Path, params.InsecureSkipVerify) |
| 172 | +} |
0 commit comments