Skip to content

Commit

Permalink
Fix in the comparison, and added fetching shoot to minimize runtime r…
Browse files Browse the repository at this point in the history
…ecreation
  • Loading branch information
akgalwas committed Nov 22, 2024
1 parent 0a48f6c commit 4a11ced
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
38 changes: 34 additions & 4 deletions hack/runtime-migrator/cmd/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/pkg/errors"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"log/slog"

"github.com/gardener/gardener/pkg/apis/core/v1beta1"
Expand Down Expand Up @@ -84,9 +85,14 @@ func (m Migration) Do(ctx context.Context, runtimeIDs []string) error {
}

run := func(runtimeID string) {
shoot := findShoot(runtimeID, shootList)
if shoot == nil {
reportError(runtimeID, "", "Failed to find shoot", errors.New("no shoot with given runtimeID found"))
shoot, err := m.fetchShoot(ctx, shootList, m.shootClient, runtimeID)
if err != nil {
reportError(runtimeID, "", "Failed to fetch shoot", err)
return
}

if shootIsBeingDeleted(shoot) {
reportError(runtimeID, shoot.Name, "Runtime is being deleted", nil)
return
}

Expand Down Expand Up @@ -158,15 +164,39 @@ main:
return nil
}

func findShoot(runtimeID string, shootList *v1beta1.ShootList) *v1beta1.Shoot {
func getShoot(runtimeID string, shootList *v1beta1.ShootList) *v1beta1.Shoot {
for _, shoot := range shootList.Items {
if shoot.Annotations[runtimeIDAnnotation] == runtimeID {
return &shoot
}
}

return nil
}

func (m Migration) fetchShoot(ctx context.Context, shootList *v1beta1.ShootList, shootClient gardener_types.ShootInterface, runtimeID string) (*v1beta1.Shoot, error) {
shoot := getShoot(runtimeID, shootList)
if shoot == nil {
return nil, errors.New("shoot was deleted or the runtime ID is incorrect")
}

// We are fetching the shoot from the gardener to make sure the runtime didn't get deleted during the migration process
refreshedShoot, err := m.shootClient.Get(ctx, shoot.Name, v1.GetOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
return nil, errors.New("shoot was deleted")
}

return nil, err
}

return refreshedShoot, nil
}

func shootIsBeingDeleted(shoot *v1beta1.Shoot) bool {
return !shoot.DeletionTimestamp.IsZero()
}

func (m Migration) applyRuntimeCR(runtime runtimev1.Runtime) error {
// TODO: This method covers create scenario only, we should implement update as well
return m.kcpClient.Create(context.Background(), &runtime)
Expand Down
1 change: 1 addition & 0 deletions hack/shoot-comparator/pkg/shoot/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ func newKubeAPIServerMatcher(k v1beta1.Kubernetes) types.GomegaMatcher {
"DefaultUnreachableTolerationSeconds": gstruct.Ignore(),
"EncryptionConfig": gstruct.Ignore(),
"StructuredAuthentication": gstruct.Ignore(),
"StructuredAuthorization": gstruct.Ignore(),
},
))
}
Expand Down

0 comments on commit 4a11ced

Please sign in to comment.