Skip to content

Commit

Permalink
Add support to enable or disable scale-to-zero
Browse files Browse the repository at this point in the history
- Init autoscaling subcommand
- Support scale-to-zero config
- Update examples in README
knative#18
  • Loading branch information
Gong Zhang committed May 22, 2020
1 parent 909a4c3 commit a412735
Show file tree
Hide file tree
Showing 192 changed files with 7,398 additions and 2,009 deletions.
47 changes: 41 additions & 6 deletions plugins/admin/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@ A plugin of kn client to manage Knative for administrators.
For example:
kn admin domain set - to set Knative route domain
kn admin private-registry enable - to enable deployment from the private registry
kn admin autoscaling update - to manage autoscaling config
Usage:
kn admin [command]
Available Commands:
domain Manage route domain
help Help about any command
private-registry Manage private-registry
autoscaling Manage autoscaling config
domain Manage route domain
help Help about any command
registry Manage registry
version Prints the plugin version
Flags:
--config string config file (default is $HOME/.config/kn/plugins/kn-admin.yaml)
-h, --help help for admin
--config string config file (default is $HOME/.config/kn/plugins/admin.yaml)
-h, --help help for kn admin
-t, --toggle Help message for toggle
Use "admin [command] --help" for more information about a command.
Use "kn admin [command] --help" for more information about a command.
----

#### `kn admin domain`
Expand Down Expand Up @@ -87,6 +90,28 @@ Use "admin registry [command] --help" for more information about a command.
----

#### `kn admin autoscaling`

----
Manage autoscaling provided by Knative Pod Autoscaler (KPA). For example:
kn admin autoscaling update - to manage autoscaling config
Usage:
kn admin autoscaling [command]
Available Commands:
update update autoscaling config
Flags:
-h, --help help for autoscaling
Global Flags:
--config string config file (default is $HOME/.config/kn/plugins/admin.yaml)
Use "kn admin autoscaling [command] --help" for more information about a command.
----
### Examples

#### As a Knative administrator, I want to update Knative route domain with my custom domain.
Expand Down Expand Up @@ -121,3 +146,13 @@ $ kn admin registry add \
--docker-password=[PRIVATE_REGISTRY_PASSWORD]
-----
=====

#### As a Knative administrator, I want to enable scale-to-zero for autoscaling.

.Enable scale-to-zero for autoscaling.
=====
-----
$ kn admin autoscaling update --scale-to-zero
Updated Knative autoscaling config enable-scale-to-zero: true
-----
=====
3 changes: 3 additions & 0 deletions plugins/admin/core/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/spf13/viper"
"knative.dev/client-contrib/plugins/admin/pkg"
"knative.dev/client-contrib/plugins/admin/pkg/command"
"knative.dev/client-contrib/plugins/admin/pkg/command/autoscaling"
"knative.dev/client-contrib/plugins/admin/pkg/command/domain"
private_registry "knative.dev/client-contrib/plugins/admin/pkg/command/registry"
)
Expand All @@ -43,6 +44,7 @@ func NewAdminCommand(params ...pkg.AdminParams) *cobra.Command {
For example:
kn admin domain set - to set Knative route domain
kn admin registry add - to add registry with credentials
kn admin autoscaling update - to manage autoscaling config
`,
}
cobra.OnInitialize(initConfig)
Expand All @@ -51,6 +53,7 @@ kn admin registry add - to add registry with credentials
//
rootCmd.AddCommand(domain.NewDomainCmd(p))
rootCmd.AddCommand(private_registry.NewPrivateRegistryCmd(p))
rootCmd.AddCommand(autoscaling.NewAutoscalingCmd(p))
rootCmd.AddCommand(command.NewVersionCommand())

// Add default help page if there's unknown command
Expand Down
7 changes: 4 additions & 3 deletions plugins/admin/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ require (
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.6.2
gotest.tools v2.2.0+incompatible
k8s.io/api v0.17.3
k8s.io/apimachinery v0.17.3
k8s.io/client-go v0.17.0
k8s.io/api v0.17.4
k8s.io/apimachinery v0.17.4
k8s.io/client-go v0.17.4
knative.dev/client v0.14.0
)
264 changes: 260 additions & 4 deletions plugins/admin/go.sum

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions plugins/admin/pkg/command/autoscaling/autoscaling.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright © 2020 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package autoscaling

import (
"github.com/spf13/cobra"
"knative.dev/client-contrib/plugins/admin/pkg"
)

// domainCmd represents the domain command
func NewAutoscalingCmd(p *pkg.AdminParams) *cobra.Command {
var AutoscalingCmd = &cobra.Command{
Use: "autoscaling",
Short: "Manage autoscaling config",
Long: `Manage autoscaling provided by Knative Pod Autoscaler (KPA). For example:
kn admin autoscaling update - to manage autoscaling config`,
}
AutoscalingCmd.AddCommand(NewAutoscalingUpdateCommand(p))
AutoscalingCmd.InitDefaultHelpFlag()
return AutoscalingCmd
}
90 changes: 90 additions & 0 deletions plugins/admin/pkg/command/autoscaling/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright © 2020 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package autoscaling

import (
"errors"
"fmt"
"os"

"knative.dev/client-contrib/plugins/admin/pkg"

"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"

"knative.dev/client/pkg/kn/flags"
)

var (
ScaleToZero bool
enableScaleToZero = "enable-scale-to-zero"
knativeServing = "knative-serving"
configAutoscaler = "config-autoscaler"
)

func NewAutoscalingUpdateCommand(p *pkg.AdminParams) *cobra.Command {
AutoscalingUpdateCommand := &cobra.Command{
Use: "update",
Short: "update autoscaling config",
Long: `update autoscaling config provided by Knative Pod Autoscaler (KPA)
For example:
# To enable scale-to-zero
kn admin autoscaling update --scale-to-zero
# To disable scale-to-zero
kn admin autoscaling update --no-scale-to-zero
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if cmd.Flags().NFlag() == 0 {
return errors.New("'autoscaling update' requires flag(s)")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
var scaleToZero string
if cmd.Flags().Changed("scale-to-zero") {
scaleToZero = "true"
} else if cmd.Flags().Changed("no-scale-to-zero") {
scaleToZero = "false"
}
currentCm := &corev1.ConfigMap{}
currentCm, err := p.ClientSet.CoreV1().ConfigMaps(knativeServing).Get(configAutoscaler, metav1.GetOptions{})
if err != nil {
fmt.Println("Failed to get ConfigMaps:", err)
os.Exit(1)
}
desiredCm := currentCm.DeepCopy()
desiredCm.Data[enableScaleToZero] = scaleToZero
if !equality.Semantic.DeepEqual(desiredCm.Data[enableScaleToZero], currentCm.Data[enableScaleToZero]) {
_, err = p.ClientSet.CoreV1().ConfigMaps(knativeServing).Update(desiredCm)
if err != nil {
fmt.Println("Failed to update ConfigMaps:", err)
os.Exit(1)
}
fmt.Printf("Updated Knative autoscaling config %s: %s\n", enableScaleToZero, scaleToZero)
} else {
fmt.Printf("Knative autoscaling config %s: %s not changed\n", enableScaleToZero, currentCm.Data[enableScaleToZero])
}
},
}

flags.AddBothBoolFlagsUnhidden(AutoscalingUpdateCommand.Flags(), &ScaleToZero, "scale-to-zero", "", true,
"Enable scale-to-zero if set.")

return AutoscalingUpdateCommand
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a412735

Please sign in to comment.