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

pkg/destroy/libvirt: Add behind a libvirt_destroy build tag #387

Merged
merged 1 commit 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
3 changes: 2 additions & 1 deletion cmd/openshift-install/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/openshift/installer/pkg/asset"
"github.com/openshift/installer/pkg/asset/stock"
"github.com/openshift/installer/pkg/destroy"
_ "github.com/openshift/installer/pkg/destroy/libvirt"
wking marked this conversation as resolved.
Show resolved Hide resolved
)

var (
Expand Down Expand Up @@ -77,7 +78,7 @@ func main() {
}
}
case destroyCommand.FullCommand():
destroyer, err := destroy.NewDestroyer(l, *dirFlag)
destroyer, err := destroy.New(l, *dirFlag)
if err != nil {
log.Fatalf("failed to create destroyer: %v", err)
os.Exit(1)
Expand Down
4 changes: 4 additions & 0 deletions docs/dev/dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ The following dependencies must be installed on your system before you can build
sudo dnf install golang-bin gcc-c++
```

If you need support for [libvirt destroy](libvirt-howto#cleanup), you should also install `libvirt-devel`.

### CentOS, RHEL

```sh
sudo yum install golang-bin gcc-c++
```

If you need support for [libvirt destroy](libvirt-howto#cleanup), you should also install `libvirt-devel`.

## Go

We follow a hard flattening approach; i.e. direct and inherited dependencies are installed in the base `vendor/`.
Expand Down
17 changes: 14 additions & 3 deletions docs/dev/libvirt-howto.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ EOF
## Build and run the installer

With [libvirt configured](#install-and-enable-libvirt), you can proceed with [the usual quick-start](../../README.md#quick-start).
Set `TAGS` when building if you need `destroy-cluster` support for libvirt; this is not enabled by default because it requires [cgo][]:

```sh
TAGS=libvirt_destroy hack/build.sh
```

To avoid being prompted repeatedly, you can set [environment variables](../user/environment-variables.md) to reflect your libvirt choices. For example, selecting libvirt, setting [our earlier name choices](#pick-names), [our pull secret](#get-a-pull-secret), and telling both the installer and the machine-API operator to contact `libvirtd` at [the usual libvirt IP](#firewall), you can use:

```sh
Expand All @@ -196,9 +202,13 @@ export OPENSHIFT_INSTALL_LIBVIRT_URI=qemu+tcp://192.168.122.1/system

## Cleanup

Be sure to destroy your cluster when you're done with it, or else you will need to manually use `virsh` to clean up the leaked resources.
The [`virsh-cleanup`](../../scripts/maintenance/virsh-cleanup.sh) script may help with this, but note it will currently destroy *all* libvirt resources.
We plan on adding `openshift-install destroy-cluster` support for libvirt in the near future.
If you compiled with `libvirt_destroy`, you can use:

```sh
openshift-install destroy-cluster
```

If you did not compile with `libvirt_destroy`, you can use [`virsh-cleanup.sh`](../../scripts/maintenance/virsh-cleanup.sh), but note it will currently destroy *all* libvirt resources.

### Firewall

Expand Down Expand Up @@ -328,6 +338,7 @@ If your issue is not reported, please do.
[arch_firewall_superuser]: https://superuser.com/questions/1063240/libvirt-failed-to-initialize-a-valid-firewall-backend
[brokenmacosissue201]: https://github.com/openshift/installer/issues/201
[bugzilla_libvirt_race]: https://bugzilla.redhat.com/show_bug.cgi?id=1576464
[cgo]: https://golang.org/cmd/cgo/
[issues_libvirt]: https://github.com/openshift/installer/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+libvirt
[libvirt_selinux_issues]: https://github.com/dmacvicar/terraform-provider-libvirt/issues/142#issuecomment-409040151
[tfprovider_libvirt_race]: https://github.com/dmacvicar/terraform-provider-libvirt/issues/402#issuecomment-419500064
12 changes: 9 additions & 3 deletions hack/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ set -ex
cd "$(dirname "$0")/.."

MODE="${MODE:-release}"
TAGS=
TAGS="${TAGS:-}"
export CGO_ENABLED=0

case "${MODE}" in
release)
TAGS=release
TAGS="${TAGS} release"
GOPATH="${PWD}/vendor:${GOPATH}" go generate ./data
;;
dev)
Expand All @@ -19,4 +20,9 @@ dev)
exit 1
esac

CGO_ENABLED=0 go build -tags "${TAGS}" -o ./bin/openshift-install ./cmd/openshift-install
if (echo "${TAGS}" | grep -q 'libvirt_destroy')
then
export CGO_ENABLED=1
fi

go build -tags "${TAGS}" -o ./bin/openshift-install ./cmd/openshift-install
30 changes: 30 additions & 0 deletions pkg/destroy/aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package destroy

import (
"os"

atd "github.com/openshift/hive/contrib/pkg/awstagdeprovision"
"github.com/openshift/installer/pkg/types"
log "github.com/sirupsen/logrus"
)

// NewAWS returns an AWS destroyer from ClusterMetadata.
func NewAWS(level log.Level, metadata *types.ClusterMetadata) (Destroyer, error) {
return &atd.ClusterUninstaller{
Filters: metadata.ClusterPlatformMetadata.AWS.Identifier,
Region: metadata.ClusterPlatformMetadata.AWS.Region,
ClusterName: metadata.ClusterName,
Logger: log.NewEntry(&log.Logger{
Out: os.Stdout,
Formatter: &log.TextFormatter{
FullTimestamp: true,
},
Hooks: make(log.LevelHooks),
Level: level,
}),
}, nil
}

func init() {
Registry["aws"] = NewAWS
}
63 changes: 17 additions & 46 deletions pkg/destroy/destroyer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"

atd "github.com/openshift/hive/contrib/pkg/awstagdeprovision"
log "github.com/sirupsen/logrus"

"github.com/openshift/installer/pkg/asset/metadata"
Expand All @@ -20,60 +18,33 @@ type Destroyer interface {
Run() error
}

// NewDestroyer returns Destroyer based on `metadata.json` in `rootDir`.
func NewDestroyer(level log.Level, rootDir string) (Destroyer, error) {
// NewFunc is an interface for creating platform-specific destroyers.
type NewFunc func(level log.Level, metadata *types.ClusterMetadata) (Destroyer, error)

// Registry maps ClusterMetadata.Platform() to per-platform Destroyer creators.
var Registry = make(map[string]NewFunc)

// New returns a Destroyer based on `metadata.json` in `rootDir`.
func New(level log.Level, rootDir string) (Destroyer, error) {
path := filepath.Join(rootDir, metadata.MetadataFilename)
raw, err := ioutil.ReadFile(filepath.Join(rootDir, metadata.MetadataFilename))
if err != nil {
return nil, err
}

var cmetadata types.ClusterMetadata
var cmetadata *types.ClusterMetadata
if err := json.Unmarshal(raw, &cmetadata); err != nil {
return nil, err
}

var ret Destroyer
switch {
case cmetadata.ClusterPlatformMetadata.AWS != nil:
ret = NewAWSDestroyer(level, &cmetadata)
case cmetadata.ClusterPlatformMetadata.Libvirt != nil:
// ret = NewLibvirtDestroyer(level, &cmetadata)
return nil, fmt.Errorf("libvirt destroyer is not yet supported")
default:
return nil, fmt.Errorf("couldn't find Destroyer for %q", metadata.MetadataFilename)
platform := cmetadata.Platform()
if platform == "" {
return nil, fmt.Errorf("no platform configured in %q", path)
}
return ret, nil
}

// // NewLibvirtDestroyer returns libvirt Uninstaller from ClusterMetadata.
// func NewLibvirtDestroyer(level log.Level, metadata *types.ClusterMetadata) *lpd.ClusterUninstaller {
// return &lpd.ClusterUninstaller{
// LibvirtURI: metadata.ClusterPlatformMetadata.Libvirt.URI,
// Filter: lpd.AlwaysTrueFilter(), //TODO: change to ClusterNamePrefixFilter when all resources are prefixed.
// Logger: log.NewEntry(&log.Logger{
// Out: os.Stdout,
// Formatter: &log.TextFormatter{
// FullTimestamp: true,
// },
// Hooks: make(log.LevelHooks),
// Level: level,
// }),
// }
// }

// NewAWSDestroyer returns aws Uninstaller from ClusterMetadata.
func NewAWSDestroyer(level log.Level, metadata *types.ClusterMetadata) *atd.ClusterUninstaller {
return &atd.ClusterUninstaller{
Filters: metadata.ClusterPlatformMetadata.AWS.Identifier,
Region: metadata.ClusterPlatformMetadata.AWS.Region,
ClusterName: metadata.ClusterName,
Logger: log.NewEntry(&log.Logger{
Out: os.Stdout,
Formatter: &log.TextFormatter{
FullTimestamp: true,
},
Hooks: make(log.LevelHooks),
Level: level,
}),
creator, ok := Registry[platform]
if !ok {
return nil, fmt.Errorf("no destroyers registered for %q", platform)
}
return creator(level, cmetadata)
}
2 changes: 2 additions & 0 deletions pkg/destroy/libvirt/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package libvirt provides a cluster-destroyer for libvirt clusters.
package libvirt
20 changes: 20 additions & 0 deletions pkg/destroy/libvirt/libvirt_prefix_deprovision.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build libvirt_destroy

package libvirt

import (
Expand All @@ -6,6 +8,8 @@ import (
"time"

libvirt "github.com/libvirt/libvirt-go"
"github.com/openshift/installer/pkg/destroy"
"github.com/openshift/installer/pkg/types"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/wait"
)
Expand Down Expand Up @@ -239,3 +243,19 @@ func deleteNetwork(conn *libvirt.Connect, filter filterFunc, logger log.FieldLog
}
return true, nil
}

// New returns libvirt Uninstaller from ClusterMetadata.
func New(level log.Level, metadata *types.ClusterMetadata) (destroy.Destroyer, error) {
return &ClusterUninstaller{
LibvirtURI: metadata.ClusterPlatformMetadata.Libvirt.URI,
Filter: AlwaysTrueFilter(), //TODO: change to ClusterNamePrefixFilter when all resources are prefixed.
Logger: log.NewEntry(&log.Logger{
Out: os.Stdout,
Formatter: &log.TextFormatter{
FullTimestamp: true,
},
Hooks: make(log.LevelHooks),
Level: level,
}),
}, nil
}
11 changes: 11 additions & 0 deletions pkg/destroy/libvirt/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// +build libvirt_destroy

package libvirt

import (
"github.com/openshift/installer/pkg/destroy"
)

func init() {
destroy.Registry["libvirt"] = New
}
16 changes: 16 additions & 0 deletions pkg/types/clustermetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ type ClusterPlatformMetadata struct {
Libvirt *ClusterLibvirtPlatformMetadata `json:"libvirt,omitempty"`
}

// Platform returns a string representation of the platform
// (e.g. "aws" if AWS is non-nil). It returns an empty string if no
// platform is configured.
func (cpm *ClusterPlatformMetadata) Platform() string {
if cpm == nil {
return ""
}
if cpm.AWS != nil {
return "aws"
}
if cpm.Libvirt != nil {
return "libvirt"
}
return ""
}

// ClusterAWSPlatformMetadata contains AWS metadata.
type ClusterAWSPlatformMetadata struct {
Region string `json:"region"`
Expand Down