mirror of
https://github.com/alibaba/higress.git
synced 2026-04-23 04:57:27 +08:00
Record the progress of the OSPP 2023 hgctl project (#453)
This commit is contained in:
30
pkg/cmd/hgctl/plugin/config/config.go
Normal file
30
pkg/cmd/hgctl/plugin/config/config.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright (c) 2022 Alibaba Group Holding Ltd.
|
||||
//
|
||||
// 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 config
|
||||
|
||||
import "github.com/spf13/cobra"
|
||||
|
||||
func NewCommand() *cobra.Command {
|
||||
configCmd := &cobra.Command{
|
||||
Use: "config",
|
||||
Aliases: []string{"conf", "cnf"},
|
||||
Short: "Configure the WasmPlugin manifest",
|
||||
}
|
||||
|
||||
configCmd.AddCommand(newCreateCommand())
|
||||
configCmd.AddCommand(newEditCommand())
|
||||
|
||||
return configCmd
|
||||
}
|
||||
76
pkg/cmd/hgctl/plugin/config/create.go
Normal file
76
pkg/cmd/hgctl/plugin/config/create.go
Normal file
@@ -0,0 +1,76 @@
|
||||
// Copyright (c) 2022 Alibaba Group Holding Ltd.
|
||||
//
|
||||
// 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 config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/alibaba/higress/pkg/cmd/hgctl/plugin/utils"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||
)
|
||||
|
||||
func newCreateCommand() *cobra.Command {
|
||||
var target string
|
||||
|
||||
createCmd := &cobra.Command{
|
||||
Use: "create",
|
||||
Aliases: []string{"c"},
|
||||
Short: "Create the WASM plugin configuration template file",
|
||||
Example: ` hgctl plugin config create`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
cmdutil.CheckErr(create(cmd.OutOrStdout(), target))
|
||||
},
|
||||
}
|
||||
|
||||
createCmd.PersistentFlags().StringVarP(&target, "target", "t", "./", "Directory where the configuration is generated")
|
||||
|
||||
return createCmd
|
||||
}
|
||||
|
||||
func create(w io.Writer, target string) error {
|
||||
target, err := utils.GetAbsolutePath(target)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "invalid target path")
|
||||
}
|
||||
if err = os.MkdirAll(target, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = GenPluginConfYAML(configHelpTmpl, target); err != nil {
|
||||
return errors.Wrap(err, "failed to create configuration template")
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "Created configuration template %q\n", fmt.Sprintf("%s/%s", target, "plugin-conf.yaml"))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var configHelpTmpl = &PluginConf{
|
||||
Name: "Plugin Name",
|
||||
Namespace: "higress-system",
|
||||
Title: "Display Name",
|
||||
Description: "Plugin Description",
|
||||
IconUrl: "Plugin Icon",
|
||||
Version: "0.1.0",
|
||||
Category: "auth | security | protocol | flow-control | flow-monitor | custom",
|
||||
Phase: "UNSPECIFIED_PHASE | AUTHN | AUTHZ | STATS",
|
||||
Priority: 0,
|
||||
Config: " Plugin Configuration",
|
||||
Url: "Plugin Image URL",
|
||||
}
|
||||
143
pkg/cmd/hgctl/plugin/config/edit.go
Normal file
143
pkg/cmd/hgctl/plugin/config/edit.go
Normal file
@@ -0,0 +1,143 @@
|
||||
// Copyright (c) 2022 Alibaba Group Holding Ltd.
|
||||
//
|
||||
// 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 config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
k8s "github.com/alibaba/higress/pkg/cmd/hgctl/kubernetes"
|
||||
"github.com/alibaba/higress/pkg/cmd/options"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
k8serr "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/util/yaml"
|
||||
"k8s.io/cli-runtime/pkg/printers"
|
||||
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||
"k8s.io/kubectl/pkg/cmd/util/editor"
|
||||
)
|
||||
|
||||
func newEditCommand() *cobra.Command {
|
||||
var name string
|
||||
|
||||
editCmd := &cobra.Command{
|
||||
Use: "edit",
|
||||
Aliases: []string{"e"},
|
||||
Short: "Edit the installed WASM plugin configuration",
|
||||
Example: ` # Edit the installed WASM plugin 'request-block'
|
||||
hgctl plugin config edit -p request-block
|
||||
`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
cmdutil.CheckErr(edit(cmd.OutOrStdout(), name))
|
||||
},
|
||||
}
|
||||
|
||||
flags := editCmd.PersistentFlags()
|
||||
options.AddKubeConfigFlags(flags)
|
||||
k8s.AddHigressNamespaceFlags(flags)
|
||||
flags.StringVarP(&name, "name", "p", "", "Name of the WASM plugin that needs to be edited")
|
||||
|
||||
return editCmd
|
||||
}
|
||||
|
||||
func edit(w io.Writer, name string) error {
|
||||
// TODO(WeixinX): Use WasmPlugin Object type instead of Unstructured
|
||||
dynCli, err := k8s.NewDynamicClient(options.DefaultConfigFlags.ToRawKubeConfigLoader())
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to build kubernetes dynamic client")
|
||||
}
|
||||
cli := k8s.NewWasmPluginClient(dynCli)
|
||||
|
||||
originalObj, err := cli.Get(context.TODO(), name)
|
||||
if err != nil {
|
||||
if k8serr.IsNotFound(err) {
|
||||
return errors.Errorf("wasm plugin %q is not found", fmt.Sprintf("%s/%s", k8s.HigressNamespace, name))
|
||||
}
|
||||
return errors.Wrapf(err, "failed to get wasm plugin %q", fmt.Sprintf("%s/%s", k8s.HigressNamespace, name))
|
||||
}
|
||||
|
||||
originalObj.SetGroupVersionKind(k8s.WasmPluginGVK)
|
||||
originalObj.SetManagedFields(nil) // TODO(WeixinX): Managed Fields should be written back
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
var wObj io.Writer = buf
|
||||
printer := printers.YAMLPrinter{}
|
||||
if err = printer.PrintObj(originalObj.DeepCopyObject(), wObj); err != nil {
|
||||
return err
|
||||
}
|
||||
original := buf.Bytes()
|
||||
e := editor.NewDefaultEditor(editorEnvs())
|
||||
edited, file, err := e.LaunchTempFile("higress-wasm-edit-", ".yaml", buf)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to launch editor")
|
||||
}
|
||||
defer os.Remove(file)
|
||||
|
||||
if bytes.Equal(cmdutil.StripComments(original), cmdutil.StripComments(edited)) { // no change
|
||||
fmt.Fprintf(w, "edit %q canceled, no change\n",
|
||||
fmt.Sprintf("%s/%s", originalObj.GetNamespace(), originalObj.GetName()))
|
||||
return nil
|
||||
}
|
||||
|
||||
var editedObj unstructured.Unstructured
|
||||
eBuf := bytes.NewReader(edited)
|
||||
dc := yaml.NewYAMLOrJSONDecoder(eBuf, 4096)
|
||||
if err = dc.Decode(&editedObj); err != nil {
|
||||
return err
|
||||
}
|
||||
if !keepSameMeta(&editedObj, originalObj) {
|
||||
fmt.Fprintln(w, "Warning: ensure that the apiVersion, kind, namespace, and name are the same as the original and are automatically corrected")
|
||||
}
|
||||
|
||||
ret, err := cli.Update(context.TODO(), &editedObj)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to update wasm plugin %q",
|
||||
fmt.Sprintf("%s/%s", originalObj.GetNamespace(), originalObj.GetName()))
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "Edited wasm plugin %q\n", fmt.Sprintf("%s/%s", ret.GetNamespace(), ret.GetName()))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func editorEnvs() []string {
|
||||
return []string{
|
||||
"KUBE_EDITOR",
|
||||
"EDITOR",
|
||||
}
|
||||
}
|
||||
|
||||
// to avoid changing the apiVersion, kind, namespace and name, keep them the same as the original
|
||||
func keepSameMeta(edited, original *unstructured.Unstructured) bool {
|
||||
same := true
|
||||
if edited.GroupVersionKind().String() != original.GroupVersionKind().String() {
|
||||
edited.SetGroupVersionKind(original.GroupVersionKind())
|
||||
same = false
|
||||
}
|
||||
if edited.GetNamespace() != original.GetNamespace() {
|
||||
edited.SetNamespace(original.GetNamespace())
|
||||
same = false
|
||||
}
|
||||
if edited.GetName() != original.GetName() {
|
||||
edited.SetName(original.GetName())
|
||||
same = false
|
||||
}
|
||||
return same
|
||||
}
|
||||
160
pkg/cmd/hgctl/plugin/config/templates.go
Normal file
160
pkg/cmd/hgctl/plugin/config/templates.go
Normal file
@@ -0,0 +1,160 @@
|
||||
// Copyright (c) 2022 Alibaba Group Holding Ltd.
|
||||
//
|
||||
// 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 config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/alibaba/higress/pkg/cmd/hgctl/plugin/types"
|
||||
"github.com/alibaba/higress/pkg/cmd/hgctl/plugin/utils"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// TODO(WeixinX): Use 'hgctl plugin push' command to fill the image url automatically
|
||||
const pluginConfYAML = `# File generated by hgctl. Modify as required.
|
||||
# See: https://higress.io/zh-cn/docs/plugins/intro
|
||||
|
||||
apiVersion: extensions.higress.io/v1alpha1
|
||||
kind: WasmPlugin
|
||||
metadata:
|
||||
name: {{ .Name }}
|
||||
namespace: {{ .Namespace }}
|
||||
annotations:
|
||||
higress.io/wasm-plugin-title: {{ .Title }}
|
||||
higress.io/wasm-plugin-description: {{ .Description }}
|
||||
higress.io/wasm-plugin-icon: {{ .IconUrl }}
|
||||
labels:
|
||||
higress.io/wasm-plugin-name: {{ .Name }}
|
||||
higress.io/wasm-plugin-category: {{ .Category }}
|
||||
higress.io/wasm-plugin-version: {{ .Version }}
|
||||
higress.io/resource-definer: higress
|
||||
higress.io/wasm-plugin-built-in: "false"
|
||||
spec:
|
||||
phase: {{ .Phase }}
|
||||
priority: {{ .Priority }}
|
||||
{{ .Config }}
|
||||
# Please fill the image url in according to your needs
|
||||
url: {{ .Url }}
|
||||
`
|
||||
|
||||
type PluginConf struct {
|
||||
Name string
|
||||
Namespace string
|
||||
Title string
|
||||
Description string
|
||||
IconUrl string
|
||||
Version string
|
||||
Category string
|
||||
Phase string
|
||||
Priority int64
|
||||
Config string
|
||||
Url string
|
||||
}
|
||||
|
||||
func (pc *PluginConf) String() string {
|
||||
b, err := json.MarshalIndent(pc, "", " ")
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// GenPluginConfYAML generates plugin-conf.yaml based on the template
|
||||
func GenPluginConfYAML(p *PluginConf, dir string) error {
|
||||
path := fmt.Sprintf("%s/plugin-conf.yaml", dir)
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if err = template.Must(template.New("PluginConfYAML").Parse(pluginConfYAML)).Execute(f, p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ExtractPluginConfFrom extracts the params of plugin-conf.yaml from spec.yaml.
|
||||
// input params `config`, `url` are only used to implement the command `hgctl plugin install -g <go-project>`
|
||||
func ExtractPluginConfFrom(spec *types.WasmPluginMeta, config, url string) (*PluginConf, error) {
|
||||
if config == "" {
|
||||
// by default, Example from spec.yaml is used as the defaultConfig for the wasm plugin
|
||||
var obj map[string]interface{}
|
||||
example := spec.GetConfigExample()
|
||||
if err := yaml.Unmarshal([]byte(example), &obj); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conf := struct {
|
||||
DefaultConfig map[string]interface{} `yaml:"defaultConfig,omitempty"`
|
||||
}{DefaultConfig: obj}
|
||||
b, err := utils.MarshalYamlWithIndent(conf, 2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
config = string(b)
|
||||
}
|
||||
|
||||
pc := &PluginConf{
|
||||
Name: spec.Info.Name,
|
||||
Namespace: "higress-system",
|
||||
Title: spec.Info.Title,
|
||||
Description: spec.Info.Description,
|
||||
IconUrl: spec.Info.IconUrl,
|
||||
Version: spec.Info.Version,
|
||||
Category: string(spec.Info.Category),
|
||||
Phase: string(spec.Spec.Phase),
|
||||
Priority: spec.Spec.Priority,
|
||||
Config: utils.AddIndent(config, strings.Repeat(" ", 2)),
|
||||
Url: url,
|
||||
}
|
||||
pc.withDefaultValue()
|
||||
|
||||
return pc, nil
|
||||
}
|
||||
|
||||
func (pc *PluginConf) withDefaultValue() {
|
||||
if pc.Name == "" {
|
||||
pc.Name = "Unnamed"
|
||||
}
|
||||
if pc.Namespace == "" {
|
||||
pc.Namespace = "higress-system"
|
||||
}
|
||||
if pc.Title == "" {
|
||||
pc.Title = "Untitled"
|
||||
}
|
||||
if pc.Description == "" {
|
||||
pc.Description = "No description"
|
||||
}
|
||||
if pc.IconUrl == "" {
|
||||
pc.IconUrl = types.Category2IconUrl(types.Category(pc.Category))
|
||||
}
|
||||
if pc.Version == "" {
|
||||
pc.Version = "0.1.0"
|
||||
}
|
||||
if pc.Category == "" {
|
||||
pc.Category = string(types.CategoryDefault)
|
||||
}
|
||||
if pc.Phase == "" {
|
||||
pc.Phase = string(types.PhaseDefault)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user