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

add container name validation during creation and push #3959

16 changes: 16 additions & 0 deletions pkg/devfile/adapters/kubernetes/component/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

componentlabels "github.com/openshift/odo/pkg/component/labels"
"github.com/openshift/odo/pkg/envinfo"
"github.com/openshift/odo/pkg/util"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -22,6 +23,7 @@ import (
"github.com/openshift/odo/pkg/devfile/adapters/kubernetes/storage"
"github.com/openshift/odo/pkg/devfile/adapters/kubernetes/utils"
versionsCommon "github.com/openshift/odo/pkg/devfile/parser/data/common"
"github.com/openshift/odo/pkg/devfile/validate"
"github.com/openshift/odo/pkg/kclient"
"github.com/openshift/odo/pkg/log"
odoutil "github.com/openshift/odo/pkg/odo/util"
Expand Down Expand Up @@ -122,6 +124,20 @@ func (a Adapter) Push(parameters common.PushParameters) (err error) {
// Validate the devfile build and run commands
log.Info("\nValidation")
s := log.Spinner("Validating the devfile")
err = util.ValidateK8sResourceName("component name", a.ComponentName)
if err != nil {
return err
}
err = validate.ValidateContainerName(a.Devfile.Data)
if err != nil {
return err
}

err = util.ValidateK8sResourceName("component namespace", parameters.EnvSpecificInfo.GetNamespace())
if err != nil {
return err
}

pushDevfileCommands, err := common.ValidateAndGetPushDevfileCommands(a.Devfile.Data, a.devfileBuildCmd, a.devfileRunCmd)
if err != nil {
s.End(false)
Expand Down
15 changes: 15 additions & 0 deletions pkg/devfile/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package validate
import (
"fmt"

adaptersCommon "github.com/openshift/odo/pkg/devfile/adapters/common"
"github.com/openshift/odo/pkg/devfile/parser/data"
"github.com/openshift/odo/pkg/devfile/parser/data/common"
"github.com/openshift/odo/pkg/util"
"k8s.io/klog"

v200 "github.com/openshift/odo/pkg/devfile/parser/data/2.0.0"
Expand Down Expand Up @@ -44,3 +47,15 @@ func ValidateDevfileData(data interface{}) error {
return nil

}

// ValidateContainerName validates whether the container name is valid for K8
func ValidateContainerName(devfileData data.DevfileData) error {
containerComponents := adaptersCommon.GetDevfileContainerComponents(devfileData)
for _, comp := range containerComponents {
err := util.ValidateK8sResourceName("container name", comp.Name)
if err != nil {
return err
}
}
return nil
}
129 changes: 129 additions & 0 deletions pkg/devfile/validate/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package validate

import (
"testing"

"github.com/openshift/odo/pkg/devfile/parser/data"
versionsCommon "github.com/openshift/odo/pkg/devfile/parser/data/common"
"github.com/openshift/odo/pkg/testingutil"
)

func TestValidateContainerName(t *testing.T) {

tests := []struct {
name string
devfileData data.DevfileData
wantErr bool
}{
{
name: "Case 1: Valid container name",
devfileData: &testingutil.TestDevfileData{
Components: []versionsCommon.DevfileComponent{
{
Name: "runtime",
Container: &versionsCommon.Container{
Image: "quay.io/nodejs-12",
Endpoints: []versionsCommon.Endpoint{
{
Name: "port-3000",
TargetPort: 3000,
},
},
},
},
},
},
wantErr: false,
},
{
name: "Case 2: long container name",
devfileData: &testingutil.TestDevfileData{
Components: []versionsCommon.DevfileComponent{
{
Name: "runtimeruntimeruntimeruntimeruntimeruntimeruntimeruntimeruntimeruntime",
Container: &versionsCommon.Container{
Image: "quay.io/nodejs-12",
Endpoints: []versionsCommon.Endpoint{
{
Name: "port-3000",
TargetPort: 3000,
},
},
},
},
},
},
wantErr: true,
},
{
name: "Case 3: special character in container name",
devfileData: &testingutil.TestDevfileData{
Components: []versionsCommon.DevfileComponent{
{
Name: "run@time",
Container: &versionsCommon.Container{
Image: "quay.io/nodejs-12",
Endpoints: []versionsCommon.Endpoint{
{
Name: "port-3000",
TargetPort: 3000,
},
},
},
},
},
},
wantErr: true,
},
{
name: "Case 4: numeric container name",
devfileData: &testingutil.TestDevfileData{
Components: []versionsCommon.DevfileComponent{
{
Name: "12345",
Container: &versionsCommon.Container{
Image: "quay.io/nodejs-12",
Endpoints: []versionsCommon.Endpoint{
{
Name: "port-3000",
TargetPort: 3000,
},
},
},
},
},
},
wantErr: true,
},
{
name: "Case 5: container name with capitalised character",
devfileData: &testingutil.TestDevfileData{
Components: []versionsCommon.DevfileComponent{
{
Name: "runTime",
Container: &versionsCommon.Container{
Image: "quay.io/nodejs-12",
Endpoints: []versionsCommon.Endpoint{
{
Name: "port-3000",
TargetPort: 3000,
},
},
},
},
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateContainerName(tt.devfileData)
if !tt.wantErr && err != nil {
t.Errorf("unexpected error: %v", err)
}

})
}

}
7 changes: 6 additions & 1 deletion pkg/odo/cli/component/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/openshift/odo/pkg/devfile"
"github.com/openshift/odo/pkg/devfile/parser"
"github.com/openshift/odo/pkg/devfile/parser/data/common"
"github.com/openshift/odo/pkg/devfile/validate"
"github.com/openshift/odo/pkg/envinfo"
"github.com/openshift/odo/pkg/kclient"
"github.com/openshift/odo/pkg/log"
Expand Down Expand Up @@ -835,13 +836,13 @@ func (co *CreateOptions) Validate() (err error) {
if err != nil {
return err
}

// Only validate namespace if pushtarget isn't docker
if !pushtarget.IsPushTargetDocker() {
err := util.ValidateK8sResourceName("component namespace", co.devfileMetadata.componentNamespace)
if err != nil {
return err
}

}

spinner.End(true)
Expand Down Expand Up @@ -1079,6 +1080,10 @@ func (co *CreateOptions) devfileRun() (err error) {
if err != nil {
return errors.Wrap(err, "unable to parse devfile")
}
err = validate.ValidateDevfileData(devObj.Data)
if err != nil {
return err
}

err = co.downloadStarterProject(devObj, co.devfileMetadata.starter, co.interactive)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/devfile/cmd_devfile_env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
var _ = Describe("odo devfile env command tests", func() {
const (
testName = "testname"
testProject = "testProject"
testProject = "testproject"
testDebugPort = "8888"
fakeParameter = "fakeParameter"
)
Expand Down