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

knative#18
  • Loading branch information
Gong Zhang committed May 10, 2020
1 parent f0ebfab commit 59c9943
Show file tree
Hide file tree
Showing 191 changed files with 7,357 additions and 2,003 deletions.
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"
autoscaling "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 set - to set 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 59c9943

Please sign in to comment.