Skip to content

Commit b16f719

Browse files
author
Oleg Sucharevich
authored
Refactor operator into plugins
With better breaking of dependencies
1 parent 07b87d5 commit b16f719

19 files changed

+718
-817
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "venona",
3-
"version": "0.16.1",
3+
"version": "0.17.0",
44
"description": "Codefresh agent to run on Codefresh's runtime environment and execute pipeline",
55
"main": "index.js",
66
"scripts": {

venonactl/cmd/cmdutils.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import (
1010
"github.com/codefresh-io/go-sdk/pkg/codefresh"
1111
sdkUtils "github.com/codefresh-io/go-sdk/pkg/utils"
1212
"github.com/codefresh-io/venona/venonactl/pkg/certs"
13-
runtimectl "github.com/codefresh-io/venona/venonactl/pkg/operators"
13+
"github.com/codefresh-io/venona/venonactl/pkg/plugins"
1414
"github.com/codefresh-io/venona/venonactl/pkg/store"
15+
"github.com/olekukonko/tablewriter"
1516
"github.com/sirupsen/logrus"
1617
)
1718

@@ -148,5 +149,24 @@ func isUsingDefaultStorageClass(sc string) bool {
148149
if sc == "" {
149150
return true
150151
}
151-
return strings.HasPrefix(sc, runtimectl.DefaultStorageClassNamePrefix)
152+
return strings.HasPrefix(sc, plugins.DefaultStorageClassNamePrefix)
153+
}
154+
155+
func dieOnError(err error) {
156+
if err != nil {
157+
logrus.Error(err)
158+
os.Exit(1)
159+
}
160+
}
161+
162+
func createTable() *tablewriter.Table {
163+
table := tablewriter.NewWriter(os.Stdout)
164+
table.SetBorder(false)
165+
table.SetAlignment(tablewriter.ALIGN_LEFT)
166+
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
167+
table.SetRowLine(false)
168+
table.SetHeaderLine(false)
169+
table.SetColumnSeparator(" ")
170+
table.SetColWidth(100)
171+
return table
152172
}

venonactl/cmd/delete.go

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ limitations under the License.
1818

1919
import (
2020
"errors"
21-
"fmt"
2221
"os"
2322

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

27-
runtimectl "github.com/codefresh-io/venona/venonactl/pkg/operators"
26+
"github.com/codefresh-io/venona/venonactl/pkg/plugins"
2827
"github.com/spf13/cobra"
2928
)
3029

@@ -61,15 +60,17 @@ var deleteCmd = &cobra.Command{
6160
var errors []DeletionError
6261
s.KubernetesAPI.InCluster = deleteCmdOptions.kube.inCluster
6362
for _, name := range args {
63+
builder := plugins.NewBuilder()
64+
6465
re, err := s.CodefreshAPI.Client.RuntimeEnvironments().Get(name)
65-
errors = collectError(errors, err, name, "Get Runtime-Environment from Codefresh")
66+
errors = collectError(errors, err, name)
6667

6768
if deleteCmdOptions.revertTo != "" {
6869
_, err := s.CodefreshAPI.Client.RuntimeEnvironments().Default(deleteCmdOptions.revertTo)
69-
errors = collectError(errors, err, name, fmt.Sprintf("Revert Runtime-Environment to: %s", deleteCmdOptions.revertTo))
70+
errors = collectError(errors, err, name)
7071
}
7172
deleted, err := s.CodefreshAPI.Client.RuntimeEnvironments().Delete(name)
72-
errors = collectError(errors, err, name, "Delete Runtime-Environment from Codefresh")
73+
errors = collectError(errors, err, name)
7374

7475
if deleted {
7576
contextName := re.RuntimeScheduler.Cluster.ClusterProvider.Selector
@@ -78,37 +79,19 @@ var deleteCmd = &cobra.Command{
7879
}
7980
s.KubernetesAPI.ContextName = contextName
8081
s.KubernetesAPI.Namespace = re.RuntimeScheduler.Cluster.Namespace
81-
err = runtimectl.GetOperator(runtimectl.RuntimeEnvironmentOperatorType).Delete()
82-
if err != nil {
83-
errors = append(errors, DeletionError{
84-
err: err,
85-
name: name,
86-
operation: "Delete Runtime-Environment Kubernetes resoruces",
87-
})
88-
continue
89-
}
82+
83+
builder.Add(plugins.RuntimeEnvironmentPluginType)
9084
if isUsingDefaultStorageClass(re.RuntimeScheduler.Pvcs.Dind.StorageClassName) {
91-
err = runtimectl.GetOperator(runtimectl.VolumeProvisionerOperatorType).Delete()
92-
if err != nil {
93-
errors = append(errors, DeletionError{
94-
err: err,
95-
name: name,
96-
operation: "Delete volume provisioner related components",
97-
})
98-
continue
99-
}
85+
builder.Add(plugins.VolumeProvisionerPluginType)
10086
}
10187

10288
if re.Metadata.Agent {
103-
err = runtimectl.GetOperator(runtimectl.VenonaOperatorType).Delete()
104-
if err != nil {
105-
errors = append(errors, DeletionError{
106-
err: err,
107-
name: name,
108-
operation: "Delete Venona's agent Kubernetes resoruces",
109-
})
110-
continue
111-
}
89+
builder.Add(plugins.VenonaPluginType)
90+
}
91+
92+
for _, p := range builder.Get() {
93+
err := p.Delete(nil)
94+
collectError(errors, err, name)
11295
}
11396

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

138-
func collectError(errors []DeletionError, err error, reName string, op string) []DeletionError {
121+
func collectError(errors []DeletionError, err error, reName string) []DeletionError {
139122
if err == nil {
140123
return errors
141124
}
142125
return append(errors, DeletionError{
143-
err: err,
144-
name: reName,
145-
operation: op,
126+
err: err,
127+
name: reName,
146128
})
147129
}

venonactl/cmd/install.go

Lines changed: 32 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ import (
2424

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

27-
"github.com/codefresh-io/venona/venonactl/internal"
28-
29-
"github.com/codefresh-io/venona/venonactl/pkg/codefresh"
30-
runtimectl "github.com/codefresh-io/venona/venonactl/pkg/operators"
27+
"github.com/codefresh-io/venona/venonactl/pkg/plugins"
3128
"github.com/spf13/cobra"
3229
"github.com/spf13/viper"
3330
)
@@ -61,6 +58,16 @@ var installCmd = &cobra.Command{
6158
extendStoreWithCodefershClient()
6259
extendStoreWithKubeClient()
6360

61+
builder := plugins.NewBuilder()
62+
builderInstallOpt := &plugins.InstallOptions{
63+
CodefreshHost: s.CodefreshAPI.Host,
64+
CodefreshToken: s.CodefreshAPI.Token,
65+
ClusterNamespace: s.KubernetesAPI.Namespace,
66+
MarkAsDefault: installCmdOptions.setDefaultRuntime,
67+
StorageClass: installCmdOptions.storageClass,
68+
IsDefaultStorageClass: isUsingDefaultStorageClass(installCmdOptions.storageClass),
69+
}
70+
6471
if installCmdOptions.kube.context == "" {
6572
config := clientcmd.GetConfigFromFileOrDie(s.KubernetesAPI.ConfigPath)
6673
installCmdOptions.kube.context = config.CurrentContext
@@ -91,27 +98,40 @@ var installCmd = &cobra.Command{
9198
}
9299
s.ClusterInCodefresh = installCmdOptions.clusterNameInCodefresh
93100
if installCmdOptions.installOnlyRuntimeEnvironment == true && installCmdOptions.skipRuntimeInstallation == true {
94-
internal.DieOnError(fmt.Errorf("Cannot use both flags skip-runtime-installation and only-runtime-environment"))
101+
dieOnError(fmt.Errorf("Cannot use both flags skip-runtime-installation and only-runtime-environment"))
95102
}
96103
if installCmdOptions.installOnlyRuntimeEnvironment == true {
97-
registerRuntimeEnvironment()
98-
return
104+
builder.Add(plugins.RuntimeEnvironmentPluginType)
99105
} else if installCmdOptions.skipRuntimeInstallation == true {
100106
if installCmdOptions.runtimeEnvironmentName == "" {
101-
internal.DieOnError(fmt.Errorf("runtime-environment flag is required when using flag skip-runtime-installation"))
107+
dieOnError(fmt.Errorf("runtime-environment flag is required when using flag skip-runtime-installation"))
102108
}
103109
s.RuntimeEnvironment = installCmdOptions.runtimeEnvironmentName
104110
logrus.Info("Skipping installation of runtime environment, installing venona only")
105-
installvenona()
111+
builder.Add(plugins.VenonaPluginType)
106112
} else {
107-
registerRuntimeEnvironment()
108-
installvenona()
113+
builder.
114+
Add(plugins.RuntimeEnvironmentPluginType).
115+
Add(plugins.VenonaPluginType)
109116
}
110117
if isUsingDefaultStorageClass(installCmdOptions.storageClass) {
111-
configureVolumeProvisioner()
118+
builder.Add(plugins.VolumeProvisionerPluginType)
112119
} else {
113120
logrus.Info("Non default StorageClass is set, skipping installation of volume provisioner")
114121
}
122+
123+
builderInstallOpt.ClusterName = s.KubernetesAPI.ContextName
124+
builderInstallOpt.RegisterWithAgent = true
125+
if s.ClusterInCodefresh != "" {
126+
builderInstallOpt.ClusterName = s.ClusterInCodefresh
127+
builderInstallOpt.RegisterWithAgent = false
128+
}
129+
130+
for _, p := range builder.Get() {
131+
if err := p.Install(builderInstallOpt); err != nil {
132+
dieOnError(err)
133+
}
134+
}
115135
logrus.Info("Installation completed Successfully\n")
116136
},
117137
}
@@ -135,49 +155,3 @@ func init() {
135155
installCmd.Flags().BoolVar(&installCmdOptions.dryRun, "dry-run", false, "Set to true to simulate installation")
136156
installCmd.Flags().BoolVar(&installCmdOptions.setDefaultRuntime, "set-default", false, "Mark the install runtime-environment as default one after installation")
137157
}
138-
139-
func registerRuntimeEnvironment() {
140-
s := store.GetStore()
141-
registerWithAgent := true
142-
name := s.KubernetesAPI.ContextName
143-
if s.ClusterInCodefresh != "" {
144-
registerWithAgent = false
145-
name = s.ClusterInCodefresh
146-
}
147-
opt := &codefresh.APIOptions{
148-
Logger: logrus.New(),
149-
CodefreshHost: s.CodefreshAPI.Host,
150-
CodefreshToken: s.CodefreshAPI.Token,
151-
ClusterName: name,
152-
ClusterNamespace: s.KubernetesAPI.Namespace,
153-
RegisterWithAgent: registerWithAgent,
154-
MarkAsDefault: installCmdOptions.setDefaultRuntime,
155-
StorageClass: installCmdOptions.storageClass,
156-
IsDefaultStorageClass: isUsingDefaultStorageClass(installCmdOptions.storageClass),
157-
}
158-
cf := codefresh.NewCodefreshAPI(opt)
159-
160-
cert, err := cf.Sign()
161-
internal.DieOnError(err)
162-
err = cf.Validate()
163-
internal.DieOnError(err)
164-
165-
err = runtimectl.GetOperator(runtimectl.RuntimeEnvironmentOperatorType).Install()
166-
internal.DieOnError(err)
167-
168-
re, err := cf.Register()
169-
internal.DieOnError(err)
170-
171-
s.RuntimeEnvironment = re.Metadata.Name
172-
s.ServerCert = cert
173-
}
174-
175-
func installvenona() {
176-
err := runtimectl.GetOperator(runtimectl.VenonaOperatorType).Install()
177-
internal.DieOnError(err)
178-
}
179-
180-
func configureVolumeProvisioner() {
181-
err := runtimectl.GetOperator(runtimectl.VolumeProvisionerOperatorType).Install()
182-
internal.DieOnError(err)
183-
}

venonactl/cmd/root.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ limitations under the License.
1919
import (
2020
"github.com/spf13/viper"
2121

22-
"github.com/codefresh-io/venona/venonactl/internal"
23-
2422
"github.com/spf13/cobra"
2523
)
2624

@@ -32,7 +30,7 @@ var rootCmd = &cobra.Command{
3230
// Execute - execute the root command
3331
func Execute() {
3432
err := rootCmd.Execute()
35-
internal.DieOnError(err)
33+
dieOnError(err)
3634
}
3735

3836
func init() {

venonactl/cmd/status.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ import (
2222
"github.com/sirupsen/logrus"
2323

2424
"github.com/codefresh-io/go-sdk/pkg/codefresh"
25-
"github.com/codefresh-io/venona/venonactl/internal"
26-
runtimectl "github.com/codefresh-io/venona/venonactl/pkg/operators"
25+
"github.com/codefresh-io/venona/venonactl/pkg/plugins"
2726
"github.com/codefresh-io/venona/venonactl/pkg/store"
2827
humanize "github.com/dustin/go-humanize"
2928

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

5150
s := store.GetStore()
52-
table := internal.CreateTable()
51+
table := createTable()
5352
// When requested status for specific runtime
5453
if len(args) > 0 {
5554
name := args[0]
5655
re, err := s.CodefreshAPI.Client.RuntimeEnvironments().Get(name)
57-
internal.DieOnError(err)
56+
dieOnError(err)
5857
if re == nil {
59-
internal.DieOnError(fmt.Errorf("Runtime-Environment %s not found", name))
58+
dieOnError(fmt.Errorf("Runtime-Environment %s not found", name))
6059
}
6160
if re.Metadata.Agent == true {
6261
table.SetHeader([]string{"Runtime Name", "Last Message", "Reported"})
@@ -78,7 +77,7 @@ var statusCmd = &cobra.Command{
7877

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

101100
func printTableWithKubernetesRelatedResources(re *codefresh.RuntimeEnvironment, context string) {
102-
table := internal.CreateTable()
101+
builder := plugins.NewBuilder()
102+
103+
table := createTable()
103104
table.SetHeader([]string{"Kind", "Name", "Status"})
104105
s := store.GetStore()
105106
if re.RuntimeScheduler.Cluster.Namespace != "" {
@@ -108,13 +109,16 @@ func printTableWithKubernetesRelatedResources(re *codefresh.RuntimeEnvironment,
108109
}
109110
s.KubernetesAPI.ContextName = context
110111
s.KubernetesAPI.Namespace = re.RuntimeScheduler.Cluster.Namespace
111-
112-
rows, err := runtimectl.GetOperator(runtimectl.RuntimeEnvironmentOperatorType).Status()
113-
internal.DieOnError(err)
114-
table.AppendBulk(rows)
115-
rows, err = runtimectl.GetOperator(runtimectl.VenonaOperatorType).Status()
116-
internal.DieOnError(err)
117-
table.AppendBulk(rows)
112+
builder.
113+
Add(plugins.RuntimeEnvironmentPluginType).
114+
Add(plugins.VenonaPluginType).
115+
Add(plugins.VolumeProvisionerPluginType)
116+
117+
for _, p := range builder.Get() {
118+
rows, err := p.Status(nil)
119+
dieOnError(err)
120+
table.AppendBulk(rows)
121+
}
118122
}
119123
table.Render()
120124
}

0 commit comments

Comments
 (0)