forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This introduces a set of centrally managed resource.Resource[T] objects. These aim to replace the current k8s/watchers and k8s/watchers/subscriber, with a single interface (Resource[T]) that one can be observed and its store queried from any cell. With this we'll gradually remove K8sWatcher by moving code from k8s/watchers/ into feature specific packages that subscribe to Resource[T]. See pkg/k8s/resource/example.go for usage example of Resource[T]. To start things off, in this patch the Resource[*Service] and Resource[*Node] are introduced into the object graph and the existing service and node watchers are refactored to be implemented on top of it. This keeps the existing code working, but allows new cells to use these objects directly without dependency on K8sWatcher. Signed-off-by: Jussi Maki <jussi@isovalent.com>
- Loading branch information
Showing
10 changed files
with
198 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Cilium | ||
|
||
package k8s | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/fields" | ||
|
||
"github.com/cilium/cilium/pkg/hive" | ||
"github.com/cilium/cilium/pkg/hive/cell" | ||
"github.com/cilium/cilium/pkg/k8s/client" | ||
"github.com/cilium/cilium/pkg/k8s/resource" | ||
slim_corev1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/api/core/v1" | ||
"github.com/cilium/cilium/pkg/k8s/utils" | ||
nodeTypes "github.com/cilium/cilium/pkg/node/types" | ||
"github.com/cilium/cilium/pkg/option" | ||
) | ||
|
||
var ( | ||
// SharedResourceCell provides a set of shared handles to Kubernetes resources used throughout the | ||
// Cilium agent. Each of the resources share a client-go informer and backing store so we only | ||
// have one watch API call for each resource kind and that we maintain only one copy of each object. | ||
// | ||
// See pkg/k8s/resource/resource.go for documentation on the Resource[T] type. | ||
SharedResourcesCell = cell.Module( | ||
"k8s-shared-resources", | ||
"Shared Kubernetes resources", | ||
|
||
cell.Provide( | ||
serviceResource, | ||
localNodeResource, | ||
), | ||
) | ||
) | ||
|
||
type SharedResources struct { | ||
cell.In | ||
Services resource.Resource[*slim_corev1.Service] | ||
LocalNode resource.Resource[*corev1.Node] | ||
} | ||
|
||
func serviceResource(lc hive.Lifecycle, cs client.Clientset) (resource.Resource[*slim_corev1.Service], error) { | ||
if !cs.IsEnabled() { | ||
return nil, nil | ||
} | ||
optsModifier, err := utils.GetServiceListOptionsModifier(option.Config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
lw := utils.ListerWatcherFromTyped[*slim_corev1.ServiceList](cs.Slim().CoreV1().Services("")) | ||
lw = utils.ListerWatcherWithModifier(lw, optsModifier) | ||
return resource.New[*slim_corev1.Service](lc, lw), nil | ||
} | ||
|
||
func localNodeResource(lc hive.Lifecycle, cs client.Clientset) (resource.Resource[*corev1.Node], error) { | ||
if !cs.IsEnabled() { | ||
return nil, nil | ||
} | ||
lw := utils.ListerWatcherFromTyped[*corev1.NodeList](cs.CoreV1().Nodes()) | ||
lw = utils.ListerWatcherWithFields(lw, fields.ParseSelectorOrDie("metadata.name="+nodeTypes.GetName())) | ||
return resource.New[*corev1.Node](lc, lw), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.