Skip to content

Commit

Permalink
Refactor operator into plugins
Browse files Browse the repository at this point in the history
With better breaking of dependencies
  • Loading branch information
Oleg Sucharevich authored Feb 17, 2019
1 parent 07b87d5 commit b16f719
Show file tree
Hide file tree
Showing 19 changed files with 718 additions and 817 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "venona",
"version": "0.16.1",
"version": "0.17.0",
"description": "Codefresh agent to run on Codefresh's runtime environment and execute pipeline",
"main": "index.js",
"scripts": {
Expand Down
24 changes: 22 additions & 2 deletions venonactl/cmd/cmdutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
"github.com/codefresh-io/go-sdk/pkg/codefresh"
sdkUtils "github.com/codefresh-io/go-sdk/pkg/utils"
"github.com/codefresh-io/venona/venonactl/pkg/certs"
runtimectl "github.com/codefresh-io/venona/venonactl/pkg/operators"
"github.com/codefresh-io/venona/venonactl/pkg/plugins"
"github.com/codefresh-io/venona/venonactl/pkg/store"
"github.com/olekukonko/tablewriter"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -148,5 +149,24 @@ func isUsingDefaultStorageClass(sc string) bool {
if sc == "" {
return true
}
return strings.HasPrefix(sc, runtimectl.DefaultStorageClassNamePrefix)
return strings.HasPrefix(sc, plugins.DefaultStorageClassNamePrefix)
}

func dieOnError(err error) {
if err != nil {
logrus.Error(err)
os.Exit(1)
}
}

func createTable() *tablewriter.Table {
table := tablewriter.NewWriter(os.Stdout)
table.SetBorder(false)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetRowLine(false)
table.SetHeaderLine(false)
table.SetColumnSeparator(" ")
table.SetColWidth(100)
return table
}
54 changes: 18 additions & 36 deletions venonactl/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ limitations under the License.

import (
"errors"
"fmt"
"os"

"github.com/codefresh-io/venona/venonactl/pkg/store"
"github.com/sirupsen/logrus"

runtimectl "github.com/codefresh-io/venona/venonactl/pkg/operators"
"github.com/codefresh-io/venona/venonactl/pkg/plugins"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -61,15 +60,17 @@ var deleteCmd = &cobra.Command{
var errors []DeletionError
s.KubernetesAPI.InCluster = deleteCmdOptions.kube.inCluster
for _, name := range args {
builder := plugins.NewBuilder()

re, err := s.CodefreshAPI.Client.RuntimeEnvironments().Get(name)
errors = collectError(errors, err, name, "Get Runtime-Environment from Codefresh")
errors = collectError(errors, err, name)

if deleteCmdOptions.revertTo != "" {
_, err := s.CodefreshAPI.Client.RuntimeEnvironments().Default(deleteCmdOptions.revertTo)
errors = collectError(errors, err, name, fmt.Sprintf("Revert Runtime-Environment to: %s", deleteCmdOptions.revertTo))
errors = collectError(errors, err, name)
}
deleted, err := s.CodefreshAPI.Client.RuntimeEnvironments().Delete(name)
errors = collectError(errors, err, name, "Delete Runtime-Environment from Codefresh")
errors = collectError(errors, err, name)

if deleted {
contextName := re.RuntimeScheduler.Cluster.ClusterProvider.Selector
Expand All @@ -78,37 +79,19 @@ var deleteCmd = &cobra.Command{
}
s.KubernetesAPI.ContextName = contextName
s.KubernetesAPI.Namespace = re.RuntimeScheduler.Cluster.Namespace
err = runtimectl.GetOperator(runtimectl.RuntimeEnvironmentOperatorType).Delete()
if err != nil {
errors = append(errors, DeletionError{
err: err,
name: name,
operation: "Delete Runtime-Environment Kubernetes resoruces",
})
continue
}

builder.Add(plugins.RuntimeEnvironmentPluginType)
if isUsingDefaultStorageClass(re.RuntimeScheduler.Pvcs.Dind.StorageClassName) {
err = runtimectl.GetOperator(runtimectl.VolumeProvisionerOperatorType).Delete()
if err != nil {
errors = append(errors, DeletionError{
err: err,
name: name,
operation: "Delete volume provisioner related components",
})
continue
}
builder.Add(plugins.VolumeProvisionerPluginType)
}

if re.Metadata.Agent {
err = runtimectl.GetOperator(runtimectl.VenonaOperatorType).Delete()
if err != nil {
errors = append(errors, DeletionError{
err: err,
name: name,
operation: "Delete Venona's agent Kubernetes resoruces",
})
continue
}
builder.Add(plugins.VenonaPluginType)
}

for _, p := range builder.Get() {
err := p.Delete(nil)
collectError(errors, err, name)
}

logrus.Infof("Deleted %s", name)
Expand All @@ -135,13 +118,12 @@ func init() {
deleteCmd.Flags().BoolVar(&deleteCmdOptions.kube.inCluster, "in-cluster", false, "Set flag if venona is been installed from inside a cluster")
}

func collectError(errors []DeletionError, err error, reName string, op string) []DeletionError {
func collectError(errors []DeletionError, err error, reName string) []DeletionError {
if err == nil {
return errors
}
return append(errors, DeletionError{
err: err,
name: reName,
operation: op,
err: err,
name: reName,
})
}
90 changes: 32 additions & 58 deletions venonactl/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ import (

"github.com/codefresh-io/venona/venonactl/pkg/store"

"github.com/codefresh-io/venona/venonactl/internal"

"github.com/codefresh-io/venona/venonactl/pkg/codefresh"
runtimectl "github.com/codefresh-io/venona/venonactl/pkg/operators"
"github.com/codefresh-io/venona/venonactl/pkg/plugins"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -61,6 +58,16 @@ var installCmd = &cobra.Command{
extendStoreWithCodefershClient()
extendStoreWithKubeClient()

builder := plugins.NewBuilder()
builderInstallOpt := &plugins.InstallOptions{
CodefreshHost: s.CodefreshAPI.Host,
CodefreshToken: s.CodefreshAPI.Token,
ClusterNamespace: s.KubernetesAPI.Namespace,
MarkAsDefault: installCmdOptions.setDefaultRuntime,
StorageClass: installCmdOptions.storageClass,
IsDefaultStorageClass: isUsingDefaultStorageClass(installCmdOptions.storageClass),
}

if installCmdOptions.kube.context == "" {
config := clientcmd.GetConfigFromFileOrDie(s.KubernetesAPI.ConfigPath)
installCmdOptions.kube.context = config.CurrentContext
Expand Down Expand Up @@ -91,27 +98,40 @@ var installCmd = &cobra.Command{
}
s.ClusterInCodefresh = installCmdOptions.clusterNameInCodefresh
if installCmdOptions.installOnlyRuntimeEnvironment == true && installCmdOptions.skipRuntimeInstallation == true {
internal.DieOnError(fmt.Errorf("Cannot use both flags skip-runtime-installation and only-runtime-environment"))
dieOnError(fmt.Errorf("Cannot use both flags skip-runtime-installation and only-runtime-environment"))
}
if installCmdOptions.installOnlyRuntimeEnvironment == true {
registerRuntimeEnvironment()
return
builder.Add(plugins.RuntimeEnvironmentPluginType)
} else if installCmdOptions.skipRuntimeInstallation == true {
if installCmdOptions.runtimeEnvironmentName == "" {
internal.DieOnError(fmt.Errorf("runtime-environment flag is required when using flag skip-runtime-installation"))
dieOnError(fmt.Errorf("runtime-environment flag is required when using flag skip-runtime-installation"))
}
s.RuntimeEnvironment = installCmdOptions.runtimeEnvironmentName
logrus.Info("Skipping installation of runtime environment, installing venona only")
installvenona()
builder.Add(plugins.VenonaPluginType)
} else {
registerRuntimeEnvironment()
installvenona()
builder.
Add(plugins.RuntimeEnvironmentPluginType).
Add(plugins.VenonaPluginType)
}
if isUsingDefaultStorageClass(installCmdOptions.storageClass) {
configureVolumeProvisioner()
builder.Add(plugins.VolumeProvisionerPluginType)
} else {
logrus.Info("Non default StorageClass is set, skipping installation of volume provisioner")
}

builderInstallOpt.ClusterName = s.KubernetesAPI.ContextName
builderInstallOpt.RegisterWithAgent = true
if s.ClusterInCodefresh != "" {
builderInstallOpt.ClusterName = s.ClusterInCodefresh
builderInstallOpt.RegisterWithAgent = false
}

for _, p := range builder.Get() {
if err := p.Install(builderInstallOpt); err != nil {
dieOnError(err)
}
}
logrus.Info("Installation completed Successfully\n")
},
}
Expand All @@ -135,49 +155,3 @@ func init() {
installCmd.Flags().BoolVar(&installCmdOptions.dryRun, "dry-run", false, "Set to true to simulate installation")
installCmd.Flags().BoolVar(&installCmdOptions.setDefaultRuntime, "set-default", false, "Mark the install runtime-environment as default one after installation")
}

func registerRuntimeEnvironment() {
s := store.GetStore()
registerWithAgent := true
name := s.KubernetesAPI.ContextName
if s.ClusterInCodefresh != "" {
registerWithAgent = false
name = s.ClusterInCodefresh
}
opt := &codefresh.APIOptions{
Logger: logrus.New(),
CodefreshHost: s.CodefreshAPI.Host,
CodefreshToken: s.CodefreshAPI.Token,
ClusterName: name,
ClusterNamespace: s.KubernetesAPI.Namespace,
RegisterWithAgent: registerWithAgent,
MarkAsDefault: installCmdOptions.setDefaultRuntime,
StorageClass: installCmdOptions.storageClass,
IsDefaultStorageClass: isUsingDefaultStorageClass(installCmdOptions.storageClass),
}
cf := codefresh.NewCodefreshAPI(opt)

cert, err := cf.Sign()
internal.DieOnError(err)
err = cf.Validate()
internal.DieOnError(err)

err = runtimectl.GetOperator(runtimectl.RuntimeEnvironmentOperatorType).Install()
internal.DieOnError(err)

re, err := cf.Register()
internal.DieOnError(err)

s.RuntimeEnvironment = re.Metadata.Name
s.ServerCert = cert
}

func installvenona() {
err := runtimectl.GetOperator(runtimectl.VenonaOperatorType).Install()
internal.DieOnError(err)
}

func configureVolumeProvisioner() {
err := runtimectl.GetOperator(runtimectl.VolumeProvisionerOperatorType).Install()
internal.DieOnError(err)
}
4 changes: 1 addition & 3 deletions venonactl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ limitations under the License.
import (
"github.com/spf13/viper"

"github.com/codefresh-io/venona/venonactl/internal"

"github.com/spf13/cobra"
)

Expand All @@ -32,7 +30,7 @@ var rootCmd = &cobra.Command{
// Execute - execute the root command
func Execute() {
err := rootCmd.Execute()
internal.DieOnError(err)
dieOnError(err)
}

func init() {
Expand Down
32 changes: 18 additions & 14 deletions venonactl/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import (
"github.com/sirupsen/logrus"

"github.com/codefresh-io/go-sdk/pkg/codefresh"
"github.com/codefresh-io/venona/venonactl/internal"
runtimectl "github.com/codefresh-io/venona/venonactl/pkg/operators"
"github.com/codefresh-io/venona/venonactl/pkg/plugins"
"github.com/codefresh-io/venona/venonactl/pkg/store"
humanize "github.com/dustin/go-humanize"

Expand All @@ -49,14 +48,14 @@ var statusCmd = &cobra.Command{
extendStoreWithKubeClient()

s := store.GetStore()
table := internal.CreateTable()
table := createTable()
// When requested status for specific runtime
if len(args) > 0 {
name := args[0]
re, err := s.CodefreshAPI.Client.RuntimeEnvironments().Get(name)
internal.DieOnError(err)
dieOnError(err)
if re == nil {
internal.DieOnError(fmt.Errorf("Runtime-Environment %s not found", name))
dieOnError(fmt.Errorf("Runtime-Environment %s not found", name))
}
if re.Metadata.Agent == true {
table.SetHeader([]string{"Runtime Name", "Last Message", "Reported"})
Expand All @@ -78,7 +77,7 @@ var statusCmd = &cobra.Command{

// When requested status for all runtimes
res, err := s.CodefreshAPI.Client.RuntimeEnvironments().List()
internal.DieOnError(err)
dieOnError(err)
table.SetHeader([]string{"Runtime Name", "Last Message", "Reported"})
for _, re := range res {
if re.Metadata.Agent == true {
Expand All @@ -99,7 +98,9 @@ var statusCmd = &cobra.Command{
}

func printTableWithKubernetesRelatedResources(re *codefresh.RuntimeEnvironment, context string) {
table := internal.CreateTable()
builder := plugins.NewBuilder()

table := createTable()
table.SetHeader([]string{"Kind", "Name", "Status"})
s := store.GetStore()
if re.RuntimeScheduler.Cluster.Namespace != "" {
Expand All @@ -108,13 +109,16 @@ func printTableWithKubernetesRelatedResources(re *codefresh.RuntimeEnvironment,
}
s.KubernetesAPI.ContextName = context
s.KubernetesAPI.Namespace = re.RuntimeScheduler.Cluster.Namespace

rows, err := runtimectl.GetOperator(runtimectl.RuntimeEnvironmentOperatorType).Status()
internal.DieOnError(err)
table.AppendBulk(rows)
rows, err = runtimectl.GetOperator(runtimectl.VenonaOperatorType).Status()
internal.DieOnError(err)
table.AppendBulk(rows)
builder.
Add(plugins.RuntimeEnvironmentPluginType).
Add(plugins.VenonaPluginType).
Add(plugins.VolumeProvisionerPluginType)

for _, p := range builder.Get() {
rows, err := p.Status(nil)
dieOnError(err)
table.AppendBulk(rows)
}
}
table.Render()
}
Expand Down
Loading

0 comments on commit b16f719

Please sign in to comment.