Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource API refactoring and shared resources #21744

Merged
merged 8 commits into from
Dec 22, 2022
26 changes: 1 addition & 25 deletions clustermesh-apiserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import (
slim_corev1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/api/core/v1"
"github.com/cilium/cilium/pkg/k8s/synced"
"github.com/cilium/cilium/pkg/k8s/types"
"github.com/cilium/cilium/pkg/k8s/utils"
"github.com/cilium/cilium/pkg/kvstore"
"github.com/cilium/cilium/pkg/kvstore/store"
"github.com/cilium/cilium/pkg/labels"
Expand Down Expand Up @@ -105,34 +104,11 @@ var (
identityStore = cache.NewStore(cache.DeletionHandlingMetaNamespaceKeyFunc)
)

var resources = cell.Module(
"resources",
"Resources",

cell.Provide(
newSvcResource,
),
)

func newSvcResource(lc hive.Lifecycle, c k8sClient.Clientset) (resource.Resource[*slim_corev1.Service], error) {
optsModifier, err := utils.GetServiceListOptionsModifier(cfg)
if err != nil {
return nil, err
}
return resource.New[*slim_corev1.Service](
lc,
utils.ListerWatcherWithModifier(
utils.ListerWatcherFromTyped[*slim_corev1.ServiceList](c.Slim().CoreV1().Services("")),
optsModifier),
resource.WithErrorHandler(resource.AlwaysRetry),
), nil
}

func init() {
rootHive = hive.New(
gops.Cell(defaults.GopsPortApiserver),
k8sClient.Cell,
resources,
k8s.SharedResourcesCell,
healthAPIServerCell,

cell.Invoke(registerHooks),
Expand Down
5 changes: 5 additions & 0 deletions daemon/cmd/cells.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/cilium/cilium/pkg/defaults"
"github.com/cilium/cilium/pkg/gops"
"github.com/cilium/cilium/pkg/hive/cell"
"github.com/cilium/cilium/pkg/k8s"
k8sClient "github.com/cilium/cilium/pkg/k8s/client"
"github.com/cilium/cilium/pkg/node"
"github.com/cilium/cilium/pkg/option"
Expand Down Expand Up @@ -52,6 +53,10 @@ var (
// observing changes to it.
node.LocalNodeStoreCell,

// Shared resources provide access to k8s resources as event streams or as
// read-only stores.
k8s.SharedResourcesCell,
joamaki marked this conversation as resolved.
Show resolved Hide resolved

// daemonCell wraps the legacy daemon initialization and provides Promise[*Daemon].
daemonCell,

Expand Down
2 changes: 2 additions & 0 deletions daemon/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ func newDaemon(ctx context.Context, cleaner *daemonCleanup,
epMgr *endpointmanager.EndpointManager, dp datapath.Datapath,
wgAgent *wg.Agent,
clientset k8sClient.Clientset,
sharedResources k8s.SharedResources,
) (*Daemon, *endpointRestoreState, error) {

var (
Expand Down Expand Up @@ -701,6 +702,7 @@ func newDaemon(ctx context.Context, cleaner *daemonCleanup,
option.Config,
d.ipcache,
d.cgroupManager,
sharedResources,
)
nd.RegisterK8sGetters(d.k8sWatcher)

Expand Down
18 changes: 10 additions & 8 deletions daemon/cmd/daemon_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1638,13 +1638,14 @@ var daemonCell = cell.Module(
type daemonParams struct {
cell.In

Lifecycle hive.Lifecycle
Clientset k8sClient.Clientset
Datapath datapath.Datapath
WGAgent *wg.Agent `optional:"true"`
LocalNodeStore node.LocalNodeStore
BGPController *bgpv1.Controller
Shutdowner hive.Shutdowner
Lifecycle hive.Lifecycle
Clientset k8sClient.Clientset
Datapath datapath.Datapath
WGAgent *wg.Agent `optional:"true"`
LocalNodeStore node.LocalNodeStore
BGPController *bgpv1.Controller
Shutdowner hive.Shutdowner
SharedResources k8s.SharedResources
}

func newDaemonPromise(params daemonParams) promise.Promise[*Daemon] {
Expand All @@ -1671,7 +1672,8 @@ func newDaemonPromise(params daemonParams) promise.Promise[*Daemon] {
WithDefaultEndpointManager(daemonCtx, params.Clientset, endpoint.CheckHealth),
params.Datapath,
params.WGAgent,
params.Clientset)
params.Clientset,
params.SharedResources)
if err != nil {
return fmt.Errorf("daemon creation failed: %w", err)
}
Expand Down
4 changes: 3 additions & 1 deletion daemon/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ var (
dotGraphCmd = &cobra.Command{
Use: "dot-graph",
Short: "Output the internal dependencies of cilium-agent in graphviz dot format",
Run: func(cmd *cobra.Command, args []string) { agentHive.PrintDotGraph() },
Run: func(cmd *cobra.Command, args []string) {
agentHive.PrintDotGraph()
},
}

objectsCmd = &cobra.Command{
Expand Down
61 changes: 0 additions & 61 deletions operator/cmd/resources.go

This file was deleted.

9 changes: 4 additions & 5 deletions operator/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ var (

// These cells are started only after the operator is elected leader.
WithLeaderLifecycle(
resourcesCell,
k8s.SharedResourcesCell,
lbipam.Cell,

legacyCell,
Expand Down Expand Up @@ -373,7 +373,7 @@ func kvstoreEnabled() bool {

var legacyCell = cell.Invoke(registerLegacyOnLeader)

func registerLegacyOnLeader(lc hive.Lifecycle, clientset k8sClient.Clientset, resources SharedResources) {
func registerLegacyOnLeader(lc hive.Lifecycle, clientset k8sClient.Clientset, resources k8s.SharedResources) {
ctx, cancel := context.WithCancel(context.Background())
legacy := &legacyOnLeader{
ctx: ctx,
Expand All @@ -391,9 +391,8 @@ type legacyOnLeader struct {
ctx context.Context
cancel context.CancelFunc
clientset k8sClient.Clientset
resources SharedResources

wg sync.WaitGroup
wg sync.WaitGroup
resources k8s.SharedResources
}

func (legacy *legacyOnLeader) onStop(_ hive.HookContext) error {
Expand Down