Implement Preperation API: build helm chart & onboard 55/11255/3
authorYouhwan Seol <yh.seol@samsung.com>
Tue, 30 May 2023 06:27:44 +0000 (15:27 +0900)
committerJoseph Thaliath <jo.thaliath@samsung.com>
Wed, 7 Jun 2023 11:14:15 +0000 (11:14 +0000)
Change-Id: I48b0c829a336be2093a7a71a2a9a81cce33f645c
Signed-off-by: Youhwan Seol <yh.seol@samsung.com>
pkg/api/rest_server.go
pkg/api/v1/preparation/preparation.go
pkg/commons/errors/errors.go
pkg/controller/v1/adapter/controller.go
pkg/helm/chart_builder.go
pkg/helm/chart_builder_test.go

index 28b983c..5388d5e 100755 (executable)
@@ -84,7 +84,7 @@ func setupRouter() (router *gin.Engine) {
 
                preparation := v1.Group(url.IPS() + url.Preparation())
                {
-                       preparation.POST("", preparationExecutor.Post)
+                       preparation.POST("", preparationExecutor.Preperation)
                }
                _, _, _, _, _ = healthcheck, revision, status, info, preparation
        }
index c705635..688d2a2 100755 (executable)
@@ -20,46 +20,49 @@ limitations under the License.
 package preparation
 
 import (
-       "encoding/json"
        "net/http"
 
-       "github.com/gin-gonic/gin"
        "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/api/commons/utils"
        "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/commons/errors"
        "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/commons/logger"
-       "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/helm"
+       "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/controller/v1/adapter"
+       "github.com/gin-gonic/gin"
 )
 
 type Command interface {
-       Post(c *gin.Context)
+       Preperation(c *gin.Context)
 }
 
 type Executor struct {
        Command
 }
 
+var ipsAdapter adapter.Command
+
 func init() {
+       ipsAdapter = adapter.Executor{}
 }
 
-func (Executor) Post(c *gin.Context) {
+func (Executor) Preperation(c *gin.Context) {
        logger.Logging(logger.DEBUG, "IN")
        defer logger.Logging(logger.DEBUG, "OUT")
 
        configFile := c.Query("configfile")
-       schemaFile := c.Query("schemafile")
-
-       chartBuilder := helm.NewChartBuilder(configFile, schemaFile)
-       chartPath, err := chartBuilder.PackageChart()
+       if configFile == "" {
+               utils.WriteError(c.Writer, errors.InvalidConfigFile{Message: "Empty Query"})
+               return
+       }
 
-       if err != nil {
-               utils.WriteError(c.Writer, errors.InternalServerError{Message: err.Error()})
+       schemaFile := c.Query("schemafile")
+       if schemaFile == "" {
+               utils.WriteError(c.Writer, errors.InvalidSchemaFile{Message: "Empty Query"})
                return
        }
 
-       data, err := json.Marshal(chartPath)
+       chartPath, err := ipsAdapter.Preperation(configFile, schemaFile)
        if err != nil {
                utils.WriteError(c.Writer, err)
                return
        }
-       utils.WriteSuccess(c.Writer, http.StatusAccepted, data)
+       utils.WriteSuccess(c.Writer, http.StatusCreated, []byte(chartPath))
 }
index 6fe9ba2..c0e0805 100644 (file)
@@ -82,3 +82,19 @@ type InternalServerError struct {
 func (e InternalServerError) Error() string {
        return "internal server error: " + e.Message
 }
+
+type InvalidConfigFile struct {
+       Message string `json:"message" example:"invalid config file: {reason}" format:"string"`
+}
+
+func (e InvalidConfigFile) Error() string {
+       return "invalid Config file: " + e.Message
+}
+
+type InvalidSchemaFile struct {
+       Message string `json:"message" example:"invalid schema file: {reason}" format:"string"`
+}
+
+func (e InvalidSchemaFile) Error() string {
+       return "invalid Schema file: " + e.Message
+}
index e4826c8..0abaa4b 100644 (file)
@@ -27,6 +27,7 @@ import (
        "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/commons/errors"
        "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/commons/logger"
        "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/commons/types"
+       "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/helm"
 )
 
 //go:generate mockgen -source=controller.go -destination=./mock/mock_controller.go -package=mock
@@ -37,6 +38,7 @@ type Command interface {
        Revision(name string) (revisionList types.Revision, err error)
        Status(name string) (types.Status, error)
        Info(name string) (types.Info, error)
+       Preperation(configFile string, schemaFile string) (chartPath string, err error)
 }
 
 type Executor struct {
@@ -161,3 +163,17 @@ func (Executor) Info(name string) (result types.Info, err error) {
        result, err = kserveClient.Info(name)
        return
 }
+
+func (Executor) Preperation(configFile string, schemaFile string) (chartPath string, err error) {
+       logger.Logging(logger.DEBUG, "IN")
+       defer logger.Logging(logger.DEBUG, "OUT")
+
+       chartBuilder := helm.NewChartBuilder(configFile, schemaFile)
+       chartPath, err = chartBuilder.PackageChart()
+
+       if err != nil {
+               return
+       }
+
+       return
+}
index 5909145..0a8f522 100755 (executable)
@@ -83,6 +83,7 @@ func NewChartBuilder(configFile string, schemaFile string) *ChartBuilder {
        chartBuilder.chartVersion = chartBuilder.config.Version
        chartBuilder.chartWorkspacePath = os.Getenv(ENV_CHART_WORKSPACE_PATH) + "/" + chartBuilder.chartName + "-" + chartBuilder.chartVersion
 
+       logger.Logging(logger.INFO, "chartBuilder.chartWorkspacePath", chartBuilder.chartWorkspacePath)
        _, err = os.Stat(chartBuilder.chartWorkspacePath)
        if err != nil {
                if !os.IsNotExist(err) {
@@ -110,7 +111,7 @@ func NewChartBuilder(configFile string, schemaFile string) *ChartBuilder {
 
        apiVersion := chartBuilder.config.InferenceService.ApiVersion
        resource := resourceVersionMap[apiVersion]
-       err = chartBuilder.copyDirectory("data/"+resource, chartBuilder.chartWorkspacePath+"/"+chartBuilder.chartName)
+       err = chartBuilder.copyDirectory(chartBuilder.chartWorkspacePath+"/../../data/"+resource, chartBuilder.chartWorkspacePath+"/"+chartBuilder.chartName)
        if err != nil {
                logger.Logging(logger.ERROR, err.Error())
                return nil
index a81b77e..6b6f02d 100755 (executable)
@@ -131,9 +131,9 @@ func TestCopyDirectory(t *testing.T) {
        }
 }
 func TestValidateChartMaterials(t *testing.T) {
-       os.Setenv("CHART_WORKSPACE_PATH", ".")
+       os.Setenv("CHART_WORKSPACE_PATH", "./data")
        chartBuilder := NewChartBuilder("data/sample_config.json", "data/sample_schema.json")
        err := chartBuilder.ValidateChartMaterials()
-       defer os.RemoveAll(os.Getenv("CHART_WORKSPACE_PATH") + "/" + chartBuilder.chartName + "-" + chartBuilder.chartVersion)
+       defer os.RemoveAll(chartBuilder.chartWorkspacePath)
        assert.Nil(t, err)
 }