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

Commit 12f9ff5

Browse files
authored
Lazily initialize kubernetes client only when using kube api watcher (#56)
* Lazily initialize kubernetes client only when using kube api watcher Signed-off-by: Jeev B <jeevb@users.noreply.github.com> * Fix parsing of podname to watch from CLI args Signed-off-by: Jeev B <jeevb@users.noreply.github.com> * Minor fix Signed-off-by: Jeev B <jeevb@users.noreply.github.com> --------- Signed-off-by: Jeev B <jeevb@users.noreply.github.com>
1 parent 97772fd commit 12f9ff5

File tree

3 files changed

+15
-23
lines changed

3 files changed

+15
-23
lines changed

cmd/containerwatcher/kubeapi_watcher.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
v13 "k8s.io/api/core/v1"
99
v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
1010
"k8s.io/apimachinery/pkg/fields"
11+
"k8s.io/client-go/kubernetes"
1112
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
13+
"k8s.io/client-go/tools/clientcmd"
1214
)
1315

1416
type ContainerInformation struct {
@@ -80,6 +82,14 @@ func (k kubeAPIWatcher) WaitToExit(ctx context.Context) error {
8082
return k.wait(ctx, k.info, f)
8183
}
8284

83-
func NewKubeAPIWatcher(_ context.Context, coreClient v1.CoreV1Interface, info ContainerInformation) (Watcher, error) {
84-
return kubeAPIWatcher{coreClient: coreClient, info: info}, nil
85+
func NewKubeAPIWatcher(_ context.Context, clientConfig clientcmd.ClientConfig, info ContainerInformation) (Watcher, error) {
86+
restConfig, err := clientConfig.ClientConfig()
87+
if err != nil {
88+
return kubeAPIWatcher{info: info}, err
89+
}
90+
kubeClient, err := kubernetes.NewForConfig(restConfig)
91+
if err != nil {
92+
return kubeAPIWatcher{info: info}, err
93+
}
94+
return kubeAPIWatcher{coreClient: kubeClient.CoreV1(), info: info}, nil
8595
}

cmd/root.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424
"github.com/spf13/cobra"
2525
"github.com/spf13/pflag"
2626
"k8s.io/apimachinery/pkg/util/wait"
27-
"k8s.io/client-go/kubernetes"
28-
"k8s.io/client-go/rest"
2927
"k8s.io/client-go/tools/clientcmd"
3028
"k8s.io/klog"
3129
)
@@ -34,8 +32,6 @@ type RootOptions struct {
3432
*clientcmd.ConfigOverrides
3533
showSource bool
3634
clientConfig clientcmd.ClientConfig
37-
restConfig *rest.Config
38-
kubeClient kubernetes.Interface
3935
Scope promutils.Scope
4036
Store *storage.DataStore
4137
configAccessor config.Accessor
@@ -129,7 +125,7 @@ func NewDataCommand() *cobra.Command {
129125
return errors.Wrap(err, "failed to create datastore client")
130126
}
131127
rootOpts.Store = store
132-
return rootOpts.ConfigureClient()
128+
return nil
133129
},
134130
RunE: func(cmd *cobra.Command, args []string) error {
135131
return rootOpts.executeRootCmd()
@@ -182,20 +178,6 @@ func (r *RootOptions) initConfig(cmd *cobra.Command, _ []string) error {
182178
return nil
183179
}
184180

185-
func (r *RootOptions) ConfigureClient() error {
186-
restConfig, err := r.clientConfig.ClientConfig()
187-
if err != nil {
188-
return err
189-
}
190-
k, err := kubernetes.NewForConfig(restConfig)
191-
if err != nil {
192-
return err
193-
}
194-
r.restConfig = restConfig
195-
r.kubeClient = k
196-
return nil
197-
}
198-
199181
func init() {
200182
klog.InitFlags(flag.CommandLine)
201183
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)

cmd/sidecar.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (u *UploadOptions) createWatcher(ctx context.Context, w containerwatcher.Wa
4848
case containerwatcher.WatcherTypeKubeAPI:
4949
// TODO, in this case container info should have namespace and podname and we can get it using downwardapi
5050
// TODO https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/
51-
return containerwatcher.NewKubeAPIWatcher(ctx, u.RootOptions.kubeClient.CoreV1(), u.containerInfo)
51+
return containerwatcher.NewKubeAPIWatcher(ctx, u.RootOptions.clientConfig, u.containerInfo)
5252
case containerwatcher.WatcherTypeFile:
5353
return containerwatcher.NewSuccessFileWatcher(ctx, u.localDirectoryPath, StartFile, SuccessFile, ErrorFile)
5454
case containerwatcher.WatcherTypeSharedProcessNS:
@@ -181,6 +181,6 @@ func NewUploadCommand(opts *RootOptions) *cobra.Command {
181181
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))
182182
uploadCmd.Flags().StringVarP(&uploadOptions.containerInfo.Name, "watch-container", "", "", "For KubeAPI watcher, Wait for this container to exit.")
183183
uploadCmd.Flags().StringVarP(&uploadOptions.containerInfo.Namespace, "namespace", "", "", "For KubeAPI watcher, Namespace of the pod [optional]")
184-
uploadCmd.Flags().StringVarP(&uploadOptions.containerInfo.Name, "pod-name", "", "", "For KubeAPI watcher, Name of the pod [optional].")
184+
uploadCmd.Flags().StringVarP(&uploadOptions.containerInfo.PodName, "pod-name", "", "", "For KubeAPI watcher, Name of the pod [optional].")
185185
return uploadCmd
186186
}

0 commit comments

Comments
 (0)