Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/destroy/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/openshift/hypershift/cmd/bastion"
"github.com/openshift/hypershift/cmd/cluster"
"github.com/openshift/hypershift/cmd/infra"
"github.com/openshift/hypershift/cmd/oadp"

"github.com/spf13/cobra"
)
Expand All @@ -19,6 +20,9 @@ func NewCommand() *cobra.Command {
destroyCmd.AddCommand(infra.NewDestroyCommand())
destroyCmd.AddCommand(infra.NewDestroyIAMCommand())
destroyCmd.AddCommand(bastion.NewDestroyCommand())
destroyCmd.AddCommand(oadp.NewDestroyBackupCommand())
destroyCmd.AddCommand(oadp.NewDestroyRestoreCommand())
destroyCmd.AddCommand(oadp.NewDestroyScheduleCommand())

return destroyCmd
}
21 changes: 21 additions & 0 deletions cmd/get/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package get

import (
"github.com/openshift/hypershift/cmd/oadp"

"github.com/spf13/cobra"
)

func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "get",
Short: "Commands for listing HyperShift resources",
SilenceUsage: true,
}

cmd.AddCommand(oadp.NewGetBackupsCommand())
cmd.AddCommand(oadp.NewGetRestoresCommand())
cmd.AddCommand(oadp.NewGetSchedulesCommand())

return cmd
}
5 changes: 3 additions & 2 deletions cmd/oadp/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (o *CreateOptions) RunBackup(ctx context.Context) error {
if o.Client != nil {
// Step 1: Validate HostedCluster exists and get platform
o.Log.Info("Validating HostedCluster...")
detectedPlatform, err := oadp.ValidateAndGetHostedClusterPlatform(ctx, o.Client, o.HCName, o.HCNamespace)
platformInfo, err := oadp.ValidateAndGetHostedClusterPlatformInfo(ctx, o.Client, o.HCName, o.HCNamespace)
if err != nil {
if o.Render {
o.Log.Info("Warning: HostedCluster validation failed, using default platform (AWS)", "error", err.Error())
Expand All @@ -125,7 +125,8 @@ func (o *CreateOptions) RunBackup(ctx context.Context) error {
return fmt.Errorf("HostedCluster validation failed: %w", err)
}
} else {
platform = detectedPlatform
platform = platformInfo.Type
autoIncludeAgentNamespace(o, platformInfo)
}

if !o.Render {
Expand Down
16 changes: 16 additions & 0 deletions cmd/oadp/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"encoding/hex"
"fmt"
"os"
"slices"
"strings"

"github.com/openshift/hypershift/api/hypershift/v1beta1"
"github.com/openshift/hypershift/support/netutil"
"github.com/openshift/hypershift/support/oadp"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -265,3 +267,17 @@ func buildIncludedNamespaces(hcNamespace, hcName string, additionalNamespaces []

return namespaces
}

// autoIncludeAgentNamespace appends the Agent platform's agentNamespace to the
// IncludeNamespaces list when the platform is AGENT, deduplicating if the user
// already specified it explicitly.
func autoIncludeAgentNamespace(o *CreateOptions, info *oadp.PlatformInfo) {
if info.Type != "AGENT" || info.AgentNamespace == "" {
return
}
if slices.Contains(o.IncludeNamespaces, info.AgentNamespace) {
return
}
o.IncludeNamespaces = append(o.IncludeNamespaces, info.AgentNamespace)
o.Log.Info("Auto-detected agent namespace, including in operation", "namespace", info.AgentNamespace)
}
115 changes: 115 additions & 0 deletions cmd/oadp/destroy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package oadp

import (
"context"
"fmt"

"github.com/openshift/hypershift/cmd/log"
"github.com/openshift/hypershift/cmd/util"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/go-logr/logr"
"github.com/spf13/cobra"
)

// DestroyOptions holds common configuration for destroy commands.
type DestroyOptions struct {
Name string
OADPNamespace string
Log logr.Logger
Client client.Client
}

func NewDestroyBackupCommand() *cobra.Command {
opts := &DestroyOptions{Log: log.Log}
cmd := &cobra.Command{
Use: "oadp-backup",
Short: "Delete an OADP backup",
Long: `Delete a Velero backup resource from the OADP namespace.
Examples:
# Delete a specific backup
hypershift destroy oadp-backup --name example-clusters-lkbtzw`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
return opts.runDestroy(cmd.Context(), "Backup")
},
}
addDestroyFlags(cmd, opts)
_ = cmd.MarkFlagRequired("name")
return cmd
}

func NewDestroyRestoreCommand() *cobra.Command {
opts := &DestroyOptions{Log: log.Log}
cmd := &cobra.Command{
Use: "oadp-restore",
Short: "Delete an OADP restore",
Long: `Delete a Velero restore resource from the OADP namespace.
Examples:
# Delete a specific restore
hypershift destroy oadp-restore --name restore-example-clusters-abc123`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
return opts.runDestroy(cmd.Context(), "Restore")
},
}
addDestroyFlags(cmd, opts)
_ = cmd.MarkFlagRequired("name")
return cmd
}

func NewDestroyScheduleCommand() *cobra.Command {
opts := &DestroyOptions{Log: log.Log}
cmd := &cobra.Command{
Use: "oadp-schedule",
Short: "Delete an OADP schedule",
Long: `Delete a Velero schedule resource from the OADP namespace.
Examples:
# Delete a specific schedule
hypershift destroy oadp-schedule --name example-clusters-daily`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
return opts.runDestroy(cmd.Context(), "Schedule")
},
}
addDestroyFlags(cmd, opts)
_ = cmd.MarkFlagRequired("name")
return cmd
}

func addDestroyFlags(cmd *cobra.Command, opts *DestroyOptions) {
cmd.Flags().StringVar(&opts.Name, "name", "", "Name of the resource to delete (required)")
cmd.Flags().StringVar(&opts.OADPNamespace, "oadp-namespace", "openshift-adp", "Namespace where OADP operator is installed")
}

func (o *DestroyOptions) runDestroy(ctx context.Context, kind string) error {
if o.Client == nil {
var err error
o.Client, err = util.GetClient()
if err != nil {
return fmt.Errorf("failed to create kubernetes client: %w", err)
}
}

obj := &unstructured.Unstructured{}
obj.SetAPIVersion("velero.io/v1")
obj.SetKind(kind)

key := client.ObjectKey{Name: o.Name, Namespace: o.OADPNamespace}
if err := o.Client.Get(ctx, key, obj); err != nil {
return fmt.Errorf("%s '%s' not found in namespace '%s': %w", kind, o.Name, o.OADPNamespace, err)
}

if err := o.Client.Delete(ctx, obj); err != nil {
return fmt.Errorf("failed to delete %s '%s': %w", kind, o.Name, err)
}

o.Log.Info(fmt.Sprintf("%s deleted successfully", kind), "name", o.Name, "namespace", o.OADPNamespace)
return nil
}
Loading
Loading