Skip to content

Commit

Permalink
cmd,pkg/destroy: add non-terraform destroy
Browse files Browse the repository at this point in the history
libvirt destroyer impl is working but has been commented out because:
`bazel build tarball` cannot build with `github.com/libvirt-go/libvirt` as one of the
dependencies, due to some missing cgo dependencies. `go build cmd/openshift-install` works
perfectly fine.
revisit add libvirt destroyer when bazel is dropped.
  • Loading branch information
abhinavdahiya committed Sep 25, 2018
1 parent 78c3118 commit 998ba30
Show file tree
Hide file tree
Showing 7 changed files with 386 additions and 15 deletions.
3 changes: 2 additions & 1 deletion cmd/openshift-install/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ go_library(
deps = [
"//pkg/asset:go_default_library",
"//pkg/asset/stock:go_default_library",
"//vendor/github.com/Sirupsen/logrus:go_default_library",
"//pkg/destroy:go_default_library",
"//vendor/github.com/sirupsen/logrus:go_default_library",
"//vendor/gopkg.in/alecthomas/kingpin.v2:go_default_library",
],
)
Expand Down
49 changes: 35 additions & 14 deletions cmd/openshift-install/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package main
import (
"os"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"gopkg.in/alecthomas/kingpin.v2"

"github.com/openshift/installer/pkg/asset"
"github.com/openshift/installer/pkg/asset/stock"
"github.com/openshift/installer/pkg/destroy"
)

var (
Expand All @@ -16,12 +17,20 @@ var (
manifestsCommand = kingpin.Command("manifests", "Generate the Kubernetes manifests")
clusterCommand = kingpin.Command("cluster", "Create an OpenShift cluster")

destroyCommand = kingpin.Command("destroy-cluster", "Destroy an OpenShift cluster")

dirFlag = kingpin.Flag("dir", "assets directory").Default(".").String()
logLevel = kingpin.Flag("log-level", "log level (e.g. \"debug\")").Default("warn").Enum("debug", "info", "warn", "error", "fatal", "panic")
)

func main() {
command := kingpin.Parse()
l, err := log.ParseLevel(*logLevel)
if err != nil {
// By definition we should never enter this condition since kingpin should be guarding against incorrect values.
log.Fatalf("invalid log-level: %v", err)
}
log.SetLevel(l)

assetStock := stock.EstablishStock()

Expand All @@ -45,27 +54,39 @@ func main() {
assetStock.TFVars(),
assetStock.KubeconfigAdmin(),
assetStock.Cluster(),
assetStock.Metadata(),
}
}

l, err := log.ParseLevel(*logLevel)
if err != nil {
// By definition we should never enter this condition since kingpin should be guarding against incorrect values.
log.Fatalf("invalid log-level: %v", err)
}
log.SetLevel(l)
switch command {
case installConfigCommand.FullCommand(),
ignitionConfigsCommand.FullCommand(),
manifestsCommand.FullCommand(),
clusterCommand.FullCommand():
assetStore := &asset.StoreImpl{}
for _, asset := range targetAssets {
st, err := assetStore.Fetch(asset)
if err != nil {
log.Fatalf("failed to generate asset: %v", err)
os.Exit(1)
}

assetStore := &asset.StoreImpl{}
for _, asset := range targetAssets {
st, err := assetStore.Fetch(asset)
if err := st.PersistToFile(*dirFlag); err != nil {
log.Fatalf("failed to write target to disk: %v", err)
os.Exit(1)
}
}
case destroyCommand.FullCommand():
destroyer, err := destroy.NewDestroyer(l, *dirFlag)
if err != nil {
log.Fatalf("failed to generate asset: %v", err)
log.Fatalf("failed to create destroyer: %v", err)
os.Exit(1)
}

if err := st.PersistToFile(*dirFlag); err != nil {
log.Fatalf("failed to write target to disk: %v", err)
if err := destroyer.Run(); err != nil {
log.Fatalf("destroy failed: %v", err)
os.Exit(1)
}

}

}
14 changes: 14 additions & 0 deletions pkg/destroy/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["destroyer.go"],
importpath = "github.com/openshift/installer/pkg/destroy",
visibility = ["//visibility:public"],
deps = [
"//pkg/asset/metadata:go_default_library",
"//pkg/types:go_default_library",
"//vendor/github.com/openshift/hive/contrib/pkg/aws_tag_deprovision:go_default_library",
"//vendor/github.com/sirupsen/logrus:go_default_library",
],
)
79 changes: 79 additions & 0 deletions pkg/destroy/destroyer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package destroy

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"

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

"github.com/openshift/installer/pkg/asset/metadata"
"github.com/openshift/installer/pkg/types"
)

// Destroyer allows multiple implementations of destroy
// for different platforms.
type Destroyer interface {
Run() error
}

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

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)
}
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,
}),
}
}
2 changes: 2 additions & 0 deletions pkg/destroy/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package destroy contains tools for destroying clusters based on their metadata.
package destroy
13 changes: 13 additions & 0 deletions pkg/destroy/libvirt/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["libvirt_prefix_deprovision.go"],
importpath = "github.com/openshift/installer/pkg/destroy/libvirt",
visibility = ["//visibility:public"],
deps = [
"//vendor/github.com/libvirt/libvirt-go:go_default_library",
"//vendor/github.com/sirupsen/logrus:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
],
)
Loading

0 comments on commit 998ba30

Please sign in to comment.