Skip to content

Commit

Permalink
add klog
Browse files Browse the repository at this point in the history
  • Loading branch information
joyme123 committed Jun 27, 2023
1 parent 0c69d4a commit eb2b3e8
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
8 changes: 7 additions & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -17,8 +17,10 @@ package cmd

import (
"log"
"time"

"github.com/spf13/cobra"
"k8s.io/klog/v2"
)

type listOption struct {
Expand All @@ -44,11 +46,15 @@ func list() {
log.Fatalf("fatal: %v", err)
}

startTime := time.Now()

c, err := m.List()
if err != nil {
log.Fatalf("list configs error: %v", err)
}
c.Print(listOpt.Current)
cost := time.Since(startTime)
klog.V(4).Infof("list cost: %d ms", cost.Milliseconds())
}

func init() {
Expand Down
11 changes: 10 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -16,13 +16,16 @@ limitations under the License.
package cmd

import (
goflags "flag"
"path"

"github.com/joyme123/kubecm/pkg/consts"
"github.com/joyme123/kubecm/pkg/manager"
"github.com/joyme123/kubecm/pkg/util"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/klog/v2"
)

// rootCmd represents the base command when called without any subcommands
Expand All @@ -43,6 +46,12 @@ func Execute() {
cobra.CheckErr(rootCmd.Execute())
}

func init() {
klog.InitFlags(nil)
goflags.Parse()
pflag.CommandLine.AddGoFlagSet(goflags.CommandLine)
}

func newManagerInterface() (manager.Interface, error) {
home := util.GetHomeDir()
configDir := path.Join(home, consts.DefaultConfigDir)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ require (
github.com/spf13/cobra v1.1.3
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e
k8s.io/client-go v0.0.0-20220328171824-1110612dc6e5
k8s.io/klog/v2 v2.100.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v0.4.0 // indirect
github.com/go-logr/logr v1.2.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.0 // indirect
github.com/google/go-cmp v0.5.5 // indirect
Expand All @@ -35,7 +36,6 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/apimachinery v0.0.0-20220328170958-67c29d29c4cd // indirect
k8s.io/klog/v2 v2.9.0 // indirect
k8s.io/utils v0.0.0-20211116205334-6203023598ed // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
github.com/go-logr/logr v0.4.0 h1:K7/B1jt6fIBQVd4Owv2MqGQClcgf0R266+7C/QjRcLc=
github.com/go-logr/logr v0.4.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-logr/logr v1.2.0 h1:QK40JKJyMdUDz+h+xvCsru/bJhvG0UxvePV0ufL/AcE=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-openapi/jsonpointer v0.19.2/go.mod h1:3akKfEdA7DF1sugOqz1dVQHBcuDBPKZGEoHC/NkiQRg=
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/jsonreference v0.19.2/go.mod h1:jMjeRr2HHw6nAVajTXJ4eiUwohSTlpa0o73RUL1owJc=
Expand Down Expand Up @@ -536,8 +537,9 @@ k8s.io/client-go v0.0.0-20220328171824-1110612dc6e5 h1:t+hfkGq9eLq6nkUZB9bEpXNHf
k8s.io/client-go v0.0.0-20220328171824-1110612dc6e5/go.mod h1:2+UZE1aeMy8RLxq203VzMitsvGrQyEUHuusSxtJpBSc=
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=
k8s.io/klog/v2 v2.9.0 h1:D7HV+n1V57XeZ0m6tdRkfknthUaM06VFbWldOFh8kzM=
k8s.io/klog/v2 v2.9.0/go.mod h1:hy9LJ/NvuK+iVyP4Ehqva4HxZG/oXyIS3n3Jmire4Ec=
k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg=
k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/kube-openapi v0.0.0-20211110012726-3cc51fd1e909/go.mod h1:wXW5VT87nVfh/iLV8FpR2uDvrFyomxbtb1KivDbvPTE=
k8s.io/utils v0.0.0-20211116205334-6203023598ed h1:ck1fRPWPJWsMd8ZRFsWc6mh/zHp5fZ/shhbrgPUxDAE=
k8s.io/utils v0.0.0-20211116205334-6203023598ed/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -15,7 +15,9 @@ limitations under the License.
*/
package main

import "github.com/joyme123/kubecm/cmd"
import (
"github.com/joyme123/kubecm/cmd"
)

func main() {
cmd.Execute()
Expand Down
8 changes: 5 additions & 3 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import (
"sync"
"time"

"github.com/google/uuid"

"github.com/joyme123/kubecm/pkg/loader"
"github.com/joyme123/kubecm/pkg/types"

"github.com/joyme123/kubecm/pkg/util"

"github.com/ghodss/yaml"
"github.com/google/uuid"
"k8s.io/klog/v2"
)

type Interface interface {
Expand Down Expand Up @@ -48,10 +47,13 @@ func NewInterface(configDir string, configPath string, kubePath string) (Interfa
conf: &types.Configuration{},
}

startTime := time.Now()
err := i.init()
if err != nil {
return nil, err
}
cost := time.Since(startTime)
klog.V(4).Infof("init cost: %d ms", cost.Milliseconds())
return i, nil
}

Expand Down

0 comments on commit eb2b3e8

Please sign in to comment.