Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new command patch #5463

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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