Adding random path generator to fetch helm package within random path 79/10979/1
authorYouhwan Seol <yh.seol@samsung.com>
Wed, 8 Feb 2023 07:26:01 +0000 (16:26 +0900)
committerYouhwan Seol <yh.seol@samsung.com>
Wed, 26 Apr 2023 00:23:36 +0000 (09:23 +0900)
Change-Id: Ie7471780ffe94f7ddb220af65ad4a0a5dca48986
Signed-off-by: Youhwan Seol <yh.seol@samsung.com>
pkg/client/onboard/utils.go

index aff2cb0..a555345 100644 (file)
@@ -23,11 +23,13 @@ import (
        "archive/tar"
        "compress/gzip"
        "io"
+       "math/rand"
        "net/http"
        netUrl "net/url"
        "os"
        "path/filepath"
        "strings"
+       "time"
 
        "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/api/commons/url"
        "github.com/pkg/errors"
@@ -35,6 +37,20 @@ import (
        "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter/pkg/commons/logger"
 )
 
+const (
+       pathLength    = 20
+       letterBytes   = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+       letterIdxBits = 6
+       letterIdxMask = 1<<letterIdxBits - 1
+       letterIdxMax  = 63 / letterIdxBits
+)
+
+var randSrc rand.Source
+
+func init() {
+       randSrc = rand.NewSource(time.Now().UnixNano())
+}
+
 func fetchHelmPackageFromOnboard(url string) (path string, err error) {
        logger.Logging(logger.DEBUG, "IN")
        defer logger.Logging(logger.DEBUG, "OUT")
@@ -45,7 +61,7 @@ func fetchHelmPackageFromOnboard(url string) (path string, err error) {
        }
        defer resp.Body.Close()
 
-       path = "/tmp"
+       path = generateRandPath(pathLength)
 
        err = Untar(path, resp.Body)
        if err != nil {
@@ -173,3 +189,24 @@ func mkdirectory(name string) (err error) {
        }
        return
 }
+
+func generateRandPath(n int) string {
+       logger.Logging(logger.DEBUG, "IN")
+       defer logger.Logging(logger.DEBUG, "OUT")
+
+       sb := strings.Builder{}
+       sb.Grow(n)
+       for i, cache, remain := n-1, randSrc.Int63(), letterIdxMax; i >= 0; {
+               if remain == 0 {
+                       cache, remain = randSrc.Int63(), letterIdxMax
+               }
+               if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
+                       sb.WriteByte(letterBytes[idx])
+                       i--
+               }
+               cache >>= letterIdxBits
+               remain--
+       }
+
+       return sb.String()
+}