-
Notifications
You must be signed in to change notification settings - Fork 923
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add execute mode for karmadactl interpret subcommand
Signed-off-by: yingjinhui <yingjinhui@didiglobal.com>
- Loading branch information
1 parent
63a67d7
commit f70eafd
Showing
11 changed files
with
813 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,157 @@ | ||
package interpret | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v3" | ||
"k8s.io/cli-runtime/pkg/resource" | ||
"k8s.io/kubectl/pkg/cmd/util" | ||
|
||
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2" | ||
"github.com/karmada-io/karmada/pkg/karmadactl/util/genericresource" | ||
"github.com/karmada-io/karmada/pkg/resourceinterpreter/configurableinterpreter" | ||
) | ||
|
||
func (o *Options) completeExecute(_ util.Factory, _ *cobra.Command, _ []string) []error { | ||
return nil | ||
func (o *Options) completeExecute(f util.Factory, _ *cobra.Command, _ []string) []error { | ||
var errs []error | ||
if o.DesiredFile != "" { | ||
o.DesiredResult = f.NewBuilder(). | ||
Unstructured(). | ||
FilenameParam(false, &resource.FilenameOptions{Filenames: []string{o.DesiredFile}}). | ||
RequireObject(true). | ||
Local(). | ||
Do() | ||
errs = append(errs, o.DesiredResult.Err()) | ||
} | ||
|
||
if o.ObservedFile != "" { | ||
o.ObservedResult = f.NewBuilder(). | ||
Unstructured(). | ||
FilenameParam(false, &resource.FilenameOptions{Filenames: []string{o.ObservedFile}}). | ||
RequireObject(true). | ||
Local(). | ||
Do() | ||
errs = append(errs, o.ObservedResult.Err()) | ||
} | ||
|
||
if len(o.StatusFile) > 0 { | ||
o.StatusResult = genericresource.NewBuilder(). | ||
Constructor(func() interface{} { return &workv1alpha2.AggregatedStatusItem{} }). | ||
Filename(true, o.StatusFile). | ||
Do() | ||
errs = append(errs, o.StatusResult.Err()) | ||
} | ||
return errs | ||
} | ||
|
||
func (o *Options) runExecute() error { | ||
return fmt.Errorf("not implement") | ||
if o.Operation == "" { | ||
return fmt.Errorf("operation is not set for executing") | ||
} | ||
|
||
customizations, err := o.getCustomizationObject() | ||
if err != nil { | ||
return fmt.Errorf("fail to get customization object: %v", err) | ||
} | ||
|
||
desired, err := getUnstructuredObjectFromResult(o.DesiredResult) | ||
if err != nil { | ||
return fmt.Errorf("fail to get desired object: %v", err) | ||
} | ||
|
||
observed, err := getUnstructuredObjectFromResult(o.ObservedResult) | ||
if err != nil { | ||
return fmt.Errorf("fail to get observed object: %v", err) | ||
} | ||
|
||
status, err := o.getAggregatedStatusItems() | ||
if err != nil { | ||
return fmt.Errorf("fail to get status items: %v", err) | ||
} | ||
|
||
args := ruleArgs{ | ||
Desired: desired, | ||
Observed: observed, | ||
Status: status, | ||
Replica: int64(o.DesiredReplica), | ||
} | ||
|
||
interpreter := configurableinterpreter.NewConfigurableInterpreter(nil) | ||
interpreter.LoadConfig(customizations) | ||
|
||
r := o.Rules.GetByOperation(o.Operation) | ||
if r == nil { | ||
// Shall never occur, because we validate it before. | ||
return fmt.Errorf("operation %s is not supported. Use one of: %s", o.Operation, strings.Join(o.Rules.Names(), ", ")) | ||
} | ||
result := r.Run(interpreter, args) | ||
printExecuteResult(o.Out, o.ErrOut, r.Name(), result) | ||
return nil | ||
} | ||
|
||
func printExecuteResult(w, errOut io.Writer, name string, result *ruleResult) { | ||
if result.Err != nil { | ||
fmt.Fprintf(errOut, "Execute %s error: %v\n", name, result.Err) | ||
return | ||
} | ||
|
||
for i, res := range result.Results { | ||
func() { | ||
fmt.Fprintln(w, "---") | ||
fmt.Fprintf(w, "# [%v/%v] %s:\n", i+1, len(result.Results), res.Name) | ||
if err := printObjectYaml(w, res.Value); err != nil { | ||
fmt.Fprintf(errOut, "ERROR: %v\n", err) | ||
} | ||
}() | ||
} | ||
} | ||
|
||
// MarshalJSON doesn't work for yaml encoder, so unstructured.Unstructured and runtime.RawExtension objects | ||
// will be encoded into unexpected data. | ||
// Example1: | ||
// | ||
// &unstructured.Unstructured{ | ||
// Object: map[string]interface{}{ | ||
// "foo": "bar" | ||
// }, | ||
// } | ||
// | ||
// will be encoded into: | ||
// | ||
// Object: | ||
// foo: bar | ||
// | ||
// Example2: | ||
// | ||
// &runtime.RawExtension{ | ||
// Raw: []byte("{}"), | ||
// } | ||
// | ||
// will be encoded into: | ||
// | ||
// raw: | ||
// - 123 | ||
// - 125 | ||
// | ||
// Inspired from https://github.com/kubernetes/kubernetes/blob/8fb423bfabe0d53934cc94c154c7da2dc3ce1332/staging/src/k8s.io/kubectl/pkg/cmd/get/get.go#L781-L786 | ||
// we convert it to map[string]interface{} by json, then encode the converted object to yaml. | ||
func printObjectYaml(w io.Writer, obj interface{}) error { | ||
data, err := json.Marshal(obj) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var converted map[string]interface{} | ||
err = json.Unmarshal(data, &converted) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
encoder := yaml.NewEncoder(w) | ||
defer encoder.Close() | ||
return encoder.Encode(converted) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.