From 7dbd46b077e8a9ad66a29bbbd18c5152d63133b0 Mon Sep 17 00:00:00 2001 From: Youhwan Seol Date: Tue, 30 May 2023 15:27:44 +0900 Subject: [PATCH] Implement Preperation API: build helm chart & onboard Change-Id: I48b0c829a336be2093a7a71a2a9a81cce33f645c Signed-off-by: Youhwan Seol --- pkg/api/rest_server.go | 2 +- pkg/api/v1/preparation/preparation.go | 29 ++++++++++++++++------------- pkg/commons/errors/errors.go | 16 ++++++++++++++++ pkg/controller/v1/adapter/controller.go | 16 ++++++++++++++++ pkg/helm/chart_builder.go | 3 ++- pkg/helm/chart_builder_test.go | 4 ++-- 6 files changed, 53 insertions(+), 17 deletions(-) diff --git a/pkg/api/rest_server.go b/pkg/api/rest_server.go index 28b983c..5388d5e 100755 --- a/pkg/api/rest_server.go +++ b/pkg/api/rest_server.go @@ -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 } diff --git a/pkg/api/v1/preparation/preparation.go b/pkg/api/v1/preparation/preparation.go index c705635..688d2a2 100755 --- a/pkg/api/v1/preparation/preparation.go +++ b/pkg/api/v1/preparation/preparation.go @@ -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)) } diff --git a/pkg/commons/errors/errors.go b/pkg/commons/errors/errors.go index 6fe9ba2..c0e0805 100644 --- a/pkg/commons/errors/errors.go +++ b/pkg/commons/errors/errors.go @@ -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 +} diff --git a/pkg/controller/v1/adapter/controller.go b/pkg/controller/v1/adapter/controller.go index e4826c8..0abaa4b 100644 --- a/pkg/controller/v1/adapter/controller.go +++ b/pkg/controller/v1/adapter/controller.go @@ -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 +} diff --git a/pkg/helm/chart_builder.go b/pkg/helm/chart_builder.go index 5909145..0a8f522 100755 --- a/pkg/helm/chart_builder.go +++ b/pkg/helm/chart_builder.go @@ -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 diff --git a/pkg/helm/chart_builder_test.go b/pkg/helm/chart_builder_test.go index a81b77e..6b6f02d 100755 --- a/pkg/helm/chart_builder_test.go +++ b/pkg/helm/chart_builder_test.go @@ -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) } -- 2.16.6