Files
kubevela/references/cli/components.go
Somefive 7103f8ff52 Feat: merge master into apiserver (#2660)
* Feat(rollout): fill rolloutBatches if empty when scale up/down (#2569)

* Feat: fill rolloutBatches if empty

* Fix: fix unit-test

* Test: add more test

Fix: lint

Fix: fix lint

* Update release.yml (#2537)

* Feat: add registry, merge registry and cap center (#2528)

* Feat: add registry command

* Refactor: comp/trait command combine with registry

* Feat: refactor `vela comp/trait`

* Fix: import

* Fix: fix if type is autodetects.core.oam.dev

* Fix: fix list from url

* Fix: test

* Feat: add test

* Fix: remove dup test

* Fix: test

* Fix: test

* Fix: fix label filter

* Fix: reviewable

* Fix test

* fix personal repo in test

* Fix test

* Fix test

* add some boundary check

* reviewable

* Fix: fix nocalhost trait (#2577)

* fix incorrect addon status (#2576)

* Fix(cli): client-side throttling in vela CLI (#2581)

* fix cli throttling

* fix import

* set to a lower value

* remove addon with no defs (#2574)

* Feat: vela logs support multicluster (#2593)

* Feat: add basic multiple cluster logs

* fix context

* Fix select style

* Fix select style

* remove useless env

* fix naming

* Feat: vela cluster support use ocm to join/list/detach cluster (#2599)

* Feat: add render component and apply component remaining (#2587)

* Feat: add render component and apply component remaining

* fix ut

* fix e2e

* allow import package in custom status cue template (#2585)

Co-authored-by: chwetion <chwetion@foxmail.com>

* Fix: abnormal aux name (#2612)

* Feat: store workflow step def properties in cm (#2592)

* Fix: fix notification def

* Feat: store workflow step def properties in cm

* fix ci

* fix data race

* Fix: change Initializer to Application for addon Observability (#2615)

In this doc, updated the Observability implementation from initializer
to Application. I also store definitions as it's not well stored in
vela-templates/addons/observability

* Fix: fix backport param (#2611)

* Fix: add owner reference in workflow context cm (#2573)

* Fix: add owner reference in workflow context cm

* fix ci

* delete useless test case

* Fix: op.delete bugs (#2622)

* Fix: op.delete some bugs

* Fix: app status update error

Fix: make reviewable

* Fix: show reconcile error log (#2626)

* Feat: add reconcile timeout configuration for vela-core (#2630)

* Fix: patch status retry while conflict happens (#2629)

* Fix: allow definition schema cm can be same name in different definition type (#2618)

* Fix: fix definition schema cm name

* fix ut

* fix ut

* fix show

* add switch default case

* Feat: remove envbinding policy into workflow (#2556)

Fix: add more test

* Feat: add vela prob to test cluster (#2635)

* Fix: upgrade stern lib to avoid panic for vela logs (#2650)

* Fix: filter loggable workload in vela logs (#2651)

* Fix: filter loggable workload in vela logs

* reviewable

* Feat: add vela exec for multi cluster (#2299)

fix

support vela exec

* Fix: health check will check for multiclusters (#2645)

* Fix: minor fix for vela cli printing (#2655)

* Fix: minor fix for vela cli printing

* add dockerfile go mod cache

* Feat: support apiserver-related multicluster features (#2625)

* Feat: remove envbinding policy into workflow

Feat: add support for env change (env gc)

Fix: fix rollout timeout setting bug

* Feat: support disable trait and env without workflow

* Fix: add hint for replaced value

Co-authored-by: wyike <wangyike_wyk@163.com>
Co-authored-by: basefas <basefas@hotmail.com>
Co-authored-by: qiaozp <47812250+chivalryq@users.noreply.github.com>
Co-authored-by: Tianxin Dong <dongtianxin.tx@alibaba-inc.com>
Co-authored-by: yangsoon <yangsoonlx@gmail.com>
Co-authored-by: Chwetion <137953601@qq.com>
Co-authored-by: chwetion <chwetion@foxmail.com>
Co-authored-by: Jian.Li <74582607+leejanee@users.noreply.github.com>
Co-authored-by: Zheng Xi Zhou <zzxwill@gmail.com>
Co-authored-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com>
2021-11-09 13:06:55 +08:00

249 lines
7.3 KiB
Go

/*
Copyright 2021 The KubeVela 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 cli
import (
"context"
"encoding/json"
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
core "github.com/oam-dev/kubevela/apis/core.oam.dev"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/apis/types"
common2 "github.com/oam-dev/kubevela/pkg/utils/common"
cmdutil "github.com/oam-dev/kubevela/pkg/utils/util"
"github.com/oam-dev/kubevela/references/common"
)
// NewComponentsCommand creates `components` command
func NewComponentsCommand(c common2.Args, ioStreams cmdutil.IOStreams) *cobra.Command {
var isDiscover bool
cmd := &cobra.Command{
Use: "components",
Aliases: []string{"comp", "component"},
Short: "List/get components",
Long: "List components & get components in registry",
Example: `vela comp`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return c.SetConfig()
},
RunE: func(cmd *cobra.Command, args []string) error {
// parse label filter
if label != "" {
words := strings.Split(label, "=")
if len(words) < 2 {
return errors.New("label is invalid")
}
filter = createLabelFilter(words[0], words[1])
}
var registry Registry
var err error
if isDiscover {
if regURL != "" {
ioStreams.Infof("Listing component definition from url: %s\n", regURL)
registry, err = NewRegistry(context.Background(), token, "temporary-registry", regURL)
if err != nil {
return errors.Wrap(err, "creating registry err, please check registry url")
}
} else {
ioStreams.Infof("Listing component definition from registry: %s\n", regName)
registry, err = GetRegistry(regName)
if err != nil {
return errors.Wrap(err, "get registry err")
}
}
return PrintComponentListFromRegistry(registry, ioStreams, filter)
}
return PrintInstalledCompDef(ioStreams, filter)
},
Annotations: map[string]string{
types.TagCommandType: types.TypeCap,
},
}
cmd.SetOut(ioStreams.Out)
cmd.AddCommand(
NewCompGetCommand(c, ioStreams),
)
cmd.Flags().BoolVar(&isDiscover, "discover", false, "discover traits in registries")
cmd.PersistentFlags().StringVar(&regURL, "url", "", "specify the registry URL")
cmd.PersistentFlags().StringVar(&regName, "registry", DefaultRegistry, "specify the registry name")
cmd.PersistentFlags().StringVar(&token, "token", "", "specify token when using --url to specify registry url")
cmd.Flags().StringVar(&label, types.LabelArg, "", "a label to filter components, the format is `--label type=terraform`")
cmd.SetOut(ioStreams.Out)
return cmd
}
// NewCompGetCommand creates `comp get` command
func NewCompGetCommand(c common2.Args, ioStreams cmdutil.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "get <component>",
Short: "get component from registry",
Long: "get component from registry",
Example: "vela comp get <component>",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
ioStreams.Error("you must specify a component name")
return nil
}
name := args[0]
var registry Registry
var err error
if regURL != "" {
ioStreams.Infof("Getting component definition from url: %s\n", regURL)
registry, err = NewRegistry(context.Background(), token, "temporary-registry", regURL)
if err != nil {
return errors.Wrap(err, "creating registry err, please check registry url")
}
} else {
ioStreams.Infof("Getting component definition from registry: %s\n", regName)
registry, err = GetRegistry(regName)
if err != nil {
return errors.Wrap(err, "get registry err")
}
}
return errors.Wrap(InstallCompByNameFromRegistry(c, ioStreams, name, registry), "install component definition err")
},
}
return cmd
}
// filterFunc to filter whether to print the capability
type filterFunc func(capability types.Capability) bool
func createLabelFilter(key, value string) filterFunc {
return func(capability types.Capability) bool {
return capability.Labels[key] == value
}
}
// PrintComponentListFromRegistry print a table which shows all components from registry
func PrintComponentListFromRegistry(registry Registry, ioStreams cmdutil.IOStreams, filter filterFunc) error {
var scheme = runtime.NewScheme()
err := core.AddToScheme(scheme)
if err != nil {
return err
}
err = clientgoscheme.AddToScheme(scheme)
if err != nil {
return err
}
k8sClient, err := client.New(config.GetConfigOrDie(), client.Options{Scheme: scheme})
if err != nil {
return err
}
caps, err := registry.ListCaps()
if err != nil {
return err
}
var installedList v1beta1.ComponentDefinitionList
err = k8sClient.List(context.Background(), &installedList, client.InNamespace(types.DefaultKubeVelaNS))
if err != nil {
return err
}
table := newUITable()
table.AddRow("NAME", "REGISTRY", "DEFINITION", "STATUS")
for _, c := range caps {
if filter != nil && !filter(c) {
continue
}
c.Status = uninstalled
if c.Type != types.TypeComponentDefinition {
continue
}
for _, ins := range installedList.Items {
if ins.Name == c.Name {
c.Status = installed
}
}
table.AddRow(c.Name, "default", c.CrdName, c.Status)
}
ioStreams.Info(table.String())
return nil
}
// InstallCompByNameFromRegistry will install given componentName comp to cluster from registry
func InstallCompByNameFromRegistry(args common2.Args, ioStream cmdutil.IOStreams, compName string, registry Registry) error {
capObj, data, err := registry.GetCap(compName)
if err != nil {
return err
}
k8sClient, err := args.GetClient()
if err != nil {
return err
}
err = common.InstallComponentDefinition(k8sClient, data, ioStream, &capObj)
if err != nil {
return err
}
ioStream.Info("Successfully install component:", compName)
return nil
}
// PrintInstalledCompDef will print all ComponentDefinition in cluster
func PrintInstalledCompDef(io cmdutil.IOStreams, filter filterFunc) error {
var list v1beta1.ComponentDefinitionList
err := clt.List(context.Background(), &list)
if err != nil {
return errors.Wrap(err, "get component definition list error")
}
dm, err := (&common2.Args{}).GetDiscoveryMapper()
if err != nil {
return errors.Wrap(err, "get discovery mapper error")
}
table := newUITable()
table.AddRow("NAME", "DEFINITION")
for _, cd := range list.Items {
data, err := json.Marshal(cd)
if err != nil {
io.Infof("error encoding definition: %s\n", cd.Name)
continue
}
capa, err := ParseCapability(dm, data)
if err != nil {
io.Errorf("error parsing capability: %s\n", cd.Name)
continue
}
if filter != nil && !filter(capa) {
continue
}
table.AddRow(capa.Name, capa.CrdName)
}
io.Infof(table.String())
return nil
}