forked from koderover/zadig
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request koderover#985 from flyer103/feature/cache-pvc
support pvc cache
- Loading branch information
Showing
39 changed files
with
1,023 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/* | ||
Copyright 2022 The KodeRover 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 migrate | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
|
||
internalmodels "github.com/koderover/zadig/pkg/cli/upgradeassistant/internal/repository/models" | ||
internalmongodb "github.com/koderover/zadig/pkg/cli/upgradeassistant/internal/repository/mongodb" | ||
"github.com/koderover/zadig/pkg/cli/upgradeassistant/internal/upgradepath" | ||
"github.com/koderover/zadig/pkg/tool/log" | ||
"github.com/koderover/zadig/pkg/types" | ||
) | ||
|
||
func init() { | ||
upgradepath.RegisterHandler("1.9.0", "1.10.0", V190ToV1100) | ||
upgradepath.RegisterHandler("1.10.0", "1.9.0", V1100ToV190) | ||
} | ||
|
||
// V190ToV1100 migrates data `caches` and `pre_build.clean_workspace` fields in `zadig.module_build` and `zadig.module_testing` | ||
// to new fields `cache_enable`, `cache_dir_type` and `cache_user_dir`. | ||
func V190ToV1100() error { | ||
log.Info("Migrate data from 1.9.0 to 1.10.0.") | ||
|
||
log.Info("Migrate data in `zadig.module_build`.") | ||
if err := migrateModuleBuild(); err != nil { | ||
return fmt.Errorf("failed to migrate data in `zadig.module_build`: %s", err) | ||
} | ||
|
||
log.Info("Migrate data in `zadig.module_testing`.") | ||
if err := migrateModuleTesting(); err != nil { | ||
return fmt.Errorf("failed to migrate data in `zadig.module_testing`: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Since the old data has not been changed, no changes are required. | ||
func V1100ToV190() error { | ||
log.Info("Rollback data from 1.10.0 to 1.9.0") | ||
return nil | ||
} | ||
|
||
func migrateModuleBuild() error { | ||
buildCol := internalmongodb.NewBuildColl() | ||
|
||
builds, err := buildCol.List(&internalmongodb.BuildListOption{}) | ||
if err != nil { | ||
return fmt.Errorf("failed to list all data in `zadig.module_build`: %s", err) | ||
} | ||
|
||
var ms []mongo.WriteModel | ||
for _, build := range builds { | ||
if err := migrateOneBuild(build); err != nil { | ||
log.Errorf("Failed to migrate data: %v. Err: %s", build, err) | ||
continue | ||
} | ||
|
||
ms = append(ms, | ||
mongo.NewUpdateOneModel(). | ||
SetFilter(bson.D{{"_id", build.ID}}). | ||
SetUpdate(bson.D{{"$set", | ||
bson.D{ | ||
{"cache_enable", build.CacheEnable}, | ||
{"cache_dir_type", build.CacheDirType}, | ||
{"cache_user_dir", build.CacheUserDir}}}, | ||
}), | ||
) | ||
} | ||
|
||
_, err = buildCol.BulkWrite(context.TODO(), ms) | ||
|
||
return err | ||
} | ||
|
||
func migrateModuleTesting() error { | ||
testingCol := internalmongodb.NewTestingColl() | ||
|
||
testings, err := testingCol.List(&internalmongodb.ListTestOption{}) | ||
if err != nil { | ||
return fmt.Errorf("failed to list all data in `zadig.module_testing`: %s", err) | ||
} | ||
|
||
var ms []mongo.WriteModel | ||
for _, testing := range testings { | ||
if err := migrateOneTesting(testing); err != nil { | ||
log.Errorf("Failed to migrate data: %v. Err: %s", testing, err) | ||
continue | ||
} | ||
|
||
ms = append(ms, | ||
mongo.NewUpdateOneModel(). | ||
SetFilter(bson.D{{"_id", testing.ID}}). | ||
SetUpdate(bson.D{{"$set", | ||
bson.D{ | ||
{"cache_enable", testing.CacheEnable}, | ||
{"cache_dir_type", testing.CacheDirType}, | ||
{"cache_user_dir", testing.CacheUserDir}}}, | ||
}), | ||
) | ||
} | ||
|
||
_, err = testingCol.BulkWrite(context.TODO(), ms) | ||
|
||
return err | ||
} | ||
|
||
func migrateOneBuild(build *internalmodels.Build) error { | ||
if build.PreBuild != nil && build.PreBuild.CleanWorkspace { | ||
return nil | ||
} | ||
|
||
build.CacheEnable = true | ||
|
||
if len(build.Caches) == 0 { | ||
build.CacheDirType = types.WorkspaceCacheDir | ||
return nil | ||
} | ||
|
||
build.CacheDirType = types.UserDefinedCacheDir | ||
build.CacheUserDir = build.Caches[0] | ||
|
||
return nil | ||
} | ||
|
||
func migrateOneTesting(testing *internalmodels.Testing) error { | ||
if testing.PreTest != nil && testing.PreTest.CleanWorkspace { | ||
return nil | ||
} | ||
|
||
testing.CacheEnable = true | ||
|
||
if len(testing.Caches) == 0 { | ||
testing.CacheDirType = types.WorkspaceCacheDir | ||
return nil | ||
} | ||
|
||
testing.CacheDirType = types.UserDefinedCacheDir | ||
testing.CacheUserDir = testing.Caches[0] | ||
|
||
return nil | ||
} |
46 changes: 46 additions & 0 deletions
46
pkg/cli/upgradeassistant/internal/repository/models/build.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
Copyright 2022 The KodeRover 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 models | ||
|
||
import ( | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
|
||
"github.com/koderover/zadig/pkg/types" | ||
) | ||
|
||
type Build struct { | ||
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` | ||
PreBuild *PreBuild `bson:"pre_build" json:"pre_build"` | ||
|
||
// TODO: Deprecated. | ||
Caches []string `bson:"caches" json:"caches"` | ||
|
||
// New since V1.10.0. | ||
CacheEnable bool `bson:"cache_enable" json:"cache_enable"` | ||
CacheDirType types.CacheDirType `bson:"cache_dir_type" json:"cache_dir_type"` | ||
CacheUserDir string `bson:"cache_user_dir" json:"cache_user_dir"` | ||
} | ||
|
||
// PreBuild prepares an environment for a job | ||
type PreBuild struct { | ||
// TODO: Deprecated. | ||
CleanWorkspace bool `bson:"clean_workspace" json:"clean_workspace"` | ||
} | ||
|
||
func (Build) TableName() string { | ||
return "module_build" | ||
} |
46 changes: 46 additions & 0 deletions
46
pkg/cli/upgradeassistant/internal/repository/models/testing.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
Copyright 2022 The KodeRover 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 models | ||
|
||
import ( | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
|
||
"github.com/koderover/zadig/pkg/types" | ||
) | ||
|
||
type Testing struct { | ||
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` | ||
PreTest *PreTest `bson:"pre_test" json:"pre_test"` | ||
|
||
// TODO: Deprecated. | ||
Caches []string `bson:"caches" json:"caches"` | ||
|
||
// New since V1.10.0. | ||
CacheEnable bool `bson:"cache_enable" json:"cache_enable"` | ||
CacheDirType types.CacheDirType `bson:"cache_dir_type" json:"cache_dir_type"` | ||
CacheUserDir string `bson:"cache_user_dir" json:"cache_user_dir"` | ||
} | ||
|
||
// PreTest prepares an environment for a job | ||
type PreTest struct { | ||
// TODO: Deprecated. | ||
CleanWorkspace bool `bson:"clean_workspace" json:"clean_workspace"` | ||
} | ||
|
||
func (Testing) TableName() string { | ||
return "module_testing" | ||
} |
67 changes: 67 additions & 0 deletions
67
pkg/cli/upgradeassistant/internal/repository/mongodb/build.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright 2022 The KodeRover 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 mongodb | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
|
||
"github.com/koderover/zadig/pkg/cli/upgradeassistant/internal/repository/models" | ||
"github.com/koderover/zadig/pkg/microservice/aslan/config" | ||
mongotool "github.com/koderover/zadig/pkg/tool/mongo" | ||
) | ||
|
||
type BuildListOption struct { | ||
} | ||
|
||
type BuildColl struct { | ||
*mongo.Collection | ||
|
||
coll string | ||
} | ||
|
||
func NewBuildColl() *BuildColl { | ||
name := models.Build{}.TableName() | ||
return &BuildColl{Collection: mongotool.Database(config.MongoDatabase()).Collection(name), coll: name} | ||
} | ||
|
||
func (c *BuildColl) List(opt *BuildListOption) ([]*models.Build, error) { | ||
if opt == nil { | ||
return nil, errors.New("nil ListOption") | ||
} | ||
|
||
query := bson.M{} | ||
|
||
var resp []*models.Build | ||
ctx := context.Background() | ||
opts := options.Find() | ||
cursor, err := c.Collection.Find(ctx, query, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = cursor.All(ctx, &resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return resp, nil | ||
} |
66 changes: 66 additions & 0 deletions
66
pkg/cli/upgradeassistant/internal/repository/mongodb/testing.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
Copyright 2022 The KodeRover 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 mongodb | ||
|
||
import ( | ||
"context" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
|
||
"github.com/koderover/zadig/pkg/cli/upgradeassistant/internal/repository/models" | ||
"github.com/koderover/zadig/pkg/microservice/aslan/config" | ||
mongotool "github.com/koderover/zadig/pkg/tool/mongo" | ||
) | ||
|
||
type ListTestOption struct { | ||
} | ||
|
||
type TestingColl struct { | ||
*mongo.Collection | ||
|
||
coll string | ||
} | ||
|
||
func NewTestingColl() *TestingColl { | ||
name := models.Testing{}.TableName() | ||
return &TestingColl{Collection: mongotool.Database(config.MongoDatabase()).Collection(name), coll: name} | ||
} | ||
|
||
func (c *TestingColl) GetCollectionName() string { | ||
return c.coll | ||
} | ||
|
||
func (c *TestingColl) List(opt *ListTestOption) ([]*models.Testing, error) { | ||
query := bson.M{} | ||
|
||
var resp []*models.Testing | ||
ctx := context.Background() | ||
opts := options.Find() | ||
cursor, err := c.Collection.Find(ctx, query, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = cursor.All(ctx, &resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return resp, nil | ||
} |
Oops, something went wrong.