Skip to content

SDK log injection and uniform logging in operator-sdk binary #707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions commands/operator-sdk/cmd/add/api.go
Original file line number Diff line number Diff line change
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 @@ -68,6 +67,9 @@ func apiRun(cmd *cobra.Command, args []string) {

// Create and validate new resource
projutil.MustInProjectRoot()

log.Infof("Generating api version %s for kind %s.", apiVersion, kind)

r, err := scaffold.NewResource(apiVersion, kind)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -95,9 +97,11 @@ 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
generate.K8sCodegen()

log.Info("Api generation complete.")
}
8 changes: 6 additions & 2 deletions commands/operator-sdk/cmd/add/controller.go
Original file line number Diff line number Diff line change
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 Expand Up @@ -61,6 +60,9 @@ func controllerRun(cmd *cobra.Command, args []string) {
projutil.MustGoProjectCmd(cmd)

projutil.MustInProjectRoot()

log.Infof("Generating controller version %s for kind %s.", apiVersion, kind)

// Create and validate new resource
r, err := scaffold.NewResource(apiVersion, kind)
if err != nil {
Expand All @@ -80,4 +82,6 @@ func controllerRun(cmd *cobra.Command, args []string) {
if err != nil {
log.Fatalf("add scaffold failed: (%v)", err)
}

log.Info("Controller generation complete.")
}
15 changes: 8 additions & 7 deletions commands/operator-sdk/cmd/add/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package add

import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
Expand All @@ -25,6 +23,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,27 +59,29 @@ func crdFunc(cmd *cobra.Command, args []string) {
verifyCrdFlags()
verifyCrdDeployPath()

fmt.Fprintln(os.Stdout, "Generating custom resource definition (CRD) file")
log.Infof("Generating Custom Resource Definition (CRD) version %s for kind %s.", apiVersion, kind)

// 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)
}

log.Info("CRD generation complete.")
}

func verifyCrdFlags() {
Expand All @@ -103,7 +104,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
33 changes: 21 additions & 12 deletions commands/operator-sdk/cmd/build.go
Original file line number Diff line number Diff line change
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 @@ -163,15 +163,18 @@ func buildFunc(cmd *cobra.Command, args []string) {
if enableTests {
baseImageName += "-intermediate"
}

log.Infof("Building Docker image %s", baseImageName)

dbcmd := exec.Command("docker", "build", ".", "-f", "build/Dockerfile", "-t", baseImageName)
dbcmd.Stdout = os.Stdout
dbcmd.Stderr = os.Stderr
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,13 +186,15 @@ 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)
_, err = os.Stat(testDockerfile)
if err != nil && os.IsNotExist(err) {

log.Info("Generating build manifests for test-framework.")

absProjectPath := projutil.MustGetwd()
cfg := &input.Config{
Repo: projutil.CheckAndGetProjectGoPkg(),
Expand All @@ -204,20 +209,24 @@ 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-framework manifest scaffold failed: (%v)", err)
}
}

log.Infof("Building test Docker image %s", image)

testDbcmd := exec.Command("docker", "build", ".", "-f", testDockerfile, "-t", image, "--build-arg", "NAMESPACEDMAN="+namespacedManBuild, "--build-arg", "BASEIMAGE="+baseImageName)
testDbcmd.Stdout = os.Stdout
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)
}

log.Info("Operator build complete.")
}

func mainExists() bool {
Expand Down
20 changes: 14 additions & 6 deletions commands/operator-sdk/cmd/generate/k8s.go
Original file line number Diff line number Diff line change
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,22 +62,26 @@ 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.Infof("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
genGroupsCmd := "vendor/k8s.io/code-generator/generate-groups.sh"
args := []string{
"deepcopy",
outputPkg,
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))

log.Info("Code-generation complete.")
}

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

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

return groupVersions, nil
}
Loading