-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add experimental OLMv1 support behind envvar toggle
Signed-off-by: Joe Lanford <joe.lanford@gmail.com>
- Loading branch information
1 parent
3df32d1
commit c9430db
Showing
8 changed files
with
236 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package olmv1 | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/operator-framework/kubectl-operator/internal/cmd/internal/log" | ||
experimentalaction "github.com/operator-framework/kubectl-operator/internal/pkg/experimental/action" | ||
"github.com/operator-framework/kubectl-operator/pkg/action" | ||
) | ||
|
||
func NewOperatorInstallCmd(cfg *action.Configuration) *cobra.Command { | ||
i := experimentalaction.NewOperatorInstall(cfg) | ||
i.Logf = log.Printf | ||
|
||
cmd := &cobra.Command{ | ||
Use: "install <operator>", | ||
Short: "Install an operator", | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
i.Package = args[0] | ||
_, err := i.Run(cmd.Context()) | ||
if err != nil { | ||
log.Fatalf("failed to install operator: %v", err) | ||
} | ||
log.Printf("operator %q created", i.Package) | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package olmv1 | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/operator-framework/kubectl-operator/internal/cmd/internal/log" | ||
experimentalaction "github.com/operator-framework/kubectl-operator/internal/pkg/experimental/action" | ||
"github.com/operator-framework/kubectl-operator/pkg/action" | ||
) | ||
|
||
func NewOperatorUninstallCmd(cfg *action.Configuration) *cobra.Command { | ||
u := experimentalaction.NewOperatorUninstall(cfg) | ||
u.Logf = log.Printf | ||
|
||
cmd := &cobra.Command{ | ||
Use: "uninstall <operator>", | ||
Short: "Uninstall an operator", | ||
Long: `Uninstall deletes the named Operator object. | ||
Warning: this command permanently deletes objects from the cluster. If the | ||
uninstalled Operator bundle contains CRDs, the CRDs will be deleted, which | ||
cascades to the deletion of all operands. | ||
`, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
u.Package = args[0] | ||
if err := u.Run(cmd.Context()); err != nil { | ||
log.Fatalf("uninstall operator: %v", err) | ||
} | ||
log.Printf("deleted operator %q", u.Package) | ||
}, | ||
} | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package action | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
olmv1 "github.com/operator-framework/operator-controller/api/v1alpha1" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
|
||
"github.com/operator-framework/kubectl-operator/pkg/action" | ||
) | ||
|
||
type OperatorInstall struct { | ||
config *action.Configuration | ||
|
||
Package string | ||
|
||
Logf func(string, ...interface{}) | ||
} | ||
|
||
func NewOperatorInstall(cfg *action.Configuration) *OperatorInstall { | ||
return &OperatorInstall{ | ||
config: cfg, | ||
Logf: func(string, ...interface{}) {}, | ||
} | ||
} | ||
|
||
func (i *OperatorInstall) Run(ctx context.Context) (*olmv1.Operator, error) { | ||
|
||
// TODO(developer): Lookup package information when the OLMv1 equivalent of the | ||
// packagemanifests API is available. That way, we can check to see if the | ||
// package is actually available to the cluster before creating the Operator | ||
// object. | ||
|
||
opKey := types.NamespacedName{Name: i.Package} | ||
op := &olmv1.Operator{ | ||
ObjectMeta: metav1.ObjectMeta{Name: opKey.Name}, | ||
Spec: olmv1.OperatorSpec{PackageName: i.Package}, | ||
} | ||
if err := i.config.Client.Create(ctx, op); err != nil { | ||
return nil, err | ||
} | ||
|
||
// TODO(developer): Improve the logic in this poll wait once the Operator reconciler | ||
// and conditions types and reasons are improved. For now, this will stop waiting as | ||
// soon as a Ready condition is found, but we should probably wait until the Operator | ||
// stops progressing. | ||
|
||
if err := wait.PollImmediateUntil(time.Millisecond*250, func() (bool, error) { | ||
if err := i.config.Client.Get(ctx, opKey, op); err != nil { | ||
return false, err | ||
} | ||
readyCondition := meta.FindStatusCondition(op.Status.Conditions, olmv1.TypeReady) | ||
if readyCondition == nil { | ||
return false, nil | ||
} | ||
return true, nil | ||
}, ctx.Done()); err != nil { | ||
return nil, fmt.Errorf("waiting for operator to become ready: %v", err) | ||
} | ||
|
||
return op, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package action | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
olmv1 "github.com/operator-framework/operator-controller/api/v1alpha1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/operator-framework/kubectl-operator/pkg/action" | ||
) | ||
|
||
type OperatorUninstall struct { | ||
config *action.Configuration | ||
|
||
Package string | ||
|
||
Logf func(string, ...interface{}) | ||
} | ||
|
||
func NewOperatorUninstall(cfg *action.Configuration) *OperatorUninstall { | ||
return &OperatorUninstall{ | ||
config: cfg, | ||
Logf: func(string, ...interface{}) {}, | ||
} | ||
} | ||
|
||
func (u *OperatorUninstall) Run(ctx context.Context) error { | ||
opKey := types.NamespacedName{Name: u.Package} | ||
op := &olmv1.Operator{} | ||
op.SetName(opKey.Name) | ||
op.SetGroupVersionKind(olmv1.GroupVersion.WithKind("Operator")) | ||
|
||
lowerKind := strings.ToLower(op.GetObjectKind().GroupVersionKind().Kind) | ||
if err := u.config.Client.Delete(ctx, op); err != nil && !apierrors.IsNotFound(err) { | ||
return fmt.Errorf("delete %s %q: %v", lowerKind, op.GetName(), err) | ||
} | ||
return waitForDeletion(ctx, u.config.Client, op) | ||
} | ||
|
||
func objectKeyForObject(obj client.Object) types.NamespacedName { | ||
return types.NamespacedName{ | ||
Namespace: obj.GetNamespace(), | ||
Name: obj.GetName(), | ||
} | ||
} | ||
|
||
func waitForDeletion(ctx context.Context, cl client.Client, objs ...client.Object) error { | ||
for _, obj := range objs { | ||
obj := obj | ||
lowerKind := strings.ToLower(obj.GetObjectKind().GroupVersionKind().Kind) | ||
key := objectKeyForObject(obj) | ||
if err := wait.PollImmediateUntil(250*time.Millisecond, func() (bool, error) { | ||
if err := cl.Get(ctx, key, obj); apierrors.IsNotFound(err) { | ||
return true, nil | ||
} else if err != nil { | ||
return false, err | ||
} | ||
return false, nil | ||
}, ctx.Done()); err != nil { | ||
return fmt.Errorf("wait for %s %q deleted: %v", lowerKind, key.Name, err) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters