Files
kubevela/pkg/apiserver/sync/cr2ux.go
barnettZQG 044c4bf73c Feat: add RBAC support (#3493)
* Feat: add the rbac data model

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: add some api about the project

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: add CRUD about the project and the project user

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: add CRUD about the role and perm check filter function

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: update swagger config

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: add default roles and perm policies

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: add perm check filter for all webservice

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: change the method that find project name

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: query applications and envs by user perm

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: support get login user info

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Fix: change default permissions

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: change PermPolicy to Permission

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Feat: add some unit test and fix the e2e test error

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Fix: change some comment word

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>

* Fix: e2e api path error

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>
2022-03-28 16:03:11 +08:00

195 lines
6.0 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 sync
import (
"context"
"sync"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/pkg/apiserver/datastore"
"github.com/oam-dev/kubevela/pkg/apiserver/log"
"github.com/oam-dev/kubevela/pkg/apiserver/model"
"github.com/oam-dev/kubevela/pkg/apiserver/rest/usecase"
"github.com/oam-dev/kubevela/pkg/oam"
)
// CheckSoTFromCR will check the source of truth of the application
func CheckSoTFromCR(targetApp *v1beta1.Application) string {
if sot := targetApp.Annotations[model.LabelSourceOfTruth]; sot != "" {
return sot
}
// if no LabelSourceOfTruth label, it means the app is existing ones, check the existing labels and annotations
if _, appName := targetApp.Annotations[oam.AnnotationAppName]; appName {
return model.FromUX
}
// no labels mean it's created by K8s resources.
return model.FromCR
}
// CheckSoTFromAppMeta will check the source of truth marked in datastore
func (c *CR2UX) CheckSoTFromAppMeta(ctx context.Context, appName, namespace string, sotFromCR string) string {
app, _, err := c.getApp(ctx, appName, namespace)
if err != nil {
return sotFromCR
}
if app.Labels == nil || app.Labels[model.LabelSourceOfTruth] == "" {
return sotFromCR
}
return app.Labels[model.LabelSourceOfTruth]
}
// getApp will return the app and appname if exists
func (c *CR2UX) getApp(ctx context.Context, name, namespace string) (*model.Application, string, error) {
alreadyCreated := &model.Application{Name: formatAppComposedName(name, namespace)}
err1 := c.ds.Get(ctx, alreadyCreated)
if err1 == nil {
return alreadyCreated, alreadyCreated.Name, nil
}
// check if it's created the first in database
existApp := &model.Application{Name: name}
err2 := c.ds.Get(ctx, existApp)
if err2 == nil {
en := existApp.Labels[model.LabelSyncNamespace]
// it means the namespace/app is not created yet, the appname is occupied by app from other namespace
if en != namespace {
return nil, formatAppComposedName(name, namespace), err1
}
return existApp, name, nil
}
return nil, name, err2
}
// CR2UX provides the Add/Update/Delete method
type CR2UX struct {
ds datastore.DataStore
cli client.Client
cache sync.Map
usecases map[string]interface{}
}
func formatAppComposedName(name, namespace string) string {
return name + "-" + namespace
}
// we need to prevent the case that one app is deleted ant it's name is pure appName, then other app with namespace suffix will be mixed
func (c *CR2UX) getAppMetaName(ctx context.Context, name, namespace string) string {
_, appName, _ := c.getApp(ctx, name, namespace)
return appName
}
// AddOrUpdate will sync application CR to storage of VelaUX automatically
func (c *CR2UX) AddOrUpdate(ctx context.Context, targetApp *v1beta1.Application) error {
ds := c.ds
if !c.shouldSync(ctx, targetApp, false) {
return nil
}
dsApp, err := c.ConvertApp2DatastoreApp(ctx, targetApp)
if err != nil {
log.Logger.Errorf("Convert App to data store err %v", err)
return err
}
pu, ok := c.usecases["project"].(usecase.ProjectUsecase)
if !ok {
log.Logger.Warnf("not provide project usecase instance")
}
if err = StoreProject(ctx, dsApp.AppMeta.Project, ds, pu); err != nil {
log.Logger.Errorf("get or create project for sync process err %v", err)
return err
}
if err = StoreEnv(ctx, dsApp, ds); err != nil {
log.Logger.Errorf("Store Env Metadata to data store err %v", err)
return err
}
if err = StoreEnvBinding(ctx, dsApp.Eb, ds); err != nil {
log.Logger.Errorf("Store EnvBinding Metadata to data store err %v", err)
return err
}
if err = StoreComponents(ctx, dsApp.AppMeta.Name, dsApp.Comps, ds); err != nil {
log.Logger.Errorf("Store Components Metadata to data store err %v", err)
return err
}
if err = StorePolicy(ctx, dsApp.AppMeta.Name, dsApp.Policies, ds); err != nil {
log.Logger.Errorf("Store Policy Metadata to data store err %v", err)
return err
}
if err = StoreWorkflow(ctx, dsApp, ds); err != nil {
log.Logger.Errorf("Store Workflow Metadata to data store err %v", err)
return err
}
/*
if err = StoreTargets(ctx, dsApp, ds); err != nil {
log.Logger.Errorf("Store targets to data store err %v", err)
return err
}
*/
if err = StoreAppMeta(ctx, dsApp, ds); err != nil {
log.Logger.Errorf("Store App Metadata to data store err %v", err)
return err
}
// update cache
c.syncCache(dsApp.AppMeta.PrimaryKey(), targetApp.Generation, int64(len(dsApp.Targets)))
return nil
}
// DeleteApp will delete the application as the CR was deleted
func (c *CR2UX) DeleteApp(ctx context.Context, targetApp *v1beta1.Application) error {
ds := c.ds
if !c.shouldSync(ctx, targetApp, true) {
return nil
}
appName := c.getAppMetaName(ctx, targetApp.Name, targetApp.Namespace)
_ = ds.Delete(ctx, &model.Application{Name: appName})
cmps, err := ds.List(ctx, &model.ApplicationComponent{AppPrimaryKey: appName}, &datastore.ListOptions{})
if err != nil {
return err
}
for _, entity := range cmps {
comp := entity.(*model.ApplicationComponent)
if comp.Creator == model.AutoGenComp {
_ = ds.Delete(ctx, comp)
}
}
plcs, err := ds.List(ctx, &model.ApplicationPolicy{AppPrimaryKey: appName}, &datastore.ListOptions{})
if err != nil {
return err
}
for _, entity := range plcs {
comp := entity.(*model.ApplicationPolicy)
if comp.Creator == model.AutoGenPolicy {
_ = ds.Delete(ctx, comp)
}
}
_ = ds.Delete(ctx, &model.Workflow{Name: model.AutoGenWorkflowNamePrefix + appName})
return nil
}