Skip to content

Commit

Permalink
*: use logrus instead of std log
Browse files Browse the repository at this point in the history
  • Loading branch information
estroz committed Nov 14, 2018
1 parent 48d8946 commit 9e5ae1c
Show file tree
Hide file tree
Showing 18 changed files with 96 additions and 88 deletions.
5 changes: 2 additions & 3 deletions commands/operator-sdk/cmd/add/api.go
Expand Up @@ -15,13 +15,12 @@
package add

import (
"log"

"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/generate"
"github.com/operator-framework/operator-sdk/internal/util/projutil"
"github.com/operator-framework/operator-sdk/pkg/scaffold"
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -95,7 +94,7 @@ func apiRun(cmd *cobra.Command, args []string) {

// update deploy/role.yaml for the given resource r.
if err := scaffold.UpdateRoleForResource(r, absProjectPath); err != nil {
log.Fatalf("failed to update the RBAC manifest for the resource (%v, %v): %v", r.APIVersion, r.Kind, err)
log.Fatalf("failed to update the RBAC manifest for the resource (%v, %v): (%v)", r.APIVersion, r.Kind, err)
}

// Run k8s codegen for deepcopy
Expand Down
3 changes: 1 addition & 2 deletions commands/operator-sdk/cmd/add/controller.go
Expand Up @@ -15,12 +15,11 @@
package add

import (
"log"

"github.com/operator-framework/operator-sdk/internal/util/projutil"
"github.com/operator-framework/operator-sdk/pkg/scaffold"
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down
12 changes: 6 additions & 6 deletions commands/operator-sdk/cmd/add/crd.go
Expand Up @@ -16,7 +16,6 @@ package add

import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
Expand All @@ -25,6 +24,7 @@ import (
"github.com/operator-framework/operator-sdk/pkg/scaffold"
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -60,26 +60,26 @@ func crdFunc(cmd *cobra.Command, args []string) {
verifyCrdFlags()
verifyCrdDeployPath()

fmt.Fprintln(os.Stdout, "Generating custom resource definition (CRD) file")
fmt.Println("Generating custom resource definition (CRD) file")

// generate CR/CRD file
resource, err := scaffold.NewResource(apiVersion, kind)
if err != nil {
log.Fatalf("%v", err)
log.Fatal(err)
}

s := scaffold.Scaffold{}
err = s.Execute(cfg,
&scaffold.Crd{Resource: resource},
&scaffold.Cr{Resource: resource},
)

if err != nil {
log.Fatalf("add scaffold failed: (%v)", err)
}

// update deploy/role.yaml for the given resource r.
if err := scaffold.UpdateRoleForResource(resource, cfg.AbsProjectPath); err != nil {
log.Fatalf("failed to update the RBAC manifest for the resource (%v, %v): %v", resource.APIVersion, resource.Kind, err)
log.Fatalf("failed to update the RBAC manifest for the resource (%v, %v): (%v)", resource.APIVersion, resource.Kind, err)
}
}

Expand All @@ -103,7 +103,7 @@ func verifyCrdFlags() {
func verifyCrdDeployPath() {
wd, err := os.Getwd()
if err != nil {
log.Fatalf("failed to determine the full path of the current directory: %v", err)
log.Fatalf("failed to determine the full path of the current directory: (%v)", err)
}
// check if the deploy sub-directory exist
_, err = os.Stat(filepath.Join(wd, scaffold.DeployDir))
Expand Down
24 changes: 12 additions & 12 deletions commands/operator-sdk/cmd/build.go
Expand Up @@ -19,7 +19,6 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
Expand All @@ -30,6 +29,7 @@ import (
"github.com/operator-framework/operator-sdk/pkg/test"

"github.com/ghodss/yaml"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -78,11 +78,11 @@ func verifyDeploymentImage(yamlFile []byte, imageName string) error {
yamlMap := make(map[string]interface{})
err := yaml.Unmarshal(yamlSpec, &yamlMap)
if err != nil {
log.Fatal("Could not unmarshal yaml namespaced spec")
log.Fatalf("could not unmarshal yaml namespaced spec: (%v)", err)
}
kind, ok := yamlMap["kind"].(string)
if !ok {
log.Fatal("Yaml manifest file contains a 'kind' field that is not a string")
log.Fatal("yaml manifest file contains a 'kind' field that is not a string")
}
if kind == "Deployment" {
// this is ugly and hacky; we should probably make this cleaner
Expand Down Expand Up @@ -122,13 +122,13 @@ func verifyDeploymentImage(yamlFile []byte, imageName string) error {
func verifyTestManifest(image string) {
namespacedBytes, err := ioutil.ReadFile(namespacedManBuild)
if err != nil {
log.Fatalf("could not read namespaced manifest: %v", err)
log.Fatalf("could not read namespaced manifest: (%v)", err)
}

err = verifyDeploymentImage(namespacedBytes, image)
// the error from verifyDeploymentImage is just a warning, not fatal error
if err != nil {
fmt.Printf("%v\n", err)
log.Warn(err)
}
}

Expand All @@ -141,7 +141,7 @@ func buildFunc(cmd *cobra.Command, args []string) {
goBuildEnv := append(os.Environ(), "GOOS=linux", "GOARCH=amd64", "CGO_ENABLED=0")
wd, err := os.Getwd()
if err != nil {
log.Fatalf("could not identify current working directory: %v", err)
log.Fatalf("could not identify current working directory: (%v)", err)
}

// Don't need to build go code if Ansible Operator
Expand All @@ -154,7 +154,7 @@ func buildFunc(cmd *cobra.Command, args []string) {
buildCmd.Stderr = os.Stderr
err = buildCmd.Run()
if err != nil {
log.Fatalf("failed to build operator binary: %v", err)
log.Fatalf("failed to build operator binary: (%v)", err)
}
}

Expand All @@ -169,9 +169,9 @@ func buildFunc(cmd *cobra.Command, args []string) {
err = dbcmd.Run()
if err != nil {
if enableTests {
log.Fatalf("failed to build intermediate image for %s image: %v", image, err)
log.Fatalf("failed to output intermediate image %s: (%v)", image, err)
} else {
log.Fatalf("failed to output build image %s: %v", image, err)
log.Fatalf("failed to output build image %s: (%v)", image, err)
}
}

Expand All @@ -183,7 +183,7 @@ func buildFunc(cmd *cobra.Command, args []string) {
buildTestCmd.Stderr = os.Stderr
err = buildTestCmd.Run()
if err != nil {
log.Fatalf("failed to build test binary: %v", err)
log.Fatalf("failed to build test binary: (%v)", err)
}
// if a user is using an older sdk repo as their library, make sure they have required build files
testDockerfile := filepath.Join(scaffold.BuildTestDir, scaffold.DockerfileFile)
Expand All @@ -204,7 +204,7 @@ func buildFunc(cmd *cobra.Command, args []string) {
&scaffold.TestPod{Image: image, TestNamespaceEnv: test.TestNamespaceEnv},
)
if err != nil {
log.Fatalf("build scaffold failed: (%v)", err)
log.Fatalf("test scaffold failed: (%v)", err)
}
}

Expand All @@ -213,7 +213,7 @@ func buildFunc(cmd *cobra.Command, args []string) {
testDbcmd.Stderr = os.Stderr
err = testDbcmd.Run()
if err != nil {
log.Fatalf("failed to output build image %s: %v", image, err)
log.Fatalf("failed to output test image %s: (%v)", image, err)
}
// Check image name of deployments in namespaced manifest
verifyTestManifest(image)
Expand Down
16 changes: 11 additions & 5 deletions commands/operator-sdk/cmd/generate/k8s.go
Expand Up @@ -17,14 +17,14 @@ package generate
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"

"github.com/operator-framework/operator-sdk/internal/util/projutil"
"github.com/operator-framework/operator-sdk/pkg/scaffold"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -41,7 +41,7 @@ to comply with kube-API requirements.

func k8sFunc(cmd *cobra.Command, args []string) {
if len(args) != 0 {
log.Fatalf("k8s command doesn't accept any arguments.")
log.Fatal("k8s command doesn't accept any arguments")
}

// Only Go projects can generate k8s deepcopy code.
Expand All @@ -62,7 +62,7 @@ func K8sCodegen() {
log.Fatalf("failed to parse group versions: (%v)", err)
}

fmt.Fprintf(os.Stdout, "Running code-generation for custom resource group versions: [%s]\n", groupVersions)
log.Printf("Running code-generation for custom resource group versions: [%s]\n", groupVersions)
// TODO: Replace generate-groups.sh by building the vendored generators(deepcopy, lister etc)
// and running them directly
// TODO: remove dependency on boilerplate.go.txt
Expand All @@ -73,11 +73,13 @@ func K8sCodegen() {
apisPkg,
groupVersions,
}
out, err := exec.Command(genGroupsCmd, args...).CombinedOutput()
cgCmd := exec.Command(genGroupsCmd, args...)
cgCmd.Stdout = os.Stdout
cgCmd.Stderr = os.Stderr
err = cgCmd.Run()
if err != nil {
log.Fatalf("failed to perform code-generation: (%v)", err)
}
fmt.Fprintln(os.Stdout, string(out))
}

// getGroupVersions parses the layout of pkg/apis to return the API groups and versions
Expand Down Expand Up @@ -108,5 +110,9 @@ func parseGroupVersions() (string, error) {
}
}

if groupVersions == "" {
return "", fmt.Errorf("no groups or versions found in %s", scaffold.ApisDir)
}

return groupVersions, nil
}
16 changes: 8 additions & 8 deletions commands/operator-sdk/cmd/new.go
Expand Up @@ -17,7 +17,6 @@ package cmd
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
Expand All @@ -28,6 +27,7 @@ import (
"github.com/operator-framework/operator-sdk/pkg/scaffold/ansible"
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -150,13 +150,13 @@ func doAnsibleScaffold() {

resource, err := scaffold.NewResource(apiVersion, kind)
if err != nil {
log.Fatal("Invalid apiVersion and kind.")
log.Fatalf("invalid apiVersion and kind: (%v)", err)
}

s := &scaffold.Scaffold{}
tmpdir, err := ioutil.TempDir("", "osdk")
if err != nil {
log.Fatal("unable to get temp directory")
log.Fatalf("unable to get temp directory: (%v)", err)
}

galaxyInit := &ansible.GalaxyInit{
Expand Down Expand Up @@ -196,7 +196,7 @@ func doAnsibleScaffold() {
},
)
if err != nil {
log.Fatalf("new scaffold failed: (%v)", err)
log.Fatalf("new playbook scaffold failed: (%v)", err)
}
}

Expand All @@ -210,12 +210,12 @@ func doAnsibleScaffold() {
// everything.
tmpDirectorySlice := strings.Split(os.TempDir(), "/")
if err = os.RemoveAll(filepath.Join(galaxyInit.AbsProjectPath, tmpDirectorySlice[1])); err != nil {
log.Fatalf("failed to remove the galaxy init script")
log.Fatalf("failed to remove the galaxy init script: (%v)", err)
}

// update deploy/role.yaml for the given resource r.
if err := scaffold.UpdateRoleForResource(resource, cfg.AbsProjectPath); err != nil {
log.Fatalf("failed to update the RBAC manifest for the resource (%v, %v): %v", resource.APIVersion, resource.Kind, err)
log.Fatalf("failed to update the RBAC manifest for the resource (%v, %v): (%v)", resource.APIVersion, resource.Kind, err)
}
}

Expand Down Expand Up @@ -254,14 +254,14 @@ func execCmd(stdout *os.File, cmd string, args ...string) {
dc.Stderr = os.Stderr
err := dc.Run()
if err != nil {
log.Fatalf("failed to exec %s %#v: %v", cmd, args, err)
log.Fatalf("failed to exec %s %#v: (%v)", cmd, args, err)
}
}

func pullDep() {
_, err := exec.LookPath(dep)
if err != nil {
log.Fatalf("looking for dep in $PATH: %v", err)
log.Fatalf("looking for dep in $PATH: (%v)", err)
}
fmt.Fprintln(os.Stdout, "Run dep ensure ...")
execCmd(os.Stdout, dep, ensureCmd, "-v")
Expand Down
5 changes: 3 additions & 2 deletions commands/operator-sdk/cmd/test/cluster.go
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/operator-framework/operator-sdk/pkg/scaffold"
"github.com/operator-framework/operator-sdk/pkg/test"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -110,7 +111,7 @@ func testClusterFunc(cmd *cobra.Command, args []string) error {
defer func() {
err = kubeclient.CoreV1().Pods(tcConfig.namespace).Delete(testPod.Name, &metav1.DeleteOptions{})
if err != nil {
fmt.Printf("Warning: failed to delete test pod")
log.Warn("failed to delete test pod")
}
}()
err = wait.Poll(time.Second*5, time.Second*time.Duration(tcConfig.pendingTimeout), func() (bool, error) {
Expand Down Expand Up @@ -140,7 +141,7 @@ func testClusterFunc(cmd *cobra.Command, args []string) error {
time.Sleep(time.Second * 5)
continue
} else if testPod.Status.Phase == v1.PodSucceeded {
fmt.Printf("Test Successfully Completed\n")
fmt.Println("Test Successfully Completed")
return nil
} else if testPod.Status.Phase == v1.PodFailed {
req := kubeclient.CoreV1().Pods(tcConfig.namespace).GetLogs(testPod.Name, &v1.PodLogOptions{})
Expand Down

0 comments on commit 9e5ae1c

Please sign in to comment.