diff --git a/apis/types/types.go b/apis/types/types.go index 3882622cc..b84c6f6a9 100644 --- a/apis/types/types.go +++ b/apis/types/types.go @@ -186,3 +186,17 @@ const ( // VelaCoreConfig is to mark application, config and its secret or Terraform provider lelong to a KubeVela config VelaCoreConfig = "velacore-config" ) + +const ( + // LabelSourceOfTruth describes the source of this app + LabelSourceOfTruth = "app.oam.dev/source-of-truth" + + // FromCR means the data source of truth is from k8s CR + FromCR = "from-k8s-resource" + // FromUX means the data source of truth is from velaux data store + FromUX = "from-velaux" + // FromInner means the data source of truth is from KubeVela inner usage + // the configuration that don't want to be synced + // the addon application should be synced, but set to readonly mode + FromInner = "from-inner-system" +) diff --git a/pkg/addon/addon.go b/pkg/addon/addon.go index adf166287..25d52606f 100644 --- a/pkg/addon/addon.go +++ b/pkg/addon/addon.go @@ -60,7 +60,6 @@ import ( common2 "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/apis/types" - apiutils "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/config" "github.com/oam-dev/kubevela/pkg/cue/script" "github.com/oam-dev/kubevela/pkg/definition" @@ -1086,7 +1085,7 @@ func (h *Installer) checkDependency(addon *InstallPackage) ([]string, error) { // createOrUpdate will return true if updated func (h *Installer) createOrUpdate(app *v1beta1.Application) (bool, error) { // Set the publish version for the addon application - oam.SetPublishVersion(app, apiutils.GenerateVersion("addon")) + oam.SetPublishVersion(app, util.GenerateVersion("addon")) var existApp v1beta1.Application err := h.cli.Get(h.ctx, client.ObjectKey{Name: app.Name, Namespace: app.Namespace}, &existApp) if apierrors.IsNotFound(err) { diff --git a/pkg/addon/type.go b/pkg/addon/type.go index f65df049b..e14d8cc5b 100644 --- a/pkg/addon/type.go +++ b/pkg/addon/type.go @@ -20,15 +20,15 @@ import ( "github.com/getkin/kin-openapi/openapi3" "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" - "github.com/oam-dev/kubevela/pkg/apiserver/utils" + "github.com/oam-dev/kubevela/pkg/utils/schema" ) // UIData contains all information represent an addon for UI type UIData struct { Meta - APISchema *openapi3.Schema `json:"schema"` - UISchema []*utils.UIParameter `json:"uiSchema"` + APISchema *openapi3.Schema `json:"schema"` + UISchema []*schema.UIParameter `json:"uiSchema"` // Detail is README.md in an addon Detail string `json:"detail,omitempty"` diff --git a/pkg/apiserver/domain/model/application.go b/pkg/apiserver/domain/model/application.go index 78b919d42..20356c71f 100644 --- a/pkg/apiserver/domain/model/application.go +++ b/pkg/apiserver/domain/model/application.go @@ -23,6 +23,7 @@ import ( workflowv1alpha1 "github.com/kubevela/workflow/api/v1alpha1" "github.com/oam-dev/kubevela/apis/core.oam.dev/common" + "github.com/oam-dev/kubevela/apis/types" ) func init() { @@ -83,8 +84,8 @@ func (a *Application) IsSynced() bool { if a.Labels == nil { return false } - sot := a.Labels[LabelSourceOfTruth] - if sot == FromCR || sot == FromInner { + sot := a.Labels[types.LabelSourceOfTruth] + if sot == types.FromCR || sot == types.FromInner { return true } return false @@ -96,8 +97,8 @@ func (a *Application) IsReadOnly() bool { if a.Labels == nil { return false } - sot := a.Labels[LabelSourceOfTruth] - return sot == FromInner + sot := a.Labels[types.LabelSourceOfTruth] + return sot == types.FromInner } // ClusterSelector cluster selector diff --git a/pkg/apiserver/domain/model/whole.go b/pkg/apiserver/domain/model/whole.go index 4532eda17..eaf01a6c0 100644 --- a/pkg/apiserver/domain/model/whole.go +++ b/pkg/apiserver/domain/model/whole.go @@ -44,20 +44,6 @@ const ( LabelSyncNamespace = "ux.oam.dev/from-namespace" ) -const ( - // LabelSourceOfTruth describes the source of this app - LabelSourceOfTruth = "app.oam.dev/source-of-truth" - - // FromCR means the data source of truth is from k8s CR - FromCR = "from-k8s-resource" - // FromUX means the data source of truth is from velaux data store - FromUX = "from-velaux" - // FromInner means the data source of truth is from KubeVela inner usage - // the configuration that don't want to be synced - // the addon application should be synced, but set to readonly mode - FromInner = "from-inner-system" -) - const ( // DefaultInitName is default object name for initialization diff --git a/pkg/apiserver/domain/service/addon.go b/pkg/apiserver/domain/service/addon.go index 928abc7fb..98d97056a 100644 --- a/pkg/apiserver/domain/service/addon.go +++ b/pkg/apiserver/domain/service/addon.go @@ -43,7 +43,6 @@ import ( pkgaddon "github.com/oam-dev/kubevela/pkg/addon" "github.com/oam-dev/kubevela/pkg/apiserver/infrastructure/clients" apis "github.com/oam-dev/kubevela/pkg/apiserver/interfaces/api/dto/v1" - "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/apiserver/utils/bcode" "github.com/oam-dev/kubevela/pkg/definition" "github.com/oam-dev/kubevela/pkg/multicluster" @@ -51,6 +50,7 @@ import ( addonutil "github.com/oam-dev/kubevela/pkg/utils/addon" "github.com/oam-dev/kubevela/pkg/utils/apply" velaerr "github.com/oam-dev/kubevela/pkg/utils/errors" + "github.com/oam-dev/kubevela/pkg/utils/schema" ) // AddonService handle CRUD and installation of addons @@ -539,7 +539,7 @@ func convertAppStateToAddonPhase(state common2.ApplicationPhase) apis.AddonPhase } } -func renderAddonCustomUISchema(ctx context.Context, cli client.Client, addonName string, defaultSchema []*utils.UIParameter) []*utils.UIParameter { +func renderAddonCustomUISchema(ctx context.Context, cli client.Client, addonName string, defaultSchema []*schema.UIParameter) []*schema.UIParameter { var cm v1.ConfigMap if err := cli.Get(ctx, k8stypes.NamespacedName{ Namespace: types.DefaultKubeVelaNS, @@ -554,7 +554,7 @@ func renderAddonCustomUISchema(ctx context.Context, cli client.Client, addonName if !ok { return defaultSchema } - schema := []*utils.UIParameter{} + schema := []*schema.UIParameter{} if err := json.Unmarshal([]byte(data), &schema); err != nil { klog.Errorf("unmarshal ui schema failure %s", err.Error()) return defaultSchema diff --git a/pkg/apiserver/domain/service/addon_test.go b/pkg/apiserver/domain/service/addon_test.go index 7307d36e7..df5f381e6 100644 --- a/pkg/apiserver/domain/service/addon_test.go +++ b/pkg/apiserver/domain/service/addon_test.go @@ -22,6 +22,7 @@ import ( "os" "github.com/oam-dev/kubevela/pkg/oam/util" + "github.com/oam-dev/kubevela/pkg/utils/schema" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -30,7 +31,6 @@ import ( "sigs.k8s.io/yaml" "github.com/oam-dev/kubevela/apis/types" - "github.com/oam-dev/kubevela/pkg/apiserver/utils" ) var _ = Describe("addon service test", func() { @@ -56,7 +56,7 @@ var _ = Describe("addon service test", func() { }} Expect(k8sClient.Create(ctx, &cm)).Should(BeNil()) - defaultSchema := []*utils.UIParameter{ + defaultSchema := []*schema.UIParameter{ { JSONKey: "version", Sort: 3, @@ -81,7 +81,7 @@ var _ = Describe("addon service test", func() { It("Test render without ui-schema", func() { addonName := "test-without-schema" - defaultSchema := []*utils.UIParameter{ + defaultSchema := []*schema.UIParameter{ { JSONKey: "version", Sort: 3, diff --git a/pkg/apiserver/domain/service/application.go b/pkg/apiserver/domain/service/application.go index b02214bd2..1d3a62359 100644 --- a/pkg/apiserver/domain/service/application.go +++ b/pkg/apiserver/domain/service/application.go @@ -47,6 +47,7 @@ import ( apisv1 "github.com/oam-dev/kubevela/pkg/apiserver/interfaces/api/dto/v1" "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/apiserver/utils/bcode" + "github.com/oam-dev/kubevela/pkg/appfile" "github.com/oam-dev/kubevela/pkg/appfile/dryrun" "github.com/oam-dev/kubevela/pkg/oam" "github.com/oam-dev/kubevela/pkg/oam/discoverymapper" @@ -555,8 +556,8 @@ func (c *applicationServiceImpl) UpdateApplication(ctx context.Context, app *mod req.Labels[model.LabelSyncNamespace] = app.Labels[model.LabelSyncNamespace] } - if _, exist := app.Labels[model.LabelSourceOfTruth]; exist { - req.Labels[model.LabelSourceOfTruth] = app.Labels[model.LabelSourceOfTruth] + if _, exist := app.Labels[velatypes.LabelSourceOfTruth]; exist { + req.Labels[velatypes.LabelSourceOfTruth] = app.Labels[velatypes.LabelSourceOfTruth] } if _, exist := app.Labels[model.LabelSyncGeneration]; exist { @@ -803,7 +804,7 @@ func (c *applicationServiceImpl) Deploy(ctx context.Context, app *model.Applicat if app.Labels == nil { app.Labels = make(map[string]string) } - app.Labels[model.LabelSourceOfTruth] = model.FromUX + app.Labels[velatypes.LabelSourceOfTruth] = velatypes.FromUX if err := c.Store.Put(ctx, app); err != nil { klog.Warningf("failed to update app %s", err.Error()) } @@ -853,7 +854,7 @@ func (c *applicationServiceImpl) renderOAMApplication(ctx context.Context, appMo } labels[oam.AnnotationAppName] = appModel.Name // To take over the application - labels[model.LabelSourceOfTruth] = model.FromUX + labels[velatypes.LabelSourceOfTruth] = velatypes.FromUX deployAppName := envbinding.AppDeployName if deployAppName == "" { @@ -1671,7 +1672,7 @@ func (c *applicationServiceImpl) resetApp(ctx context.Context, targetApp *v1beta for _, comp := range targetComps { // add or update new app's components from old app - if utils.StringsContain(readyToAdd, comp.Name) || utils.StringsContain(readyToUpdate, comp.Name) { + if pkgUtils.StringsContain(readyToAdd, comp.Name) || pkgUtils.StringsContain(readyToUpdate, comp.Name) { compModel, err := convert.FromCRComponent(appPrimaryKey, comp) if err != nil { return &apisv1.AppResetResponse{}, bcode.ErrInvalidProperties @@ -1714,7 +1715,7 @@ func (c *applicationServiceImpl) RollbackWithRevision(ctx context.Context, appli if revision.RevisionCRName == revision.Version || revision.RevisionCRName == "" { noRevision = true } else { - _, appCR, err := app.RollbackApplicationWithRevision(ctx, c.KubeClient, appCR.Name, appCR.Namespace, revision.RevisionCRName, publishVersion) + _, appCR, err := app.RollbackApplicationWithRevision(context.WithValue(ctx, &app.RevisionContextKey, utils.WithProject(ctx, "")), c.KubeClient, appCR.Name, appCR.Namespace, revision.RevisionCRName, publishVersion) if err != nil { switch { case errors.Is(err, app.ErrNotMatchRevision): @@ -1795,6 +1796,10 @@ func dryRunApplication(ctx context.Context, c commonutil.Args, app *v1beta1.Appl return buff, err } dryRunOpt := dryrun.NewDryRunOption(newClient, config, dm, pd, objects, true) + dryRunOpt.GenerateAppFile = func(ctx context.Context, app *v1beta1.Application) (*appfile.Appfile, error) { + generateCtx := utils.WithProject(ctx, "") + return dryRunOpt.Parser.GenerateAppFileFromApp(generateCtx, app) + } comps, policies, err := dryRunOpt.ExecuteDryRun(ctx, app) if err != nil { return buff, err diff --git a/pkg/apiserver/domain/service/authentication.go b/pkg/apiserver/domain/service/authentication.go index 72f7982e8..647dff56c 100644 --- a/pkg/apiserver/domain/service/authentication.go +++ b/pkg/apiserver/domain/service/authentication.go @@ -18,7 +18,6 @@ package service import ( "context" - "encoding/json" "errors" "fmt" "net/http" @@ -42,7 +41,9 @@ import ( "github.com/oam-dev/kubevela/pkg/apiserver/domain/model" "github.com/oam-dev/kubevela/pkg/apiserver/infrastructure/datastore" apisv1 "github.com/oam-dev/kubevela/pkg/apiserver/interfaces/api/dto/v1" + apiutils "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/apiserver/utils/bcode" + "github.com/oam-dev/kubevela/pkg/oam" ) const ( @@ -379,28 +380,8 @@ func restartDex(ctx context.Context, kubeClient client.Client) error { } return err } - for i, comp := range dexApp.Spec.Components { - if comp.Name == keyDex { - var v model.JSONStruct - err := json.Unmarshal(comp.Properties.Raw, &v) - if err != nil { - return err - } - // restart the dex server - if _, ok := v["values"]; ok { - v["values"].(map[string]interface{})["env"] = map[string]string{ - "TIME_STAMP": time.Now().Format(time.RFC3339), - } - } - dexApp.Spec.Components[i].Properties = v.RawExtension() - if err := kubeClient.Update(ctx, dexApp); err != nil { - return err - } - break - } - } - - return nil + oam.SetPublishVersion(dexApp, apiutils.GenerateVersion("addon")) + return kubeClient.Update(ctx, dexApp) } func getDexConfig(ctx context.Context, kubeClient client.Client) (*model.DexConfig, error) { diff --git a/pkg/apiserver/domain/service/cloudshell.go b/pkg/apiserver/domain/service/cloudshell.go index 62fab6bf2..1d674a941 100644 --- a/pkg/apiserver/domain/service/cloudshell.go +++ b/pkg/apiserver/domain/service/cloudshell.go @@ -45,6 +45,7 @@ import ( "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/apiserver/utils/bcode" "github.com/oam-dev/kubevela/pkg/auth" + pkgutils "github.com/oam-dev/kubevela/pkg/utils" ) const ( @@ -259,7 +260,7 @@ func (c *cloudShellServiceImpl) prepareKubeConfig(ctx context.Context) error { } groups = append(groups, utils.TemplateReaderGroup) - if utils.StringsContain(user.UserRoles, "admin") { + if pkgutils.StringsContain(user.UserRoles, "admin") { groups = append(groups, utils.KubeVelaAdminGroupPrefix+"admin") } diff --git a/pkg/apiserver/domain/service/cloudshell_test.go b/pkg/apiserver/domain/service/cloudshell_test.go index 572961f0f..48086daa3 100644 --- a/pkg/apiserver/domain/service/cloudshell_test.go +++ b/pkg/apiserver/domain/service/cloudshell_test.go @@ -40,6 +40,7 @@ import ( "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/apiserver/utils/bcode" "github.com/oam-dev/kubevela/pkg/auth" + pkgutils "github.com/oam-dev/kubevela/pkg/utils" ) var _ = Describe("Test cloudshell service function", func() { @@ -179,8 +180,8 @@ var _ = Describe("Test cloudshell service function", func() { var identity auth.Identity err = yaml.Unmarshal([]byte(cm.Data["identity"]), &identity) Expect(err).Should(BeNil()) - Expect(utils.StringsContain(identity.Groups, utils.KubeVelaAdminGroupPrefix+"admin")).Should(BeTrue()) - Expect(utils.StringsContain(identity.Groups, utils.TemplateReaderGroup)).Should(BeTrue()) + Expect(pkgutils.StringsContain(identity.Groups, utils.KubeVelaAdminGroupPrefix+"admin")).Should(BeTrue()) + Expect(pkgutils.StringsContain(identity.Groups, utils.TemplateReaderGroup)).Should(BeTrue()) } checkConfig() diff --git a/pkg/apiserver/domain/service/definition.go b/pkg/apiserver/domain/service/definition.go index 4ad221391..2b9b3d56c 100644 --- a/pkg/apiserver/domain/service/definition.go +++ b/pkg/apiserver/domain/service/definition.go @@ -25,6 +25,7 @@ import ( "github.com/oam-dev/kubevela/pkg/utils/addon" "github.com/oam-dev/kubevela/pkg/utils/filters" + "github.com/oam-dev/kubevela/pkg/utils/schema" "github.com/getkin/kin-openapi/openapi3" "github.com/pkg/errors" @@ -40,8 +41,8 @@ import ( "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" "github.com/oam-dev/kubevela/apis/types" apisv1 "github.com/oam-dev/kubevela/pkg/apiserver/interfaces/api/dto/v1" - "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/apiserver/utils/bcode" + "github.com/oam-dev/kubevela/pkg/utils" ) // DefinitionService definition service, Implement the management of ComponentDefinition、TraitDefinition and WorkflowStepDefinition. @@ -51,7 +52,7 @@ type DefinitionService interface { // DetailDefinition get definition detail DetailDefinition(ctx context.Context, name, defType string) (*apisv1.DetailDefinitionResponse, error) // AddDefinitionUISchema add or update custom definition ui schema - AddDefinitionUISchema(ctx context.Context, name, defType string, schema []*utils.UIParameter) ([]*utils.UIParameter, error) + AddDefinitionUISchema(ctx context.Context, name, defType string, schema []*schema.UIParameter) ([]*schema.UIParameter, error) // UpdateDefinitionStatus update the status of definition UpdateDefinitionStatus(ctx context.Context, name string, status apisv1.UpdateDefinitionStatusRequest) (*apisv1.DetailDefinitionResponse, error) } @@ -278,7 +279,7 @@ func (d *definitionServiceImpl) DetailDefinition(ctx context.Context, name, defT return definition, nil } -func renderCustomUISchema(ctx context.Context, cli client.Client, name, defType string, defaultSchema []*utils.UIParameter) []*utils.UIParameter { +func renderCustomUISchema(ctx context.Context, cli client.Client, name, defType string, defaultSchema []*schema.UIParameter) []*schema.UIParameter { var cm v1.ConfigMap if err := cli.Get(ctx, k8stypes.NamespacedName{ Namespace: types.DefaultKubeVelaNS, @@ -293,7 +294,7 @@ func renderCustomUISchema(ctx context.Context, cli client.Client, name, defType if !ok { return defaultSchema } - schema := []*utils.UIParameter{} + schema := []*schema.UIParameter{} if err := json.Unmarshal([]byte(data), &schema); err != nil { klog.Errorf("unmarshal ui schema failure %s", err.Error()) return defaultSchema @@ -302,7 +303,7 @@ func renderCustomUISchema(ctx context.Context, cli client.Client, name, defType } // AddDefinitionUISchema add definition custom ui schema config -func (d *definitionServiceImpl) AddDefinitionUISchema(ctx context.Context, name, defType string, schema []*utils.UIParameter) ([]*utils.UIParameter, error) { +func (d *definitionServiceImpl) AddDefinitionUISchema(ctx context.Context, name, defType string, schema []*schema.UIParameter) ([]*schema.UIParameter, error) { dataBate, err := json.Marshal(schema) if err != nil { klog.Errorf("json marshal failure %s", err.Error()) @@ -376,8 +377,8 @@ func (d *definitionServiceImpl) UpdateDefinitionStatus(ctx context.Context, name return d.DetailDefinition(ctx, name, update.DefinitionType) } -func patchSchema(defaultSchema, customSchema []*utils.UIParameter) []*utils.UIParameter { - var customSchemaMap = make(map[string]*utils.UIParameter, len(customSchema)) +func patchSchema(defaultSchema, customSchema []*schema.UIParameter) []*schema.UIParameter { + var customSchemaMap = make(map[string]*schema.UIParameter, len(customSchema)) for i, custom := range customSchema { customSchemaMap[custom.JSONKey] = customSchema[i] } @@ -428,14 +429,14 @@ func patchSchema(defaultSchema, customSchema []*utils.UIParameter) []*utils.UIPa return defaultSchema } -func renderDefaultUISchema(apiSchema *openapi3.Schema) []*utils.UIParameter { +func renderDefaultUISchema(apiSchema *openapi3.Schema) []*schema.UIParameter { if apiSchema == nil { return nil } - var params []*utils.UIParameter + var params []*schema.UIParameter for key, property := range apiSchema.Properties { if property.Value != nil { - param := renderUIParameter(key, utils.FirstUpper(key), property, apiSchema.Required) + param := renderUIParameter(key, schema.FirstUpper(key), property, apiSchema.Required) params = append(params, param) } } @@ -449,7 +450,7 @@ func renderDefaultUISchema(apiSchema *openapi3.Schema) []*utils.UIParameter { // 3.If validate.required or subParameters is equal, sort by Label // // The sort number starts with 100. -func sortDefaultUISchema(params []*utils.UIParameter) { +func sortDefaultUISchema(params []*schema.UIParameter) { sort.Slice(params, func(i, j int) bool { switch { case params[i].Validate.Required && !params[j].Validate.Required: @@ -472,8 +473,8 @@ func sortDefaultUISchema(params []*utils.UIParameter) { } } -func renderUIParameter(key, label string, property *openapi3.SchemaRef, required []string) *utils.UIParameter { - var parameter utils.UIParameter +func renderUIParameter(key, label string, property *openapi3.SchemaRef, required []string) *schema.UIParameter { + var parameter schema.UIParameter subType := "" if property.Value.Items != nil { if property.Value.Items.Value != nil { @@ -488,18 +489,18 @@ func renderUIParameter(key, label string, property *openapi3.SchemaRef, required parameter.SubParameters = renderDefaultUISchema(property.Value.AdditionalProperties.Value) var enable = true value := property.Value.AdditionalProperties.Value - parameter.AdditionalParameter = renderUIParameter(value.Title, utils.FirstUpper(value.Title), property.Value.AdditionalProperties, value.Required) + parameter.AdditionalParameter = renderUIParameter(value.Title, schema.FirstUpper(value.Title), property.Value.AdditionalProperties, value.Required) parameter.Additional = &enable } - parameter.Validate = &utils.Validate{} + parameter.Validate = &schema.Validate{} parameter.Validate.DefaultValue = property.Value.Default for _, enum := range property.Value.Enum { - parameter.Validate.Options = append(parameter.Validate.Options, utils.Option{Label: utils.RenderLabel(enum), Value: enum}) + parameter.Validate.Options = append(parameter.Validate.Options, schema.Option{Label: schema.RenderLabel(enum), Value: enum}) } parameter.JSONKey = key parameter.Description = property.Value.Description parameter.Label = label - parameter.UIType = utils.GetDefaultUIType(property.Value.Type, len(parameter.Validate.Options) != 0, subType, len(property.Value.Properties) > 0) + parameter.UIType = schema.GetDefaultUIType(property.Value.Type, len(parameter.Validate.Options) != 0, subType, len(property.Value.Properties) > 0) parameter.Validate.Max = property.Value.Max parameter.Validate.MaxLength = property.Value.MaxLength parameter.Validate.Min = property.Value.Min diff --git a/pkg/apiserver/domain/service/definition_test.go b/pkg/apiserver/domain/service/definition_test.go index 4d48968b6..9fcc38d06 100644 --- a/pkg/apiserver/domain/service/definition_test.go +++ b/pkg/apiserver/domain/service/definition_test.go @@ -34,8 +34,8 @@ import ( "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" "github.com/oam-dev/kubevela/apis/types" v1 "github.com/oam-dev/kubevela/pkg/apiserver/interfaces/api/dto/v1" - "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/oam/util" + "github.com/oam-dev/kubevela/pkg/utils/schema" ) var _ = Describe("Test namespace service functions", func() { @@ -217,7 +217,7 @@ var _ = Describe("Test namespace service functions", func() { Expect(cmp.Diff(len(ddr.APISchema.Required), 3)).Should(BeEmpty()) defaultschema := renderDefaultUISchema(ddr.APISchema) - customschema := []*utils.UIParameter{} + customschema := []*schema.UIParameter{} cdata, err := os.ReadFile("./testdata/ui-custom-schema.yaml") Expect(err).Should(Succeed()) err = yaml.Unmarshal(cdata, &customschema) @@ -237,7 +237,7 @@ var _ = Describe("Test namespace service functions", func() { } cdata, err := os.ReadFile("./testdata/workflowstep-apply-object.yaml") Expect(err).Should(Succeed()) - var schema utils.UISchema + var schema schema.UISchema yaml.Unmarshal(cdata, &schema) uiSchema, err := du.AddDefinitionUISchema(context.TODO(), "apply-object", "workflowstep", schema) Expect(err).Should(Succeed()) @@ -272,22 +272,22 @@ var _ = Describe("Test namespace service functions", func() { }) func testSortDefaultUISchema() { - var params = []*utils.UIParameter{ + var params = []*schema.UIParameter{ { Label: "P1", - Validate: &utils.Validate{ + Validate: &schema.Validate{ Required: true, }, - SubParameters: []*utils.UIParameter{ + SubParameters: []*schema.UIParameter{ {Label: "P1S1"}, }, Sort: 100, }, { Label: "T2", - Validate: &utils.Validate{ + Validate: &schema.Validate{ Required: true, }, - SubParameters: []*utils.UIParameter{ + SubParameters: []*schema.UIParameter{ {Label: "T2S1"}, {Label: "T2S2"}, {Label: "T2S3"}, @@ -295,32 +295,32 @@ func testSortDefaultUISchema() { Sort: 100, }, { Label: "T3", - Validate: &utils.Validate{ + Validate: &schema.Validate{ Required: false, }, Sort: 100, }, { Label: "P4", - Validate: &utils.Validate{ + Validate: &schema.Validate{ Required: false, }, Sort: 100, }, { Label: "T5", - Validate: &utils.Validate{ + Validate: &schema.Validate{ Required: true, }, - SubParameters: []*utils.UIParameter{ + SubParameters: []*schema.UIParameter{ {Label: "T5S1"}, {Label: "T5S2"}, }, Sort: 100, }, { Label: "P6", - Validate: &utils.Validate{ + Validate: &schema.Validate{ Required: true, }, - SubParameters: []*utils.UIParameter{ + SubParameters: []*schema.UIParameter{ {Label: "P6S1"}, {Label: "P6S2"}, {Label: "P6S3"}, @@ -329,32 +329,32 @@ func testSortDefaultUISchema() { }, } - var expectedParams = []*utils.UIParameter{ + var expectedParams = []*schema.UIParameter{ { Label: "P1", - Validate: &utils.Validate{ + Validate: &schema.Validate{ Required: true, }, - SubParameters: []*utils.UIParameter{ + SubParameters: []*schema.UIParameter{ {Label: "P1S1"}, }, Sort: 100, }, { Label: "T5", - Validate: &utils.Validate{ + Validate: &schema.Validate{ Required: true, }, - SubParameters: []*utils.UIParameter{ + SubParameters: []*schema.UIParameter{ {Label: "T5S1"}, {Label: "T5S2"}, }, Sort: 101, }, { Label: "P6", - Validate: &utils.Validate{ + Validate: &schema.Validate{ Required: true, }, - SubParameters: []*utils.UIParameter{ + SubParameters: []*schema.UIParameter{ {Label: "P6S1"}, {Label: "P6S2"}, {Label: "P6S3"}, @@ -362,10 +362,10 @@ func testSortDefaultUISchema() { Sort: 102, }, { Label: "T2", - Validate: &utils.Validate{ + Validate: &schema.Validate{ Required: true, }, - SubParameters: []*utils.UIParameter{ + SubParameters: []*schema.UIParameter{ {Label: "T2S1"}, {Label: "T2S2"}, {Label: "T2S3"}, @@ -373,13 +373,13 @@ func testSortDefaultUISchema() { Sort: 103, }, { Label: "P4", - Validate: &utils.Validate{ + Validate: &schema.Validate{ Required: false, }, Sort: 104, }, { Label: "T3", - Validate: &utils.Validate{ + Validate: &schema.Validate{ Required: false, }, Sort: 105, diff --git a/pkg/apiserver/domain/service/env.go b/pkg/apiserver/domain/service/env.go index 9aa0423cd..114d6303a 100644 --- a/pkg/apiserver/domain/service/env.go +++ b/pkg/apiserver/domain/service/env.go @@ -251,7 +251,7 @@ func (p *envServiceImpl) UpdateEnv(ctx context.Context, name string, req apisv1. func (p *envServiceImpl) GetAppCountInEnv(ctx context.Context, env *model.Env) (int, error) { var appList v1beta1.ApplicationList - if err := p.KubeClient.List(ctx, &appList, client.InNamespace(env.Namespace), client.MatchingLabels{model.LabelSourceOfTruth: model.FromUX}); err != nil { + if err := p.KubeClient.List(ctx, &appList, client.InNamespace(env.Namespace), client.MatchingLabels{types.LabelSourceOfTruth: types.FromUX}); err != nil { return 0, err } return len(appList.Items), nil diff --git a/pkg/apiserver/domain/service/env_test.go b/pkg/apiserver/domain/service/env_test.go index ddac96a7a..a1b1be78e 100644 --- a/pkg/apiserver/domain/service/env_test.go +++ b/pkg/apiserver/domain/service/env_test.go @@ -30,6 +30,7 @@ import ( "github.com/oam-dev/kubevela/apis/core.oam.dev/common" "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" + velatypes "github.com/oam-dev/kubevela/apis/types" "github.com/oam-dev/kubevela/pkg/apiserver/domain/model" "github.com/oam-dev/kubevela/pkg/apiserver/infrastructure/datastore" apisv1 "github.com/oam-dev/kubevela/pkg/apiserver/interfaces/api/dto/v1" @@ -132,7 +133,7 @@ var _ = Describe("Test env service functions", func() { Name: "env-app", Namespace: env.Namespace, Labels: map[string]string{ - model.LabelSourceOfTruth: model.FromUX, + velatypes.LabelSourceOfTruth: velatypes.FromUX, }, }, Spec: v1beta1.ApplicationSpec{ diff --git a/pkg/apiserver/domain/service/envbinding.go b/pkg/apiserver/domain/service/envbinding.go index 7983956db..6b51a9ddc 100644 --- a/pkg/apiserver/domain/service/envbinding.go +++ b/pkg/apiserver/domain/service/envbinding.go @@ -32,7 +32,6 @@ import ( "github.com/oam-dev/kubevela/pkg/apiserver/infrastructure/datastore" assembler "github.com/oam-dev/kubevela/pkg/apiserver/interfaces/api/assembler/v1" apisv1 "github.com/oam-dev/kubevela/pkg/apiserver/interfaces/api/dto/v1" - "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/apiserver/utils/bcode" pkgUtils "github.com/oam-dev/kubevela/pkg/utils" ) @@ -87,7 +86,7 @@ func (e *envBindingServiceImpl) GetEnvBinding(ctx context.Context, app *model.Ap func CheckAppEnvBindingsContainTarget(envBindings []*apisv1.EnvBindingBase, targetName string) (bool, error) { var filteredList []*apisv1.EnvBindingBase for _, envBinding := range envBindings { - if utils.StringsContain(envBinding.TargetNames, targetName) { + if pkgUtils.StringsContain(envBinding.TargetNames, targetName) { filteredList = append(filteredList, envBinding) } } diff --git a/pkg/apiserver/domain/service/pipeline.go b/pkg/apiserver/domain/service/pipeline.go index 46dc9da70..9f1927b54 100644 --- a/pkg/apiserver/domain/service/pipeline.go +++ b/pkg/apiserver/domain/service/pipeline.go @@ -45,7 +45,7 @@ import ( "k8s.io/klog/v2" "sigs.k8s.io/controller-runtime/pkg/client" - types2 "github.com/oam-dev/kubevela/apis/types" + velatypes "github.com/oam-dev/kubevela/apis/types" "github.com/oam-dev/kubevela/pkg/apiserver/domain/model" "github.com/oam-dev/kubevela/pkg/apiserver/infrastructure/datastore" apis "github.com/oam-dev/kubevela/pkg/apiserver/interfaces/api/dto/v1" @@ -491,7 +491,7 @@ func (p pipelineRunServiceImpl) GetPipelineRunLog(ctx context.Context, pipelineR switch { case logConfig.Data: logs, err = getResourceLogs(ctx, p.KubeConfig, p.KubeClient, []wfTypes.Resource{{ - Namespace: types2.DefaultKubeVelaNS, + Namespace: velatypes.DefaultKubeVelaNS, LabelSelector: map[string]string{"app.kubernetes.io/name": "vela-workflow"}, }}, []string{fmt.Sprintf(`step_name="%s"`, step), fmt.Sprintf("%s/%s", project.GetNamespace(), pipelineRun.PipelineRunName), "cue logs"}) if err != nil { @@ -767,8 +767,8 @@ func (p pipelineServiceImpl) RunPipeline(ctx context.Context, pipeline apis.Pipe run.Spec.Mode = &req.Mode run.SetLabels(map[string]string{ - labelPipeline: pipeline.Name, - model.LabelSourceOfTruth: model.FromUX, + labelPipeline: pipeline.Name, + velatypes.LabelSourceOfTruth: velatypes.FromUX, }) if p.Version != "" { if err := k8s.AddAnnotation(&run, wfTypes.AnnotationControllerRequirement, p.Version); err != nil { diff --git a/pkg/apiserver/domain/service/velaql.go b/pkg/apiserver/domain/service/velaql.go index cc12bd544..ba1cc249e 100644 --- a/pkg/apiserver/domain/service/velaql.go +++ b/pkg/apiserver/domain/service/velaql.go @@ -29,6 +29,7 @@ import ( "github.com/oam-dev/kubevela/pkg/apiserver/infrastructure/clients" apis "github.com/oam-dev/kubevela/pkg/apiserver/interfaces/api/dto/v1" + "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/apiserver/utils/bcode" "github.com/oam-dev/kubevela/pkg/oam/discoverymapper" "github.com/oam-dev/kubevela/pkg/velaql" @@ -70,7 +71,7 @@ func (v *velaQLServiceImpl) QueryView(ctx context.Context, velaQL string) (*apis return nil, bcode.ErrParseVelaQL } - queryValue, err := velaql.NewViewHandler(v.KubeClient, v.KubeConfig, v.dm, v.pd).QueryView(ctx, query) + queryValue, err := velaql.NewViewHandler(v.KubeClient, v.KubeConfig, v.dm, v.pd).QueryView(utils.ContextWithUserInfo(ctx), query) if err != nil { klog.Errorf("fail to query the view %s", err.Error()) return nil, bcode.ErrViewQuery diff --git a/pkg/apiserver/event/sync/cache.go b/pkg/apiserver/event/sync/cache.go index 70cd56045..8b326b3da 100644 --- a/pkg/apiserver/event/sync/cache.go +++ b/pkg/apiserver/event/sync/cache.go @@ -24,6 +24,7 @@ import ( "k8s.io/klog/v2" "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" + "github.com/oam-dev/kubevela/apis/types" "github.com/oam-dev/kubevela/pkg/apiserver/domain/model" "github.com/oam-dev/kubevela/pkg/apiserver/infrastructure/datastore" "github.com/oam-dev/kubevela/pkg/oam" @@ -67,11 +68,11 @@ func (c *CR2UX) initCache(ctx context.Context) error { func (c *CR2UX) shouldSync(ctx context.Context, targetApp *v1beta1.Application, del bool) bool { if targetApp != nil && targetApp.Labels != nil { // the source is inner and is not the addon application, ignore it. - if targetApp.Labels[model.LabelSourceOfTruth] == model.FromInner && targetApp.Labels[oam.LabelAddonName] == "" { + if targetApp.Labels[types.LabelSourceOfTruth] == types.FromInner && targetApp.Labels[oam.LabelAddonName] == "" { return false } // the source is UX, ignore it - if targetApp.Labels[model.LabelSourceOfTruth] == model.FromUX { + if targetApp.Labels[types.LabelSourceOfTruth] == types.FromUX { return false } } diff --git a/pkg/apiserver/event/sync/cache_test.go b/pkg/apiserver/event/sync/cache_test.go index 0050ca1ab..ce304213c 100644 --- a/pkg/apiserver/event/sync/cache_test.go +++ b/pkg/apiserver/event/sync/cache_test.go @@ -27,6 +27,7 @@ import ( "github.com/oam-dev/kubevela/apis/core.oam.dev/common" "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" + velatypes "github.com/oam-dev/kubevela/apis/types" "github.com/oam-dev/kubevela/pkg/apiserver/domain/model" "github.com/oam-dev/kubevela/pkg/apiserver/infrastructure/datastore" "github.com/oam-dev/kubevela/pkg/oam/util" @@ -59,9 +60,9 @@ var _ = Describe("Test Cache", func() { }})).Should(BeNil()) Expect(ds.Add(ctx, &model.Application{Name: "app3", Labels: map[string]string{ - model.LabelSyncGeneration: "1", - model.LabelSyncNamespace: "app3-ns", - model.LabelSourceOfTruth: model.FromUX, + model.LabelSyncGeneration: "1", + model.LabelSyncNamespace: "app3-ns", + velatypes.LabelSourceOfTruth: velatypes.FromUX, }})).Should(BeNil()) Expect(cr2ux.initCache(ctx)).Should(BeNil()) @@ -87,7 +88,7 @@ var _ = Describe("Test Cache", func() { app3.Namespace = "app3-ns" app3.ResourceVersion = "3" app3.Labels = map[string]string{ - model.LabelSourceOfTruth: model.FromUX, + velatypes.LabelSourceOfTruth: velatypes.FromUX, } Expect(cr2ux.shouldSync(ctx, app3, false)).Should(BeEquivalentTo(false)) @@ -124,7 +125,7 @@ var _ = Describe("Test Cache", func() { app1.Generation = 1 app1.Spec.Components = []common.ApplicationComponent{} app1.Labels = make(map[string]string) - app1.Labels[model.LabelSourceOfTruth] = model.FromInner + app1.Labels[velatypes.LabelSourceOfTruth] = velatypes.FromInner Expect(k8sClient.Create(ctx, app1)).Should(BeNil()) Expect(cr2ux.shouldSync(ctx, app1, false)).Should(BeEquivalentTo(false)) }) diff --git a/pkg/apiserver/event/sync/convert.go b/pkg/apiserver/event/sync/convert.go index b921c2deb..79dc55d97 100644 --- a/pkg/apiserver/event/sync/convert.go +++ b/pkg/apiserver/event/sync/convert.go @@ -32,8 +32,8 @@ import ( "github.com/oam-dev/kubevela/pkg/apiserver/domain/model" "github.com/oam-dev/kubevela/pkg/apiserver/event/sync/convert" v1 "github.com/oam-dev/kubevela/pkg/apiserver/interfaces/api/dto/v1" - "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/oam" + pkgutils "github.com/oam-dev/kubevela/pkg/utils" "github.com/oam-dev/kubevela/pkg/workflow/step" ) @@ -44,10 +44,10 @@ func (c *CR2UX) ConvertApp2DatastoreApp(ctx context.Context, targetApp *v1beta1. project := v1.CreateProjectRequest{ Name: model.DefaultInitName, } - sourceOfTruth := model.FromCR + sourceOfTruth := apitypes.FromCR if _, ok := targetApp.Labels[oam.LabelAddonName]; ok && strings.HasPrefix(targetApp.Name, "addon-") && targetApp.Namespace == apitypes.DefaultKubeVelaNS { project = c.generateSystemProject(ctx, targetApp.Namespace) - sourceOfTruth = model.FromInner + sourceOfTruth = apitypes.FromInner } appMeta := &model.Application{ @@ -56,10 +56,10 @@ func (c *CR2UX) ConvertApp2DatastoreApp(ctx context.Context, targetApp *v1beta1. Alias: targetApp.Name, Project: project.Name, Labels: map[string]string{ - model.LabelSyncNamespace: targetApp.Namespace, - model.LabelSyncGeneration: strconv.FormatInt(targetApp.Generation, 10), - model.LabelSyncRevision: getRevision(*targetApp), - model.LabelSourceOfTruth: sourceOfTruth, + model.LabelSyncNamespace: targetApp.Namespace, + model.LabelSyncGeneration: strconv.FormatInt(targetApp.Generation, 10), + model.LabelSyncRevision: getRevision(*targetApp), + apitypes.LabelSourceOfTruth: sourceOfTruth, }, } appMeta.CreateTime = targetApp.CreationTimestamp.Time @@ -186,7 +186,7 @@ func (c *CR2UX) generateEnv(ctx context.Context, defaultProject string, envNames if len(existEnvs) > 0 { env := existEnvs[0].(*model.Env) for name, project := range envTargetNames { - if !utils.StringsContain(env.Targets, name) && project == env.Project { + if !pkgutils.StringsContain(env.Targets, name) && project == env.Project { env.Targets = append(env.Targets, name) } } diff --git a/pkg/apiserver/event/sync/convert/convert.go b/pkg/apiserver/event/sync/convert/convert.go index 53f0e4ab9..8791c143d 100644 --- a/pkg/apiserver/event/sync/convert/convert.go +++ b/pkg/apiserver/event/sync/convert/convert.go @@ -32,10 +32,10 @@ import ( "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" "github.com/oam-dev/kubevela/pkg/apiserver/domain/model" "github.com/oam-dev/kubevela/pkg/apiserver/infrastructure/datastore" - "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/multicluster" "github.com/oam-dev/kubevela/pkg/oam" "github.com/oam-dev/kubevela/pkg/policy" + "github.com/oam-dev/kubevela/pkg/utils/schema" ) // FromCRComponent concerts Application CR Component object into velaux data store component @@ -97,7 +97,7 @@ func FromCRWorkflow(ctx context.Context, cli client.Client, appPrimaryKey string AppPrimaryKey: appPrimaryKey, EnvName: envName, Name: name, - Alias: fmt.Sprintf("%s Workflow", utils.FirstUpper(envName)), + Alias: fmt.Sprintf("%s Workflow", schema.FirstUpper(envName)), Description: model.AutoGenDesc, Default: &defaultWorkflow, } diff --git a/pkg/apiserver/event/sync/cr2ux_test.go b/pkg/apiserver/event/sync/cr2ux_test.go index a0b0c8804..844cadf45 100644 --- a/pkg/apiserver/event/sync/cr2ux_test.go +++ b/pkg/apiserver/event/sync/cr2ux_test.go @@ -28,6 +28,7 @@ import ( "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/apis/types" "github.com/oam-dev/kubevela/pkg/apiserver/domain/model" "github.com/oam-dev/kubevela/pkg/apiserver/domain/service" "github.com/oam-dev/kubevela/pkg/apiserver/infrastructure/datastore" @@ -86,7 +87,7 @@ var _ = Describe("Test CR convert to ux", func() { By("app get the created app") gotApp, gotAppName, err = cr2ux.getApp(context.Background(), apName1, appNS2) Expect(gotAppName).Should(BeEquivalentTo(apName1)) - Expect(gotApp.Labels[model.LabelSourceOfTruth]).Should(BeEquivalentTo(model.FromCR)) + Expect(gotApp.Labels[types.LabelSourceOfTruth]).Should(BeEquivalentTo(types.FromCR)) Expect(err).Should(BeNil()) Expect(gotApp.IsSynced()).Should(BeEquivalentTo(true)) }) diff --git a/pkg/apiserver/interfaces/api/definition.go b/pkg/apiserver/interfaces/api/definition.go index 2f9e184ff..2c5b892fa 100644 --- a/pkg/apiserver/interfaces/api/definition.go +++ b/pkg/apiserver/interfaces/api/definition.go @@ -24,8 +24,8 @@ import ( "github.com/oam-dev/kubevela/pkg/apiserver/domain/service" apis "github.com/oam-dev/kubevela/pkg/apiserver/interfaces/api/dto/v1" - "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/apiserver/utils/bcode" + "github.com/oam-dev/kubevela/pkg/utils/schema" ) type definition struct { @@ -69,7 +69,7 @@ func (d *definition) GetWebServiceRoute() *restful.WebService { Filter(d.RbacService.CheckPerm("definition", "update")). Metadata(restfulspec.KeyOpenAPITags, tags). Reads(apis.UpdateUISchemaRequest{}). - Returns(200, "update successfully", utils.UISchema{}). + Returns(200, "update successfully", schema.UISchema{}). Writes(apis.DetailDefinitionResponse{}).Do(returns200, returns500)) ws.Route(ws.PUT("/{definitionName}/status").To(d.updateDefinitionStatus). @@ -77,7 +77,7 @@ func (d *definition) GetWebServiceRoute() *restful.WebService { Filter(d.RbacService.CheckPerm("definition", "update")). Metadata(restfulspec.KeyOpenAPITags, tags). Reads(apis.UpdateDefinitionStatusRequest{}). - Returns(200, "update successfully", utils.UISchema{}). + Returns(200, "update successfully", schema.UISchema{}). Writes(apis.DetailDefinitionResponse{}).Do(returns200, returns500)) ws.Filter(authCheckFilter) diff --git a/pkg/apiserver/interfaces/api/dto/v1/types.go b/pkg/apiserver/interfaces/api/dto/v1/types.go index db7c2c8f5..1fcf21f53 100644 --- a/pkg/apiserver/interfaces/api/dto/v1/types.go +++ b/pkg/apiserver/interfaces/api/dto/v1/types.go @@ -33,9 +33,9 @@ import ( "github.com/oam-dev/kubevela/apis/types" "github.com/oam-dev/kubevela/pkg/addon" "github.com/oam-dev/kubevela/pkg/apiserver/domain/model" - "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/cloudprovider" "github.com/oam-dev/kubevela/pkg/config" + "github.com/oam-dev/kubevela/pkg/utils/schema" ) var ( @@ -166,7 +166,7 @@ type DetailAddonResponse struct { addon.Meta APISchema *openapi3.Schema `json:"schema"` - UISchema utils.UISchema `json:"uiSchema"` + UISchema schema.UISchema `json:"uiSchema"` // More details about the addon, e.g. README Detail string `json:"detail,omitempty"` @@ -238,7 +238,7 @@ type ConfigTemplate struct { type ConfigTemplateDetail struct { ConfigTemplate APISchema *openapi3.Schema `json:"schema"` - UISchema utils.UISchema `json:"uiSchema"` + UISchema schema.UISchema `json:"uiSchema"` } // Config define the metadata of a config @@ -898,13 +898,13 @@ type ListDefinitionResponse struct { type DetailDefinitionResponse struct { DefinitionBase APISchema *openapi3.Schema `json:"schema"` - UISchema utils.UISchema `json:"uiSchema"` + UISchema schema.UISchema `json:"uiSchema"` } // UpdateUISchemaRequest the request body struct about updated ui schema type UpdateUISchemaRequest struct { - DefinitionType string `json:"type"` - UISchema utils.UISchema `json:"uiSchema"` + DefinitionType string `json:"type"` + UISchema schema.UISchema `json:"uiSchema"` } // UpdateDefinitionStatusRequest the request body struct about updated definition diff --git a/pkg/appfile/dryrun/dryrun.go b/pkg/appfile/dryrun/dryrun.go index 7a7aa71d6..187518bc5 100644 --- a/pkg/appfile/dryrun/dryrun.go +++ b/pkg/appfile/dryrun/dryrun.go @@ -40,7 +40,6 @@ import ( "github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha1" "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" "github.com/oam-dev/kubevela/apis/types" - apiutils "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/appfile" "github.com/oam-dev/kubevela/pkg/cue/definition" "github.com/oam-dev/kubevela/pkg/oam" @@ -59,16 +58,21 @@ type DryRun interface { // NewDryRunOption creates a dry-run option func NewDryRunOption(c client.Client, cfg *rest.Config, dm discoverymapper.DiscoveryMapper, pd *packages.PackageDiscover, as []oam.Object, serverSideDryRun bool) *Option { - return &Option{c, dm, pd, cfg, as, serverSideDryRun} + parser := appfile.NewDryRunApplicationParser(c, dm, pd, as) + return &Option{c, dm, pd, parser, parser.GenerateAppFileFromApp, cfg, as, serverSideDryRun} } +// GenerateAppFileFunc generate the app file model from an application +type GenerateAppFileFunc func(ctx context.Context, app *v1beta1.Application) (*appfile.Appfile, error) + // Option contains options to execute dry-run type Option struct { Client client.Client DiscoveryMapper discoverymapper.DiscoveryMapper PackageDiscover *packages.PackageDiscover - - cfg *rest.Config + Parser *appfile.Parser + GenerateAppFile GenerateAppFileFunc + cfg *rest.Config // Auxiliaries are capability definitions used to parse application. // DryRun will use capabilities in Auxiliaries as higher priority than // getting one from cluster. @@ -137,12 +141,10 @@ func (d *Option) ValidateApp(ctx context.Context, filename string) error { // resources but not persist them into cluster. func (d *Option) ExecuteDryRun(ctx context.Context, application *v1beta1.Application) ([]*types.ComponentManifest, []*unstructured.Unstructured, error) { app := application.DeepCopy() - parser := appfile.NewDryRunApplicationParser(d.Client, d.DiscoveryMapper, d.PackageDiscover, d.Auxiliaries) if app.Namespace != "" { ctx = oamutil.SetNamespaceInCtx(ctx, app.Namespace) } - generateCtx := apiutils.WithProject(ctx, "") - appFile, err := parser.GenerateAppFileFromApp(generateCtx, app) + appFile, err := d.GenerateAppFile(ctx, app) if err != nil { return nil, nil, errors.WithMessage(err, "cannot generate appFile from application") } diff --git a/pkg/config/factory.go b/pkg/config/factory.go index 30e9f2021..a4736efe8 100644 --- a/pkg/config/factory.go +++ b/pkg/config/factory.go @@ -42,13 +42,12 @@ import ( "github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha1" "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" "github.com/oam-dev/kubevela/apis/types" - "github.com/oam-dev/kubevela/pkg/apiserver/domain/model" - "github.com/oam-dev/kubevela/pkg/apiserver/utils" icontext "github.com/oam-dev/kubevela/pkg/config/context" "github.com/oam-dev/kubevela/pkg/config/writer" "github.com/oam-dev/kubevela/pkg/cue" "github.com/oam-dev/kubevela/pkg/cue/script" "github.com/oam-dev/kubevela/pkg/oam" + "github.com/oam-dev/kubevela/pkg/oam/util" "github.com/oam-dev/kubevela/pkg/utils/apply" ) @@ -787,13 +786,13 @@ func (k *kubeConfigFactory) CreateOrUpdateDistribution(ctx context.Context, ns, Name: name, Namespace: ns, Labels: map[string]string{ - model.LabelSourceOfTruth: model.FromInner, + types.LabelSourceOfTruth: types.FromInner, // This label will override the secret label, then change the catalog of the distributed secrets. types.LabelConfigCatalog: types.CatalogConfigDistribution, }, Annotations: map[string]string{ types.AnnotationConfigDistributionSpec: string(reqByte), - oam.AnnotationPublishVersion: utils.GenerateVersion("config"), + oam.AnnotationPublishVersion: util.GenerateVersion("config"), }, }, Spec: v1beta1.ApplicationSpec{ @@ -821,7 +820,7 @@ func (k *kubeConfigFactory) CreateOrUpdateDistribution(ctx context.Context, ns, func (k *kubeConfigFactory) ListDistributions(ctx context.Context, ns string) ([]*Distribution, error) { var apps v1beta1.ApplicationList if err := k.cli.List(ctx, &apps, client.MatchingLabels{ - model.LabelSourceOfTruth: model.FromInner, + types.LabelSourceOfTruth: types.FromInner, types.LabelConfigCatalog: types.CatalogConfigDistribution, }, client.InNamespace(ns)); err != nil { return nil, err diff --git a/pkg/oam/util/version.go b/pkg/oam/util/version.go new file mode 100644 index 000000000..5bad7a4b9 --- /dev/null +++ b/pkg/oam/util/version.go @@ -0,0 +1,34 @@ +/* +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 util + +import ( + "fmt" + "time" + + "cuelang.org/go/pkg/strings" +) + +// GenerateVersion Generate version numbers by time +func GenerateVersion(pre string) string { + timeStr := time.Now().Format("20060102150405.000") + timeStr = strings.Replace(timeStr, ".", "", 1) + if pre != "" { + return fmt.Sprintf("%s-%s", pre, timeStr) + } + return timeStr +} diff --git a/pkg/oam/util/version_test.go b/pkg/oam/util/version_test.go new file mode 100644 index 000000000..7bba8c3d3 --- /dev/null +++ b/pkg/oam/util/version_test.go @@ -0,0 +1,35 @@ +/* +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 util + +import ( + "strings" + + "github.com/google/go-cmp/cmp" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Test version utils", func() { + It("Test New version function", func() { + s := GenerateVersion("") + Expect(s).ShouldNot(BeNil()) + + s2 := GenerateVersion("pre") + Expect(cmp.Diff(strings.HasPrefix(s2, "pre-"), true)).ShouldNot(BeNil()) + }) +}) diff --git a/pkg/utils/app/app.go b/pkg/utils/app/app.go index 9a56917c0..14ac7f682 100644 --- a/pkg/utils/app/app.go +++ b/pkg/utils/app/app.go @@ -29,7 +29,6 @@ import ( apicommon "github.com/oam-dev/kubevela/apis/core.oam.dev/common" "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" - apiutils "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/component" "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev/v1alpha2/application" "github.com/oam-dev/kubevela/pkg/controller/utils" @@ -45,10 +44,16 @@ var ErrPublishVersionNotChange = errors.Errorf("the PublishVersion is not change // ErrRevisionNotChange - var ErrRevisionNotChange = errors.Errorf("the revision is not changed") -// RollbackApplicationWithRevision make the exist application rollback to specified revision. -func RollbackApplicationWithRevision(ctx context.Context, cli client.Client, appName, appNamespace, revisionName, publishVersion string) (*v1beta1.ApplicationRevision, *v1beta1.Application, error) { +// RevisionContextKey if this key is exit in ctx, we should use it preferentially +var RevisionContextKey = "revision-context-key" - revisionCtx := apiutils.WithProject(ctx, "") +// RollbackApplicationWithRevision make the exist application rollback to specified revision. +// revisionCtx the context used to manage the application revision. +func RollbackApplicationWithRevision(ctx context.Context, cli client.Client, appName, appNamespace, revisionName, publishVersion string) (*v1beta1.ApplicationRevision, *v1beta1.Application, error) { + revisionCtx, ok := ctx.Value(&RevisionContextKey).(context.Context) + if !ok { + revisionCtx = ctx + } // check revision revs, err := application.GetSortedAppRevisions(revisionCtx, cli, appName, appNamespace) if err != nil { diff --git a/pkg/utils/cache.go b/pkg/utils/cache.go new file mode 100644 index 000000000..c66dbb81e --- /dev/null +++ b/pkg/utils/cache.go @@ -0,0 +1,102 @@ +/* +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 utils + +import ( + "context" + "sync" + "time" +) + +// memoryCache memory cache, support time expired +type memoryCache struct { + data interface{} + cacheDuration time.Duration + startTime time.Time +} + +// NewMemoryCache new memory cache instance +func newMemoryCache(data interface{}, cacheDuration time.Duration) *memoryCache { + mc := &memoryCache{data: data, cacheDuration: cacheDuration, startTime: time.Now()} + return mc +} + +// IsExpired whether the cache data expires +func (m *memoryCache) IsExpired() bool { + if m.cacheDuration <= 0 { + return false + } + return time.Now().After(m.startTime.Add(m.cacheDuration)) +} + +// GetData get cache data +func (m *memoryCache) GetData() interface{} { + return m.data +} + +// MemoryCacheStore a sample memory cache instance, if data set cache duration, will auto clear after timeout. +// But, Expired cleanup is not necessarily accurate, it has a 3-second window. +type MemoryCacheStore struct { + store sync.Map +} + +// NewMemoryCacheStore memory cache store +func NewMemoryCacheStore(ctx context.Context) *MemoryCacheStore { + mcs := &MemoryCacheStore{ + store: sync.Map{}, + } + go mcs.run(ctx) + return mcs +} + +func (m *MemoryCacheStore) run(ctx context.Context) { + ticker := time.NewTicker(time.Second * 3) + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + m.store.Range(func(key, value interface{}) bool { + if value.(*memoryCache).IsExpired() { + m.store.Delete(key) + } + return true + }) + } + } +} + +// Put cache data, if cacheDuration>0, store will clear data after timeout. +func (m *MemoryCacheStore) Put(key, value interface{}, cacheDuration time.Duration) { + mc := newMemoryCache(value, cacheDuration) + m.store.Store(key, mc) +} + +// Delete cache data from store +func (m *MemoryCacheStore) Delete(key interface{}) { + m.store.Delete(key) +} + +// Get cache data from store, if not exist or timeout, will return nil +func (m *MemoryCacheStore) Get(key interface{}) (value interface{}) { + mc, ok := m.store.Load(key) + if ok && !mc.(*memoryCache).IsExpired() { + return mc.(*memoryCache).GetData() + } + return nil +} diff --git a/pkg/utils/cache_test.go b/pkg/utils/cache_test.go new file mode 100644 index 000000000..a51b2d7fd --- /dev/null +++ b/pkg/utils/cache_test.go @@ -0,0 +1,78 @@ +/* +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 utils + +import ( + "context" + "fmt" + "testing" + "time" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Test cache utils", func() { + It("should return false for IsExpired()", func() { + c := newMemoryCache("test", 10*time.Hour) + Expect(c.IsExpired()).Should(BeFalse()) + }) + + It("test cache store", func() { + store := NewMemoryCacheStore(context.TODO()) + store.Put("test", "test data", time.Second*2) + store.Put("test2", "test data", 0) + store.Put("test3", "test data", -1) + time.Sleep(3 * time.Second) + Expect(store.Get("test")).Should(BeNil()) + Expect(store.Get("test2")).Should(Equal("test data")) + Expect(store.Get("test3")).Should(Equal("test data")) + }) + + It("test cache store delete key", func() { + store := NewMemoryCacheStore(context.TODO()) + store.Put("test", "test data", time.Minute*2) + store.Delete("test") + Expect(store.Get("test")).Should(BeNil()) + }) +}) + +var store *MemoryCacheStore + +// BenchmarkWrite +func BenchmarkWrite(b *testing.B) { + store = NewMemoryCacheStore(context.TODO()) + + for i := 0; i < b.N; i++ { + store.Put(fmt.Sprintf("%d", i), i, 0) + } +} + +// BenchmarkRead +func BenchmarkRead(b *testing.B) { + for i := 0; i < b.N; i++ { + store.Get(fmt.Sprintf("%d", i)) + } +} + +// BenchmarkRW +func BenchmarkRW(b *testing.B) { + for i := 0; i < b.N; i++ { + store.Put(fmt.Sprintf("%d", i), i, 1) + store.Get(fmt.Sprintf("%d", i)) + } +} diff --git a/pkg/utils/helm/helm_helper.go b/pkg/utils/helm/helm_helper.go index cd167a8fe..7b36c8008 100644 --- a/pkg/utils/helm/helm_helper.go +++ b/pkg/utils/helm/helm_helper.go @@ -49,7 +49,6 @@ import ( k8scmdutil "k8s.io/kubectl/pkg/cmd/util" "sigs.k8s.io/yaml" - utils2 "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/utils" "github.com/oam-dev/kubevela/pkg/utils/common" cmdutil "github.com/oam-dev/kubevela/pkg/utils/util" @@ -68,7 +67,7 @@ type ChartValues struct { // Helper provides helper functions for common Helm operations type Helper struct { - cache *utils2.MemoryCacheStore + cache *utils.MemoryCacheStore } // NewHelper creates a Helper @@ -79,7 +78,7 @@ func NewHelper() *Helper { // NewHelperWithCache creates a Helper with cache usually used by apiserver func NewHelperWithCache() *Helper { return &Helper{ - cache: utils2.NewMemoryCacheStore(context.Background()), + cache: utils.NewMemoryCacheStore(context.Background()), } } diff --git a/pkg/apiserver/utils/ui_schema.go b/pkg/utils/schema/ui_schema.go similarity index 95% rename from pkg/apiserver/utils/ui_schema.go rename to pkg/utils/schema/ui_schema.go index b3b8520a6..6f5f9a9b2 100644 --- a/pkg/apiserver/utils/ui_schema.go +++ b/pkg/utils/schema/ui_schema.go @@ -14,11 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. */ -package utils +package schema import ( "fmt" "strings" + + "github.com/oam-dev/kubevela/pkg/utils" ) // UISchema ui schema @@ -85,7 +87,7 @@ func (c Condition) Validate() error { if c.Action != "enable" && c.Action != "disable" && c.Action != "" { return fmt.Errorf("the action of the condition only supports enable, disable or leave it empty") } - if c.Op != "" && !StringsContain([]string{"==", "!=", "in"}, c.Op) { + if c.Op != "" && !utils.StringsContain([]string{"==", "!=", "in"}, c.Op) { return fmt.Errorf("the op of the condition must be `==` 、`!=` and `in`") } return nil @@ -180,13 +182,3 @@ func RenderLabel(source interface{}) string { return FirstUpper(fmt.Sprintf("%v", v)) } } - -// StringsContain strings contain -func StringsContain(items []string, source string) bool { - for _, item := range items { - if item == source { - return true - } - } - return false -} diff --git a/pkg/apiserver/utils/ui_schema_test.go b/pkg/utils/schema/ui_schema_test.go similarity index 99% rename from pkg/apiserver/utils/ui_schema_test.go rename to pkg/utils/schema/ui_schema_test.go index 05b8c1f50..f02403fad 100644 --- a/pkg/apiserver/utils/ui_schema_test.go +++ b/pkg/utils/schema/ui_schema_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package utils +package schema import ( . "github.com/onsi/ginkgo" diff --git a/pkg/velaql/providers/query/handler.go b/pkg/velaql/providers/query/handler.go index faf336108..4b2338a00 100644 --- a/pkg/velaql/providers/query/handler.go +++ b/pkg/velaql/providers/query/handler.go @@ -41,7 +41,6 @@ import ( "github.com/kubevela/workflow/pkg/types" "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" - "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/multicluster" querytypes "github.com/oam-dev/kubevela/pkg/velaql/providers/query/types" ) @@ -230,7 +229,7 @@ func (h *provider) CollectLogsInPod(ctx monitorContext.Context, wfCtx wfContext. if err = val.UnmarshalTo(opts); err != nil { return errors.Wrapf(err, "invalid log options content") } - cliCtx := utils.ContextWithUserInfo(multicluster.ContextWithClusterName(ctx, cluster)) + cliCtx := multicluster.ContextWithClusterName(ctx, cluster) h.cfg.Wrap(pkgmulticluster.NewTransportWrapper()) clientSet, err := kubernetes.NewForConfig(h.cfg) if err != nil { diff --git a/pkg/velaql/suite_test.go b/pkg/velaql/suite_test.go index f62229b0d..fdea3f6e4 100644 --- a/pkg/velaql/suite_test.go +++ b/pkg/velaql/suite_test.go @@ -33,7 +33,6 @@ import ( "github.com/kubevela/workflow/pkg/cue/packages" - "github.com/oam-dev/kubevela/pkg/apiserver/infrastructure/clients" "github.com/oam-dev/kubevela/pkg/oam/discoverymapper" "github.com/oam-dev/kubevela/pkg/utils/common" ) @@ -69,7 +68,6 @@ var _ = BeforeSuite(func(done Done) { Expect(err).Should(BeNil()) Expect(k8sClient).ToNot(BeNil()) By("new kube client success") - clients.SetKubeClient(k8sClient) dm, err := discoverymapper.New(cfg) Expect(err).To(BeNil()) diff --git a/references/cli/addon.go b/references/cli/addon.go index 77319f28d..3a0965b16 100644 --- a/references/cli/addon.go +++ b/references/cli/addon.go @@ -41,7 +41,6 @@ import ( "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" "github.com/oam-dev/kubevela/apis/types" pkgaddon "github.com/oam-dev/kubevela/pkg/addon" - "github.com/oam-dev/kubevela/pkg/apiserver/domain/service" "github.com/oam-dev/kubevela/pkg/oam" addonutil "github.com/oam-dev/kubevela/pkg/utils/addon" "github.com/oam-dev/kubevela/pkg/utils/apply" @@ -231,18 +230,6 @@ func AdditionalEndpointPrinter(ctx context.Context, c common.Args, k8sClient cli fmt.Println("Get application endpoints error:", err) return } - if name == "velaux" { - if !isUpgrade { - fmt.Printf("\nInitialized admin username and password: admin / %s \n\n", service.InitAdminPassword) - } - fmt.Println(`To open the dashboard directly by port-forward:`) - fmt.Println() - fmt.Println(` vela port-forward -n vela-system addon-velaux 9082:80`) - fmt.Println() - fmt.Println(`Select "local | velaux | velaux" from the prompt.`) - fmt.Println() - fmt.Println(`Please refer to https://kubevela.io/docs/reference/addons/velaux for more VelaUX addon installation and visiting method.`) - } if len(info) > 0 { fmt.Println(info) } diff --git a/references/cli/uischema.go b/references/cli/uischema.go index 09bc42d08..2870f3f0f 100644 --- a/references/cli/uischema.go +++ b/references/cli/uischema.go @@ -35,8 +35,8 @@ import ( "sigs.k8s.io/yaml" "github.com/oam-dev/kubevela/apis/types" - "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/utils/common" + "github.com/oam-dev/kubevela/pkg/utils/schema" "github.com/oam-dev/kubevela/pkg/utils/util" ) @@ -152,7 +152,7 @@ func applyUISchemaFile(client client.Client, uischemaFile string) error { } func addDefinitionUISchema(ctx context.Context, client client.Client, name, defType, configRaw string) error { - var uiParameters []*utils.UIParameter + var uiParameters []*schema.UIParameter err := yaml.Unmarshal([]byte(configRaw), &uiParameters) if err != nil { return err diff --git a/test/e2e-apiserver-test/definition_test.go b/test/e2e-apiserver-test/definition_test.go index 3077eba11..7e096069e 100644 --- a/test/e2e-apiserver-test/definition_test.go +++ b/test/e2e-apiserver-test/definition_test.go @@ -25,9 +25,9 @@ import ( corev1 "k8s.io/api/core/v1" apisv1 "github.com/oam-dev/kubevela/pkg/apiserver/interfaces/api/dto/v1" - "github.com/oam-dev/kubevela/pkg/apiserver/utils" "github.com/oam-dev/kubevela/pkg/oam/util" "github.com/oam-dev/kubevela/pkg/utils/common" + "github.com/oam-dev/kubevela/pkg/utils/schema" ) var _ = Describe("Test definitions rest api", func() { @@ -72,7 +72,7 @@ var _ = Describe("Test definitions rest api", func() { defer GinkgoRecover() req := apisv1.UpdateUISchemaRequest{ DefinitionType: "component", - UISchema: utils.UISchema{ + UISchema: schema.UISchema{ { JSONKey: "image", UIType: "ImageInput", @@ -80,7 +80,7 @@ var _ = Describe("Test definitions rest api", func() { }, } res := put("/definitions/webservice/uischema", req) - var schema utils.UISchema + var schema schema.UISchema Expect(decodeResponseBody(res, &schema)).Should(Succeed()) }) @@ -88,11 +88,11 @@ var _ = Describe("Test definitions rest api", func() { defer GinkgoRecover() req := apisv1.UpdateUISchemaRequest{ DefinitionType: "component", - UISchema: utils.UISchema{ + UISchema: schema.UISchema{ { JSONKey: "image", UIType: "ImageInput", - Conditions: []utils.Condition{ + Conditions: []schema.Condition{ { JSONKey: "", }, @@ -105,11 +105,11 @@ var _ = Describe("Test definitions rest api", func() { req2 := apisv1.UpdateUISchemaRequest{ DefinitionType: "component", - UISchema: utils.UISchema{ + UISchema: schema.UISchema{ { JSONKey: "image", UIType: "ImageInput", - Conditions: []utils.Condition{ + Conditions: []schema.Condition{ { JSONKey: "secretName", Value: "",