# documentation
.tox
docs/_build/
+pkg/helm/data/sample-xapp-2.2.0/
+*.tgz
--- /dev/null
+################################################################################
+# 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. #
+################################################################################
+"helm":
+ "retry": 1
package main
import (
+ "flag"
"os"
+ "github.com/spf13/viper"
"gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/api"
"gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/commons/logger"
)
+const KSERVE_ADAPTER_CONFIG_FILE = "./config/kserve-adapter.yaml"
+
var (
apiServerPort string
)
func init() {
apiServerPort = os.Getenv("API_SERVER_PORT")
+
+ var fileName *string
+ fileName = flag.String("f", KSERVE_ADAPTER_CONFIG_FILE, "Specify the configuration file.")
+ flag.Parse()
+ viper.SetConfigFile(*fileName)
}
func main() {
"github.com/xeipuuv/gojsonschema"
"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/util"
+ "gopkg.in/yaml.v1"
)
const (
_, err = os.Stat(chartBuilder.chartWorkspacePath)
if err != nil {
if !os.IsNotExist(err) {
- os.RemoveAll(chartBuilder.chartWorkspacePath)
+ logger.Logging(logger.ERROR, err.Error())
+ return nil
}
}
+ err = os.RemoveAll(chartBuilder.chartWorkspacePath)
+ if err != nil {
+ logger.Logging(logger.ERROR, err.Error())
+ return nil
+ }
+
err = os.Mkdir(chartBuilder.chartWorkspacePath, os.FileMode(0744))
if err != nil {
logger.Logging(logger.ERROR, err.Error())
}
func (c *ChartBuilder) PackageChart() (err error) {
- return errors.New("not yet implemented")
+ err = c.appendConfigToValuesYaml()
+ if err != nil {
+ return
+ }
+
+ err = c.changeChartNameVersion()
+ if err != nil {
+ return
+ }
+
+ err = c.helmLint()
+ if err != nil {
+ return
+ }
+
+ output, err := util.HelmExec(fmt.Sprintf("package %s/%s -d %s", c.chartWorkspacePath, c.chartName, c.chartWorkspacePath))
+ if err != nil {
+ logger.Logging(logger.ERROR, "%s-%s helm lint failed (Caused by : %s)", c.chartName, c.chartVersion, err.Error())
+ return
+ }
+ logger.Logging(logger.INFO, "result of helm lint : %s", string(output))
+
+ return
}
func (c *ChartBuilder) parseConfigFile(configFile string) (config Config, err error) {
}
func (c *ChartBuilder) helmLint() (err error) {
- return errors.New("not yet implemented")
+
+ output, err := util.HelmExec(fmt.Sprintf("lint %s/%s", c.chartWorkspacePath, c.chartName))
+
+ if err != nil {
+ logger.Logging(logger.ERROR, err.Error())
+ logger.Logging(logger.ERROR, fmt.Sprintf("%s-%s helm lint failed (Caused by : %s)", c.chartName, c.chartVersion, err))
+ }
+ logger.Logging(logger.INFO, fmt.Sprintf("result of helm lint : %s", string(output)))
+ return
}
func (c *ChartBuilder) appendConfigToValuesYaml() (err error) {
- return errors.New("not yet implemented")
+ valueYamlPath := os.Getenv(ENV_CHART_WORKSPACE_PATH) + "/" + c.chartName + "-" + c.chartVersion + "/" + c.chartName + "/" + VALUES_YAML
+ yamlFile, err := ioutil.ReadFile(valueYamlPath)
+ if err != nil {
+ return
+ }
+
+ data := make(map[interface{}]interface{})
+ err = yaml.Unmarshal(yamlFile, &data)
+ if err != nil {
+ return
+ }
+
+ data["engine"] = c.config.InferenceService.Engine
+ data["storageUri"] = c.config.InferenceService.StorageURI
+
+ //data["resources"] = c.config.
+ data["max_replicas"] = c.config.InferenceService.MaxReplicas
+ data["min_replicas"] = c.config.InferenceService.MinReplicas
+ data["name"] = c.config.XappName
+ data["fullname"] = c.config.XappName
+ data["ric_serviceaccount_name"] = c.config.SaName
+
+ ret, err := yaml.Marshal(&data)
+ if err != nil {
+ return
+ }
+
+ err = ioutil.WriteFile(valueYamlPath, ret, os.FileMode(0644))
+ if err != nil {
+ return
+ }
+ return
}
func (c *ChartBuilder) changeChartNameVersion() (err error) {
package helm
import (
+ "flag"
"io/ioutil"
"os"
"testing"
+ "github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
}
func TestHelmLint(t *testing.T) {
+ os.Setenv("CHART_WORKSPACE_PATH", "./data")
chartBuilder := NewChartBuilder("data/sample_config.json", "data/sample_schema.json")
- err := chartBuilder.helmLint()
+ err := chartBuilder.appendConfigToValuesYaml()
+ assert.Nil(t, err)
+ err = chartBuilder.helmLint()
assert.Nil(t, err)
}
func TestAppendConfigToValuesYaml(t *testing.T) {
+ os.Setenv("CHART_WORKSPACE_PATH", "./data")
chartBuilder := NewChartBuilder("data/sample_config.json", "data/sample_schema.json")
err := chartBuilder.appendConfigToValuesYaml()
}
func TestChangeChartNameVersion(t *testing.T) {
+ os.Setenv("CHART_WORKSPACE_PATH", "./data")
chartBuilder := NewChartBuilder("data/sample_config.json", "data/sample_schema.json")
err := chartBuilder.changeChartNameVersion()
}
func TestPackageChart(t *testing.T) {
+
+ var fileName *string
+ fileName = flag.String("f", "../../config/kserve-adapter.yaml", "Specify the configuration file.")
+ flag.Parse()
+
+ viper.SetConfigFile(*fileName)
+
+ os.Setenv("CHART_WORKSPACE_PATH", "./data")
chartBuilder := NewChartBuilder("data/sample_config.json", "data/sample_schema.json")
err := chartBuilder.PackageChart()
assert.Nil(t, err)
+ 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")
}
func TestCopyFile(t *testing.T) {
{
- "xapp_name": "sample_xapp",
+ "xapp_name": "sample-xapp",
"xapp_type": "inferenceservice",
"version": "2.2.0",
"sa_name": "default",
--- /dev/null
+/*
+==================================================================================
+ 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 util
+
+import (
+ "bytes"
+ "errors"
+ "fmt"
+ "os/exec"
+ "strings"
+ "time"
+
+ "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/commons/logger"
+)
+
+func Exec(args string) (out []byte, err error) {
+ cmd := exec.Command("/bin/sh", "-c", args)
+
+ var stdout bytes.Buffer
+ var stderr bytes.Buffer
+ cmd.Stdout = &stdout
+ cmd.Stderr = &stderr
+
+ logger.Logging(logger.INFO, fmt.Sprintf("Running command: %s", cmd.Args))
+ for i := 0; i < 3; i++ {
+ if err = cmd.Run(); err != nil {
+ logger.Logging(logger.ERROR, fmt.Sprintf("Command failed : %v - %s, retrying", err.Error(), stderr.String()))
+ time.Sleep(time.Duration(2) * time.Second)
+ continue
+ }
+ break
+ }
+
+ if err == nil {
+ logger.Logging(logger.INFO, fmt.Sprintf("command success: %s", stdout.String()))
+ return stdout.Bytes(), nil
+ }
+
+ return stdout.Bytes(), errors.New(stderr.String())
+}
+
+var HelmExec = func(args string) (out []byte, err error) {
+ return Exec(strings.Join([]string{"helm", args}, " "))
+}