Skip to content

Commit b04d115

Browse files
add port name (#915)
* add port name * add port name * review comments * review comments * add unit tests for port changes * review comments for port spec changes
1 parent 1fd2844 commit b04d115

File tree

6 files changed

+90
-30
lines changed

6 files changed

+90
-30
lines changed

docs/cmd/kn_service_create.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ kn service create NAME --image IMAGE
2727
# Create or replace environment variables of service 's1' using --force flag
2828
kn service create --force s1 --env TARGET=force --env FROM=examples --image knativesamples/helloworld
2929
30-
# Create a service with port 80
31-
kn service create s2 --port 80 --image knativesamples/helloworld
30+
# Create a service with port 8080
31+
kn service create s2 --port 8080 --image knativesamples/helloworld
32+
33+
# Create a service with port 8080 and port name h2c
34+
kn service create s2 --port h2c:8080 --image knativesamples/helloworld
3235
3336
# Create or replace default resources of a service 's1' using --force flag
3437
# (earlier configured resource requests and limits will be replaced with default)
@@ -78,7 +81,7 @@ kn service create NAME --image IMAGE
7881
--no-cluster-local Do not specify that the service be private. (--no-cluster-local will make the service publicly available) (default true)
7982
--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)
8083
--no-wait Do not wait for 'service create' operation to be completed.
81-
-p, --port int32 The port where application listens on.
84+
-p, --port string The port where application listens on, in the format 'NAME:PORT', where 'NAME' is optional. Examples: '--port h2c:8080' , '--port 8080'.
8285
--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.
8386
--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-'.
8487
--requests-cpu string DEPRECATED: please use --request instead. The requested CPU (e.g., 250m).

docs/cmd/kn_service_update.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ kn service update NAME
6565
--no-cluster-local Do not specify that the service be private. (--no-cluster-local will make the service publicly available) (default true)
6666
--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)
6767
--no-wait Do not wait for 'service update' operation to be completed.
68-
-p, --port int32 The port where application listens on.
68+
-p, --port string The port where application listens on, in the format 'NAME:PORT', where 'NAME' is optional. Examples: '--port h2c:8080' , '--port 8080'.
6969
--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.
7070
--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-'.
7171
--requests-cpu string DEPRECATED: please use --request instead. The requested CPU (e.g., 250m).

pkg/kn/commands/service/configuration_edit_flags.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type ConfigurationEditFlags struct {
5151
ConcurrencyLimit int
5252
ConcurrencyUtilization int
5353
AutoscaleWindow string
54-
Port int32
54+
Port string
5555
Labels []string
5656
LabelsService []string
5757
LabelsRevision []string
@@ -207,7 +207,7 @@ func (p *ConfigurationEditFlags) addSharedFlags(command *cobra.Command) {
207207
"Percentage of concurrent requests utilization before scaling up.")
208208
p.markFlagMakesRevision("concurrency-utilization")
209209

210-
command.Flags().Int32VarP(&p.Port, "port", "p", 0, "The port where application listens on.")
210+
command.Flags().StringVarP(&p.Port, "port", "p", "", "The port where application listens on, in the format 'NAME:PORT', where 'NAME' is optional. Examples: '--port h2c:8080' , '--port 8080'.")
211211
p.markFlagMakesRevision("port")
212212

213213
command.Flags().StringArrayVarP(&p.Labels, "label", "l", []string{},

pkg/kn/commands/service/create.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ var create_example = `
4747
# Create or replace environment variables of service 's1' using --force flag
4848
kn service create --force s1 --env TARGET=force --env FROM=examples --image knativesamples/helloworld
4949
50-
# Create a service with port 80
51-
kn service create s2 --port 80 --image knativesamples/helloworld
50+
# Create a service with port 8080
51+
kn service create s2 --port 8080 --image knativesamples/helloworld
52+
53+
# Create a service with port 8080 and port name h2c
54+
kn service create s2 --port h2c:8080 --image knativesamples/helloworld
5255
5356
# Create or replace default resources of a service 's1' using --force flag
5457
# (earlier configured resource requests and limits will be replaced with default)

pkg/serving/config_changes.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ type VolumeSourceType int
4040
const (
4141
ConfigMapVolumeSourceType VolumeSourceType = iota
4242
SecretVolumeSourceType
43+
PortFormatErr = "The port specification '%s' is not valid. Please provide in the format 'NAME:PORT', where 'NAME' is optional. Examples: '--port h2c:8080' , '--port 8080'."
44+
)
45+
46+
var (
47+
UserImageAnnotationKey = "client.knative.dev/user-image"
48+
ApiTooOldError = errors.New("the service is using too old of an API format for the operation")
4349
)
4450

4551
func (vt VolumeSourceType) String() string {
@@ -50,8 +56,6 @@ func (vt VolumeSourceType) String() string {
5056
return names[vt]
5157
}
5258

53-
var UserImageAnnotationKey = "client.knative.dev/user-image"
54-
5559
// UpdateEnvVars gives the configuration all the env var values listed in the given map of
5660
// vars. Does not touch any environment variables not mentioned, but it can add
5761
// new env vars and change the values of existing ones, then sort by env key name.
@@ -236,8 +240,6 @@ func UpdateRevisionTemplateAnnotation(template *servingv1.RevisionTemplateSpec,
236240
return nil
237241
}
238242

239-
var ApiTooOldError = errors.New("the service is using too old of an API format for the operation")
240-
241243
// EnvToMap is an utility function to translate between the API list form of env vars, and the
242244
// more convenient map form.
243245
func EnvToMap(vars []corev1.EnvVar) (map[string]string, error) {
@@ -331,14 +333,34 @@ func UpdateContainerArg(template *servingv1.RevisionTemplateSpec, arg []string)
331333
return nil
332334
}
333335

334-
// UpdateContainerPort updates container with a give port
335-
func UpdateContainerPort(template *servingv1.RevisionTemplateSpec, port int32) error {
336+
// UpdateContainerPort updates container with a given name:port
337+
func UpdateContainerPort(template *servingv1.RevisionTemplateSpec, port string) error {
336338
container, err := ContainerOfRevisionTemplate(template)
337339
if err != nil {
338340
return err
339341
}
342+
343+
var containerPort int64
344+
var name string
345+
346+
elements := strings.SplitN(port, ":", 2)
347+
if len(elements) == 2 {
348+
name = elements[0]
349+
containerPort, err = strconv.ParseInt(elements[1], 10, 32)
350+
if err != nil {
351+
return fmt.Errorf(PortFormatErr, port)
352+
}
353+
} else {
354+
name = ""
355+
containerPort, err = strconv.ParseInt(elements[0], 10, 32)
356+
if err != nil {
357+
return fmt.Errorf(PortFormatErr, port)
358+
}
359+
}
360+
340361
container.Ports = []corev1.ContainerPort{{
341-
ContainerPort: port,
362+
ContainerPort: int32(containerPort),
363+
Name: name,
342364
}}
343365
return nil
344366
}

pkg/serving/config_changes_test.go

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package serving
1616

1717
import (
18+
"fmt"
1819
"reflect"
1920
"strconv"
2021
"testing"
@@ -285,21 +286,52 @@ func TestUpdateContainerArg(t *testing.T) {
285286

286287
func TestUpdateContainerPort(t *testing.T) {
287288
template, _ := getRevisionTemplate()
288-
err := UpdateContainerPort(template, 8888)
289-
assert.NilError(t, err)
290-
// Verify update is successful or not
291-
checkPortUpdate(t, template, 8888)
292-
// update template with container port info
293-
template.Spec.Containers[0].Ports[0].ContainerPort = 9090
294-
err = UpdateContainerPort(template, 80)
295-
assert.NilError(t, err)
296-
// Verify that given port overrides the existing container port
297-
checkPortUpdate(t, template, 80)
298-
}
299-
300-
func checkPortUpdate(t *testing.T, template *servingv1.RevisionTemplateSpec, port int32) {
301-
if template.Spec.Containers[0].Ports[0].ContainerPort != port {
302-
t.Error("Failed to update the container port")
289+
for _, tc := range []struct {
290+
name string
291+
input string
292+
isErr bool
293+
expPort int32
294+
expName string
295+
}{{
296+
name: "only port 8888",
297+
input: "8888",
298+
expPort: int32(8888),
299+
}, {
300+
name: "name and port h2c:8080",
301+
input: "h2c:8080",
302+
expPort: int32(8080),
303+
expName: "h2c",
304+
}, {
305+
name: "error case - not correct format",
306+
input: "h2c:800000000000000000",
307+
isErr: true,
308+
}, {
309+
name: "error case - empty port",
310+
input: "h2c:",
311+
isErr: true,
312+
}, {
313+
name: "error case - wrong format",
314+
input: "8080:h2c",
315+
isErr: true,
316+
}, {
317+
name: "error case - multiple :",
318+
input: "h2c:8080:proto",
319+
isErr: true,
320+
}, {
321+
name: "empty name no error",
322+
input: ":8888",
323+
expPort: int32(8888),
324+
}} {
325+
t.Run(tc.name, func(t *testing.T) {
326+
err := UpdateContainerPort(template, tc.input)
327+
if tc.isErr {
328+
assert.Error(t, err, fmt.Sprintf(PortFormatErr, tc.input))
329+
} else {
330+
assert.NilError(t, err)
331+
assert.Equal(t, template.Spec.Containers[0].Ports[0].ContainerPort, tc.expPort)
332+
assert.Equal(t, template.Spec.Containers[0].Ports[0].Name, tc.expName)
333+
}
334+
})
303335
}
304336
}
305337

0 commit comments

Comments
 (0)