Skip to content

Commit

Permalink
Merge pull request #5463 from zhzhuang-zju/karmadactlpatch
Browse files Browse the repository at this point in the history
add new command patch
  • Loading branch information
karmada-bot authored Aug 30, 2024
2 parents 307fee9 + 8cb9f42 commit 704a873
Show file tree
Hide file tree
Showing 4 changed files with 417 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/karmadactl/karmadactl.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/karmada-io/karmada/pkg/karmadactl/label"
"github.com/karmada-io/karmada/pkg/karmadactl/logs"
"github.com/karmada-io/karmada/pkg/karmadactl/options"
"github.com/karmada-io/karmada/pkg/karmadactl/patch"
"github.com/karmada-io/karmada/pkg/karmadactl/promote"
"github.com/karmada-io/karmada/pkg/karmadactl/register"
"github.com/karmada-io/karmada/pkg/karmadactl/taint"
Expand Down Expand Up @@ -137,6 +138,7 @@ func NewKarmadaCtlCommand(cmdUse, parentCommand string) *cobra.Command {
apply.NewCmdApply(f, parentCommand, ioStreams),
promote.NewCmdPromote(f, parentCommand),
top.NewCmdTop(f, parentCommand, ioStreams),
patch.NewCmdPatch(f, parentCommand, ioStreams),
},
},
{
Expand Down
50 changes: 50 additions & 0 deletions pkg/karmadactl/patch/patch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2024 The Karmada 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 patch

import (
"fmt"

"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericiooptions"
kubectlpatch "k8s.io/kubectl/pkg/cmd/patch"
"k8s.io/kubectl/pkg/util/templates"

"github.com/karmada-io/karmada/pkg/karmadactl/util"
)

var (
patchExample = templates.Examples(`
# Partially update a deployment using a strategic merge patch, specifying the patch as JSON
[1]%s patch deployment nginx-deployment -p '{"spec":{"replicas":2}}'
# Partially update a deployment using a strategic merge patch, specifying the patch as YAML
[1]%s patch deployment nginx-deployment -p $'spec:\n replicas: 2'
# Partially update a deployment identified by the type and name specified in "deployment.json" using strategic merge patch
[1]%s patch -f deployment.json -p '{"spec":{"replicas":2}}'
# Update a propagationpolicy's conflictResolution using a JSON patch with positional arrays
[1]%s patch pp nginx-propagation --type='json' -p='[{"op": "replace", "path": "/spec/conflictResolution", "value":"Overwrite"}]'
# Update a deployment's replicas through the 'scale' subresource using a merge patch
[1]%s patch deployment nginx-deployment --subresource='scale' --type='merge' -p '{"spec":{"replicas":2}}'`)
)

// NewCmdPatch returns new initialized instance of patch sub command
func NewCmdPatch(f util.Factory, parentCommand string, ioStreams genericiooptions.IOStreams) *cobra.Command {
cmd := kubectlpatch.NewCmdPatch(f, ioStreams)
cmd.Example = fmt.Sprintf(patchExample, parentCommand)
return cmd
}
Loading

0 comments on commit 704a873

Please sign in to comment.