Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

Commit

Permalink
Added utils to update configmap
Browse files Browse the repository at this point in the history
- Added utils to update configmp
- Chores to refine vars or logs
  • Loading branch information
Gong Zhang committed Jun 11, 2020
1 parent edd53ce commit 2f2cda2
Show file tree
Hide file tree
Showing 18 changed files with 876 additions and 113 deletions.
4 changes: 2 additions & 2 deletions plugins/admin/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ Use "kn admin autoscaling [command] --help" for more information about a comman
====
----
$ kn admin domain set --custom-domain mydomain.com
Updated Knative route domain mydomain.com
Set Knative route domain mydomain.com
----
====

.Update a custom domain with --selector and Service with a label app=v1 will use test.com
====
----
$ kn admin domain set --custom-domain test.com --selector app=v1
Updated Knative route domain test.com
Set Knative route domain test.com with selector [app=v1]
----
====

Expand Down
4 changes: 1 addition & 3 deletions plugins/admin/pkg/command/autoscaling/autoscaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ 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`,
Long: `Manage autoscaling provided by Knative Pod Autoscaler (KPA).`,
}
AutoscalingCmd.AddCommand(NewAutoscalingUpdateCommand(p))
AutoscalingCmd.InitDefaultHelpFlag()
Expand Down
49 changes: 24 additions & 25 deletions plugins/admin/pkg/command/autoscaling/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ package autoscaling
import (
"errors"
"fmt"

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

"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
scaleToZero bool
enableScaleToZero = "enable-scale-to-zero"
knativeServing = "knative-serving"
configAutoscaler = "config-autoscaler"
Expand All @@ -38,49 +40,46 @@ var (
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
`,
Short: "Update autoscaling config",
Long: `Update autoscaling config provided by Knative Pod Autoscaler (KPA)`,
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
},
RunE: func(cmd *cobra.Command, args []string) error {
var scaleToZero string
var desiredScaleToZero string
if cmd.Flags().Changed("scale-to-zero") {
scaleToZero = "true"
desiredScaleToZero = "true"
} else if cmd.Flags().Changed("no-scale-to-zero") {
scaleToZero = "false"
desiredScaleToZero = "false"
}

currentCm := &corev1.ConfigMap{}
currentCm, err := p.ClientSet.CoreV1().ConfigMaps(knativeServing).Get(configAutoscaler, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("Failed to get ConfigMaps: %+v", err)
return fmt.Errorf("failed to get ConfigMaps: %+v", err)
}
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 {
return fmt.Errorf("Failed to update ConfigMaps: %+v", err)
}
cmd.Printf("Updated Knative autoscaling config %s: %s\n", enableScaleToZero, scaleToZero)
} else {
cmd.Printf("Knative autoscaling config %s: %s not changed\n", enableScaleToZero, currentCm.Data[enableScaleToZero])
desiredCm.Data[enableScaleToZero] = desiredScaleToZero

err = utils.UpdateConfigMap(p.ClientSet, desiredCm)
if err != nil {
return fmt.Errorf("failed to update ConfigMap %s in namespace %s: %+v", configAutoscaler, knativeServing, err)
}
cmd.Printf("Updated Knative autoscaling config %s: %s\n", enableScaleToZero, desiredScaleToZero)

return nil
},
}

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

return AutoscalingUpdateCommand
Expand Down
3 changes: 1 addition & 2 deletions plugins/admin/pkg/command/autoscaling/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestNewAsUpdateSetCommand(t *testing.T) {
}
cmd := NewAutoscalingUpdateCommand(&p)
_, err := testutil.ExecuteCommand(cmd, "--scale-to-zero")
assert.ErrorContains(t, err, "Failed to get ConfigMaps", err)
assert.ErrorContains(t, err, "failed to get ConfigMaps", err)
})

t.Run("enable scale-to-zero successfully", func(t *testing.T) {
Expand Down Expand Up @@ -131,4 +131,3 @@ func TestNewAsUpdateSetCommand(t *testing.T) {

})
}

6 changes: 1 addition & 5 deletions plugins/admin/pkg/command/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ func NewDomainCmd(p *pkg.AdminParams) *cobra.Command {
var domainCmd = &cobra.Command{
Use: "domain",
Short: "Manage route domain",
Long: `Manage default route domain or custom route domain for Service with selectors. For example:
kn admin domain set - to set Knative route domain
kn admin domain unset - to unset Knative route domain`,
Long: `Manage default route domain or custom route domain for Service with selectors.`,
}
domainCmd.AddCommand(NewDomainSetCommand(p))
domainCmd.AddCommand(NewDomainUnSetCommand(p))
Expand Down
48 changes: 27 additions & 21 deletions plugins/admin/pkg/command/domain/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,34 @@ import (
"fmt"
"strings"

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

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

var selector []string
var domain string
var (
selector []string
domain string
knativeServing = "knative-serving"
configDomain = "config-domain"
)

// NewDomainSetCommand return the command to set knative custom domain
func NewDomainSetCommand(p *pkg.AdminParams) *cobra.Command {
domainSetCommand := &cobra.Command{
Use: "set",
Short: "set route domain",
Long: `Set Knative route domain for service
For example:
# To set a default route domain
kn admin domain set --custom-domain mydomain.com
# To set a route domain for service having label app=v1
kn admin domain set --custom-domain mydomain.com --selector app=v1
`,
Short: "Set route domain",
Long: `Set Knative route domain for service`,
Example:`
# To set a default route domain
kn admin domain set --custom-domain mydomain.com
# To set a route domain for service having label app=v1
kn admin domain set --custom-domain mydomain.com --selector app=v1`,
PreRunE: func(cmd *cobra.Command, args []string) error {
domain = strings.TrimSpace(domain)
if domain == "" {
Expand All @@ -52,9 +56,9 @@ kn admin domain set --custom-domain mydomain.com --selector app=v1
},
RunE: func(cmd *cobra.Command, args []string) error {
currentCm := &corev1.ConfigMap{}
currentCm, err := p.ClientSet.CoreV1().ConfigMaps("knative-serving").Get("config-domain", metav1.GetOptions{})
currentCm, err := p.ClientSet.CoreV1().ConfigMaps(knativeServing).Get(configDomain, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to get configmaps: %+v", err)
return fmt.Errorf("failed to get ConfigMap %s in namespace %s: %+v", configDomain, knativeServing, err)
}
desiredCm := currentCm.DeepCopy()
labels := "selector:\n"
Expand All @@ -79,14 +83,16 @@ kn admin domain set --custom-domain mydomain.com --selector app=v1
}

desiredCm.Data[domain] = value
if !equality.Semantic.DeepEqual(desiredCm.Data, currentCm.Data) {
_, err = p.ClientSet.CoreV1().ConfigMaps("knative-serving").Update(desiredCm)
if err != nil {
return fmt.Errorf("Failed to update ConfigMaps: %+v", err)
}
cmd.Printf("Updated knative route domain to %q\n", domain)

err = utils.UpdateConfigMap(p.ClientSet, desiredCm)
if err != nil {
return fmt.Errorf("failed to update ConfigMap %s in namespace %s: %+v", configDomain, knativeServing, err)
}

if value == "" {
cmd.Printf("Set knative route domain %q\n", domain)
} else {
cmd.Printf("Knative route domain %q not changed\n", domain)
cmd.Printf("Set knative route domain %q with selector %+v\n", domain, selector)
}
return nil
},
Expand Down
34 changes: 17 additions & 17 deletions plugins/admin/pkg/command/domain/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func TestNewDomainSetCommand(t *testing.T) {
t.Run("incompleted args", func(t *testing.T) {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "config-domain",
Namespace: "knative-serving",
Name: configDomain,
Namespace: knativeServing,
},
Data: make(map[string]string),
}
Expand All @@ -71,14 +71,14 @@ func TestNewDomainSetCommand(t *testing.T) {
}
cmd := NewDomainSetCommand(&p)
_, err := testutil.ExecuteCommand(cmd, "--custom-domain", "dummy.domain")
assert.ErrorContains(t, err, "failed to get configmaps", err)
assert.ErrorContains(t, err, "failed to get ConfigMap", err)
})

t.Run("setting domain config without selector", func(t *testing.T) {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "config-domain",
Namespace: "knative-serving",
Name: configDomain,
Namespace: knativeServing,
},
Data: make(map[string]string),
}
Expand All @@ -90,7 +90,7 @@ func TestNewDomainSetCommand(t *testing.T) {
_, err := testutil.ExecuteCommand(cmd, "--custom-domain", "dummy.domain")
assert.NilError(t, err)

cm, err = client.CoreV1().ConfigMaps("knative-serving").Get("config-domain", metav1.GetOptions{})
cm, err = client.CoreV1().ConfigMaps(knativeServing).Get(configDomain, metav1.GetOptions{})
assert.NilError(t, err)
assert.Check(t, len(cm.Data) == 1, "expected configmap lengh to be 1")

Expand All @@ -102,8 +102,8 @@ func TestNewDomainSetCommand(t *testing.T) {
t.Run("setting domain config with unchanged value", func(t *testing.T) {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "config-domain",
Namespace: "knative-serving",
Name: configDomain,
Namespace: knativeServing,
},
Data: map[string]string{
"dummy.domain": "",
Expand All @@ -118,7 +118,7 @@ func TestNewDomainSetCommand(t *testing.T) {
_, err := testutil.ExecuteCommand(cmd, "--custom-domain", "dummy.domain")
assert.NilError(t, err)

updated, err := client.CoreV1().ConfigMaps("knative-serving").Get("config-domain", metav1.GetOptions{})
updated, err := client.CoreV1().ConfigMaps(knativeServing).Get(configDomain, metav1.GetOptions{})
assert.NilError(t, err)
assert.Check(t, equality.Semantic.DeepEqual(updated, cm), "configmap should not changed")

Expand All @@ -127,8 +127,8 @@ func TestNewDomainSetCommand(t *testing.T) {
t.Run("adding domain config without selector with existing domain configuration", func(t *testing.T) {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "config-domain",
Namespace: "knative-serving",
Name: configDomain,
Namespace: knativeServing,
},
Data: map[string]string{
"foo.bar": "",
Expand All @@ -141,9 +141,9 @@ func TestNewDomainSetCommand(t *testing.T) {
cmd := NewDomainSetCommand(&p)
o, err := testutil.ExecuteCommand(cmd, "--custom-domain", "dummy.domain")
assert.NilError(t, err)
assert.Check(t, strings.Contains(o, "Updated knative route domain to"), "expected update information in standard output")
assert.Check(t, strings.Contains(o, "Set knative route domain \"dummy.domain\""), "expected update information in standard output")

cm, err = client.CoreV1().ConfigMaps("knative-serving").Get("config-domain", metav1.GetOptions{})
cm, err = client.CoreV1().ConfigMaps(knativeServing).Get(configDomain, metav1.GetOptions{})
assert.NilError(t, err)
assert.Check(t, len(cm.Data) == 1, "expected configmap lengh to be 1, actual %d", len(cm.Data))

Expand All @@ -155,8 +155,8 @@ func TestNewDomainSetCommand(t *testing.T) {
t.Run("adding domain config with selector", func(t *testing.T) {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "config-domain",
Namespace: "knative-serving",
Name: configDomain,
Namespace: knativeServing,
},
Data: map[string]string{
"foo.bar": "",
Expand All @@ -170,9 +170,9 @@ func TestNewDomainSetCommand(t *testing.T) {

o, err := testutil.ExecuteCommand(cmd, "--custom-domain", "dummy.domain", "--selector", "app=dummy")
assert.NilError(t, err)
assert.Check(t, strings.Contains(o, "Updated knative route domain to"), "invalid output %q", o)
assert.Check(t, strings.Contains(o, "Set knative route domain \"dummy.domain\" with selector [app=dummy]"), "invalid output %q", o)

cm, err = client.CoreV1().ConfigMaps("knative-serving").Get("config-domain", metav1.GetOptions{})
cm, err = client.CoreV1().ConfigMaps(knativeServing).Get(configDomain, metav1.GetOptions{})
assert.NilError(t, err)
assert.Check(t, len(cm.Data) == 2, "expected configmap lengh to be 2, actual %d", len(cm.Data))

Expand Down
21 changes: 11 additions & 10 deletions plugins/admin/pkg/command/domain/unset.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"errors"
"fmt"

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

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

"github.com/spf13/cobra"
Expand All @@ -29,13 +31,11 @@ import (
func NewDomainUnSetCommand(p *pkg.AdminParams) *cobra.Command {
domainUnSetCommand := &cobra.Command{
Use: "unset",
Short: "unset route domain",
Long: `unset Knative route domain for service
For example:
# To unset a route domain
kn admin domain unset --custom-domain mydomain.com
`,
Short: "Unset route domain",
Long: `Unset Knative route domain for service`,
Example: `
# To unset a route domain
kn admin domain unset --custom-domain mydomain.com`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if domain == "" {
return errors.New("'domain unset' requires the route name to run provided with the --custom-domain option")
Expand All @@ -44,7 +44,7 @@ kn admin domain unset --custom-domain mydomain.com
},
RunE: func(cmd *cobra.Command, args []string) error {
currentCm := &corev1.ConfigMap{}
currentCm, err := p.ClientSet.CoreV1().ConfigMaps("knative-serving").Get("config-domain", metav1.GetOptions{})
currentCm, err := p.ClientSet.CoreV1().ConfigMaps(knativeServing).Get(configDomain, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to get configmaps: %+v", err)
}
Expand All @@ -58,10 +58,11 @@ kn admin domain unset --custom-domain mydomain.com
return fmt.Errorf("Knative route domain %s not found\n", domain)
}

_, err = p.ClientSet.CoreV1().ConfigMaps("knative-serving").Update(desiredCm)
err = utils.UpdateConfigMap(p.ClientSet, desiredCm)
if err != nil {
return fmt.Errorf("Failed to update ConfigMaps: %+v", err)
return fmt.Errorf("failed to update ConfigMap %s in namespace %s: %+v", configDomain, knativeServing, err)
}

cmd.Printf("Unset Knative route domain %s\n", domain)
return nil
},
Expand Down
Loading

0 comments on commit 2f2cda2

Please sign in to comment.