Skip to content

Commit

Permalink
Added changes to support container port information while creating/up…
Browse files Browse the repository at this point in the history
…dating service
  • Loading branch information
savitaashture committed Jun 18, 2019
1 parent 8a1d412 commit a97594e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion hack/generate-docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ func main() {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
}
8 changes: 8 additions & 0 deletions pkg/kn/commands/service/configuration_edit_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type ConfigurationEditFlags struct {
MaxScale int
ConcurrencyTarget int
ConcurrencyLimit int
Port int32
}

type ResourceFlags struct {
Expand All @@ -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.")
}

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")

}

func (p *ConfigurationEditFlags) Apply(service *servingv1alpha1.Service, cmd *cobra.Command) error {
Expand Down Expand Up @@ -102,6 +105,11 @@ func (p *ConfigurationEditFlags) Apply(service *servingv1alpha1.Service, cmd *co
return err
}

err = servinglib.UpdateContainerPort(template, p.Port)
if err != nil {
return err
}

servinglib.UpdateConcurrencyConfiguration(template, p.MinScale, p.MaxScale, p.ConcurrencyTarget, p.ConcurrencyLimit)

return nil
Expand Down
4 changes: 4 additions & 0 deletions pkg/kn/commands/service/service_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 service 'mysvc' with port 80
# If no --port is given the default port 8080 is used
kn service create mysvc --port 80 --image dev.local/ns/image:latest
# 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
3 changes: 3 additions & 0 deletions pkg/kn/commands/service/service_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 'mysvc' with new port
kn service update mysvc --port 80
# Updates a service 'mysvc' with new requests and limits parameters
kn service update mysvc --requests-cpu 500m --limits-memory 1024Mi`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
Expand Down
20 changes: 20 additions & 0 deletions pkg/serving/config_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ func UpdateImage(template *servingv1alpha1.RevisionTemplateSpec, image string) e
return nil
}

// UpdateContainerPort updates container with a give port
func UpdateContainerPort(template *servingv1alpha1.RevisionTemplateSpec, port int32) error {
container, err := extractContainer(template)
if err != nil {
return err
}

if len(container.Ports) == 0 {
container.Ports = []corev1.ContainerPort{{
ContainerPort: port,
}}

} else {
// Just override the port because len of container is always 1
container.Ports[0].ContainerPort = port
}

return nil
}

func UpdateResources(template *servingv1alpha1.RevisionTemplateSpec, requestsResourceList corev1.ResourceList, limitsResourceList corev1.ResourceList) error {
container, err := extractContainer(template)
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions pkg/serving/config_changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,32 @@ func testUpdateEnvVarsModify(t *testing.T, revision *servingv1alpha1.RevisionTem
}
}

func TestUpdateContainerPort(t *testing.T) {
template, _ := getV1alpha1Config()
err := UpdateContainerPort(template, 8888)
if err != nil {
t.Fatal(err)
}
// Verify update is successful or not
checkPortUpdate(t, template, 8888)

// update template with container port info
template.Spec.Containers[0].Ports[0].ContainerPort = 9090
err = UpdateContainerPort(template, 80)
if err != nil {
t.Fatal(err)
}

// Verify that given port overrides the existing container port
checkPortUpdate(t, template, 80)
}

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

func TestUpdateEnvVarsBoth(t *testing.T) {
template, container := getV1alpha1RevisionTemplateWithOldFields()
testUpdateEnvVarsBoth(t, template, container)
Expand Down

0 comments on commit a97594e

Please sign in to comment.