Skip to content

Commit

Permalink
fix(region): validate post overlay unique_name (#21776)
Browse files Browse the repository at this point in the history
  • Loading branch information
zexi authored Dec 10, 2024
1 parent 041a0ec commit 2d4bda9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/compute/guestdrivers/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (p *SPodDriver) validateContainerData(ctx context.Context, userCred mcclien
if ctr.Name == "" {
ctr.Name = fmt.Sprintf("%s-%d", defaultNamePrefix, idx)
}
if err := models.GetContainerManager().ValidateSpec(ctx, userCred, &ctr.ContainerSpec, nil); err != nil {
if err := models.GetContainerManager().ValidateSpec(ctx, userCred, &ctr.ContainerSpec, nil, nil); err != nil {
return errors.Wrap(err, "validate container spec")
}
if err := p.validateContainerVolumeMounts(ctx, userCred, ctr, input); err != nil {
Expand Down
15 changes: 9 additions & 6 deletions pkg/compute/models/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ func (m *SContainerManager) ValidateCreateData(ctx context.Context, userCred mcc
}
pod := obj.(*SGuest)
input.GuestId = pod.GetId()
if err := m.ValidateSpec(ctx, userCred, &input.Spec, pod); err != nil {
if err := m.ValidateSpec(ctx, userCred, &input.Spec, pod, nil); err != nil {
return nil, errors.Wrap(err, "validate spec")
}
return input, nil
}

func (m *SContainerManager) ValidateSpec(ctx context.Context, userCred mcclient.TokenCredential, spec *api.ContainerSpec, pod *SGuest) error {
func (m *SContainerManager) ValidateSpec(ctx context.Context, userCred mcclient.TokenCredential, spec *api.ContainerSpec, pod *SGuest, ctr *SContainer) error {
if spec.ImagePullPolicy == "" {
spec.ImagePullPolicy = apis.ImagePullPolicyIfNotPresent
}
Expand All @@ -169,7 +169,7 @@ func (m *SContainerManager) ValidateSpec(ctx context.Context, userCred mcclient.
}

if pod != nil {
if err := m.ValidateSpecVolumeMounts(ctx, userCred, pod, spec); err != nil {
if err := m.ValidateSpecVolumeMounts(ctx, userCred, pod, spec, ctr); err != nil {
return errors.Wrap(err, "ValidateSpecVolumeMounts")
}
for idx, dev := range spec.Devices {
Expand Down Expand Up @@ -222,7 +222,7 @@ func (m *SContainerManager) ValidateSpecDevice(ctx context.Context, userCred mcc
return drv.ValidateCreateData(ctx, userCred, pod, dev)
}

func (m *SContainerManager) ValidateSpecVolumeMounts(ctx context.Context, userCred mcclient.TokenCredential, pod *SGuest, spec *api.ContainerSpec) error {
func (m *SContainerManager) ValidateSpecVolumeMounts(ctx context.Context, userCred mcclient.TokenCredential, pod *SGuest, spec *api.ContainerSpec, ctr *SContainer) error {
relation, err := m.GetVolumeMountRelations(pod, spec)
if err != nil {
return errors.Wrap(err, "GetVolumeMountRelations")
Expand All @@ -231,6 +231,9 @@ func (m *SContainerManager) ValidateSpecVolumeMounts(ctx context.Context, userCr
curCtrs, _ := m.GetContainersByPod(pod.GetId())
volUniqNames := sets.NewString()
for idx := range curCtrs {
if ctr != nil && ctr.GetId() == curCtrs[idx].GetId() {
continue
}
for _, vol := range curCtrs[idx].Spec.VolumeMounts {
if vol.UniqueName != "" {
volUniqNames.Insert(vol.UniqueName)
Expand All @@ -240,7 +243,7 @@ func (m *SContainerManager) ValidateSpecVolumeMounts(ctx context.Context, userCr
for idx, vm := range spec.VolumeMounts {
if vm.UniqueName != "" {
if volUniqNames.Has(vm.UniqueName) {
return httperrors.NewDuplicateNameError("volume_mount unique_name %s", vm.UniqueName)
return httperrors.NewDuplicateNameError("volume_mount unique_name %s", fmt.Sprintf("%s: %s, index: %d", vm.UniqueName, jsonutils.Marshal(vm), idx))
} else {
volUniqNames.Insert(vm.UniqueName)
}
Expand Down Expand Up @@ -426,7 +429,7 @@ func (c *SContainer) ValidateUpdateData(ctx context.Context, userCred mcclient.T
}
input.VirtualResourceBaseUpdateInput = baseInput

if err := GetContainerManager().ValidateSpec(ctx, userCred, &input.Spec, c.GetPod()); err != nil {
if err := GetContainerManager().ValidateSpec(ctx, userCred, &input.Spec, c.GetPod(), c); err != nil {
return nil, errors.Wrap(err, "validate spec")
}

Expand Down

0 comments on commit 2d4bda9

Please sign in to comment.