Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PWX-36486: Rsync job name to be restricted within max length limit. #347

Merged
merged 1 commit into from
Mar 21, 2024
Merged
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
1 change: 1 addition & 0 deletions pkg/controllers/dataexport/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -1862,6 +1862,7 @@ func startTransferJob(
return drv.StartJob(
drivers.WithSourcePVC(srcPVCName),
drivers.WithNamespace(dataExport.Spec.Destination.Namespace),
drivers.WithDataExportUID(string(dataExport.UID)),
drivers.WithDestinationPVC(dataExport.Spec.Destination.Name),
drivers.WithLabels(dataExport.Labels),
)
Expand Down
14 changes: 13 additions & 1 deletion pkg/drivers/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type JobOpts struct {
VolumeBackupDeleteName string
VolumeBackupDeleteNamespace string
DataExportName string
DataExportUID string
SnapshotID string
CredSecretName string
CredSecretNamespace string
Expand Down Expand Up @@ -418,13 +419,24 @@ func WithLabels(l map[string]string) JobOption {
func WithDataExportName(name string) JobOption {
return func(opts *JobOpts) error {
if strings.TrimSpace(name) == "" {
return fmt.Errorf("dataexport namespace should be set")
return fmt.Errorf("dataexport name should be set")
}
opts.DataExportName = name
return nil
}
}

// WithDataExportUID is job parameter
func WithDataExportUID(uid string) JobOption {
return func(opts *JobOpts) error {
if strings.TrimSpace(uid) == "" {
return fmt.Errorf("dataexport UID should be set")
}
opts.DataExportUID = uid
return nil
}
}

// WithCertSecretName is job parameter.
func WithCertSecretName(name string) JobOption {
return func(opts *JobOpts) error {
Expand Down
18 changes: 13 additions & 5 deletions pkg/drivers/rsync/rsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation"
)

// Driver is a rsync implementation of the data export interface.
Expand All @@ -37,7 +38,7 @@ func (d Driver) StartJob(opts ...drivers.JobOption) (id string, err error) {
return "", err
}

rsyncJob, err := jobFor(o.SourcePVCName, o.DestinationPVCName, o.Namespace, o.Labels)
rsyncJob, err := jobFor(o.SourcePVCName, o.DestinationPVCName, o.DataExportUID, o.Namespace, o.Labels)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -112,7 +113,7 @@ func (d Driver) validate(o drivers.JobOpts) error {
return nil
}

func jobFor(srcVol, dstVol, namespace string, labels map[string]string) (*batchv1.Job, error) {
func jobFor(srcVol, dstVol, dataexportUID, namespace string, labels map[string]string) (*batchv1.Job, error) {
labels = addJobLabels(labels)

rsyncFlags := "-avz"
Expand All @@ -126,7 +127,7 @@ func jobFor(srcVol, dstVol, namespace string, labels map[string]string) (*batchv
return nil, err
}

jobName := toJobName(srcVol)
jobName := toJobName(srcVol, dataexportUID)
if err := utils.SetupServiceAccount(jobName, namespace, roleFor(utils.RsyncOpenshiftSCC())); err != nil {
return nil, err
}
Expand Down Expand Up @@ -189,8 +190,15 @@ func jobFor(srcVol, dstVol, namespace string, labels map[string]string) (*batchv
}, nil
}

func toJobName(id string) string {
return fmt.Sprintf("import-rsync-%s", id)
func toJobName(id string, uid string) string {
shortUID := utils.GetShortUID(uid)
jobName := fmt.Sprintf("import-rsync-%s", id)
if len(jobName) > validation.DNS1035LabelMaxLength-len(shortUID)-1 {
jobName = fmt.Sprintf("%s-%s", jobName[:validation.DNS1035LabelMaxLength-len(shortUID)-1], shortUID)
} else {
jobName = fmt.Sprintf("%s-%s", jobName, shortUID)
}
return jobName
}

func addJobLabels(labels map[string]string) map[string]string {
Expand Down
45 changes: 45 additions & 0 deletions pkg/drivers/rsync/rsync_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package rsync

import (
"fmt"
"testing"

"github.com/portworx/kdmp/pkg/drivers/utils"
"github.com/stretchr/testify/require"
)

func TestToJobName(t *testing.T) {
id := "1234567890"
// uid is a 32 character string
uid := "abcdefghijklmnopqrstuvwxyzabcdef"

expectedJobName := fmt.Sprintf("import-rsync-%s-%s", id, utils.GetShortUID(uid))
actualJobName := toJobName(id, uid)

require.Equal(t, expectedJobName, actualJobName, "unexpected job name")

// Test when the job name exceeds the maximum length
longID := "1234567890123456789012345678901234567890123456789012345678901234567890"
expectedJobName = fmt.Sprintf("import-rsync-%s-%s", longID[:41], utils.GetShortUID(uid))
actualJobName = toJobName(longID, uid)

require.Equal(t, expectedJobName, actualJobName, "unexpected job name")

// Test when the job name exceeds the maximum length and the UID is empty
expectedJobName = fmt.Sprintf("import-rsync-%s-%s", longID[:49], "")
actualJobName = toJobName(longID, "")

require.Equal(t, expectedJobName, actualJobName, "unexpected job name")

// Test when the ID is empty
expectedJobName = fmt.Sprintf("import-rsync-%s-%s", "", utils.GetShortUID(uid))
actualJobName = toJobName("", uid)

require.Equal(t, expectedJobName, actualJobName, "unexpected job name")

// Test when the ID and UID are empty
expectedJobName = fmt.Sprintf("import-rsync-%s-%s", "", "")
actualJobName = toJobName("", "")

require.Equal(t, expectedJobName, actualJobName, "unexpected job name")
}
7 changes: 7 additions & 0 deletions pkg/drivers/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,3 +957,10 @@ func SetDisableIstioLabel(labels map[string]string, jobOpts drivers.JobOpts) map
}
return labels
}

func GetShortUID(uid string) string {
if len(uid) < 8 {
return uid
}
return uid[:8]
}
Loading