containerized-data-importer/pkg/controller/clone/rebind.go
Edu Gómez Escandell 0e750262a3
Enable autoformatting linters (#3179)
* Enable gofmt linter

From the docs:

> Gofmt checks whether code was gofmt-ed. By default this tool runs with
> -s option to check for code simplification.

https://golangci-lint.run/usage/linters/#gofmt

Signed-off-by: Edu Gómez Escandell <egomez@redhat.com>

* Run gomft on the project

Ran this command after adding the gofmt linter:

	golangci-lint run ./... --fix

Signed-off-by: Edu Gómez Escandell <egomez@redhat.com>

* Enable whitespace linter

From the docs:
> Whitespace is a linter that checks for unnecessary newlines at the
> start and end of functions, if, for, etc.

https://golangci-lint.run/usage/linters/#whitespace

Signed-off-by: Edu Gómez Escandell <egomez@redhat.com>

* Run whitespace on the project

Ran this command after adding the whitespace linter:

	golangci-lint run ./... --fix

Signed-off-by: Edu Gómez Escandell <egomez@redhat.com>

* Enable GCI linter

Per the docs:

> Gci controls Go package import order and makes it always deterministic.

https://golangci-lint.run/usage/linters/#gci

NOTE: I noticed that many files separate their imports in a particular
way, so I set the linter to enforce this standard.

Signed-off-by: Edu Gómez Escandell <egomez@redhat.com>

* Run GCI on the project

Ran this command after adding the GCI linter:

	golangci-lint run ./... --fix

Signed-off-by: Edu Gómez Escandell <egomez@redhat.com>

---------

Signed-off-by: Edu Gómez Escandell <egomez@redhat.com>
2024-04-24 13:52:22 +02:00

72 lines
1.6 KiB
Go

package clone
import (
"context"
"fmt"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
cc "kubevirt.io/containerized-data-importer/pkg/controller/common"
)
// RebindPhaseName is the name of the rebind phase
const RebindPhaseName = "Rebind"
// RebindPhase binds a PV from one PVC to another
type RebindPhase struct {
SourceNamespace string
SourceName string
TargetNamespace string
TargetName string
Client client.Client
Log logr.Logger
Recorder record.EventRecorder
}
var _ Phase = &RebindPhase{}
// Name returns the name of the phase
func (p *RebindPhase) Name() string {
return RebindPhaseName
}
// Reconcile rebinds a PV
func (p *RebindPhase) Reconcile(ctx context.Context) (*reconcile.Result, error) {
targetClaim := &corev1.PersistentVolumeClaim{}
exists, err := getResource(ctx, p.Client, p.TargetNamespace, p.TargetName, targetClaim)
if err != nil {
return nil, err
}
if !exists {
return nil, fmt.Errorf("target claim does not exist")
}
if targetClaim.Spec.VolumeName != "" {
// guess we're all done
return nil, nil
}
sourceClaim := &corev1.PersistentVolumeClaim{}
exists, err = getResource(ctx, p.Client, p.SourceNamespace, p.SourceName, sourceClaim)
if err != nil {
return nil, err
}
if !exists {
return nil, fmt.Errorf("source claim does not exist")
}
if err := cc.Rebind(ctx, p.Client, sourceClaim, targetClaim); err != nil {
return nil, err
}
return &reconcile.Result{}, nil
}