Skip to content

Commit

Permalink
chore: should be building now
Browse files Browse the repository at this point in the history
  • Loading branch information
JGAntunes committed Dec 23, 2024
1 parent fcedcc3 commit b4e81ee
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 86 deletions.
3 changes: 2 additions & 1 deletion pkg/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
kotsscheme "github.com/replicatedhq/kotskinds/client/kotsclientset/scheme"
troubleshootscheme "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
yaml "github.com/replicatedhq/yaml/v3"
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"k8s.io/client-go/kubernetes/scheme"
)

Expand All @@ -34,7 +35,7 @@ func NewHandler() *Handler {
func init() {
kotsscheme.AddToScheme(scheme.Scheme)
troubleshootscheme.AddToScheme(scheme.Scheme)
veleroscheme.AddToScheme(scheme.Scheme)
velerov1.AddToScheme(scheme.Scheme)
}

func RegisterSessionAuthRoutes(r *mux.Router, kotsStore store.Store, handler KOTSHandler, middleware *policy.Middleware) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/handlers/snapshot_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ func (h *Handler) DownloadSnapshotLogs(w http.ResponseWriter, r *http.Request) {
return
}

veleroClient, err := veleroclientv1.NewForConfig(cfg)
veleroClient, err := k8sutil.GetVeleroKubeClient(r.Context())
if err != nil {
err = errors.Wrap(err, "failed to create velero clientset")
err = errors.Wrap(err, "failed to create velero client")
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
Expand Down
23 changes: 11 additions & 12 deletions pkg/informers/informers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
kbclient "sigs.k8s.io/controller-runtime/pkg/client"
)

// Start will start the kots informers
Expand All @@ -38,12 +39,15 @@ func Start() error {
return errors.Wrap(err, "failed to detect velero namespace")
}

veleroClient, err := veleroclientv1.NewForConfig(cfg)
veleroClient, err := k8sutil.GetVeleroKubeClient(context.TODO())
if err != nil {
return errors.Wrap(err, "failed to create velero clientset")
return errors.Wrap(err, "failed to create velero client")
}

backupWatch, err := veleroClient.Backups(veleroNamespace).Watch(context.TODO(), metav1.ListOptions{ResourceVersion: "0"})
var backupList velerov1.BackupList
backupWatch, err := veleroClient.Watch(context.TODO(), &backupList, kbclient.InNamespace(veleroNamespace), &kbclient.ListOptions{
Raw: &metav1.ListOptions{ResourceVersion: "0"},
})
if err != nil {
if kuberneteserrors.IsNotFound(err) {
return nil
Expand Down Expand Up @@ -85,7 +89,8 @@ func Start() error {

backup.Annotations["kots.io/support-bundle-requested"] = time.Now().UTC().Format(time.RFC3339)

if _, err := veleroClient.Backups(backup.Namespace).Update(context.TODO(), backup, metav1.UpdateOptions{}); err != nil {
var backup velerov1.Backup
if err := veleroClient.Update(context.TODO(), &backup); err != nil {
logger.Error(err)
continue
}
Expand All @@ -96,14 +101,8 @@ func Start() error {
continue
}

updatedBackup, err := veleroClient.Backups(backup.Namespace).Get(context.TODO(), backup.Name, metav1.GetOptions{})
if err != nil {
logger.Error(err)
continue
}

updatedBackup.Annotations["kots.io/support-bundle-id"] = supportBundleID
if _, err := veleroClient.Backups(backup.Namespace).Update(context.TODO(), updatedBackup, metav1.UpdateOptions{}); err != nil {
backup.Annotations["kots.io/support-bundle-id"] = supportBundleID
if err := veleroClient.Update(context.TODO(), &backup); err != nil {
logger.Error(err)
continue
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/k8sutil/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func GetKubeClient(ctx context.Context) (kbclient.Client, error) {
return kcli, nil
}

func GetVeleroKubeClient(ctx context.Context) (kbclient.Client, error) {
func GetVeleroKubeClient(ctx context.Context) (kbclient.WithWatch, error) {
k8slogger := zap.New(func(o *zap.Options) {
o.DestWriter = io.Discard
})
Expand All @@ -181,7 +181,7 @@ func GetVeleroKubeClient(ctx context.Context) (kbclient.Client, error) {
}
scheme := runtime.NewScheme()
velerov1.AddToScheme(scheme)
kcli, err := kbclient.New(cfg, kbclient.Options{
kcli, err := kbclient.NewWithWatch(cfg, kbclient.Options{
Scheme: scheme,
})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/kotsadm/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ func MigrateExistingMinioFilesystemDeployments(log *logger.CLILogger, deployOpti
return errors.Wrap(err, "failed to create clientset")
}

veleroClient, err := veleroclientv1.NewForConfig(cfg)
veleroClient, err := k8sutil.GetVeleroKubeClient(context.TODO())
if err != nil {
return errors.Wrap(err, "failed to create velero clientset")
return errors.Wrap(err, "failed to create velero client")
}

bsl, err := snapshot.FindBackupStoreLocation(context.TODO(), clientset, veleroClient, deployOptions.Namespace)
Expand Down
70 changes: 38 additions & 32 deletions pkg/kotsadmsnapshot/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
serializer "k8s.io/apimachinery/pkg/runtime/serializer/json"
k8stypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -712,9 +713,9 @@ func ListBackupsForApp(ctx context.Context, kotsadmNamespace string, appID strin
return nil, errors.Wrap(err, "failed to create clientset")
}

veleroClient, err := veleroclient.GetBuilder().GetVeleroClient(cfg)
veleroClient, err := k8sutil.GetVeleroKubeClient(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to create velero clientset")
return nil, errors.Wrap(err, "failed to create velero client")
}

backendStorageLocation, err := kotssnapshot.FindBackupStoreLocation(ctx, clientset, veleroClient, kotsadmNamespace)
Expand All @@ -726,7 +727,8 @@ func ListBackupsForApp(ctx context.Context, kotsadmNamespace string, appID strin
return nil, errors.New("no backup store location found")
}

veleroBackups, err := veleroClient.Backups(backendStorageLocation.Namespace).List(ctx, metav1.ListOptions{})
var veleroBackups velerov1.BackupList
err = veleroClient.List(ctx, &veleroBackups, ctrlclient.InNamespace(backendStorageLocation.Namespace))
if err != nil {
return nil, errors.Wrap(err, "failed to list velero backups")
}
Expand Down Expand Up @@ -805,7 +807,7 @@ func ListInstanceBackups(ctx context.Context, kotsadmNamespace string) ([]*types
return nil, errors.Wrap(err, "failed to create clientset")
}

veleroClient, err := veleroclient.GetBuilder().GetVeleroClient(cfg)
veleroClient, err := k8sutil.GetVeleroKubeClient(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to create velero clientset")
}
Expand All @@ -819,7 +821,8 @@ func ListInstanceBackups(ctx context.Context, kotsadmNamespace string) ([]*types
return nil, errors.New("no backup store location found")
}

veleroBackups, err := veleroClient.Backups(backendStorageLocation.Namespace).List(ctx, metav1.ListOptions{})
var veleroBackups velerov1.BackupList
err = veleroClient.List(ctx, &veleroBackups, ctrlclient.InNamespace(backendStorageLocation.Namespace))
if err != nil {
return nil, errors.Wrap(err, "failed to list velero backups")
}
Expand Down Expand Up @@ -934,18 +937,14 @@ func getAppsFromAppSequences(veleroBackup velerov1.Backup) ([]types.App, error)
}

func getSnapshotVolumeSummary(ctx context.Context, veleroBackup *velerov1.Backup) (*types.VolumeSummary, error) {
cfg, err := k8sutil.GetClusterConfig()
if err != nil {
return nil, errors.Wrap(err, "failed to get cluster config")
}

veleroClient, err := veleroclient.GetBuilder().GetVeleroClient(cfg)
veleroClient, err := k8sutil.GetVeleroKubeClient(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to create clientset")
return nil, errors.Wrap(err, "failed to create client")
}

veleroPodBackupVolumes, err := veleroClient.PodVolumeBackups(veleroBackup.Namespace).List(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("velero.io/backup-name=%s", velerolabel.GetValidName(veleroBackup.Name)),
var veleroPodBackupVolumes velerov1.PodVolumeBackupList
err = veleroClient.List(ctx, &veleroPodBackupVolumes, ctrlclient.InNamespace(veleroBackup.Namespace), ctrlclient.MatchingLabels{
"velero.io/backup-name": velerolabel.GetValidName(veleroBackup.Name),
})
if err != nil {
return nil, errors.Wrap(err, "failed to list pod back up volumes")
Expand Down Expand Up @@ -985,7 +984,7 @@ func GetBackup(ctx context.Context, kotsadmNamespace string, backupID string) (*
return nil, errors.Wrap(err, "failed to create clientset")
}

veleroClient, err := veleroclient.GetBuilder().GetVeleroClient(cfg)
veleroClient, err := k8sutil.GetVeleroKubeClient(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to create velero clientset")
}
Expand All @@ -1000,12 +999,13 @@ func GetBackup(ctx context.Context, kotsadmNamespace string, backupID string) (*

veleroNamespace := bsl.Namespace

backup, err := veleroClient.Backups(veleroNamespace).Get(ctx, backupID, metav1.GetOptions{})
var backup velerov1.Backup
err = veleroClient.Get(ctx, k8stypes.NamespacedName{Namespace: veleroNamespace, Name: backupID}, &backup)
if err != nil {
return nil, errors.Wrap(err, "failed to get backup")
}

return backup, nil
return &backup, nil
}

func getBackupNameFromPrefix(appSlug string) string {
Expand All @@ -1031,9 +1031,9 @@ func DeleteBackup(ctx context.Context, kotsadmNamespace string, backupID string)
return errors.Wrap(err, "failed to create clientset")
}

veleroClient, err := veleroclient.GetBuilder().GetVeleroClient(cfg)
veleroClient, err := k8sutil.GetVeleroKubeClient(ctx)
if err != nil {
return errors.Wrap(err, "failed to create velero clientset")
return errors.Wrap(err, "failed to create velero client")
}

bsl, err := kotssnapshot.FindBackupStoreLocation(ctx, clientset, veleroClient, kotsadmNamespace)
Expand All @@ -1048,8 +1048,10 @@ func DeleteBackup(ctx context.Context, kotsadmNamespace string, backupID string)
// Default legacy behaviour is to delete the backup whose name matches the backupID
backupsToDelete := []string{backupID}

listOptions := metav1.ListOptions{LabelSelector: fmt.Sprintf("%s=%s", types.InstanceBackupNameLabel, backupID)}
veleroBackups, err := veleroClient.Backups(veleroNamespace).List(ctx, listOptions)
var veleroBackups velerov1.BackupList
err = veleroClient.List(ctx, &veleroBackups, ctrlclient.InNamespace(veleroNamespace), ctrlclient.MatchingLabels{
types.InstanceBackupNameLabel: backupID,
})
if err != nil {
return errors.Wrap(err, "failed to list velero backups for deletion")
}
Expand All @@ -1073,7 +1075,7 @@ func DeleteBackup(ctx context.Context, kotsadmNamespace string, backupID string)
},
}

_, err = veleroClient.DeleteBackupRequests(veleroNamespace).Create(ctx, veleroDeleteBackupRequest, metav1.CreateOptions{})
err = veleroClient.Create(ctx, veleroDeleteBackupRequest)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to create delete backup request for backup %s", backupToDelete))
}
Expand Down Expand Up @@ -1123,9 +1125,9 @@ func GetBackupDetail(ctx context.Context, kotsadmNamespace string, backupName st
return nil, errors.Wrap(err, "failed to create clientset")
}

veleroClient, err := veleroclient.GetBuilder().GetVeleroClient(cfg)
veleroClient, err := k8sutil.GetVeleroKubeClient(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to create velero clientset")
return nil, errors.Wrap(err, "failed to create velero client")
}

backendStorageLocation, err := kotssnapshot.FindBackupStoreLocation(ctx, clientset, veleroClient, kotsadmNamespace)
Expand Down Expand Up @@ -1157,29 +1159,33 @@ func GetBackupDetail(ctx context.Context, kotsadmNamespace string, backupName st
// listBackupsByName returns a list of backups for the specified backup name. First it tries to get
// the backup by the replicated.com/backup-name label, and if that fails, it tries to get the
// backup by the metadata name.
func listBackupsByName(ctx context.Context, veleroClient veleroclientv1.VeleroV1Interface, veleroNamespace string, backupName string) ([]velerov1.Backup, error) {
func listBackupsByName(ctx context.Context, ctrlClient ctrlclient.Client, veleroNamespace string, backupName string) ([]velerov1.Backup, error) {
// first try to get the backup from the backup-name label
backupList, err := veleroClient.Backups(veleroNamespace).List(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", types.InstanceBackupNameLabel, velerolabel.GetValidName(backupName)),
var backupList velerov1.BackupList
err := ctrlClient.List(ctx, &backupList, ctrlclient.InNamespace(veleroNamespace), ctrlclient.MatchingLabels{
types.InstanceBackupNameLabel: velerolabel.GetValidName(backupName),
})
if err != nil {
return nil, fmt.Errorf("failed to list backups by label: %w", err)
}
if len(backupList.Items) > 0 {
return backupList.Items, nil
}
backup, err := veleroClient.Backups(veleroNamespace).Get(ctx, backupName, metav1.GetOptions{})

var backup velerov1.Backup
err = ctrlClient.Get(ctx, k8stypes.NamespacedName{Namespace: veleroNamespace, Name: backupName}, &backup)
if err != nil {
return nil, fmt.Errorf("failed to get backup by name: %w", err)
}

return []velerov1.Backup{*backup}, nil
return []velerov1.Backup{backup}, nil
}

// getBackupDetailForBackup returns a BackupDetail object for the specified backup.
func getBackupDetailForBackup(ctx context.Context, veleroClient veleroclientv1.VeleroV1Interface, veleroNamespace string, backup velerov1.Backup) (*types.BackupDetail, error) {
backupVolumes, err := veleroClient.PodVolumeBackups(veleroNamespace).List(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("velero.io/backup-name=%s", velerolabel.GetValidName(backup.Name)),
func getBackupDetailForBackup(ctx context.Context, ctrlClient ctrlclient.Client, veleroNamespace string, backup velerov1.Backup) (*types.BackupDetail, error) {
backupVolumes := velerov1.PodVolumeBackupList{}
err := ctrlClient.List(ctx, &backupVolumes, ctrlclient.InNamespace(veleroNamespace), ctrlclient.MatchingLabels{
"velero.io/backup-name": velerolabel.GetValidName(backup.Name),
})
if err != nil {
return nil, errors.Wrap(err, "failed to list volumes")
Expand Down
Loading

0 comments on commit b4e81ee

Please sign in to comment.