Skip to content
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

adds migrate and run ansible commands #887

Merged
merged 5 commits into from
Jan 9, 2019
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
99 changes: 99 additions & 0 deletions commands/operator-sdk/cmd/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2019 The Operator-SDK Authors
//
// 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
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"log"
"os"
"path/filepath"

"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/ansible"
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"

"github.com/spf13/cobra"
)

// NewMigrateCmd returns a command that will add source code to an existing non-go operator
func NewMigrateCmd() *cobra.Command {
return &cobra.Command{
Use: "migrate",
Short: "Adds source code to an operator",
Long: `operator-sdk migrate adds a main.go source file and any associated source files for an operator that is not of the "go" type.`,
Run: migrateRun,
}
}

// migrateRun determines the current operator type and runs the corresponding
// migrate function.
func migrateRun(cmd *cobra.Command, args []string) {
projutil.MustInProjectRoot()

_ = projutil.CheckAndGetProjectGoPkg()

opType := projutil.GetOperatorType()
switch opType {
case projutil.OperatorTypeAnsible:
migrateAnsible()
default:
log.Fatalf("operator of type %s cannot be migrated.", opType)
}
}

// migrateAnsible runs the migration process for an ansible-based operator
func migrateAnsible() {
wd := projutil.MustGetwd()

cfg := &input.Config{
AbsProjectPath: wd,
ProjectName: filepath.Base(wd),
}

dockerfile := ansible.DockerfileHybrid{
Watches: true,
Roles: true,
}
_, err := os.Stat("playbook.yaml")
Copy link
Member

Choose a reason for hiding this comment

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

Wonder if this should be a flag or at least alert the user that we are assuming no playbook if this is not found. I could see cases, where playbooks are in a new directory and users, have changed things. Telling the users what we are doing, and what they can do to change it back if that is incorrect might be a good idea. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah a good example of this would be the etcd operator which puts all of it's ansible code under a folder ansible.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good. If they put it in a different location that what we expect, they'll have to manually add it to their new Dockerfile. Presumably they'd catch that in comparing their old Dockerfile to new, but it's worth calling out explicitly.

switch {
case err == nil:
dockerfile.Playbook = true
case os.IsNotExist(err):
log.Print("No playbook was found, so not including it in the new Dockerfile")
default:
log.Fatalf("error trying to stat playbook.yaml: (%v)", err)
}

dockerfilePath := filepath.Join(scaffold.BuildDir, scaffold.DockerfileFile)
newDockerfilePath := dockerfilePath + ".sdkold"
err = os.Rename(dockerfilePath, newDockerfilePath)
if err != nil {
log.Fatalf("failed to rename Dockerfile: (%v)", err)
}
log.Printf("renamed Dockerfile to %s and replaced with newer version", newDockerfilePath)
log.Print("Compare the new Dockerfile to your old one and manually migrate any customizations")

s := &scaffold.Scaffold{}
err = s.Execute(cfg,
&ansible.Main{},
&ansible.GopkgToml{},
&dockerfile,
&ansible.Entrypoint{},
&ansible.UserSetup{},
)
if err != nil {
log.Fatalf("add scaffold failed: (%v)", err)
}
}
2 changes: 2 additions & 0 deletions commands/operator-sdk/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func NewRootCmd() *cobra.Command {
cmd.AddCommand(NewCompletionCmd())
cmd.AddCommand(NewTestCmd())
cmd.AddCommand(NewPrintDepsCmd())
cmd.AddCommand(NewMigrateCmd())
cmd.AddCommand(NewRunCmd())

return cmd
}
33 changes: 33 additions & 0 deletions commands/operator-sdk/cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2019 The Operator-SDK Authors
//
// 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
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/run"

"github.com/spf13/cobra"
)

// NewRunCmd returns a command that contains subcommands to run specific
// operator types.
func NewRunCmd() *cobra.Command {
runCmd := &cobra.Command{
Use: "run",
Short: "Runs a generic operator",
}

runCmd.AddCommand(run.NewAnsibleCmd())
return runCmd
}
38 changes: 38 additions & 0 deletions commands/operator-sdk/cmd/run/ansible.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2019 The Operator-SDK Authors
//
// 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
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package run

import (
"github.com/operator-framework/operator-sdk/pkg/ansible"
aoflags "github.com/operator-framework/operator-sdk/pkg/ansible/flags"

"github.com/spf13/cobra"
)

var flags *aoflags.AnsibleOperatorFlags

// NewAnsibleCmd returns a command that will run an ansible operator
func NewAnsibleCmd() *cobra.Command {
newCmd := &cobra.Command{
Use: "ansible",
Short: "Runs as an ansible operator",
Run: func(cmd *cobra.Command, args []string) {
ansible.Run(flags)
},
}
flags = aoflags.AddTo(newCmd.Flags())

return newCmd
}
42 changes: 7 additions & 35 deletions commands/operator-sdk/cmd/up/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/runtime/signals"

"github.com/operator-framework/operator-sdk/internal/util/projutil"
"github.com/operator-framework/operator-sdk/pkg/ansible"
"github.com/operator-framework/operator-sdk/pkg/ansible/flags"
ansibleOperator "github.com/operator-framework/operator-sdk/pkg/ansible/operator"
proxy "github.com/operator-framework/operator-sdk/pkg/ansible/proxy"
"github.com/operator-framework/operator-sdk/pkg/helm/client"
"github.com/operator-framework/operator-sdk/pkg/helm/controller"
"github.com/operator-framework/operator-sdk/pkg/helm/release"
Expand Down Expand Up @@ -154,41 +153,14 @@ func upLocalAnsible() {
if err := os.Setenv(k8sutil.KubeConfigEnvVar, kubeConfig); err != nil {
log.Fatalf("failed to set %s environment variable: (%v)", k8sutil.KubeConfigEnvVar, err)
}

logf.SetLogger(logf.ZapLogger(false))

mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{Namespace: namespace})
if err != nil {
log.Fatal(err)
}

printVersion()
log.Infof("watching namespace: %s", namespace)
done := make(chan error)
cMap := proxy.NewControllerMap()

// start the proxy
err = proxy.Run(done, proxy.Options{
Address: "localhost",
Port: 8888,
KubeConfig: mgr.GetConfig(),
Cache: mgr.GetCache(),
RESTMapper: mgr.GetRESTMapper(),
ControllerMap: cMap,
})
if err != nil {
log.Fatalf("error starting proxy: (%v)", err)
// Set the kubeconfig that the manager will be able to grab
if namespace != "" {
if err := os.Setenv(k8sutil.WatchNamespaceEnvVar, namespace); err != nil {
log.Fatalf("failed to set %s environment variable: (%v)", k8sutil.WatchNamespaceEnvVar, err)
}
}

// start the operator
go ansibleOperator.Run(done, mgr, ansibleOperatorFlags, cMap)

// wait for either to finish
err = <-done
if err != nil {
log.Fatal(err)
}
log.Info("Exiting.")
ansible.Run(ansibleOperatorFlags)
}

func upLocalHelm() {
Expand Down
22 changes: 13 additions & 9 deletions doc/dev/testing/travis-build.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,17 @@ The Go, Ansible, and Helm tests then differ in what tests they run.
### Ansible tests

1. Run [ansible e2e tests][ansible-e2e].
1. Build base ansible operator image from [`test/ansible-operator`][ansible-base].
2. Create and configure a new ansible type memcached-operator.
3. Create cluster resources.
4. Wait for operator to be ready.
5. Create a memcached CR and wait for it to be ready.
6. Create a configmap that the memcached-operator is configured to delete using a finalizer.
7. Delete memcached CR and verify that the finalizer deleted the configmap.
1. Create base ansible operator image by running [`hack/image/scaffold-ansible-image.go`][ansible-base].
2. Build base ansible operator image.
3. Create and configure a new ansible type memcached-operator.
4. Create cluster resources.
5. Wait for operator to be ready.
6. Create a memcached CR and wait for it to be ready.
7. Create a configmap that the memcached-operator is configured to delete using a finalizer.
8. Delete memcached CR and verify that the finalizer deleted the configmap.
9. Run `operator-sdk migrate` to add go source to the operator.
10. Run `operator-sdk build` to compile the new binary and build a new image.
11. Re-run steps 4-8 to test the migrated operator.

**NOTE**: All created resources, including the namespace, are deleted using a bash trap when the test finishes

Expand Down Expand Up @@ -106,8 +110,8 @@ The markdown test does not create a new cluster and runs in a barebones travis V
[go-e2e]: ../../../hack/tests/e2e-go.sh
[tls-tests]: ../../../test/e2e/tls_util_test.go
[ansible-e2e]: ../../../hack/tests/e2e-ansible.sh
[ansible-base]: ../../../test/ansible-operator
[ansible-base]: ../../../hack/image/scaffold-ansible-image.go
[helm-e2e]: ../../../hack/tests/e2e-helm.sh
[helm-base]: ../../../test/helm-operator
[marker-github]: https://github.com/crawford/marker
[marker-local]: ../../../hack/ci/marker
[marker-local]: ../../../hack/ci/marker
17 changes: 14 additions & 3 deletions hack/image/build-ansible-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@

set -eux

source hack/lib/test_lib.sh

ROOTDIR="$(pwd)"
GOTMP="$(mktemp -d -p $GOPATH/src)"
trap_add 'rm -rf $GOTMP' EXIT
BASEIMAGEDIR="$GOTMP/ansible-operator"
mkdir -p "$BASEIMAGEDIR"

# build operator binary and base image
go build -o test/ansible-operator/ansible-operator test/ansible-operator/cmd/ansible-operator/main.go
pushd test/ansible-operator
docker build -t "$1" .
pushd "$BASEIMAGEDIR"
go run "$ROOTDIR/hack/image/scaffold-ansible-image.go"

mkdir -p build/_output/bin/
cp $(which operator-sdk) build/_output/bin/ansible-operator
operator-sdk build $1
popd
45 changes: 45 additions & 0 deletions hack/image/scaffold-ansible-image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2019 The Operator-SDK Authors
//
// 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
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

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/ansible"
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
)

// main renders scaffolds that are required to build the ansible operator base
// image. It is intended for release engineering use only. After running this,
// you can place a binary in `build/_output/bin/ansible-operator` and then run
// `operator-sdk build`.
func main() {
cfg := &input.Config{
AbsProjectPath: projutil.MustGetwd(),
ProjectName: "ansible-operator",
}

s := &scaffold.Scaffold{}
err := s.Execute(cfg,
&ansible.DockerfileHybrid{},
&ansible.Entrypoint{},
&ansible.UserSetup{},
)
if err != nil {
log.Fatalf("add scaffold failed: (%v)", err)
}
}
Loading