Skip to content

Commit

Permalink
fix(add-on): no need to decode base64 for bytes
Browse files Browse the repository at this point in the history
As encoding/json package will decode that field when unmarshal.
GetManifest is no more needed.
  • Loading branch information
orangedeng authored and JacieChao committed Aug 23, 2023
1 parent 10d3c20 commit dd94f34
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 34 deletions.
3 changes: 2 additions & 1 deletion cmd/addon/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package addon
import (
"errors"
"fmt"
"os"

"github.com/cnrancher/autok3s/pkg/common"
"github.com/cnrancher/autok3s/pkg/utils"
Expand Down Expand Up @@ -41,7 +42,7 @@ func CreateCmd() *cobra.Command {

createCmd.Run = func(cmd *cobra.Command, args []string) {
name := args[0]
manifest, err := common.GetManifest(addonFlags.FromFile, "")
manifest, err := os.ReadFile(addonFlags.FromFile)
if err != nil {
logrus.Fatalln(err)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/addon/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package addon

import (
"fmt"
"os"
"reflect"

"github.com/cnrancher/autok3s/pkg/common"
Expand Down Expand Up @@ -54,7 +55,7 @@ func UpdateCmd() *cobra.Command {
}

if addonFlags.FromFile != "" {
manifestString, err := common.GetManifest(addonFlags.FromFile, "")
manifestString, err := os.ReadFile(addonFlags.FromFile)
if err != nil {
logrus.Error(err)
return
Expand Down
15 changes: 0 additions & 15 deletions pkg/common/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,15 @@ package common
import (
"bytes"
"fmt"
"os"
"text/template"

"github.com/Masterminds/sprig/v3"
"github.com/cnrancher/autok3s/pkg/utils"
"github.com/pkg/errors"
"helm.sh/helm/v3/pkg/strvals"

"k8s.io/apimachinery/pkg/util/validation"
)

func GetManifest(manifestFile, manifestContent string) (string, error) {
if manifestFile != "" {
fileByte, err := os.ReadFile(manifestFile)
if err != nil {
return "", err
}
return string(fileByte), nil
} else if manifestContent != "" {
return utils.StringSupportBase64(manifestContent), nil
}
return "", errors.New("can't get manifest with empty manifest file or content")
}

func GenerateValues(setValues map[string]string, defaultValues map[string]string) (map[string]interface{}, error) {
values := []string{}
for key, value := range defaultValues {
Expand Down
23 changes: 6 additions & 17 deletions pkg/server/store/addon/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,14 @@ func (a *Store) Create(apiOp *types.APIRequest, schema *types.APISchema, data ty
if err := common.ValidateName(input.Name); err != nil {
return types.APIObject{}, err
}
if input.Manifest == nil {
return types.APIObject{}, errors.New("manifest file or manifest file content cannot be empty")
}

manifest, err := common.GetManifest("", string(input.Manifest))
if err != nil {
return types.APIObject{}, err
if input.Manifest == nil || len(input.Manifest) == 0 {
return types.APIObject{}, errors.New("manifest file content cannot be empty")
}

addon := &common.Addon{
Name: input.Name,
Description: input.Description,
Manifest: []byte(manifest),
Manifest: input.Manifest,
Values: input.Values,
}
err = common.DefaultDB.SaveAddon(addon)
Expand Down Expand Up @@ -91,16 +86,10 @@ func (a *Store) Update(apiOp *types.APIRequest, schema *types.APISchema, data ty
return types.APIObject{}, err
}

manifestContent := addon.Manifest
if input.Manifest != nil {
manifestContent = input.Manifest
}

manifestString, err := common.GetManifest("", string(manifestContent))
if err != nil {
return types.APIObject{}, err
manifest := addon.Manifest
if len(input.Manifest) != 0 {
manifest = input.Manifest
}
manifest := []byte(manifestString)

isChanged := false
if input.Description != "" && input.Description != addon.Description {
Expand Down

0 comments on commit dd94f34

Please sign in to comment.