Skip to content

Commit

Permalink
Fix logs with klog
Browse files Browse the repository at this point in the history
  • Loading branch information
efortin committed Jan 14, 2021
1 parent f42446a commit 0cad411
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ require (
github.com/spf13/cobra v1.1.1
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.3.0
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5
gopkg.in/yaml.v2 v2.2.8 // indirect
k8s.io/klog/v2 v2.4.0
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
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.2.0 h1:QvGt2nLcHH0WK9orKa+ppBPAxREcH364nPUedEpK0TY=
github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-test/deep v1.0.7 h1:/VSMRlnY/JSyqxQUzQLKVMAskpY/NZKFA5j2P+0pP2M=
github.com/go-test/deep v1.0.7/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8=
Expand Down Expand Up @@ -329,4 +331,7 @@ honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
k8s.io/klog v1.0.0 h1:Pt+yjF5aB1xDSVbau4VsWe+dQNzA0qv1LlXdC2dF6Q8=
k8s.io/klog/v2 v2.4.0 h1:7+X0fUguPyrKEC4WjH8iGDg3laWgMo5tMnRTIGTTxGQ=
k8s.io/klog/v2 v2.4.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
13 changes: 7 additions & 6 deletions internal/ansible/playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/ghodss/yaml"
"github.com/karrick/godirwalk"
"io/ioutil"
"k8s.io/klog/v2"
"path/filepath"
"strings"
)
Expand Down Expand Up @@ -52,20 +53,20 @@ func readPlaybook(rootPath string) (result []*Playbook, err error) {
var plays []Play
binData, err := ioutil.ReadFile(osPathname)
if err != nil {
// TODO add debug for read
klog.Error("Cannot read playbook", osPathname, ". Error: ", err.Error())
return nil
}
err = yaml.Unmarshal([]byte(binData), &plays)
if err != nil {
// TODO add debug for unmarshaling
klog.Error("Cannot unmashal playbook data", osPathname, ". Error: ", err.Error())
return nil
}
if plays == nil || len(plays) == 0 {
// TODO Log debug no play found
klog.V(8).Info("No play found inside the playbook: ", osPathname)
return nil
}
if plays[0].Hosts == "" {
// TODO Log debug do not seems to be a playbook
if plays[0].Hosts == utils.EmptyString {
klog.V(8).Info("No play found inside the playbook: ", osPathname)
return nil
}

Expand All @@ -85,7 +86,7 @@ func readPlaybook(rootPath string) (result []*Playbook, err error) {
AllTags: *allTags,
}
result = append(result, &playbook)
fmt.Printf("Struct: %v", playbook.AllTags)
klog.V(8).Info("Available tags are :", playbook.AllTags)
return nil
},
ErrorCallback: func(osPathname string, err error) godirwalk.ErrorAction {
Expand Down
9 changes: 4 additions & 5 deletions internal/ansible/role.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package ansible

import (
"fmt"
"github.com/ca-gip/dploy/internal/utils"
"github.com/ghodss/yaml"
"github.com/karrick/godirwalk"
"io/ioutil"
"k8s.io/klog/v2"
"path/filepath"
"strings"
)
Expand All @@ -24,7 +24,7 @@ func (role *Role) ReadRole(rootPath string, pathTags ...string) (err error) {
absRoot, err := filepath.Abs(rootPath + "/roles/" + role.Name)

if err != nil {
// TODO log erreor reading role
klog.Error("The role ", role.Name, "can't be read. Error:", err.Error())
return
}

Expand All @@ -39,8 +39,7 @@ func (role *Role) ReadRole(rootPath string, pathTags ...string) (err error) {

binData, err := ioutil.ReadFile(osPathname)
if err != nil {
// TODO log fatal, unable to read file
fmt.Println(err)
klog.Error("Cannot read file: ", osPathname, ". Error:", err.Error())
}

var tasks []Task
Expand All @@ -51,7 +50,7 @@ func (role *Role) ReadRole(rootPath string, pathTags ...string) (err error) {

tasks = append(tasks, Task{Tags: tags.List()})
if len(tags.List()) > 0 {
fmt.Println("Task tags:", tags.List())
klog.V(8).Info("Task tags:", tags.List())
}
return nil
},
Expand Down
14 changes: 4 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,13 @@ func main() {

//home, _ := os.UserHomeDir()
//path := fmt.Sprintf("%s/%s", home, "Projects/ansible-kube")
//k8s := ansible.LoadFromPath(path)
//
//filter := []string{"customer!=cacf_corp_hors_prod"}
//filteredInventories := k8s.FilterFromVars(filter)
//fmt.Println("Filtering ", len(filteredInventories), "/", len(k8s.Inventories))
//for _, i := range filteredInventories {
// fmt.Println(i.AbsolutePath)
//}
//k8s := services.LoadFromPath(path)
//fmt.Println("Filtering ", len(k8s.Inventories), "/", len(k8s.Inventories))
//
//fmt.Println("Playbooks")
//
//tpl := ansible.AnsibleCommandTpl{
// Inventory: filteredInventories,
//tpl := services.AnsibleCommandTpl{
// Inventory: k8s.Inventories,
// Playbook: k8s.Playbooks[1],
// Tags: []string{"tag1", "tag2"},
// Limit: []string{"limit1,limit2"},
Expand Down

0 comments on commit 0cad411

Please sign in to comment.