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 Jul 11, 2022
1 parent d2bd9d9 commit 060ef45
Show file tree
Hide file tree
Showing 30 changed files with 5,640 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,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
213 changes: 213 additions & 0 deletions pkg/karmadactl/apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
package karmadactl

import (
"fmt"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/meta"
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"
kubectlapply "k8s.io/kubectl/pkg/cmd/apply"
kcmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/templates"

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

var metadataAccessor = meta.NewAccessor()

// CommandApplyOptions contains the input to the apply command.
type CommandApplyOptions struct {
// global flags
options.GlobalCommandOptions
// apply flags
KubectlApplyFlags *kubectlapply.ApplyFlags
Namespace string
AllClusters bool

kubectlApplyOptions *kubectlapply.ApplyOptions
}

var (
applyLong = templates.LongDesc(`
Apply a configuration to a resource by file name or stdin and propagate them into member clusters.
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.
Note: It implements the function of 'kubectl apply' by default.
If you want to propagate them into member clusters, please use 'kubectl apply --all-clusters'.`)

applyExample = templates.Examples(`
# Apply the configuration without propagation into member clusters. It acts as 'kubectl apply'.
%[1]s apply -f manifest.yaml
# Apply resources from a directory and propagate them into all member clusters.
%[1]s apply -f dir/ --all-clusters`)
)

// NewCommandApplyOptions returns an initialized CommandApplyOptions instance
func NewCommandApplyOptions() *CommandApplyOptions {
streams := genericclioptions.IOStreams{In: getIn, Out: getOut, ErrOut: getErr}
flags := kubectlapply.NewApplyFlags(nil, streams)
return &CommandApplyOptions{
KubectlApplyFlags: flags,
}
}

// NewCmdApply creates the `apply` command
func NewCmdApply(karmadaConfig KarmadaConfig, parentCommand string) *cobra.Command {
o := NewCommandApplyOptions()
cmd := &cobra.Command{
Use: "apply (-f FILENAME | -k DIRECTORY)",
DisableFlagsInUseLine: true,
Short: "Apply a configuration to a resource by file name or stdin and propagate them into member clusters",
Long: applyLong,
Example: fmt.Sprintf(applyExample, parentCommand),
Run: func(cmd *cobra.Command, args []string) {
kcmdutil.CheckErr(o.Complete(karmadaConfig, cmd, parentCommand, args))
kcmdutil.CheckErr(o.Validate(cmd, args))
kcmdutil.CheckErr(o.Run())
},
}

o.GlobalCommandOptions.AddFlags(cmd.Flags())
o.KubectlApplyFlags.AddFlags(cmd)
cmd.Flags().StringVarP(&o.Namespace, "namespace", "n", o.Namespace, "If present, the namespace scope for this CLI request")
cmd.Flags().BoolVarP(&o.AllClusters, "all-clusters", "", o.AllClusters, "If present, propagates a group of resources to all member clusters.")
return cmd
}

// Complete completes all the required options
func (o *CommandApplyOptions) Complete(karmadaConfig KarmadaConfig, cmd *cobra.Command, parentCommand string, args []string) error {
restConfig, err := karmadaConfig.GetRestConfig(o.KarmadaContext, o.KubeConfig)
if err != nil {
return err
}
kubeConfigFlags := NewConfigFlags(true).WithDeprecatedPasswordFlag()
kubeConfigFlags.Namespace = &o.Namespace
kubeConfigFlags.WrapConfigFn = func(config *restclient.Config) *restclient.Config { return restConfig }
o.KubectlApplyFlags.Factory = kcmdutil.NewFactory(kubeConfigFlags)
kubectlApplyOptions, err := o.KubectlApplyFlags.ToOptions(cmd, parentCommand, args)
if err != nil {
return err
}
o.kubectlApplyOptions = kubectlApplyOptions
return nil
}

// Validate verifies if CommandApplyOptions are valid and without conflicts.
func (o *CommandApplyOptions) Validate(cmd *cobra.Command, args []string) error {
return o.kubectlApplyOptions.Validate(cmd, args)
}

// Run executes the `apply` command.
func (o *CommandApplyOptions) Run() error {
if !o.AllClusters {
return o.kubectlApplyOptions.Run()
}

if err := o.generateAndInjectPolices(); err != nil {
return err
}

return o.kubectlApplyOptions.Run()
}

// generateAndInjectPolices generates and injects policies to the given resources.
// It returns an error if any of the policies cannot be generated.
func (o *CommandApplyOptions) generateAndInjectPolices() error {
// load the resources
infos, err := o.kubectlApplyOptions.GetObjects()
if err != nil {
return err
}

// generate policies and append them to the resources
var results []*resource.Info
for _, info := range infos {
results = append(results, info)
obj := o.generatePropagationObject(info)
gvk := obj.GetObjectKind().GroupVersionKind()
mapping, err := o.kubectlApplyOptions.Mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return fmt.Errorf("unable to recognize resource: %v", err)
}
client, err := o.KubectlApplyFlags.Factory.ClientForMapping(mapping)
if err != nil {
return fmt.Errorf("unable to connect to a server to handle %q: %v", mapping.Resource, err)
}
policyName, _ := metadataAccessor.Name(obj)
ret := &resource.Info{
Namespace: info.Namespace,
Name: policyName,
Object: obj,
Mapping: mapping,
Client: client,
}
results = append(results, ret)
}

// store the results object to be sequentially applied
o.kubectlApplyOptions.SetObjects(results)
return nil
}

// generatePropagationObject generates a propagation object for the given resource info.
// It takes the resource namespace, name and GVK as input to generate policy name.
// TODO(carlory): allow users to select one or many member clusters to propagate resources.
func (o *CommandApplyOptions) generatePropagationObject(info *resource.Info) runtime.Object {
gvk := info.Mapping.GroupVersionKind
spec := policyv1alpha1.PropagationSpec{
ResourceSelectors: []policyv1alpha1.ResourceSelector{
{
APIVersion: gvk.GroupVersion().String(),
Kind: gvk.Kind,
Name: info.Name,
Namespace: info.Namespace,
},
},
}

if o.AllClusters {
spec.Placement.ClusterAffinity = &policyv1alpha1.ClusterAffinity{}
}

// for a namespaced-scope resource, we need to generate a PropagationPolicy object.
// for a cluster-scope resource, we need to generate a ClusterPropagationPolicy object.
var obj runtime.Object
policyName := names.GeneratePolicyName(info.Namespace, info.Name, gvk.String())
if info.Namespaced() {
obj = &policyv1alpha1.PropagationPolicy{
TypeMeta: metav1.TypeMeta{
APIVersion: "policy.karmada.io/v1alpha1",
Kind: "PropagationPolicy",
},
ObjectMeta: metav1.ObjectMeta{
Name: policyName,
Namespace: info.Namespace,
},
Spec: spec,
}
} else {
obj = &policyv1alpha1.ClusterPropagationPolicy{
TypeMeta: metav1.TypeMeta{
APIVersion: "policy.karmada.io/v1alpha1",
Kind: "ClusterPropagationPolicy",
},
ObjectMeta: metav1.ObjectMeta{
Name: policyName,
},
Spec: spec,
}
}
return obj
}
1 change: 1 addition & 0 deletions pkg/karmadactl/karmadactl.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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 060ef45

Please sign in to comment.