Skip to content

Commit

Permalink
Failing when port is specified with labels
Browse files Browse the repository at this point in the history
Resolves #522
Kompose will give FATAL error if labels are given but ports are not defined
  • Loading branch information
surajnarwade committed May 22, 2017
1 parent 1c64b01 commit 8a8ac81
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
6 changes: 5 additions & 1 deletion docs/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,8 @@ If the Docker Compose file has a volume specified for a service, the Deployment

If the Docker Compose file has service name with `_` in it (eg.`web_service`), then it will be replaced by `-` and the service name will be renamed accordingly (eg.`web-service`). Kompose does this because "Kubernetes" doesn't allow `_` in object name.

Please note that changing service name might break some `docker-compose` files.
Please note that changing service name might break some `docker-compose` files.

#### Warning about Labels

`kompose.service.type` label should be defined with `ports` only, otherwise `kompose` will fail.
11 changes: 11 additions & 0 deletions pkg/loader/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) {
}
}

err1 := checkLabelsPorts(len(serviceConfig.Port), composeServiceConfig.Labels["kompose.service.type"], name)
if err1 != nil {
return kobject.KomposeObject{}, errors.Wrap(err, err1.Error())
}
// convert compose labels to annotations
serviceConfig.Annotations = map[string]string(composeServiceConfig.Labels)
serviceConfig.CPUQuota = int64(composeServiceConfig.CPUQuota)
Expand Down Expand Up @@ -402,3 +406,10 @@ func handleServiceType(ServiceType string) (string, error) {
func normalizeServiceNames(svcName string) string {
return strings.Replace(svcName, "_", "-", -1)
}

func checkLabelsPorts(noOfPort int, labels string, svcName string) error {
if noOfPort == 0 && labels == "NodePort" || labels == "LoadBalancer" {
return errors.Errorf("%s defined in service %s with no ports present. Issues may occur when bringing up artifacts.", labels, svcName)
}
return nil
}
22 changes: 22 additions & 0 deletions pkg/loader/compose/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,25 @@ func TestNormalizeServiceNames(t *testing.T) {
}
}
}

func TestCheckLabelsPorts(t *testing.T) {
testCases := []struct {
name string
noOfPort int
labels string
svcName string
}{
{"ports is defined", 1, "NodePort", "foo"},
{"ports is not defined", 0, "NodePort", "foo"},
}

var err error
for _, testcase := range testCases {
t.Log(testcase.name)
err = checkLabelsPorts(testcase.noOfPort, testcase.labels, testcase.svcName)
if err != nil {
t.Log(err)
}

}
}

0 comments on commit 8a8ac81

Please sign in to comment.