4
4
"context"
5
5
"encoding/json"
6
6
"errors"
7
- "flag"
8
7
"fmt"
8
+ "log/slog"
9
9
"net/http"
10
10
"os"
11
11
"os/signal"
@@ -22,11 +22,11 @@ import (
22
22
_ "k8s.io/client-go/plugin/pkg/client/auth"
23
23
"k8s.io/client-go/rest"
24
24
"k8s.io/client-go/transport/spdy"
25
- "k8s.io/klog/v2"
26
25
27
26
"github.com/knight42/krelay/pkg/constants"
28
27
"github.com/knight42/krelay/pkg/ports"
29
28
"github.com/knight42/krelay/pkg/remoteaddr"
29
+ slogutil "github.com/knight42/krelay/pkg/slog"
30
30
"github.com/knight42/krelay/pkg/xnet"
31
31
)
32
32
@@ -41,6 +41,8 @@ type Options struct {
41
41
address string
42
42
// targetsFile is the file containing the list of targets.
43
43
targetsFile string
44
+
45
+ verbosity int
44
46
}
45
47
46
48
// setKubernetesDefaults sets default values on the provided client config for accessing the Kubernetes API.
@@ -153,7 +155,7 @@ func (o *Options) Run(ctx context.Context, args []string) error {
153
155
}
154
156
}
155
157
156
- klog . InfoS ("Creating krelay-server" , "namespace" , o .serverNamespace )
158
+ slog . Info ("Creating krelay-server" , slog . String ( "namespace" , o .serverNamespace ) )
157
159
svrPodName , err := createServerPod (ctx , cs , o .serverImage , o .serverNamespace )
158
160
if err != nil {
159
161
return fmt .Errorf ("create krelay-server pod: %w" , err )
@@ -164,7 +166,7 @@ func (o *Options) Run(ctx context.Context, args []string) error {
164
166
if err != nil {
165
167
return fmt .Errorf ("ensure krelay-server is running: %w" , err )
166
168
}
167
- klog . InfoS ("krelay-server is running" , "pod" , svrPodName , "namespace" , o .serverNamespace )
169
+ slog . Info ("krelay-server is running" , slog . String ( "pod" , svrPodName ), slog . String ( "namespace" , o .serverNamespace ) )
168
170
169
171
transport , upgrader , err := spdy .RoundTripperFor (restCfg )
170
172
if err != nil {
@@ -191,7 +193,7 @@ func (o *Options) Run(ctx context.Context, args []string) error {
191
193
for _ , pf := range portForwarders {
192
194
err := pf .listen (o .address )
193
195
if err != nil {
194
- klog . ErrorS ( err , "Fail to listen on port" , "port" , pf .ports .LocalPort )
196
+ slog . Error ( "Fail to listen on port" , slog . Any ( "port" , pf .ports .LocalPort ), slog . Any ( "error" , err ) )
195
197
} else {
196
198
succeeded = true
197
199
}
@@ -204,15 +206,14 @@ func (o *Options) Run(ctx context.Context, args []string) error {
204
206
205
207
select {
206
208
case <- streamConn .CloseChan ():
207
- klog . InfoS ("Lost connection to krelay-server pod" )
209
+ slog . Info ("Lost connection to krelay-server pod" )
208
210
case <- ctx .Done ():
209
211
}
210
212
211
213
return nil
212
214
}
213
215
214
216
func main () {
215
- klog .InitFlags (nil )
216
217
cf := genericclioptions .NewConfigFlags (true )
217
218
o := Options {
218
219
getter : cf ,
@@ -237,40 +238,29 @@ service, ip and hostname rather than only pods.`,
237
238
})
238
239
}
239
240
241
+ h := slog .NewTextHandler (cmd .ErrOrStderr (), & slog.HandlerOptions {
242
+ Level : slogutil .MapVerbosityToLogLevel (o .verbosity ),
243
+ })
244
+ slog .SetDefault (slog .New (h ))
240
245
ctx , cancel := signal .NotifyContext (context .Background (), os .Interrupt )
241
246
defer cancel ()
242
247
return o .Run (ctx , args )
243
248
},
244
249
SilenceUsage : true ,
245
250
}
246
251
flags := c .Flags ()
247
- flags .AddGoFlagSet (flag .CommandLine )
248
- cf .AddFlags (flags )
252
+ flags .SortFlags = false
253
+ flags .StringVar (cf .KubeConfig , "kubeconfig" , * cf .KubeConfig , "Path to the kubeconfig file to use for CLI requests." )
254
+ flags .StringVarP (cf .Namespace , "namespace" , "n" , * cf .Namespace , "If present, the namespace scope for this CLI request" )
255
+ flags .StringVar (cf .Context , "context" , * cf .Context , "The name of the kubeconfig context to use" )
256
+ flags .StringVar (cf .ClusterName , "cluster" , * cf .ClusterName , "The name of the kubeconfig cluster to use" )
257
+
249
258
flags .BoolVarP (& printVersion , "version" , "V" , false , "Print version info and exit." )
250
259
flags .StringVar (& o .address , "address" , "127.0.0.1" , "Address to listen on. Only accepts IP addresses as a value." )
260
+ flags .StringVarP (& o .targetsFile , "file" , "f" , "" , "Forward to the targets specified in the given file, with one target per line." )
251
261
flags .StringVar (& o .serverImage , "server.image" , "ghcr.io/knight42/krelay-server:v0.0.2" , "The krelay-server image to use." )
252
262
flags .StringVar (& o .serverNamespace , "server.namespace" , metav1 .NamespaceDefault , "The namespace in which krelay-server is located." )
253
- flags .StringVarP (& o .targetsFile , "file" , "f" , "" , "Forward to the targets specified in the given file, with one target per line." )
254
-
255
- // I do not want these flags to show up in --help.
256
- hiddenFlags := []string {
257
- "add_dir_header" ,
258
- "log_flush_frequency" ,
259
- "alsologtostderr" ,
260
- "log_backtrace_at" ,
261
- "log_dir" ,
262
- "log_file" ,
263
- "log_file_max_size" ,
264
- "one_output" ,
265
- "logtostderr" ,
266
- "skip_headers" ,
267
- "skip_log_headers" ,
268
- "stderrthreshold" ,
269
- "vmodule" ,
270
- }
271
- for _ , flagName := range hiddenFlags {
272
- _ = flags .MarkHidden (flagName )
273
- }
263
+ flags .IntVarP (& o .verbosity , "v" , "v" , 3 , "Number for the log level verbosity. The bigger the more verbose." )
274
264
275
265
_ = c .Execute ()
276
266
}
0 commit comments