Skip to content

Commit

Permalink
Fix forbidden error when updating volumes stack (#1538)
Browse files Browse the repository at this point in the history
* Fix forbidden error when updating volumes stack

Signed-off-by: Javier López Barba <javier@okteto.com>

* Add new labels and annotations

Signed-off-by: Javier López Barba <javier@okteto.com>

* Remove unused code

Signed-off-by: Javier López Barba <javier@okteto.com>

* Fix set class directly

Signed-off-by: Javier López Barba <javier@okteto.com>

* Fix return error when updating storage class name

Signed-off-by: Javier López Barba <javier@okteto.com>
  • Loading branch information
jLopezbarb committed May 18, 2021
1 parent c93f09e commit 1026e05
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 44 deletions.
17 changes: 15 additions & 2 deletions pkg/cmd/stack/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,21 @@ func deployVolume(ctx context.Context, volumeName string, s *model.Stack, c *kub
if old.Labels[okLabels.StackNameLabel] != s.Name {
return fmt.Errorf("name collision: the volume '%s' belongs to the stack '%s'", pvc.Name, old.Labels[okLabels.StackNameLabel])
}
if err := volumes.Update(ctx, &pvc, c); err != nil {
return fmt.Errorf("error updating volume of service '%s': %s", pvc.Name, err.Error())

old.Spec.Resources.Requests["storage"] = pvc.Spec.Resources.Requests["storage"]
for key, value := range pvc.Labels {
old.Labels[key] = value
}
for key, value := range pvc.Annotations {
old.Annotations[key] = value
}
if pvc.Spec.StorageClassName != nil {
old.Spec.StorageClassName = pvc.Spec.StorageClassName
}

if err := volumes.Update(ctx, old, c); err != nil {
return fmt.Errorf("error updating volume '%s': %s", old.Name, err.Error())

}
}
return nil
Expand Down
92 changes: 50 additions & 42 deletions pkg/model/stack_serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,49 +208,13 @@ func (s *Stack) UnmarshalYAML(unmarshal func(interface{}) error) error {
if len(s.Endpoints) == 0 {
s.Endpoints = getEndpointsFromPorts(stackRaw.Services)
}
volumes := make(map[string]*VolumeSpec)
for volumeName, v := range stackRaw.Volumes {
result := VolumeSpec{}
if v == nil {
result.Labels = make(Labels)
result.Annotations = make(Annotations)
} else {
result.Labels = make(Labels)
if v.Annotations == nil {
result.Annotations = make(Annotations)
} else {
result.Annotations = v.Annotations
}

for key, value := range v.Labels {
result.Annotations[key] = value
}

if v.Size.Value.Cmp(resource.MustParse("0")) > 0 {
result.Size = v.Size
}
if v.DriverOpts != nil {
for key, value := range v.DriverOpts {
if key == "size" {
qK8s, err := resource.ParseQuantity(value)
if err != nil {
return err
}
result.Size.Value = qK8s
}
if key == "class" {
result.Class = value
}
}
}
}

volumes[volumeName] = &result
}

s.Volumes = make(map[string]*VolumeSpec)
for volumeName, volume := range volumes {
s.Volumes[sanitizeName(volumeName)] = volume
for volumeName, volume := range stackRaw.Volumes {
volumeSpec, err := unmarshalVolume(volume)
if err != nil {
return err
}
s.Volumes[sanitizeName(volumeName)] = volumeSpec
}

sanitizedServicesNames := make(map[string]string)
Expand All @@ -273,6 +237,50 @@ func (s *Stack) UnmarshalYAML(unmarshal func(interface{}) error) error {
return nil
}

func unmarshalVolume(volume *VolumeTopLevel) (*VolumeSpec, error) {

result := &VolumeSpec{}
if volume == nil {
result.Labels = make(Labels)
result.Annotations = make(Annotations)
} else {
result.Labels = make(Labels)
if volume.Annotations == nil {
result.Annotations = make(Annotations)
} else {
result.Annotations = volume.Annotations
}

for key, value := range volume.Labels {
result.Annotations[key] = value
}

if volume.Size.Value.Cmp(resource.MustParse("0")) > 0 {
result.Size = volume.Size
}
if volume.DriverOpts != nil {
for key, value := range volume.DriverOpts {
if key == "size" {
qK8s, err := resource.ParseQuantity(value)
if err != nil {
return nil, err
}
result.Size.Value = qK8s
}
if key == "class" {
result.Class = value
}
}
}
if result.Class == "" {
result.Class = volume.Class
}
}

return result, nil

}

func getEndpointsFromPorts(services map[string]*ServiceRaw) EndpointSpec {
endpoints := make(EndpointSpec)
for svcName, svc := range services {
Expand Down
5 changes: 5 additions & 0 deletions pkg/model/stack_serializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,11 @@ func Test_unmarshalVolumes(t *testing.T) {
manifest: []byte("services:\n app:\n image: okteto/vote:1\nvolumes:\n v1:\n driver_opts:\n class: standard"),
expectedVolume: &VolumeSpec{Size: Quantity{resource.MustParse("1Gi")}, Class: "standard", Labels: make(map[string]string), Annotations: make(map[string]string)},
},
{
name: "volume with class",
manifest: []byte("services:\n app:\n image: okteto/vote:1\nvolumes:\n v1:\n class: standard"),
expectedVolume: &VolumeSpec{Size: Quantity{resource.MustParse("1Gi")}, Class: "standard", Labels: make(map[string]string), Annotations: make(map[string]string)},
},
{
name: "volume with labels",
manifest: []byte("services:\n app:\n image: okteto/vote:1\nvolumes:\n v1:\n labels:\n env: test"),
Expand Down

0 comments on commit 1026e05

Please sign in to comment.