Skip to content

Commit

Permalink
karmadactl support apply command
Browse files Browse the repository at this point in the history
Signed-off-by: carlory <baofa.fan@daocloud.io>
  • Loading branch information
carlory committed Jun 14, 2022
1 parent 5c3336e commit 2b5bd49
Show file tree
Hide file tree
Showing 30 changed files with 5,686 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/text v0.2.0 // indirect
Expand Down
160 changes: 160 additions & 0 deletions pkg/karmadactl/apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package karmadactl

import (
"fmt"
"strings"

"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/resource"
restclient "k8s.io/client-go/rest"
"k8s.io/kubectl/pkg/cmd/apply"
kcmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"

policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
"github.com/karmada-io/karmada/pkg/karmadactl/options"
)

type CommandApplyOptions struct {
// global flags
options.GlobalCommandOptions
}

var (
applyLong = templates.LongDesc(i18n.T(`
Apply a configuration to a resource by file name or stdin.
The resource name must be specified. This resource will be created if it doesn't exist yet.
To use 'apply', always create the resource initially with either 'apply' or 'create --save-config'.
JSON and YAML formats are accepted.
Alpha Disclaimer: the --prune functionality is not yet complete. Do not use unless you are aware of what the current state is. See https://issues.k8s.io/34274.`))

applyExample = templates.Examples(i18n.T(`
# Apply the configuration in pod.json to a pod
kubectl apply -f ./pod.json
# Apply resources from a directory containing kustomization.yaml - e.g. dir/kustomization.yaml
kubectl apply -k dir/
# Apply the JSON passed into stdin to a pod
cat pod.json | kubectl apply -f -
# Note: --prune is still in Alpha
# Apply the configuration in manifest.yaml that matches label app=nginx and delete all other resources that are not in the file and match label app=nginx
kubectl apply --prune -f manifest.yaml -l app=nginx
# Apply the configuration in manifest.yaml and delete all the other config maps that are not in the file
kubectl apply --prune -f manifest.yaml --all --prune-whitelist=core/v1/ConfigMap`))
)

// NewCmdApply creates the `apply` command
func NewCmdApply(karmadaConfig KarmadaConfig, parentCommand string) *cobra.Command {
o := &CommandApplyOptions{}
ioStreams := genericclioptions.IOStreams{In: getIn, Out: getOut, ErrOut: getErr}
flags := apply.NewApplyFlags(nil, ioStreams)
cmd := &cobra.Command{
Use: "apply (-f FILENAME | -k DIRECTORY)",
DisableFlagsInUseLine: true,
Short: i18n.T("Apply a configuration to a resource by file name or stdin"),
Long: applyLong,
Example: applyExample,
PreRunE: func(cmd *cobra.Command, args []string) error {
restConfig, err := karmadaConfig.GetRestConfig(o.KarmadaContext, o.KubeConfig)
if err != nil {
return err
}
kubeConfigFlags := NewConfigFlags(true).WithDeprecatedPasswordFlag()
kubeConfigFlags.WrapConfigFn = func(config *restclient.Config) *restclient.Config { return restConfig }
flags.Factory = kcmdutil.NewFactory(kubeConfigFlags)
return nil
},
Run: func(cmd *cobra.Command, args []string) {
o, err := flags.ToOptions(cmd, parentCommand, args)
kcmdutil.CheckErr(err)
o.PreProcessorFn = func() error {
return PreProcessorFn(flags.Factory, o)
}
kcmdutil.CheckErr(o.Validate(cmd, args))
kcmdutil.CheckErr(o.Run())
},
}
flags.AddFlags(cmd)
cmd.Flags().StringVar(&o.KubeConfig, "kubeconfig", "", "Path to the control plane kubeconfig file.")
cmd.Flags().StringVar(&o.KarmadaContext, "karmada-context", "", "Name of the cluster context in control plane kubeconfig file.")
return cmd
}

func PreProcessorFn(f kcmdutil.Factory, o *apply.ApplyOptions) error {
infos, err := o.GetObjects()
if err != nil {
return err
}

var results []*resource.Info
for _, info := range infos {
results = append(results, info)
spec := policyv1alpha1.PropagationSpec{
ResourceSelectors: []policyv1alpha1.ResourceSelector{
{
APIVersion: info.Mapping.GroupVersionKind.GroupVersion().String(),
Kind: info.Mapping.GroupVersionKind.Kind,
Name: info.Name,
Namespace: info.Namespace,
},
},
Placement: policyv1alpha1.Placement{
ClusterAffinity: &policyv1alpha1.ClusterAffinity{},
},
}
var obj runtime.Object
if info.Namespaced() {
obj = &policyv1alpha1.PropagationPolicy{
TypeMeta: metav1.TypeMeta{
APIVersion: "policy.karmada.io/v1alpha1",
Kind: "PropagationPolicy",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-%s", info.Name, strings.ToLower(info.Mapping.GroupVersionKind.Kind)),
Namespace: info.Namespace,
},
Spec: spec,
}
} else {
obj = &policyv1alpha1.ClusterPropagationPolicy{
TypeMeta: metav1.TypeMeta{
APIVersion: "policy.karmada.io/v1alpha1",
Kind: "ClusterPropagationPolicy",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-%s", info.Name, strings.ToLower(info.Mapping.GroupVersionKind.Kind)),
},
Spec: spec,
}
}
gvk := obj.GetObjectKind().GroupVersionKind()
mapping, err := o.Mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return fmt.Errorf("unable to recognize resource: %v", err)
}
client, err := f.ClientForMapping(mapping)
if err != nil {
return fmt.Errorf("unable to connect to a server to handle %q: %v", mapping.Resource, err)
}

ret := &resource.Info{
Namespace: info.Namespace,
Name: info.Name,
Object: obj,
Mapping: mapping,
Client: client,
}
results = append(results, ret)
}
o.SetObjects(results)
return nil
}
1 change: 1 addition & 0 deletions pkg/karmadactl/karmadactl.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func NewKarmadaCtlCommand(cmdUse, parentCommand string) *cobra.Command {
rootCmd.AddCommand(NewCmdCordon(karmadaConfig, parentCommand))
rootCmd.AddCommand(NewCmdUncordon(karmadaConfig, parentCommand))
rootCmd.AddCommand(NewCmdGet(karmadaConfig, parentCommand))
rootCmd.AddCommand(NewCmdApply(karmadaConfig, parentCommand))
rootCmd.AddCommand(NewCmdTaint(karmadaConfig, parentCommand))
rootCmd.AddCommand(NewCmdPromote(karmadaConfig, parentCommand))
rootCmd.AddCommand(NewCmdLogs(karmadaConfig, parentCommand))
Expand Down
12 changes: 12 additions & 0 deletions vendor/github.com/jonboulle/clockwork/.editorconfig

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

27 changes: 27 additions & 0 deletions vendor/github.com/jonboulle/clockwork/.gitignore

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

0 comments on commit 2b5bd49

Please sign in to comment.