Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
Lazily initialize kubernetes client only when using kube api watcher (#…
Browse files Browse the repository at this point in the history
…56)

* Lazily initialize kubernetes client only when using kube api watcher

Signed-off-by: Jeev B <[email protected]>

* Fix parsing of podname to watch from CLI args

Signed-off-by: Jeev B <[email protected]>

* Minor fix

Signed-off-by: Jeev B <[email protected]>

---------

Signed-off-by: Jeev B <[email protected]>
  • Loading branch information
jeevb committed Apr 6, 2023
1 parent 97772fd commit 12f9ff5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
14 changes: 12 additions & 2 deletions cmd/containerwatcher/kubeapi_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{info: info}, err
}
kubeClient, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return kubeAPIWatcher{info: info}, err
}
return kubeAPIWatcher{coreClient: kubeClient.CoreV1(), info: info}, nil
}
20 changes: 1 addition & 19 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cmd/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
}

0 comments on commit 12f9ff5

Please sign in to comment.