Skip to content

Commit

Permalink
Merge pull request #11613 from sbueringer/pr-fix-runtime-client-impor…
Browse files Browse the repository at this point in the history
…t-1.8

[release-1.8] 🐛 Export runtime.Client interface and cache package
  • Loading branch information
k8s-ci-robot authored Dec 23, 2024
2 parents 0b3fda4 + 24c449f commit cca5fec
Show file tree
Hide file tree
Showing 24 changed files with 143 additions and 108 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ linters-settings:
- pkg: sigs.k8s.io/cluster-api/exp/runtime/catalog
alias: runtimecatalog
- pkg: sigs.k8s.io/cluster-api/internal/runtime/client
alias: internalruntimeclient
- pkg: sigs.k8s.io/cluster-api/exp/runtime/client
alias: runtimeclient
- pkg: sigs.k8s.io/cluster-api/internal/runtime/registry
alias: runtimeregistry
Expand Down
2 changes: 1 addition & 1 deletion controllers/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"

"sigs.k8s.io/cluster-api/controllers/remote"
runtimeclient "sigs.k8s.io/cluster-api/exp/runtime/client"
clustercontroller "sigs.k8s.io/cluster-api/internal/controllers/cluster"
clusterclasscontroller "sigs.k8s.io/cluster-api/internal/controllers/clusterclass"
machinecontroller "sigs.k8s.io/cluster-api/internal/controllers/machine"
Expand All @@ -34,7 +35,6 @@ import (
clustertopologycontroller "sigs.k8s.io/cluster-api/internal/controllers/topology/cluster"
machinedeploymenttopologycontroller "sigs.k8s.io/cluster-api/internal/controllers/topology/machinedeployment"
machinesettopologycontroller "sigs.k8s.io/cluster-api/internal/controllers/topology/machineset"
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
)

// Following types provides access to reconcilers implemented in internal/controllers, thus
Expand Down
94 changes: 94 additions & 0 deletions exp/runtime/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package client provides the Runtime SDK client.
package client

import (
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1"
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
"sigs.k8s.io/cluster-api/util/cache"
)

// CallExtensionOption is the interface for configuration that modifies CallExtensionOptions for a CallExtension call.
type CallExtensionOption interface {
// ApplyToOptions applies this configuration to the given CallExtensionOptions.
ApplyToOptions(*CallExtensionOptions)
}

// CallExtensionCacheEntry is a cache entry for the cache that can be used with the CallExtension call via
// the WithCaching option.
type CallExtensionCacheEntry struct {
CacheKey string
Response runtimehooksv1.ResponseObject
}

// Key returns the cache key of a CallExtensionCacheEntry.
func (c CallExtensionCacheEntry) Key() string {
return c.CacheKey
}

// WithCaching enables caching for the CallExtension call.
type WithCaching struct {
Cache cache.Cache[CallExtensionCacheEntry]
CacheKeyFunc func(extensionName, extensionConfigResourceVersion string, request runtimehooksv1.RequestObject) string
}

// ApplyToOptions applies WithCaching to the given CallExtensionOptions.
func (w WithCaching) ApplyToOptions(in *CallExtensionOptions) {
in.WithCaching = true
in.Cache = w.Cache
in.CacheKeyFunc = w.CacheKeyFunc
}

// CallExtensionOptions contains the options for the CallExtension call.
type CallExtensionOptions struct {
WithCaching bool
Cache cache.Cache[CallExtensionCacheEntry]
CacheKeyFunc func(extensionName, extensionConfigResourceVersion string, request runtimehooksv1.RequestObject) string
}

// Client is the runtime client to interact with extensions.
type Client interface {
// WarmUp can be used to initialize a "cold" RuntimeClient with all
// known runtimev1.ExtensionConfigs at a given time.
// After WarmUp completes the RuntimeClient is considered ready.
WarmUp(extensionConfigList *runtimev1.ExtensionConfigList) error

// IsReady return true after the RuntimeClient finishes warmup.
IsReady() bool

// Discover makes the discovery call on the extension and returns an updated ExtensionConfig
// with extension handlers information in the ExtensionConfig status.
Discover(context.Context, *runtimev1.ExtensionConfig) (*runtimev1.ExtensionConfig, error)

// Register registers the ExtensionConfig.
Register(extensionConfig *runtimev1.ExtensionConfig) error

// Unregister unregisters the ExtensionConfig.
Unregister(extensionConfig *runtimev1.ExtensionConfig) error

// CallAllExtensions calls all the ExtensionHandler registered for the hook.
CallAllExtensions(ctx context.Context, hook runtimecatalog.Hook, forObject metav1.Object, request runtimehooksv1.RequestObject, response runtimehooksv1.ResponseObject) error

// CallExtension calls the ExtensionHandler with the given name.
CallExtension(ctx context.Context, hook runtimecatalog.Hook, forObject metav1.Object, name string, request runtimehooksv1.RequestObject, response runtimehooksv1.ResponseObject, opts ...CallExtensionOption) error
}
2 changes: 1 addition & 1 deletion exp/runtime/controllers/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"

runtimeclient "sigs.k8s.io/cluster-api/exp/runtime/client"
runtimecontrollers "sigs.k8s.io/cluster-api/exp/runtime/internal/controllers"
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
)

// ExtensionConfigReconciler reconciles an ExtensionConfig object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ import (

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1"
runtimeclient "sigs.k8s.io/cluster-api/exp/runtime/client"
tlog "sigs.k8s.io/cluster-api/internal/log"
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
"sigs.k8s.io/cluster-api/feature"
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
internalruntimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
runtimeregistry "sigs.k8s.io/cluster-api/internal/runtime/registry"
fakev1alpha1 "sigs.k8s.io/cluster-api/internal/runtime/test/v1alpha1"
"sigs.k8s.io/cluster-api/util"
Expand All @@ -59,7 +59,7 @@ func TestExtensionReconciler_Reconcile(t *testing.T) {
g.Expect(fakev1alpha1.AddToCatalog(cat)).To(Succeed())

registry := runtimeregistry.New()
runtimeClient := runtimeclient.New(runtimeclient.Options{
runtimeClient := internalruntimeclient.New(internalruntimeclient.Options{
Catalog: cat,
Registry: registry,
})
Expand Down Expand Up @@ -214,7 +214,7 @@ func TestExtensionReconciler_discoverExtensionConfig(t *testing.T) {
g.Expect(err).ToNot(HaveOccurred())
defer srv1.Close()

runtimeClient := runtimeclient.New(runtimeclient.Options{
runtimeClient := internalruntimeclient.New(internalruntimeclient.Options{
Catalog: cat,
Registry: registry,
})
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestExtensionReconciler_discoverExtensionConfig(t *testing.T) {
// srv1 := fakeSecureExtensionServer(discoveryHandler("first"))
// defer srv1.Close()

runtimeClient := runtimeclient.New(runtimeclient.Options{
runtimeClient := internalruntimeclient.New(internalruntimeclient.Options{
Catalog: cat,
Registry: registry,
})
Expand Down
2 changes: 1 addition & 1 deletion exp/runtime/internal/controllers/warmup.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"

runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1"
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
runtimeclient "sigs.k8s.io/cluster-api/exp/runtime/client"
)

const (
Expand Down
6 changes: 3 additions & 3 deletions exp/runtime/internal/controllers/warmup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
"sigs.k8s.io/cluster-api/feature"
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
internalruntimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
runtimeregistry "sigs.k8s.io/cluster-api/internal/runtime/registry"
fakev1alpha1 "sigs.k8s.io/cluster-api/internal/runtime/test/v1alpha1"
)
Expand Down Expand Up @@ -75,7 +75,7 @@ func Test_warmupRunnable_Start(t *testing.T) {
r := &warmupRunnable{
Client: env.GetClient(),
APIReader: env.GetAPIReader(),
RuntimeClient: runtimeclient.New(runtimeclient.Options{
RuntimeClient: internalruntimeclient.New(internalruntimeclient.Options{
Catalog: cat,
Registry: registry,
}),
Expand Down Expand Up @@ -140,7 +140,7 @@ func Test_warmupRunnable_Start(t *testing.T) {
r := &warmupRunnable{
Client: env.GetClient(),
APIReader: env.GetAPIReader(),
RuntimeClient: runtimeclient.New(runtimeclient.Options{
RuntimeClient: internalruntimeclient.New(internalruntimeclient.Options{
Catalog: cat,
Registry: registry,
}),
Expand Down
2 changes: 1 addition & 1 deletion exp/topology/desiredstate/desired_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ import (
"sigs.k8s.io/cluster-api/controllers/remote"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
runtimeclient "sigs.k8s.io/cluster-api/exp/runtime/client"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
"sigs.k8s.io/cluster-api/exp/topology/scope"
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/internal/contract"
"sigs.k8s.io/cluster-api/internal/controllers/topology/cluster/patches"
"sigs.k8s.io/cluster-api/internal/hooks"
tlog "sigs.k8s.io/cluster-api/internal/log"
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
"sigs.k8s.io/cluster-api/internal/topology/clustershim"
topologynames "sigs.k8s.io/cluster-api/internal/topology/names"
"sigs.k8s.io/cluster-api/internal/topology/ownerrefs"
Expand Down
12 changes: 6 additions & 6 deletions internal/controllers/clusterclass/clusterclass_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ import (
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/external"
runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1"
runtimeclient "sigs.k8s.io/cluster-api/exp/runtime/client"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
"sigs.k8s.io/cluster-api/feature"
tlog "sigs.k8s.io/cluster-api/internal/log"
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
runtimeregistry "sigs.k8s.io/cluster-api/internal/runtime/registry"
internalruntimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
"sigs.k8s.io/cluster-api/internal/topology/variables"
"sigs.k8s.io/cluster-api/internal/util/cache"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/cache"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/conversion"
"sigs.k8s.io/cluster-api/util/patch"
Expand Down Expand Up @@ -439,7 +439,7 @@ func (r *Reconciler) extensionConfigToClusterClass(ctx context.Context, o client
}
for _, patch := range clusterClass.Spec.Patches {
if patch.External != nil && patch.External.DiscoverVariablesExtension != nil {
extName, err := runtimeclient.ExtensionNameFromHandlerName(*patch.External.DiscoverVariablesExtension)
extName, err := internalruntimeclient.ExtensionNameFromHandlerName(*patch.External.DiscoverVariablesExtension)
if err != nil {
log.Error(err, "failed to reconcile ClusterClass for ExtensionConfig")
continue
Expand Down Expand Up @@ -474,9 +474,9 @@ func matchNamespace(ctx context.Context, c client.Client, selector labels.Select
return selector.Matches(labels.Set(ns.GetLabels()))
}

func cacheKeyFunc(registration *runtimeregistry.ExtensionRegistration, request runtimehooksv1.RequestObject) string {
func cacheKeyFunc(extensionName, extensionConfigResourceVersion string, request runtimehooksv1.RequestObject) string {
// Note: registration.Name is identical to the value of the patch.External.DiscoverVariablesExtension field in the ClusterClass.
s := fmt.Sprintf("%s-%s", registration.Name, registration.ExtensionConfigResourceVersion)
s := fmt.Sprintf("%s-%s", extensionName, extensionConfigResourceVersion)
for k, v := range request.GetSettings() {
s += fmt.Sprintf(",%s=%s", k, v)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ import (
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1"
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
runtimeclient "sigs.k8s.io/cluster-api/exp/runtime/client"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
"sigs.k8s.io/cluster-api/feature"
tlog "sigs.k8s.io/cluster-api/internal/log"
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
fakeruntimeclient "sigs.k8s.io/cluster-api/internal/runtime/client/fake"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/internal/util/cache"
"sigs.k8s.io/cluster-api/util/cache"
)

func TestClusterClassReconciler_reconcile(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ import (
"sigs.k8s.io/cluster-api/controllers/remote"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
runtimeclient "sigs.k8s.io/cluster-api/exp/runtime/client"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
"sigs.k8s.io/cluster-api/exp/topology/desiredstate"
"sigs.k8s.io/cluster-api/exp/topology/scope"
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/internal/controllers/topology/cluster/structuredmerge"
"sigs.k8s.io/cluster-api/internal/hooks"
tlog "sigs.k8s.io/cluster-api/internal/log"
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
"sigs.k8s.io/cluster-api/internal/util/ssa"
"sigs.k8s.io/cluster-api/internal/webhooks"
"sigs.k8s.io/cluster-api/util"
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/topology/cluster/patches/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
runtimeclient "sigs.k8s.io/cluster-api/exp/runtime/client"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
"sigs.k8s.io/cluster-api/exp/topology/scope"
"sigs.k8s.io/cluster-api/feature"
Expand All @@ -39,7 +40,6 @@ import (
"sigs.k8s.io/cluster-api/internal/controllers/topology/cluster/patches/inline"
"sigs.k8s.io/cluster-api/internal/controllers/topology/cluster/patches/variables"
tlog "sigs.k8s.io/cluster-api/internal/log"
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
)

// Engine is a patch engine which applies patches defined in a ClusterBlueprint to a ClusterState.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
runtimeclient "sigs.k8s.io/cluster-api/exp/runtime/client"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/internal/controllers/topology/cluster/patches/api"
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
)

// externalPatchGenerator generates JSON patches for a GeneratePatchesRequest based on a ClusterClassPatch.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import (
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1"
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
runtimeclient "sigs.k8s.io/cluster-api/exp/runtime/client"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
"sigs.k8s.io/cluster-api/feature"
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
)

func TestExternalPatchGenerator_Generate(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
runtimeclient "sigs.k8s.io/cluster-api/exp/runtime/client"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/internal/controllers/topology/cluster/patches/api"
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
)

// externalValidator validates templates.
Expand Down
Loading

0 comments on commit cca5fec

Please sign in to comment.