Skip to content

Commit

Permalink
use Pod informer to avoid Pod GETs
Browse files Browse the repository at this point in the history
  • Loading branch information
songjiaxun committed Jun 16, 2024
1 parent b83e1b7 commit 0d9d3c7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/csi_driver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func main() {
}()
}

clientset, err := clientset.New(*kubeconfigPath)
clientset, err := clientset.New(*kubeconfigPath, *nodeID)
if err != nil {
klog.Fatal("Failed to configure k8s client")
}
Expand Down
2 changes: 1 addition & 1 deletion deploy/base/node/node_setup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ metadata:
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get"]
verbs: ["get", "watch", "list"]
- apiGroups: [""]
resources: ["serviceaccounts"]
verbs: ["get"]
Expand Down
29 changes: 24 additions & 5 deletions pkg/cloud_provider/clientset/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ import (
authenticationv1 "k8s.io/api/authentication/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
listersv1 "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
)

type Interface interface {
GetPod(ctx context.Context, namespace, name string) (*corev1.Pod, error)
GetPod(namespace, name string) (*corev1.Pod, error)
CreateServiceAccountToken(ctx context.Context, namespace, name string, tokenRequest *authenticationv1.TokenRequest) (*authenticationv1.TokenRequest, error)
GetGCPServiceAccountName(ctx context.Context, namespace, name string) (string, error)
}
Expand All @@ -43,9 +45,10 @@ type PodInfo struct {

type Clientset struct {
k8sClients kubernetes.Interface
podLister listersv1.PodLister
}

func New(kubeconfigPath string) (Interface, error) {
func New(kubeconfigPath, nodeName string) (Interface, error) {
var err error
var rc *rest.Config
if kubeconfigPath != "" {
Expand All @@ -64,11 +67,27 @@ func New(kubeconfigPath string) (Interface, error) {
klog.Fatal("failed to configure k8s client")
}

return &Clientset{k8sClients: clientset}, nil
// Resync period of 0 means no resync
informerFactory := informers.NewSharedInformerFactoryWithOptions(
clientset,
0,
informers.WithTweakListOptions(func(options *metav1.ListOptions) {
if nodeName != "" {
options.FieldSelector = "spec.nodeName=" + nodeName
}
}),
)
podLister := informerFactory.Core().V1().Pods().Lister()

ctx := context.Background()
informerFactory.Start(ctx.Done())
informerFactory.WaitForCacheSync(ctx.Done())

return &Clientset{k8sClients: clientset, podLister: podLister}, nil
}

func (c *Clientset) GetPod(ctx context.Context, namespace, name string) (*corev1.Pod, error) {
return c.k8sClients.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{})
func (c *Clientset) GetPod(namespace, name string) (*corev1.Pod, error) {
return c.podLister.Pods(namespace).Get(name)
}

func (c *Clientset) CreateServiceAccountToken(ctx context.Context, namespace, name string, tokenRequest *authenticationv1.TokenRequest) (*authenticationv1.TokenRequest, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloud_provider/clientset/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

type FakeClientset struct{}

func (c *FakeClientset) GetPod(_ context.Context, namespace, name string) (*corev1.Pod, error) {
func (c *FakeClientset) GetPod(namespace, name string) (*corev1.Pod, error) {
config := webhook.FakeConfig()
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand Down
2 changes: 1 addition & 1 deletion pkg/csi_driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (s *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublish
}

// Check if the sidecar container was injected into the Pod
pod, err := s.k8sClients.GetPod(ctx, vc[VolumeContextKeyPodNamespace], vc[VolumeContextKeyPodName])
pod, err := s.k8sClients.GetPod(vc[VolumeContextKeyPodNamespace], vc[VolumeContextKeyPodName])
if err != nil {
return nil, status.Errorf(codes.NotFound, "failed to get pod: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var _ = func() bool {
flag.Parse()
framework.AfterReadingAllFlags(&framework.TestContext)

c, err = clientset.New(framework.TestContext.KubeConfig)
c, err = clientset.New(framework.TestContext.KubeConfig, "")
if err != nil {
klog.Fatalf("Failed to configure k8s client: %v", err)
}
Expand Down

0 comments on commit 0d9d3c7

Please sign in to comment.