Skip to content

Commit

Permalink
indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
cheina97 authored and adamjensenbot committed May 31, 2023
1 parent 47459d4 commit 2df6e0e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 19 deletions.
8 changes: 7 additions & 1 deletion cmd/liqo-controller-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import (
argsutils "github.com/liqotech/liqo/pkg/utils/args"
"github.com/liqotech/liqo/pkg/utils/csr"
liqoerrors "github.com/liqotech/liqo/pkg/utils/errors"
"github.com/liqotech/liqo/pkg/utils/indexer"
"github.com/liqotech/liqo/pkg/utils/mapper"
"github.com/liqotech/liqo/pkg/utils/restcfg"
"github.com/liqotech/liqo/pkg/vkMachinery"
Expand Down Expand Up @@ -231,6 +232,11 @@ func main() {
mgr.GetWebhookServer().Register("/validate/namespace-offloading", nsoffwh.New())
mgr.GetWebhookServer().Register("/mutate/pod", podwh.New(mgr.GetClient()))

if err := indexer.IndexField(ctx, mgr, &corev1.Pod{}, indexer.FieldNodeNameFromPod, indexer.ExtractNodeName); err != nil {
klog.Errorf("Unable to setup the indexer for the Pod nodeName field: %v", err)
os.Exit(1)
}

clientset := kubernetes.NewForConfigOrDie(config)

namespaceManager := tenantnamespace.NewCachedManager(ctx, clientset)
Expand Down Expand Up @@ -414,7 +420,7 @@ func main() {
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}
if err = nodeFailureReconciler.SetupWithManager(ctx, mgr); err != nil {
if err = nodeFailureReconciler.SetupWithManager(mgr); err != nil {
klog.Errorf("Unable to start the nodeFailureReconciler", err)
os.Exit(1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ import (

"github.com/liqotech/liqo/pkg/consts"
"github.com/liqotech/liqo/pkg/utils"
"github.com/liqotech/liqo/pkg/utils/indexer"
)

var nodeNameField = "spec.nodeName"

// NodeFailureReconciler reconciles a Node object.
type NodeFailureReconciler struct {
client.Client
Expand Down Expand Up @@ -68,7 +67,7 @@ func (r *NodeFailureReconciler) Reconcile(ctx context.Context, req ctrl.Request)
// Node NotReady: delete all terminating pods that are managed by shadowpods
var pods corev1.PodList
offloadedPodSelector := client.MatchingLabelsSelector{Selector: labels.Set{consts.ManagedByLabelKey: consts.ManagedByShadowPodValue}.AsSelector()}
nodePodSelector := client.MatchingFieldsSelector{Selector: fields.OneTermEqualSelector(nodeNameField, node.Name)}
nodePodSelector := client.MatchingFieldsSelector{Selector: fields.OneTermEqualSelector(indexer.FieldNodeNameFromPod, node.Name)}
if err := r.List(ctx, &pods, offloadedPodSelector, nodePodSelector); err != nil {
klog.Errorf("unable to list pods: %v", err)
return ctrl.Result{}, err
Expand Down Expand Up @@ -110,21 +109,8 @@ func getPodTerminatingEventHandler() handler.EventHandler {
}}
}

func extractNodeNameFromPod(rawObj client.Object) []string {
pod, ok := rawObj.(*corev1.Pod)
if !ok {
return []string{}
}
return []string{pod.Spec.NodeName}
}

// SetupWithManager monitors updates on nodes and watch for pods that are terminating and managed by a ShadowPod.
func (r *NodeFailureReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
// Add field containing node Name to the Field Indexer
if err := mgr.GetFieldIndexer().IndexField(ctx, &corev1.Pod{}, nodeNameField, extractNodeNameFromPod); err != nil {
return err
}

func (r *NodeFailureReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.Node{}).
Watches(&source.Kind{Type: &corev1.Pod{}}, getPodTerminatingEventHandler()).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/liqotech/liqo/pkg/consts"
"github.com/liqotech/liqo/pkg/utils/indexer"
)

var _ = Describe("NodeFailureController", func() {
Expand Down Expand Up @@ -135,7 +136,7 @@ var _ = Describe("NodeFailureController", func() {

fakeClientBuilder = fake.NewClientBuilder().
WithScheme(scheme.Scheme).
WithIndex(&corev1.Pod{}, nodeNameField, extractNodeNameFromPod)
WithIndex(&corev1.Pod{}, indexer.FieldNodeNameFromPod, indexer.ExtractNodeName)
})

JustBeforeEach(func() {
Expand Down
16 changes: 16 additions & 0 deletions pkg/utils/indexer/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2019-2023 The Liqo 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 indexer provides indexers for controller-runtime managers.
package indexer
43 changes: 43 additions & 0 deletions pkg/utils/indexer/indexer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2019-2023 The Liqo 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 indexer

import (
"context"

corev1 "k8s.io/api/core/v1"
ctrlruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
// FieldNodeNameFromPod is the field name of the node name of a pod.
FieldNodeNameFromPod = "spec.nodeName"
)

// ExtractNodeName returns the node name of the given object.
func ExtractNodeName(rawObj client.Object) []string {
switch obj := rawObj.(type) {
case *corev1.Pod:
return []string{obj.Spec.NodeName}
default:
return []string{}
}
}

// IndexField indexes the given field on the given object.
func IndexField(ctx context.Context, mgr ctrlruntime.Manager, obj client.Object, field string, indexerFunc client.IndexerFunc) error {
return mgr.GetFieldIndexer().IndexField(ctx, obj, field, indexerFunc)
}

0 comments on commit 2df6e0e

Please sign in to comment.