add API for preparing helm chart with mock generation script 20/11220/2
authorhoejoo.lee <hoejoo.lee@samsung.com>
Thu, 25 May 2023 09:24:21 +0000 (05:24 -0400)
committerhoejoo.lee <hoejoo.lee@samsung.com>
Thu, 25 May 2023 10:50:33 +0000 (06:50 -0400)
Change-Id: I8eba3972d948b82e7a05a26ba729890c38a45bc4
Signed-off-by: hoejoo.lee <hoejoo.lee@samsung.com>
pkg/api/commons/url/url.go [changed mode: 0644->0755]
pkg/api/rest_server.go [changed mode: 0644->0755]
pkg/api/v1/preparation/preparation.go [new file with mode: 0755]
pkg/helm/chart_builder.go
pkg/helm/chart_builder_test.go
pkg/helm/config.go
pkg/helm/schema.go

old mode 100644 (file)
new mode 100755 (executable)
index 1ad15bc..a45e096
@@ -45,3 +45,6 @@ func IPSName() string { return "/{name}" }
 
 // Version returns the version url as a type of string.
 func Version() string { return "/ver" }
+
+// Preparation returns the package url as s type of string.
+func Preparation() string { return "/preparation" }
old mode 100644 (file)
new mode 100755 (executable)
index 3d9db75..28b983c
@@ -26,6 +26,7 @@ import (
        "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/api/v1/deployment"
        "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/api/v1/healthcheck"
        "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/api/v1/info"
+       "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/api/v1/preparation"
        "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/api/v1/revision"
        "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/api/v1/status"
        "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/commons/logger"
@@ -37,6 +38,7 @@ var (
        revisionExecutor    revision.Command
        statusExecutor      status.Command
        infoExecutor        info.Command
+       preparationExecutor preparation.Command
 )
 
 func init() {
@@ -45,6 +47,7 @@ func init() {
        revisionExecutor = revision.Executor{}
        statusExecutor = status.Executor{}
        infoExecutor = info.Executor{}
+       preparationExecutor = preparation.Executor{}
 }
 
 func setupRouter() (router *gin.Engine) {
@@ -79,7 +82,11 @@ func setupRouter() (router *gin.Engine) {
                        info.GET("", infoExecutor.Get)
                }
 
-               _, _, _, _ = healthcheck, revision, status, info
+               preparation := v1.Group(url.IPS() + url.Preparation())
+               {
+                       preparation.POST("", preparationExecutor.Post)
+               }
+               _, _, _, _, _ = healthcheck, revision, status, info, preparation
        }
 
        return
diff --git a/pkg/api/v1/preparation/preparation.go b/pkg/api/v1/preparation/preparation.go
new file mode 100755 (executable)
index 0000000..c705635
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+==================================================================================
+
+Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved.
+
+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 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"
+)
+
+type Command interface {
+       Post(c *gin.Context)
+}
+
+type Executor struct {
+       Command
+}
+
+func init() {
+}
+
+func (Executor) Post(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 err != nil {
+               utils.WriteError(c.Writer, errors.InternalServerError{Message: err.Error()})
+               return
+       }
+
+       data, err := json.Marshal(chartPath)
+       if err != nil {
+               utils.WriteError(c.Writer, err)
+               return
+       }
+       utils.WriteSuccess(c.Writer, http.StatusAccepted, data)
+}
index ea0f79b..0dc6e57 100755 (executable)
@@ -46,7 +46,7 @@ const (
 )
 
 type HelmChartBuilder interface {
-       PackageChart() (err error)
+       PackageChart() (helmChartPath string, err error)
 }
 
 type ChartBuilder struct {
@@ -204,7 +204,7 @@ func (c *ChartBuilder) copyDirectory(src string, dest string) (err error) {
        return
 }
 
-func (c *ChartBuilder) PackageChart() (err error) {
+func (c *ChartBuilder) PackageChart() (chartPath string, err error) {
        err = c.appendConfigToValuesYaml()
        if err != nil {
                return
@@ -226,6 +226,8 @@ func (c *ChartBuilder) PackageChart() (err error) {
                return
        }
        logger.Logging(logger.INFO, "result of helm lint : %s", string(output))
+       slice := strings.Split(string(output), " ")
+       chartPath = slice[len(slice)-1]
 
        return
 }
@@ -313,7 +315,16 @@ func (c *ChartBuilder) appendConfigToValuesYaml() (err error) {
 }
 
 func (c *ChartBuilder) changeChartNameVersion() (err error) {
-       return errors.New("not yet implemented")
+       chartYamlPath := os.Getenv(ENV_CHART_WORKSPACE_PATH) + "/" + c.chartName + "/" + CHART_YAML
+       yamlFile, err := ioutil.ReadFile(chartYamlPath)
+       data := make(map[interface{}]interface{})
+       err = yaml.Unmarshal(yamlFile, &data)
+       if err != nil {
+               return
+       }
+       data["version"] = c.chartVersion
+       data["name"] = c.chartName
+       return
 }
 
 func (c *ChartBuilder) ValidateChartMaterials() (err error) {
index 095e67b..a81b77e 100755 (executable)
@@ -81,9 +81,10 @@ func TestPackageChart(t *testing.T) {
 
        os.Setenv("CHART_WORKSPACE_PATH", "./data")
        chartBuilder := NewChartBuilder("data/sample_config.json", "data/sample_schema.json")
-       err := chartBuilder.PackageChart()
+       chartPath, err := chartBuilder.PackageChart()
 
        assert.Nil(t, err)
+       assert.Contains(t, chartPath, "data/sample-xapp-2.2.0/inference-service-1.0.0.tgz")
        assert.FileExists(t, "./data/sample-xapp-2.2.0/inference-service-1.0.0.tgz")
 
        defer os.RemoveAll("./data/sample-xapp-2.2.0/inference-service-1.0.0.tgz")
@@ -129,3 +130,10 @@ func TestCopyDirectory(t *testing.T) {
                }
        }
 }
+func TestValidateChartMaterials(t *testing.T) {
+       os.Setenv("CHART_WORKSPACE_PATH", ".")
+       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)
+       assert.Nil(t, err)
+}
index 67ae960..dc71fb9 100755 (executable)
@@ -25,6 +25,7 @@ type Config struct {
        Version          string           `json:"version"`
        SaName           string           `json:"sa_name"`
        InferenceService InferenceService `json:"inferenceservice"`
+       configFile string
 }
 
 type InferenceService struct {
index 40355be..046374b 100755 (executable)
@@ -27,137 +27,44 @@ type Schema struct {
        Title       string      `json:"title"`
        Required    []string    `json:"required"`
        Properties  Properties  `json:"properties"`
+       schemaFile string
 }
 
 type Definitions struct {
 }
 
 type Properties struct {
-       ServicePort ServicePort `json:"service_ports"`
-       Rmr         Rmr         `json:"rmr"`
-       Envs        Envs        `json:"envs"`
+       InferenceService InferenceServiceSchema `json:"inferenceservice"`
 }
 
-type Envs struct {
-       Id         string        `json:"$id"`
-       Type       string        `json:"type"`
-       Title      string        `json:"title"`
-       Required   []string      `json:"required"`
-       Properties EnvProperties `json:"properties"`
+type InferenceServiceSchema struct {
+       Id         string                   `json:"$id"`
+       Type       string                   `json:"type"`
+       Title      string                   `json:"title"`
+       Required   []string                 `json:"required"`
+       Properties InferenceServiceProperty `json:"properties"`
 }
 
-type EnvProperties struct {
-       GNodeB                 EnvPropertyTemplate `json:"gNodeB"`
-       Threads                EnvPropertyTemplate `json:"THREADS"`
-       A1SchemaFile           EnvPropertyTemplate `json:"A1_SCHEMA_FILE"`
-       VesSchemaFile          EnvPropertyTemplate `json:"VES_SCHEMA_FILE"`
-       SampleFile             EnvPropertyTemplate `json:"SAMPLE_FILE"`
-       VesCollectorURL        EnvPropertyTemplate `json:"VES_COLLECTOR_URL"`
-       VesMeasurementInterval EnvPropertyTemplate `json:"VES_MEASUREMENT_INTERVAL"`
+type InferenceServiceProperty struct {
+       Engine      StringProperty  `json:"engine"`
+       StorageURI  StringProperty  `json:"storage_uri"`
+       ApiVersion  StringProperty  `json:"api_version"`
+       MinReplicas IntegerProperty `json:"min_replicas"`
+       MaxReplicas IntegerProperty `json:"max_replicas"`
 }
 
-type EnvPropertyTemplate struct {
+type StringProperty struct {
        Id       string   `json:"$id"`
        Type     string   `json:"type"`
        Title    string   `json:"title"`
        Default  string   `json:"default"`
        Examples []string `json:"examples"`
-       Pattern  string   `json:"pattern"`
 }
 
-type Rmr struct {
-       Id            string        `json:"$id"`
-       Type          string        `json:"type"`
-       Title         string        `json:"title"`
-       Required      []string      `json:"required"`
-       RmrProperties RmrProperties `json:"properties"`
-}
-
-type RmrProperties struct {
-       ProtPort   ProtPort       `json:"protPort"`
-       MaxSize    RmrSubProperty `json:"maxSize"`
-       NumWorkers RmrSubProperty `json:"numWorkers"`
-       TxMessages RmrMessage     `json:"txMessages"`
-       RxMessages RmrMessage     `json:"rxMessages"`
-       FilePath   FilePath       `json:"file_path"`
-       Contents   Contents       `json:"contents"`
-}
-
-type FilePath struct {
-       Id       string   `json:"$id"`
-       Type     string   `json:"type"`
-       Title    string   `json:"title"`
-       Default  string   `json:"default"`
-       Examples []string `json:"examples"`
-       Pattern  string   `json:"pattern"`
-}
-
-type Contents struct {
-       Id       string   `json:"$id"`
-       Type     string   `json:"type"`
-       Title    string   `json:"title"`
-       Default  string   `json:"default"`
-       Examples []string `json:"examples"`
-       Pattern  string   `json:"pattern"`
-}
-
-type RmrMessage struct {
-       Id    string          `json:"$id"`
-       Type  string          `json:"type"`
-       Title string          `json:"title"`
-       Items RmrMessageItems `json:"items"`
-}
-
-type RmrMessageItems struct {
-       Id       string   `json:"$id"`
-       Type     string   `json:"type"`
-       Title    string   `json:"title"`
-       Default  string   `json:"default"`
-       Examples []string `json:"examples"`
-       Pattern  string   `json:"pattern"`
-}
-
-type RmrSubProperty struct {
+type IntegerProperty struct {
        Id       string  `json:"$id"`
        Type     string  `json:"type"`
        Title    string  `json:"title"`
        Default  int32   `json:"default"`
        Examples []int32 `json:"examples"`
 }
-
-type ProtPort struct {
-       Id       string   `json:"$id"`
-       Type     string   `json:"type"`
-       Title    string   `json:"title"`
-       Default  string   `json:"default"`
-       Examples []string `json:"examples"`
-       Pattern  string   `json:"pattern"`
-}
-
-type ServicePort struct {
-       Id                    string                `json:"$id"`
-       Type                  string                `json:"type"`
-       Title                 string                `json:"title"`
-       Required              []string              `json:"required"`
-       ServicePortProperties ServicePortProperties `json:"properties"`
-}
-
-type ServicePortProperties struct {
-       XappPort XappPort `json:"xapp_port"`
-       RmrPort  RmrPort  `json:"rmr_port"`
-}
-type RmrPort struct {
-       Id       string   `json:"$id"`
-       Type     string   `json:"type"`
-       Title    string   `json:"title"`
-       Default  uint32   `json:"default"`
-       Examples []uint32 `json:"examples"`
-}
-
-type XappPort struct {
-       Id       string   `json:"$id"`
-       Type     string   `json:"type"`
-       Title    string   `json:"title"`
-       Default  uint32   `json:"default"`
-       Examples []uint32 `json:"examples"`
-}