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 port name #915

Merged
merged 6 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/cmd/kn_service_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ kn service create NAME --image IMAGE
# Create a service with port 80
kn service create s2 --port 80 --image knativesamples/helloworld

# Create a service with port 80 and port name h2c
kn service create s2 --port h2c:80 --image knativesamples/helloworld

# Create or replace default resources of a service 's1' using --force flag
# (earlier configured resource requests and limits will be replaced with default)
# (earlier configured environment variables will be cleared too if any)
Expand Down Expand Up @@ -78,7 +81,7 @@ kn service create NAME --image IMAGE
--no-cluster-local Do not specify that the service be private. (--no-cluster-local will make the service publicly available) (default true)
--no-lock-to-digest Do not keep the running image for the service constant when not explicitly specifying the image. (--no-lock-to-digest pulls the image tag afresh with each new revision)
--no-wait Do not wait for 'service create' operation to be completed.
-p, --port int32 The port where application listens on.
-p, --port string The port where application listens on. (e.g., h2c:8080 or just 8080) where h2c is port name and 8080 is container port
itsmurugappan marked this conversation as resolved.
Show resolved Hide resolved
--pull-secret string Image pull secret to set. An empty argument ("") clears the pull secret. The referenced secret must exist in the service's namespace.
--request strings The resource requirement requests for this Service. For example, 'cpu=100m,memory=256Mi'. You can use this flag multiple times. To unset a resource request, append "-" to the resource name, e.g. '--request cpu-'.
--requests-cpu string DEPRECATED: please use --request instead. The requested CPU (e.g., 250m).
Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/kn_service_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ kn service update NAME
--no-cluster-local Do not specify that the service be private. (--no-cluster-local will make the service publicly available) (default true)
--no-lock-to-digest Do not keep the running image for the service constant when not explicitly specifying the image. (--no-lock-to-digest pulls the image tag afresh with each new revision)
--no-wait Do not wait for 'service update' operation to be completed.
-p, --port int32 The port where application listens on.
-p, --port string The port where application listens on. (e.g., h2c:8080 or just 8080) where h2c is port name and 8080 is container port
--pull-secret string Image pull secret to set. An empty argument ("") clears the pull secret. The referenced secret must exist in the service's namespace.
--request strings The resource requirement requests for this Service. For example, 'cpu=100m,memory=256Mi'. You can use this flag multiple times. To unset a resource request, append "-" to the resource name, e.g. '--request cpu-'.
--requests-cpu string DEPRECATED: please use --request instead. The requested CPU (e.g., 250m).
Expand Down
4 changes: 2 additions & 2 deletions pkg/kn/commands/service/configuration_edit_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type ConfigurationEditFlags struct {
ConcurrencyLimit int
ConcurrencyUtilization int
AutoscaleWindow string
Port int32
Port string
Labels []string
LabelsService []string
LabelsRevision []string
Expand Down Expand Up @@ -207,7 +207,7 @@ func (p *ConfigurationEditFlags) addSharedFlags(command *cobra.Command) {
"Percentage of concurrent requests utilization before scaling up.")
p.markFlagMakesRevision("concurrency-utilization")

command.Flags().Int32VarP(&p.Port, "port", "p", 0, "The port where application listens on.")
command.Flags().StringVarP(&p.Port, "port", "p", "", "The port where application listens on. (e.g., h2c:8080 or just 8080) where h2c is port name and 8080 is container port")
p.markFlagMakesRevision("port")

command.Flags().StringArrayVarP(&p.Labels, "label", "l", []string{},
Expand Down
3 changes: 3 additions & 0 deletions pkg/kn/commands/service/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ var create_example = `
# Create a service with port 80
kn service create s2 --port 80 --image knativesamples/helloworld

# Create a service with port 80 and port name h2c
kn service create s2 --port h2c:80 --image knativesamples/helloworld

# Create or replace default resources of a service 's1' using --force flag
# (earlier configured resource requests and limits will be replaced with default)
# (earlier configured environment variables will be cleared too if any)
Expand Down
18 changes: 15 additions & 3 deletions pkg/serving/config_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,26 @@ func UpdateContainerArg(template *servingv1.RevisionTemplateSpec, arg []string)
return nil
}

// UpdateContainerPort updates container with a give port
func UpdateContainerPort(template *servingv1.RevisionTemplateSpec, port int32) error {
// UpdateContainerPort updates container with a given name:port
func UpdateContainerPort(template *servingv1.RevisionTemplateSpec, port string) error {
container, err := ContainerOfRevisionTemplate(template)
if err != nil {
return err
}
var containerPort int
navidshaikh marked this conversation as resolved.
Show resolved Hide resolved
var name string

if strings.Contains(port, ":") {
itsmurugappan marked this conversation as resolved.
Show resolved Hide resolved
portDetails := strings.Split(port, ":")
itsmurugappan marked this conversation as resolved.
Show resolved Hide resolved
itsmurugappan marked this conversation as resolved.
Show resolved Hide resolved
name = portDetails[0]
itsmurugappan marked this conversation as resolved.
Show resolved Hide resolved
containerPort, _ = strconv.Atoi(portDetails[1])
} else {
containerPort, _ = strconv.Atoi(port)
itsmurugappan marked this conversation as resolved.
Show resolved Hide resolved
}

container.Ports = []corev1.ContainerPort{{
ContainerPort: port,
ContainerPort: int32(containerPort),
Name: name,
}}
return nil
}
Expand Down
13 changes: 8 additions & 5 deletions pkg/serving/config_changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,22 +285,25 @@ func TestUpdateContainerArg(t *testing.T) {

func TestUpdateContainerPort(t *testing.T) {
template, _ := getRevisionTemplate()
err := UpdateContainerPort(template, 8888)
err := UpdateContainerPort(template, "8888")
assert.NilError(t, err)
// Verify update is successful or not
checkPortUpdate(t, template, 8888)
checkPortUpdate(t, template, 8888, "")
// update template with container port info
template.Spec.Containers[0].Ports[0].ContainerPort = 9090
err = UpdateContainerPort(template, 80)
err = UpdateContainerPort(template, "h2c:80")
assert.NilError(t, err)
// Verify that given port overrides the existing container port
checkPortUpdate(t, template, 80)
checkPortUpdate(t, template, 80, "h2c")
}

func checkPortUpdate(t *testing.T, template *servingv1.RevisionTemplateSpec, port int32) {
func checkPortUpdate(t *testing.T, template *servingv1.RevisionTemplateSpec, port int32, name string) {
if template.Spec.Containers[0].Ports[0].ContainerPort != port {
t.Error("Failed to update the container port")
}
if template.Spec.Containers[0].Ports[0].Name != name {
t.Error("Failed to update the container port name")
}
}

func checkUserUpdate(t *testing.T, template *servingv1.RevisionTemplateSpec, user *int64) {
Expand Down