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

Failing when port is specified with labels #590

Merged
merged 1 commit into from
Jun 15, 2017

Conversation

surajnarwade
Copy link
Contributor

Resolves #522
Kompose will give FATAL error if labels are given but ports are not defined

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label May 8, 2017
@@ -358,6 +358,12 @@ func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) {
}
}

if len(serviceConfig.Port) == 0 {
if composeServiceConfig.Labels["kompose.service.type"] == "NodePort" || composeServiceConfig.Labels["kompose.service.type"] == "LoadBalancer" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are there nested if's?

Shouldn't it be: if len(serviceConfig.Port) == 0 && composeServiceConfig.Labels["kompose.service.type"] == "NodePort" || composeServiceConfig.Labels["kompose.service.type"] == "LoadBalancer"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -358,6 +358,11 @@ func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) {
}
}

if len(serviceConfig.Port) == 0 && composeServiceConfig.Labels["kompose.service.type"] == "NodePort" || composeServiceConfig.Labels["kompose.service.type"] == "LoadBalancer" {
log.Fatal("Failed to proceed as Nodeport or loadbalancer is defined without ports")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was just thinking again (sorry for asking for another change) that this could be better:

log.Fatal("%s defined with no ports present. Issues may occur when bringing up artifacts.", composeServiceConfig.Labels["kompose.service.type"])

And also change this log to warning instead? What do you think @kadel ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log message is okay but giving warning instead of FATAL is of no use

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@surajnarwade I think it should still convert.. Let's wait for @kadel 's opinion 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @kadel made it clear that we should fail here #522 (comment)

yes, that makes sense, we should fail, as it doesn't make sense to use NodePort or LoadBalancer without port defined.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the messge though!

@@ -358,6 +358,11 @@ func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) {
}
}

if len(serviceConfig.Port) == 0 && composeServiceConfig.Labels["kompose.service.type"] == "NodePort" || composeServiceConfig.Labels["kompose.service.type"] == "LoadBalancer" {
log.Fatal("Failed to proceed as Nodeport or loadbalancer is defined without ports")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove blank line

@surajnarwade
Copy link
Contributor Author

cc @kadel @cdrage

@surajssd
Copy link
Member

@surajnarwade tests and docs update

@@ -358,6 +358,10 @@ func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) {
}
}

if len(serviceConfig.Port) == 0 && composeServiceConfig.Labels["kompose.service.type"] == "NodePort" || composeServiceConfig.Labels["kompose.service.type"] == "LoadBalancer" {
log.Fatalf("%s defined with no ports present. Issues may occur when bringing up artifacts.", composeServiceConfig.Labels["kompose.service.type"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this one does not tell which docker-compose service has that label and no port defined together?

@@ -358,6 +358,10 @@ func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) {
}
}

if len(serviceConfig.Port) == 0 && composeServiceConfig.Labels["kompose.service.type"] == "NodePort" || composeServiceConfig.Labels["kompose.service.type"] == "LoadBalancer" {
log.Fatalf("In %s service, %s defined with no ports present. Issues may occur when bringing up artifacts.", name, composeServiceConfig.Labels["kompose.service.type"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In %s service isn't grammatically correct.

My previous comment may work a bit better:

log.Fatal("%s defined in service %s with no ports present. Issues may occur when bringing up artifacts.", composeServiceConfig.Labels["kompose.service.type"], name)

@surajnarwade surajnarwade force-pushed the label-fix branch 2 times, most recently from a825926 to 8198246 Compare May 15, 2017 16:25
@surajnarwade
Copy link
Contributor Author

@cdrage @surajssd needs review

@cdrage
Copy link
Member

cdrage commented May 16, 2017

LGTM 👍

@surajssd
Copy link
Member

@surajnarwade tests and docs update

@gitlawr
Copy link
Contributor

gitlawr commented May 17, 2017

Would it be better if it return error here and leave the Fatalf() work to upper layer?
I'm investigating #464 and I think logrus.Fatalf(...) may be a broker because it finally invokes os.Exit()
Anyway, returning an error consists with other parts of the codes:
https://github.com/surajnarwade/kompose/blob/8198246bd8363642800941be75a2b9bf414ee9b5/pkg/loader/compose/compose.go#L352

@surajnarwade
Copy link
Contributor Author

@gitlawr , return error here, I didn't get you

@gitlawr
Copy link
Contributor

gitlawr commented May 17, 2017

@surajnarwade
I mean

return kobject.KomposeObject{}, errors.New("xxx defined in service xxx with no ports present. Issues may occur when bringing up artifacts.")

instead of

log.Fatalf(...)

@@ -359,6 +359,10 @@ func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) {
}
}

err = checkLabelsPorts(len(serviceConfig.Port), composeServiceConfig.Labels["kompose.service.type"], name)
if err != nil {
log.Fatal(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not use log.Fatal, instead wrap the error and pass it up the call stack. Use errors.Wrap

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what @containscafeine said ;-)


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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's wrap it using errors.Wrap to maintain consistency throughout the code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@surajnarwade if there is nothing to wrap, use errors.New()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@containscafeine , I have variable in my error message. so I used errors.Errorf() instead of errors.New()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change message a bit. It is not possible to create Headless service with type NodePort or LoadBalancer.
The Message should be more definite.
For example something in those lines:
"kompose.service.type can't be set if service doesn't expose any ports."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added message

@surajnarwade surajnarwade force-pushed the label-fix branch 3 times, most recently from 6e07ead to 8a8ac81 Compare May 22, 2017 07:12
@surajnarwade
Copy link
Contributor Author

@surajssd , needs review on this

@surajssd
Copy link
Member

@surajnarwade bear with me, also please add functional tests! so when I meant tests == (unit tests and functional tests)

@@ -363,6 +363,10 @@ func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) {
}
}

err1 := checkLabelsPorts(len(serviceConfig.Port), composeServiceConfig.Labels["kompose.service.type"], name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err1? that's not a good name for a variable. Please use err.

@@ -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())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's going on here? for errors.Wrap it should be `errors.Wrap(err, "Your error message here") or something along the lines.

@surajnarwade
Copy link
Contributor Author

@cdrage , @surajssd I have updated PR with changes and added tests as well

@@ -359,6 +359,10 @@ func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) {
}
}

err = checkLabelsPorts(len(serviceConfig.Port), composeServiceConfig.Labels["kompose.service.type"], name)
if err != nil {
log.Fatal(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what @containscafeine said ;-)


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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change message a bit. It is not possible to create Headless service with type NodePort or LoadBalancer.
The Message should be more definite.
For example something in those lines:
"kompose.service.type can't be set if service doesn't expose any ports."

@surajnarwade
Copy link
Contributor Author

@kadel @surajssd @containscafeine can you please test now


#### Warning about Labels

`kompose.service.type` label should be defined with `ports` only, otherwise `kompose` will fail.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be added under the labels section, not at the end of the user-guide file, otherwise, PR LGTM.

@surajnarwade surajnarwade force-pushed the label-fix branch 3 times, most recently from 642406a to 5ccc7cd Compare June 14, 2017 06:08
@surajnarwade
Copy link
Contributor Author

@cdrage review please :)

Copy link
Member

@surajssd surajssd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@@ -401,3 +402,5 @@ 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.


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra newlines, update PR, then go-ahead and merge!

Resolves kubernetes#522
Kompose will give FATAL error if labels are given but ports are not defined
@surajnarwade
Copy link
Contributor Author

All green now, @cdrage @surajssd

@surajssd surajssd merged commit f823a22 into kubernetes:master Jun 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants