Skip to content

adds migrate command and ansible base image scaffolds #823

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

Closed
wants to merge 2 commits into from
Closed
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
46 changes: 46 additions & 0 deletions commands/ansible-operator-base/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2018 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 `dep ensure` and then `operator-sdk build`.
func main() {
cfg := &input.Config{
AbsProjectPath: projutil.MustGetwd(),
ProjectName: "ansible-operator",
}

s := &scaffold.Scaffold{}
err := s.Execute(cfg,
&ansible.Main{},
&ansible.GopkgToml{},
&ansible.DockerfileHybrid{},
&ansible.Entrypoint{},
&ansible.UserSetup{},
)
if err != nil {
log.Fatalf("add scaffold failed: (%v)", err)
}
}
92 changes: 92 additions & 0 deletions commands/operator-sdk/cmd/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2018 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"
)

func NewMigrateCmd() *cobra.Command {
return &cobra.Command{
Use: "migrate",
Short: "Adds source code for 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,
}
}

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

func migrateAnsible() {
wd, err := os.Getwd()
if err != nil {
log.Fatalf("could not identify current working directory: (%v)", err)
}

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

dockerfile := ansible.DockerfileHybrid{
Watches: true,
Roles: true,
}
_, err = os.Stat("playbook.yaml")
if err == nil {
dockerfile.Playbook = true
} else if !os.IsNotExist(err) {
log.Fatalf("error trying to stat playbook.yaml: (%v)", err)
}

dockerfilePath := filepath.Join(scaffold.BuildDir, scaffold.DockerfileFile)
err = os.Rename(dockerfilePath, dockerfilePath+".sdkold")
if err != nil {
log.Fatalf("failed to rename Dockerfile: (%v)", err)
}

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)
}
}
1 change: 1 addition & 0 deletions commands/operator-sdk/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func NewRootCmd() *cobra.Command {
cmd.AddCommand(NewCompletionCmd())
cmd.AddCommand(NewTestCmd())
cmd.AddCommand(NewPrintDepsCmd())
cmd.AddCommand(NewMigrateCmd())

return cmd
}
19 changes: 10 additions & 9 deletions doc/dev/testing/travis-build.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ 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 source by running [`commands/ansible-operator-base/main.go`][ansible-base].
2. Build base ansible operator binary and image from source.
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.

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

Expand Down Expand Up @@ -106,8 +107,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]: ../../../commands/ansible-operator-base/main.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
22 changes: 19 additions & 3 deletions hack/image/build-ansible-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,24 @@

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/commands/ansible-operator-base/main.go"
dep ensure

# overwrite operator-sdk source with the latest source from the local checkout
pushd vendor/github.com/operator-framework/
rm -Rf operator-sdk/*
cp -a "$ROOTDIR"/{pkg,version,LICENSE} operator-sdk/
popd

operator-sdk build $1
popd
Loading