From a254cf2b5b8a1c628a2a6f174de6f9c61dd7a5c6 Mon Sep 17 00:00:00 2001 From: Jeev B Date: Thu, 6 Apr 2023 12:44:13 -0700 Subject: [PATCH 1/3] Lazily initialize kubernetes client only when using kube api watcher Signed-off-by: Jeev B --- cmd/containerwatcher/kubeapi_watcher.go | 14 ++++++++++++-- cmd/root.go | 20 +------------------- cmd/sidecar.go | 2 +- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/cmd/containerwatcher/kubeapi_watcher.go b/cmd/containerwatcher/kubeapi_watcher.go index 78329c4..3aa339a 100644 --- a/cmd/containerwatcher/kubeapi_watcher.go +++ b/cmd/containerwatcher/kubeapi_watcher.go @@ -8,7 +8,9 @@ import ( v13 "k8s.io/api/core/v1" v12 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" + "k8s.io/client-go/kubernetes" v1 "k8s.io/client-go/kubernetes/typed/core/v1" + "k8s.io/client-go/tools/clientcmd" ) type ContainerInformation struct { @@ -80,6 +82,14 @@ func (k kubeAPIWatcher) WaitToExit(ctx context.Context) error { return k.wait(ctx, k.info, f) } -func NewKubeAPIWatcher(_ context.Context, coreClient v1.CoreV1Interface, info ContainerInformation) (Watcher, error) { - return kubeAPIWatcher{coreClient: coreClient, info: info}, nil +func NewKubeAPIWatcher(_ context.Context, clientConfig clientcmd.ClientConfig, info ContainerInformation) (Watcher, error) { + restConfig, err := clientConfig.ClientConfig() + if err != nil { + return kubeAPIWatcher{}, err + } + kubeClient, err := kubernetes.NewForConfig(restConfig) + if err != nil { + return kubeAPIWatcher{}, err + } + return kubeAPIWatcher{coreClient: kubeClient.CoreV1(), info: info}, nil } diff --git a/cmd/root.go b/cmd/root.go index 6a5b4f0..4df4e98 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -24,8 +24,6 @@ import ( "github.com/spf13/cobra" "github.com/spf13/pflag" "k8s.io/apimachinery/pkg/util/wait" - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" "k8s.io/klog" ) @@ -34,8 +32,6 @@ type RootOptions struct { *clientcmd.ConfigOverrides showSource bool clientConfig clientcmd.ClientConfig - restConfig *rest.Config - kubeClient kubernetes.Interface Scope promutils.Scope Store *storage.DataStore configAccessor config.Accessor @@ -129,7 +125,7 @@ func NewDataCommand() *cobra.Command { return errors.Wrap(err, "failed to create datastore client") } rootOpts.Store = store - return rootOpts.ConfigureClient() + return nil }, RunE: func(cmd *cobra.Command, args []string) error { return rootOpts.executeRootCmd() @@ -182,20 +178,6 @@ func (r *RootOptions) initConfig(cmd *cobra.Command, _ []string) error { return nil } -func (r *RootOptions) ConfigureClient() error { - restConfig, err := r.clientConfig.ClientConfig() - if err != nil { - return err - } - k, err := kubernetes.NewForConfig(restConfig) - if err != nil { - return err - } - r.restConfig = restConfig - r.kubeClient = k - return nil -} - func init() { klog.InitFlags(flag.CommandLine) pflag.CommandLine.AddGoFlagSet(flag.CommandLine) diff --git a/cmd/sidecar.go b/cmd/sidecar.go index 8b074b5..da2d308 100644 --- a/cmd/sidecar.go +++ b/cmd/sidecar.go @@ -48,7 +48,7 @@ func (u *UploadOptions) createWatcher(ctx context.Context, w containerwatcher.Wa case containerwatcher.WatcherTypeKubeAPI: // TODO, in this case container info should have namespace and podname and we can get it using downwardapi // TODO https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/ - return containerwatcher.NewKubeAPIWatcher(ctx, u.RootOptions.kubeClient.CoreV1(), u.containerInfo) + return containerwatcher.NewKubeAPIWatcher(ctx, u.RootOptions.clientConfig, u.containerInfo) case containerwatcher.WatcherTypeFile: return containerwatcher.NewSuccessFileWatcher(ctx, u.localDirectoryPath, StartFile, SuccessFile, ErrorFile) case containerwatcher.WatcherTypeSharedProcessNS: From f4fb1bb0039108cb72656c18c547132b48258b2c Mon Sep 17 00:00:00 2001 From: Jeev B Date: Thu, 6 Apr 2023 13:54:16 -0700 Subject: [PATCH 2/3] Fix parsing of podname to watch from CLI args Signed-off-by: Jeev B --- cmd/sidecar.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/sidecar.go b/cmd/sidecar.go index da2d308..299b5eb 100644 --- a/cmd/sidecar.go +++ b/cmd/sidecar.go @@ -181,6 +181,6 @@ func NewUploadCommand(opts *RootOptions) *cobra.Command { uploadCmd.Flags().StringVarP(&uploadOptions.exitWatcherType, "exit-watcher-type", "", containerwatcher.WatcherTypeSharedProcessNS, fmt.Sprintf("Sidecar will wait for completion of the container before starting upload process. Watcher type makes the type configurable. Available Type %+v", containerwatcher.AllWatcherTypes)) uploadCmd.Flags().StringVarP(&uploadOptions.containerInfo.Name, "watch-container", "", "", "For KubeAPI watcher, Wait for this container to exit.") uploadCmd.Flags().StringVarP(&uploadOptions.containerInfo.Namespace, "namespace", "", "", "For KubeAPI watcher, Namespace of the pod [optional]") - uploadCmd.Flags().StringVarP(&uploadOptions.containerInfo.Name, "pod-name", "", "", "For KubeAPI watcher, Name of the pod [optional].") + uploadCmd.Flags().StringVarP(&uploadOptions.containerInfo.PodName, "pod-name", "", "", "For KubeAPI watcher, Name of the pod [optional].") return uploadCmd } From 8cc786e21ec886d6b40ab6d4e4b703797ddaf1ea Mon Sep 17 00:00:00 2001 From: Jeev B Date: Thu, 6 Apr 2023 14:07:29 -0700 Subject: [PATCH 3/3] Minor fix Signed-off-by: Jeev B --- cmd/containerwatcher/kubeapi_watcher.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/containerwatcher/kubeapi_watcher.go b/cmd/containerwatcher/kubeapi_watcher.go index 3aa339a..c6857b7 100644 --- a/cmd/containerwatcher/kubeapi_watcher.go +++ b/cmd/containerwatcher/kubeapi_watcher.go @@ -85,11 +85,11 @@ func (k kubeAPIWatcher) WaitToExit(ctx context.Context) error { func NewKubeAPIWatcher(_ context.Context, clientConfig clientcmd.ClientConfig, info ContainerInformation) (Watcher, error) { restConfig, err := clientConfig.ClientConfig() if err != nil { - return kubeAPIWatcher{}, err + return kubeAPIWatcher{info: info}, err } kubeClient, err := kubernetes.NewForConfig(restConfig) if err != nil { - return kubeAPIWatcher{}, err + return kubeAPIWatcher{info: info}, err } return kubeAPIWatcher{coreClient: kubeClient.CoreV1(), info: info}, nil }