Skip to content

commands/operator-sdk: Add CLI support for Ansible Operator #486

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 16 commits into from
Oct 2, 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
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ script:
- operator-sdk test local .
# test operator-sdk test flags
- operator-sdk test local . --global-manifest deploy/crd.yaml --namespaced-manifest deploy/namespace-init.yaml --go-test-flags "-parallel 1" --kubeconfig $HOME/.kube/config
# test operator-sdk test local single namespace mode
- kubectl create namespace test-memcached
- operator-sdk test local . --namespace=test-memcached
- kubectl delete namespace test-memcached
# go back to project root
- cd ../..
- go vet ./...
Expand Down
133 changes: 127 additions & 6 deletions Gopkg.lock

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

34 changes: 26 additions & 8 deletions commands/operator-sdk/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"log"
"os"
"os/exec"
"strings"

"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/cmdutil"
cmdError "github.com/operator-framework/operator-sdk/commands/operator-sdk/error"
Expand Down Expand Up @@ -135,29 +136,33 @@ func renderTestManifest(image string) {
const (
build = "./tmp/build/build.sh"
configYaml = "./config/config.yaml"
mainGo = "./cmd/%s/main.go"
)

func buildFunc(cmd *cobra.Command, args []string) {
if len(args) != 1 {
cmdError.ExitWithError(cmdError.ExitBadArgs, fmt.Errorf("build command needs exactly 1 argument"))
}

bcmd := exec.Command(build)
bcmd.Env = append(os.Environ(), fmt.Sprintf("TEST_LOCATION=%v", testLocationBuild))
bcmd.Env = append(bcmd.Env, fmt.Sprintf("ENABLE_TESTS=%v", enableTests))
o, err := bcmd.CombinedOutput()
if err != nil {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to build: (%v)", string(o)))
// Don't need to buld go code if Ansible Operator
if mainExists() {
bcmd := exec.Command(build)
bcmd.Env = append(os.Environ(), fmt.Sprintf("TEST_LOCATION=%v", testLocationBuild))
bcmd.Env = append(bcmd.Env, fmt.Sprintf("ENABLE_TESTS=%v", enableTests))
o, err := bcmd.CombinedOutput()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @AlexNPavel mentioned: You need to pass the envs from L146-147 to the build command.

bcmd.Env = append(os.Environ(), fmt.Sprintf("TEST_LOCATION=%v", testLocationBuild))
bcmd.Env = append(bcmd.Env, fmt.Sprintf("ENABLE_TESTS=%v", enableTests))

We made this change in #469

Although we're planning to remove the build script altogether in #541 so you might need to rebase again if that gets done first.

Copy link
Contributor Author

@dymurray dymurray Sep 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for updating that @johnkim76. Appreciate the heads up on #541 as well

if err != nil {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to build: (%v)", string(o)))
}
fmt.Fprintln(os.Stdout, string(o))
}
fmt.Fprintln(os.Stdout, string(o))

image := args[0]
baseImageName := image
if enableTests {
baseImageName += "-intermediate"
}
dbcmd := exec.Command("docker", "build", ".", "-f", "tmp/build/Dockerfile", "-t", baseImageName)
o, err = dbcmd.CombinedOutput()
o, err := dbcmd.CombinedOutput()
if err != nil {
if enableTests {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to build intermediate image for %s image: (%s)", image, string(o)))
Expand All @@ -178,3 +183,16 @@ func buildFunc(cmd *cobra.Command, args []string) {
renderTestManifest(image)
}
}

func mainExists() bool {
dir, err := os.Getwd()
if err != nil {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to get current working dir: %v", err))
}
dirSplit := strings.Split(dir, "/")
projectName := dirSplit[len(dirSplit)-1]
if _, err = os.Stat(fmt.Sprintf(mainGo, projectName)); err == nil {
return true
}
return false
}
1 change: 1 addition & 0 deletions commands/operator-sdk/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ func NewGenerateCmd() *cobra.Command {
}
cmd.AddCommand(generate.NewGenerateK8SCmd())
cmd.AddCommand(generate.NewGenerateOlmCatalogCmd())
cmd.AddCommand(generate.NewGenerateCrdCmd())
return cmd
}
Loading