|
| 1 | +/* |
| 2 | +Copyright 2024 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 client provides the Runtime SDK client. |
| 18 | +package client |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + |
| 25 | + runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1" |
| 26 | + runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog" |
| 27 | + runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" |
| 28 | + "sigs.k8s.io/cluster-api/util/cache" |
| 29 | +) |
| 30 | + |
| 31 | +// CallExtensionOption is the interface for configuration that modifies CallExtensionOptions for a CallExtension call. |
| 32 | +type CallExtensionOption interface { |
| 33 | + // ApplyToOptions applies this configuration to the given CallExtensionOptions. |
| 34 | + ApplyToOptions(*CallExtensionOptions) |
| 35 | +} |
| 36 | + |
| 37 | +// CallExtensionCacheEntry is a cache entry for the cache that can be used with the CallExtension call via |
| 38 | +// the WithCaching option. |
| 39 | +type CallExtensionCacheEntry struct { |
| 40 | + CacheKey string |
| 41 | + Response runtimehooksv1.ResponseObject |
| 42 | +} |
| 43 | + |
| 44 | +// Key returns the cache key of a CallExtensionCacheEntry. |
| 45 | +func (c CallExtensionCacheEntry) Key() string { |
| 46 | + return c.CacheKey |
| 47 | +} |
| 48 | + |
| 49 | +// WithCaching enables caching for the CallExtension call. |
| 50 | +type WithCaching struct { |
| 51 | + Cache cache.Cache[CallExtensionCacheEntry] |
| 52 | + CacheKeyFunc func(extensionName, extensionConfigResourceVersion string, request runtimehooksv1.RequestObject) string |
| 53 | +} |
| 54 | + |
| 55 | +// ApplyToOptions applies WithCaching to the given CallExtensionOptions. |
| 56 | +func (w WithCaching) ApplyToOptions(in *CallExtensionOptions) { |
| 57 | + in.WithCaching = true |
| 58 | + in.Cache = w.Cache |
| 59 | + in.CacheKeyFunc = w.CacheKeyFunc |
| 60 | +} |
| 61 | + |
| 62 | +// CallExtensionOptions contains the options for the CallExtension call. |
| 63 | +type CallExtensionOptions struct { |
| 64 | + WithCaching bool |
| 65 | + Cache cache.Cache[CallExtensionCacheEntry] |
| 66 | + CacheKeyFunc func(extensionName, extensionConfigResourceVersion string, request runtimehooksv1.RequestObject) string |
| 67 | +} |
| 68 | + |
| 69 | +// Client is the runtime client to interact with extensions. |
| 70 | +type Client interface { |
| 71 | + // WarmUp can be used to initialize a "cold" RuntimeClient with all |
| 72 | + // known runtimev1.ExtensionConfigs at a given time. |
| 73 | + // After WarmUp completes the RuntimeClient is considered ready. |
| 74 | + WarmUp(extensionConfigList *runtimev1.ExtensionConfigList) error |
| 75 | + |
| 76 | + // IsReady return true after the RuntimeClient finishes warmup. |
| 77 | + IsReady() bool |
| 78 | + |
| 79 | + // Discover makes the discovery call on the extension and returns an updated ExtensionConfig |
| 80 | + // with extension handlers information in the ExtensionConfig status. |
| 81 | + Discover(context.Context, *runtimev1.ExtensionConfig) (*runtimev1.ExtensionConfig, error) |
| 82 | + |
| 83 | + // Register registers the ExtensionConfig. |
| 84 | + Register(extensionConfig *runtimev1.ExtensionConfig) error |
| 85 | + |
| 86 | + // Unregister unregisters the ExtensionConfig. |
| 87 | + Unregister(extensionConfig *runtimev1.ExtensionConfig) error |
| 88 | + |
| 89 | + // CallAllExtensions calls all the ExtensionHandler registered for the hook. |
| 90 | + CallAllExtensions(ctx context.Context, hook runtimecatalog.Hook, forObject metav1.Object, request runtimehooksv1.RequestObject, response runtimehooksv1.ResponseObject) error |
| 91 | + |
| 92 | + // CallExtension calls the ExtensionHandler with the given name. |
| 93 | + CallExtension(ctx context.Context, hook runtimecatalog.Hook, forObject metav1.Object, name string, request runtimehooksv1.RequestObject, response runtimehooksv1.ResponseObject, opts ...CallExtensionOption) error |
| 94 | +} |
0 commit comments