Skip to content

Commit

Permalink
sonar image initialization & upgrade (koderover#1496)
Browse files Browse the repository at this point in the history
* added sonar image into system integration

Signed-off-by: Min Min <[email protected]>

* added initialize logic

Signed-off-by: Min Min <[email protected]>
  • Loading branch information
jamsman94 authored May 13, 2022
1 parent bbbbc2f commit 9e1656f
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/cli/upgradeassistant/cmd/migrate/1120.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func V1110ToV1120() error {
return err
}

if err := integrateSonar(); err != nil {
log.Errorf("add sonar integration err:%s", err)
return err
}

return nil
}

Expand Down Expand Up @@ -274,6 +279,19 @@ func buildAddJenkinsID() error {
return err
}

func integrateSonar() error {
sonarImageList, err := internalmongodb.NewBasicImageColl().GetSonarTypeImage()
if err != nil {
return fmt.Errorf("failed to list basic images of type sonar ,err: %s", err)
}

// if no sonar image list is included, we add one
if len(sonarImageList) == 0 {
return internalmongodb.NewBasicImageColl().CreateZadigSonarImage()
}
return nil
}

func buildAddJenkinsIDRollBack() error {
builds, err := internalmongodb.NewBuildColl().List(&internalmongodb.BuildListOption{})
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
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"
)

const (
ImageFromKoderover = "koderover"
ImageFromCustom = "custom"
)

// BasicImage ...
type BasicImage struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
Value string `bson:"value" json:"value"`
Label string `bson:"label" json:"label"`
ImageFrom string `bson:"image_from" json:"image_from"`
CreateTime int64 `bson:"create_time" json:"create_time"`
UpdateTime int64 `bson:"update_time" json:"update_time"`
UpdateBy string `bson:"update_by" json:"update_by"`
// New field since 1.12
// ImageType is the type of the image, currently only one type is supported called sonar
ImageType string `bson:"image_type" json:"image_type"`
}

func (BasicImage) TableName() string {
return "basic_image"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Copyright 2021 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"
"github.com/koderover/zadig/pkg/microservice/aslan/config"
"github.com/koderover/zadig/pkg/microservice/aslan/core/common/repository/models"
mongotool "github.com/koderover/zadig/pkg/tool/mongo"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"time"
)

type BasicImageOpt struct {
Value string `bson:"value"`
Label string `bson:"label"`
ImageFrom string `bson:"image_from"`
ImageType string `bson:"image_type"`
}

type BasicImageColl struct {
*mongo.Collection

coll string
}

func NewBasicImageColl() *BasicImageColl {
name := models.BasicImage{}.TableName()
coll := &BasicImageColl{Collection: mongotool.Database(config.MongoDatabase()).Collection(name), coll: name}

return coll
}

func (c *BasicImageColl) GetSonarTypeImage() ([]*models.BasicImage, error) {
query := bson.M{}
query["image_type"] = "sonar"

ctx := context.Background()
resp := make([]*models.BasicImage, 0)

cursor, err := c.Collection.Find(ctx, query)
if err != nil {
return nil, err
}

err = cursor.All(ctx, &resp)
if err != nil {
return nil, err
}

return resp, err
}

func (c *BasicImageColl) CreateZadigSonarImage() error {
args := &models.BasicImage{
Value: "sonarsource/sonar-scanner-cli",
Label: "sonar:latest",
CreateTime: time.Now().Unix(),
UpdateTime: time.Now().Unix(),
UpdateBy: "system",
ImageType: "sonar",
}

_, err := c.InsertOne(context.TODO(), args)
return err
}
8 changes: 8 additions & 0 deletions pkg/microservice/aslan/core/system/service/basic_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ func InitbasicImageInfos() []*commonmodels.BasicImage {
UpdateTime: time.Now().Unix(),
UpdateBy: "system",
},
{
Value: "sonarsource/sonar-scanner-cli",
Label: "sonar:latest",
CreateTime: time.Now().Unix(),
UpdateTime: time.Now().Unix(),
UpdateBy: "system",
ImageType: "sonar",
},
}

return basicImageInfos
Expand Down

0 comments on commit 9e1656f

Please sign in to comment.