Skip to content

Commit

Permalink
[Shipa-2871] Adds volume name validators (#258)
Browse files Browse the repository at this point in the history
Explicitly checking if volume name that gets passed in IsDNS1123Label and pvc exists
  • Loading branch information
kavinaravind authored Jun 7, 2022
1 parent 4105c20 commit 4d7b379
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
13 changes: 10 additions & 3 deletions internal/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,22 @@ func deployImage(ctx context.Context, svc *Services, app *ketchv1.App, params *C
return err
}

volume, _ := params.getVolumeName()
volumeMounts, _ := params.getVolumeMounts()
volumes, _ := params.getVolumes()
for _, volume := range volumes {
_, err = svc.KubeClient.CoreV1().PersistentVolumeClaims(framework.Spec.NamespaceName).Get(ctx, volume.PersistentVolumeClaim.ClaimName, metav1.GetOptions{})
if err != nil {
return errors.Wrap(err, "create pvc or input correct pvc name")
}
}

steps, _ := params.getSteps()
stepWeight, _ := params.getStepWeight()
interval, _ := params.getStepInterval()
units, _ := params.getUnits()
version, _ := params.getVersion()
process, _ := params.getProcess()
volume, _ := params.getVolumeName()
volumes, _ := params.getVolumes()
volumeMounts, _ := params.getVolumeMounts()

currentTime := time.Now()

Expand Down
7 changes: 7 additions & 0 deletions internal/deploy/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"os"
"path"
"strconv"
"strings"
"time"

"github.com/spf13/pflag"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/yaml"

Expand Down Expand Up @@ -400,6 +402,11 @@ func (c *ChangeSet) getVolumeName() (string, error) {
if c.volume == nil {
return "", newMissingError(FlagVolume)
}
if errs := validation.IsDNS1123Label(*c.volume); len(errs) > 0 {
return "", fmt.Errorf("%w %s, %s",
newInvalidValueError(FlagVolume), FlagVolume, strings.Join(errs[:], ","))
}

return *c.volume, nil
}

Expand Down
5 changes: 5 additions & 0 deletions internal/deploy/parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func TestChangeSet_getVolumeName(t *testing.T) {
set: ChangeSet{},
wantErr: `"volume" missing`,
},
{
name: "invalid volume name",
set: ChangeSet{volume: stringRef("aaa/bbb")},
wantErr: `"volume" invalid value volume, a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')`,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 4d7b379

Please sign in to comment.