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

Added changes to support container port information while creating/updating service #191

Merged
merged 1 commit into from
Jun 28, 2019

Conversation

savitaashture
Copy link
Contributor

Issue: #142

Description:
When someone writes application which listens on port other than 8080(default port in knative-serving) then deploy using cli(kn) then there is no way to specify the port.

Current:
Supported flags are

Flags:
      --concurrency-limit int    Hard Limit of concurrent requests to be processed by a single replica.
      --concurrency-target int   Recommendation for when to scale up based on the concurrent number of incoming request. Defaults to --concurrency-limit when given.
  -e, --env stringArray          Environment variable to set. NAME=value; you may provide this flag any number of times to set multiple environment variables.
      --force                    Create service forcefully, replaces existing service if any.
  -h, --help                     help for create
      --image string             Image to run.
      --limits-cpu string        The limits on the requested CPU (e.g., 1000m).
      --limits-memory string     The limits on the requested CPU (e.g., 1024Mi).
      --max-scale int            Maximal number of replicas.
      --min-scale int            Minimal number of replicas.
  -n, --namespace string         List the requested object(s) in given namespace.
      --requests-cpu string      The requested CPU (e.g., 250m).
      --requests-memory string   The requested CPU (e.g., 64Mi).

Expected:
Along with current flags it should support port flag so that user can specify port at which application is running.

ex: something like below

Flags:
   ...
  -p, --port int32               The port that the application should run. (default 8080)
   ...

Usage:

kn service create mysvc --port 80 --image dev.local/ns/image:latest
kn service update mysvc --port 80

@googlebot googlebot added the cla: yes Indicates the PR's author has signed the CLA. label Jun 18, 2019
@knative-prow-robot knative-prow-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jun 18, 2019
@knative-prow-robot
Copy link
Contributor

Hi @savitaashture. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@knative-prow-robot knative-prow-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jun 18, 2019
Copy link
Contributor

@rhuss rhuss left a comment

Choose a reason for hiding this comment

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

Thanks ! Beside some minor nit-picks, I would verify and assert that there is only one port and one container in the template's PodSpec (see comments).

@@ -33,6 +33,9 @@ func NewServiceUpdateCommand(p *commands.KnParams) *cobra.Command {
# Updates a service 'mysvc' with new environment variables
kn service update mysvc --env KEY1=VALUE1 --env KEY2=VALUE2

# Update a service with given port
Copy link
Contributor

Choose a reason for hiding this comment

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

# Update service "mysvc" to expose port 80

Be more specific, it's an example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Modified with proper comments

@@ -46,6 +46,10 @@ func NewServiceCreateCommand(p *commands.KnParams) *cobra.Command {
# Create or replace environment variables of service 's1' using --force flag
kn service create --force s1 --env KEY1=NEW_VALUE1 --env NEW_KEY2=NEW_VALUE2 --image dev.local/ns/image:v1

# Create a service with given port
# If port is not specified it will take 8080 as a default port
Copy link
Contributor

Choose a reason for hiding this comment

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

# Create service 'mysvc' with port 80
# If no --port is given the default port 8080 is used

I would be more specific and more concise in the documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Modified with proper comments

@@ -29,9 +29,9 @@ func main() {
if len(os.Args) > 1 {
dir = os.Args[1]
}
err := doc.GenMarkdownTree(rootCmd, dir + "/docs/cmd/")
err := doc.GenMarkdownTree(rootCmd, dir+"/docs/cmd/")
Copy link
Contributor

Choose a reason for hiding this comment

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

would revert that as it's not related to this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When i ran go fmt for complete client code its modified i have reverted back now.

}}

} else {
for i, _ := range container.Ports {
Copy link
Contributor

Choose a reason for hiding this comment

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

There should be only one. Please either assert that there is only one port (that is what Knative serving allows) and return an error or truncate to take only the first.

Tbh, I would just overwrite unconditionally any existing port.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Overwritten the existing port

t.Fatal(err)
}
// Verify update is successful or not
for p, _ := range template.Spec.Containers {
Copy link
Contributor

Choose a reason for hiding this comment

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

assert that there are only 1 container and 1 port. Then just assert on template.Spec.Containers[0].Ports[0]

Copy link
Contributor Author

@savitaashture savitaashture Jun 18, 2019

Choose a reason for hiding this comment

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

Modified in all the places

@@ -139,6 +139,43 @@ func testUpdateEnvVarsModify(t *testing.T, revision *servingv1alpha1.RevisionTem
}
}

func TestUpdateContainerPort(t *testing.T) {
template, _ := getV1alpha1Config()
err := UpdateContainerPort(template, 8080)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you use the default port for the update ? I think for clarities sake its better to use something different.

// update template with container port info
for i, _ := range template.Spec.Containers {
for j, _ := range template.Spec.Containers[i].Ports {
template.Spec.Containers[i].Ports[j].ContainerPort = 9090
Copy link
Contributor

Choose a reason for hiding this comment

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

there is only 1 container and 1 port. If there are more, than that's an error.

}

// Verify that given port overrides the existing container port
for p, _ := range template.Spec.Containers {
Copy link
Contributor

Choose a reason for hiding this comment

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

assert again, that there is only 1 container and 1 port after the update (you could put that into an extrac func as its needed twice)

@rhuss
Copy link
Contributor

rhuss commented Jun 18, 2019

/ok-to-test

@knative-prow-robot knative-prow-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jun 18, 2019
@rhuss rhuss self-assigned this Jun 18, 2019
@savitaashture savitaashture force-pushed the issue-142 branch 2 times, most recently from a97594e to 1bf28e6 Compare June 18, 2019 18:37
return err
}

if len(container.Ports) == 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

@savitaashture : I think @rhuss mentioned to worry about the len of container ports, just replace it with container.Ports[0].ContainerPort = port

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@navidshaikh this condition i have added because while executing
kn service create --image imagename --port 80
during that time contanier.Ports is nil so iam adding that condition to check and initialize the container.Ports.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would just then unconditionally set

container.Ports = []corev1.ContainerPort{{
			ContainerPort: port,
		}}

in any case. This makes the code simple to read, and we are sure that only 1 port is set (although as mentioned the slice should have only one entry)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

@@ -102,6 +105,11 @@ func (p *ConfigurationEditFlags) Apply(service *servingv1alpha1.Service, cmd *co
return err
}

err = servinglib.UpdateContainerPort(template, p.Port)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Say I have configured my service to run on port 9090, now I want to update my service with an environment variable, this Apply gets called, irrespective of user intent to update the port, this will update the container port to default one (8080).

invoke this operation by flag check
if cmd.Flag().Changed('port') (as done for image above)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

@@ -54,12 +55,14 @@ func (p *ConfigurationEditFlags) AddUpdateFlags(command *cobra.Command) {
command.Flags().IntVar(&p.MaxScale, "max-scale", 0, "Maximal number of replicas.")
command.Flags().IntVar(&p.ConcurrencyTarget, "concurrency-target", 0, "Recommendation for when to scale up based on the concurrent number of incoming request. Defaults to --concurrency-limit when given.")
command.Flags().IntVar(&p.ConcurrencyLimit, "concurrency-limit", 0, "Hard Limit of concurrent requests to be processed by a single replica.")
command.Flags().Int32VarP(&p.Port, "port", "p", 8080, "The port that the application should run.")
Copy link
Collaborator

Choose a reason for hiding this comment

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

*The port where application listens on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

modified

@savitaashture savitaashture force-pushed the issue-142 branch 2 times, most recently from a359585 to 911d1ca Compare June 20, 2019 16:01
}

func (p *ConfigurationEditFlags) AddCreateFlags(command *cobra.Command) {
p.AddUpdateFlags(command)
command.Flags().BoolVar(&p.ForceCreate, "force", false, "Create service forcefully, replaces existing service if any.")
command.MarkFlagRequired("image")

Copy link
Contributor

Choose a reason for hiding this comment

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

why this extra line?

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 by mistake :) removed now.
Thanku

Copy link
Contributor

@maximilien maximilien left a comment

Choose a reason for hiding this comment

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

Nice job overall. Needed and welcome addition to kn.

Did you consider also adding to the e2e test suite? Would be easy to change one of the created services update its port. Thanks.

@savitaashture
Copy link
Contributor Author

Nice job overall. Needed and welcome addition to kn.

Did you consider also adding to the e2e test suite? Would be easy to change one of the created services update its port. Thanks.

@maximilien Thank you
I will add e2e testcases.

@rhuss
Copy link
Contributor

rhuss commented Jun 21, 2019

Build error is the same as in #192 (comment) so let's wait until this if fixed and retest here again.

@savitaashture savitaashture force-pushed the issue-142 branch 2 times, most recently from 93617ae to 3512d0c Compare June 24, 2019 17:07
@@ -102,6 +104,13 @@ func (p *ConfigurationEditFlags) Apply(service *servingv1alpha1.Service, cmd *co
return err
}

if cmd.Flags().Changed("port") {

Choose a reason for hiding this comment

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

i think it's a bit weird for us to not set container port if we are going to specify it as a default in the above flag definition.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right! Apply gets called for create as well update both.

@savitaashture: We'll probably need to identify the command in Apply definition.
cmd.Name() returns the command name, we can update this check to not set the port when cmd.Name() == "update" && !cmd.Flags().Changed("port") and set for all other cases.

WDYT ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@cppforlife:

i think it's a bit weird for us to not set container port if we are going to specify it as a default in the above flag definition.

Do you mean, we should update the port value to default one for service update, even if user doesnt specify -p port for service update command ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@savitaashture : or we just remove the default value of port and the mentioned conditional should suffice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes we can just remove default port value 8080 from the flag and if cmd.Flags().Changed("port") { condition will work for all the scenario.

Choose a reason for hiding this comment

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

i think it's best to rely on the server default and not specify default in the cli (shows up in -h). if user does specify the port configuration then it should be applied.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes i have removed saying default value from (shows up in -h) and if user specify it will be applied.

@csantanapr
Copy link
Member

Glad to see that this is being added. I just hit this problem where the container I want to use doesn't listen on port 8080

@savitaashture savitaashture force-pushed the issue-142 branch 3 times, most recently from e3d3cfd to 1134b3e Compare June 26, 2019 12:06
@navidshaikh
Copy link
Collaborator

/test pull-knative-client-integration-tests

Copy link
Contributor

@rhuss rhuss left a comment

Choose a reason for hiding this comment

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

Except for some minor feature request the PR lgtm.

@knative-metrics-robot
Copy link

The following is the coverage report on pkg/.
Say /test pull-knative-client-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/kn/commands/service/configuration_edit_flags.go 82.4% 78.6% -3.8
pkg/serving/config_changes.go 66.1% 67.2% 1.0

Copy link
Contributor

@rhuss rhuss left a comment

Choose a reason for hiding this comment

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

👍

@knative-prow-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: rhuss, savitaashture

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow-robot knative-prow-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 27, 2019
@rhuss
Copy link
Contributor

rhuss commented Jun 27, 2019

@savitaashture Thanks a lot, looks good to me. I'm on the integration-test issue, and if this is resolved (it unrelated to this PR), lets merge it.

@savitaashture
Copy link
Contributor Author

@rhuss Thank you :)

@rhuss
Copy link
Contributor

rhuss commented Jun 28, 2019

/retest pull-knative-client-integration-tests

@rhuss
Copy link
Contributor

rhuss commented Jun 28, 2019

/retest

@rhuss
Copy link
Contributor

rhuss commented Jun 28, 2019

/lgtm

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Jun 28, 2019
@knative-prow-robot knative-prow-robot merged commit 4cc5c84 into knative:master Jun 28, 2019
maximilien pushed a commit to maximilien/client that referenced this pull request Jul 1, 2019
dsimansk added a commit to dsimansk/client that referenced this pull request Jul 3, 2023
* [release-v1.9] Add kn-plugin-source-kafka v1.9.1

* Update vendor dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cla: yes Indicates the PR's author has signed the CLA. lgtm Indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants