Files
kubevela/pkg/workflow/debug/context.go
Tianxin Dong 21216055fb Feat: add vela debug command (#3580)
* Feat: add debug configmap if debug policy is specified

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* Feat: add vela debug command

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* make code reviewable

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* fix sonartype lift

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* fix cue string

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* Feat: display better for debug

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* tidy the go mod

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* Feat: add debug test

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* change uitable vendor

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* add more tests

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* pass resource keeper from handler

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* fix lint

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* fix rebase

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

* Pending test temporary

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>
2022-04-18 11:06:14 +08:00

117 lines
3.1 KiB
Go

/*
Copyright 2022 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 debug
import (
"context"
"fmt"
"reflect"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/pkg/cue/model/value"
"github.com/oam-dev/kubevela/pkg/oam/util"
"github.com/oam-dev/kubevela/pkg/resourcekeeper"
"github.com/oam-dev/kubevela/pkg/utils/apply"
)
// ContextImpl is workflow debug context interface
type ContextImpl interface {
Set(v *value.Value) error
}
// Context is debug context.
type Context struct {
cli client.Client
app *v1beta1.Application
step string
rk resourcekeeper.ResourceKeeper
}
// Set sets debug content into context
func (d *Context) Set(v *value.Value) error {
data, err := v.String()
if err != nil {
return err
}
err = setStore(context.Background(), d.cli, d.rk, d.app, d.step, data)
if err != nil {
return err
}
return nil
}
func setStore(ctx context.Context, cli client.Client, rk resourcekeeper.ResourceKeeper, app *v1beta1.Application, step, data string) error {
cm := &corev1.ConfigMap{}
if err := cli.Get(ctx, types.NamespacedName{
Namespace: app.Namespace,
Name: GenerateContextName(app.Name, step),
}, cm); err != nil {
if errors.IsNotFound(err) {
cm.Name = GenerateContextName(app.Name, step)
cm.Namespace = app.Namespace
cm.Data = map[string]string{
"debug": data,
}
u, err := util.Object2Unstructured(cm)
if err != nil {
return err
}
u.SetGroupVersionKind(
corev1.SchemeGroupVersion.WithKind(
reflect.TypeOf(corev1.ConfigMap{}).Name(),
),
)
if err := rk.Dispatch(ctx, []*unstructured.Unstructured{u}, []apply.ApplyOption{apply.DisableUpdateAnnotation()}, resourcekeeper.MetaOnlyOption{}, resourcekeeper.CreatorOption{Creator: common.DebugResourceCreator}); err != nil {
return err
}
return nil
}
return err
}
cm.Data = map[string]string{
"debug": data,
}
if err := cli.Update(ctx, cm); err != nil {
return err
}
return nil
}
// NewContext new workflow context without initialize data.
func NewContext(cli client.Client, rk resourcekeeper.ResourceKeeper, app *v1beta1.Application, step string) ContextImpl {
return &Context{
cli: cli,
app: app,
step: step,
rk: rk,
}
}
// GenerateContextName generate context name
func GenerateContextName(app, step string) string {
return fmt.Sprintf("%s-%s-debug", app, step)
}