Skip to content

Commit b987def

Browse files
Code for extension install and delete
Signed-off-by: Lalatendu Mohanty <[email protected]>
1 parent b496b10 commit b987def

File tree

7 files changed

+211
-89
lines changed

7 files changed

+211
-89
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ gen-demo:
6060
lint: $(GOLANGCI_LINT)
6161
$(GOLANGCI_LINT) --timeout 3m run
6262

63+
.PHONY: lint-fix
64+
lint-fix: $(GOLANGCI_LINT) #HELP Run golangci linter.
65+
$(GOLANGCI_LINT) run --fix --timeout 3m
66+
6367
.PHONY: release
6468
RELEASE_ARGS?=release --clean --snapshot
6569
release: $(GORELEASER)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package olmv1
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"github.com/spf13/pflag"
6+
7+
"github.com/operator-framework/kubectl-operator/internal/cmd/internal/log"
8+
v1action "github.com/operator-framework/kubectl-operator/internal/pkg/v1/action"
9+
"github.com/operator-framework/kubectl-operator/pkg/action"
10+
)
11+
12+
func NewExtensionDeleteCmd(cfg *action.Configuration) *cobra.Command {
13+
e := v1action.NewExtensionDelete(cfg)
14+
e.Logf = log.Printf
15+
16+
cmd := &cobra.Command{
17+
Use: "extension <extension-name>",
18+
Short: "Delete an extension",
19+
Long: `Warning: Permanently deletes the named cluster extension object.
20+
If the extension contains CRDs, the CRDs will be deleted, which
21+
cascades to the deletion of all operands.`,
22+
Args: cobra.ExactArgs(1),
23+
Run: func(cmd *cobra.Command, args []string) {
24+
e.ExtensionName = args[0]
25+
if err := e.Run(cmd.Context()); err != nil {
26+
log.Fatalf("delete extension: %v", err)
27+
}
28+
log.Printf("deleted extension %q", e.ExtensionName)
29+
},
30+
}
31+
bindExtensionDeleteFlags(cmd.Flags(), e)
32+
return cmd
33+
}
34+
35+
func bindExtensionDeleteFlags(fs *pflag.FlagSet, e *v1action.ExtensionDeletion) {
36+
fs.BoolVarP(&e.DeleteAll, "all", "a", false, "delete all extensions")
37+
}

internal/cmd/internal/olmv1/extension_install.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package olmv1
22

33
import (
4+
"time"
5+
46
"github.com/spf13/cobra"
7+
"github.com/spf13/pflag"
58

69
"github.com/operator-framework/kubectl-operator/internal/cmd/internal/log"
710
v1action "github.com/operator-framework/kubectl-operator/internal/pkg/v1/action"
@@ -13,18 +16,31 @@ func NewExtensionInstallCmd(cfg *action.Configuration) *cobra.Command {
1316
i.Logf = log.Printf
1417

1518
cmd := &cobra.Command{
16-
Use: "install <extension>",
19+
Use: "extension <extension_name>",
1720
Short: "Install an extension",
1821
Args: cobra.ExactArgs(1),
1922
Run: func(cmd *cobra.Command, args []string) {
20-
i.Package = args[0]
23+
i.ExtensionName = args[0]
2124
_, err := i.Run(cmd.Context())
2225
if err != nil {
2326
log.Fatalf("failed to install extension: %v", err)
2427
}
25-
log.Printf("extension %q created", i.Package)
28+
log.Printf("extension %q created", i.ExtensionName)
2629
},
2730
}
31+
bindOperatorInstallFlags(cmd.Flags(), i)
2832

2933
return cmd
3034
}
35+
36+
func bindOperatorInstallFlags(fs *pflag.FlagSet, i *v1action.ExtensionInstall) {
37+
fs.StringVarP(&i.Namespace.Name, "namespace", "n", "", "namespace to install the operator in")
38+
fs.ArgsLenAtDash()
39+
fs.StringVarP(&i.PackageName, "package-name", "p", "", "package name of the operator to install")
40+
fs.StringSliceVarP(&i.Channels, "channels", "c", []string{}, "channels which would be to used for getting updates")
41+
fs.StringVarP(&i.Version, "version", "v", "", "version (or version range) from which to resolve bundles")
42+
fs.StringVarP(&i.ServiceAccount, "service-account", "s", "default", "service account name to use for the extension installation")
43+
fs.StringToStringVarP(&i.CatalogSelector.MatchLabels, "labels", "l", map[string]string{}, "labels that will be used to select catalog")
44+
fs.BoolVarP(&i.UnsafeCreateClusterRoleBinding, "unsafe-create-cluster-role-binding", "u", false, "create a cluster role binding for the operator's service account")
45+
fs.DurationVarP(&i.CleanupTimeout, "cleanup-timeout", "d", time.Minute, "the amount of time to wait before cancelling cleanup after a failed creation attempt")
46+
}

internal/cmd/internal/olmv1/extension_uninstall.go

Lines changed: 0 additions & 34 deletions
This file was deleted.

internal/cmd/olmv1.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ func newOlmV1Cmd(cfg *action.Configuration) *cobra.Command {
3636
Short: "Delete a resource",
3737
Long: "Delete a resource",
3838
}
39-
deleteCmd.AddCommand(olmv1.NewCatalogDeleteCmd(cfg))
39+
deleteCmd.AddCommand(
40+
olmv1.NewCatalogDeleteCmd(cfg),
41+
olmv1.NewExtensionDeleteCmd(cfg),
42+
)
4043

4144
updateCmd := &cobra.Command{
4245
Use: "update",
@@ -47,9 +50,15 @@ func newOlmV1Cmd(cfg *action.Configuration) *cobra.Command {
4750
olmv1.NewExtensionUpdateCmd(cfg),
4851
)
4952

53+
installCmd := &cobra.Command{
54+
Use: "install",
55+
Short: "Install a resource",
56+
Long: "Install a resource",
57+
}
58+
installCmd.AddCommand(olmv1.NewExtensionInstallCmd(cfg))
59+
5060
cmd.AddCommand(
51-
olmv1.NewExtensionInstallCmd(cfg),
52-
olmv1.NewExtensionUninstallCmd(cfg),
61+
installCmd,
5362
getCmd,
5463
createCmd,
5564
deleteCmd,

internal/pkg/v1/action/extension_uninstall.go renamed to internal/pkg/v1/action/extension_delete.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,27 @@ import (
1313
"github.com/operator-framework/kubectl-operator/pkg/action"
1414
)
1515

16-
type ExtensionUninstall struct {
17-
config *action.Configuration
18-
19-
Package string
20-
21-
Logf func(string, ...interface{})
16+
type ExtensionDeletion struct {
17+
config *action.Configuration
18+
ExtensionName string
19+
DeleteAll bool
20+
Logf func(string, ...interface{})
2221
}
2322

24-
func NewExtensionUninstall(cfg *action.Configuration) *ExtensionUninstall {
25-
return &ExtensionUninstall{
23+
func NewExtensionDelete(cfg *action.Configuration) *ExtensionDeletion {
24+
return &ExtensionDeletion{
2625
config: cfg,
2726
Logf: func(string, ...interface{}) {},
2827
}
2928
}
3029

31-
func (u *ExtensionUninstall) Run(ctx context.Context) error {
32-
opKey := types.NamespacedName{Name: u.Package}
30+
func (u *ExtensionDeletion) Run(ctx context.Context) error {
31+
opKey := types.NamespacedName{Name: u.ExtensionName}
3332
op := &olmv1.ClusterExtension{}
3433
op.SetName(opKey.Name)
35-
op.SetGroupVersionKind(olmv1.GroupVersion.WithKind("Extension"))
36-
34+
op.SetGroupVersionKind(olmv1.GroupVersion.WithKind("ClusterExtension"))
3735
lowerKind := strings.ToLower(op.GetObjectKind().GroupVersionKind().Kind)
36+
//Lala:Fixme: return error if extension does not exist
3837
if err := u.config.Client.Delete(ctx, op); err != nil && !apierrors.IsNotFound(err) {
3938
return fmt.Errorf("delete %s %q: %v", lowerKind, op.GetName(), err)
4039
}

0 commit comments

Comments
 (0)