Skip to content

Commit

Permalink
draft yamlvalueapplier
Browse files Browse the repository at this point in the history
  • Loading branch information
Sara Wei committed Jan 29, 2024
1 parent 707943a commit e790153
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
k8s.io/klog/v2 v2.100.1
k8s.io/kubectl v0.28.4
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2
sigs.k8s.io/yaml v1.4.0
)

require (
Expand Down Expand Up @@ -143,5 +144,4 @@ require (
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -529,5 +529,5 @@ sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 h1:W6cLQc5pnqM
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3/go.mod h1:JWP1Fj0VWGHyw3YUPjXSQnRnrwezrZSrApfX5S0nIag=
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 h1:PRbqxJClWWYMNV1dhaG4NsibJbArud9kFxnAMREiWFE=
sigs.k8s.io/structured-merge-diff/v4 v4.2.3/go.mod h1:qjx8mGObPmV2aSZepjQjbmb2ihdVs8cGKBraizNC69E=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
34 changes: 34 additions & 0 deletions helmcli/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"time"

"gopkg.in/yaml.v2"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/storage/driver"
Expand Down Expand Up @@ -40,6 +41,39 @@ func StringPathValuesApplier(values ...string) ValuesApplier {
}
}

func YAMLValuesApplier(yamlValues string) (ValuesApplier, error) {
values := make(map[string]interface{})
err := yaml.Unmarshal([]byte(yamlValues), &values)
if err != nil {
return nil, err
}
return func(to map[string]interface{}) error {
for k, v := range values {
// If 'to' doesn't have key 'k'
if _, checkKey := to[k]; !checkKey {
to[k] = v
} else {
// If 'to' has key 'k'
switch v := v.(type) {
case map[string]interface{}:
// If 'v' is of type map[string]interface{}
if toMap, checkKey := to[k].(map[string]interface{}); checkKey {
for subKey, subValue := range v {
toMap[subKey] = subValue
}
} else {
to[k] = v
}
default:
// If 'v' is not of type map[string]interface{}
to[k] = v
}
}
}
return nil
}, nil
}

// ReleaseCli is a client to deploy helm chart with secret storage.
type ReleaseCli struct {
namespace string
Expand Down
26 changes: 24 additions & 2 deletions virtualcluster/nodes_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/Azure/kperf/helmcli"
"sigs.k8s.io/yaml"
)

var (
Expand Down Expand Up @@ -98,12 +99,33 @@ func WithNodepoolNodeControllerAffinity(nodeSelectors map[string][]string) Nodep
// NOTE: Please align with ../manifests/virtualcluster/nodes/values.yaml
//
// TODO: Add YAML ValuesAppliers to support array type.
func (cfg *nodepoolConfig) toHelmValuesAppliers(nodepoolName string) []helmcli.ValuesApplier {
func (cfg *nodepoolConfig) toHelmValuesAppliers(nodepoolName string) ([]helmcli.ValuesApplier, error) {
res := make([]string, 0, 4)

res = append(res, fmt.Sprintf("name=%s", nodepoolName))
res = append(res, fmt.Sprintf("replicas=%d", cfg.count))
res = append(res, fmt.Sprintf("cpu=%d", cfg.cpu))
res = append(res, fmt.Sprintf("memory=%d", cfg.memory))
return []helmcli.ValuesApplier{helmcli.StringPathValuesApplier(res...)}

stringPathApplier := helmcli.StringPathValuesApplier(res...)

// Marshal labels and nodeSelectors to YAML
labelsYaml, err := yaml.Marshal(cfg.labels)
if err != nil {
return nil, err
}
nodeSelectorsYaml, err := yaml.Marshal(cfg.nodeSelectors)
if err != nil {
return nil, err
}
// Create YAML ValuesAppliers for labels and nodeSelectors
labelsApplier, err := helmcli.YAMLValuesApplier(string(labelsYaml))
if err != nil {
return nil, err
}
nodeSelectorsApplier, err := helmcli.YAMLValuesApplier(string(nodeSelectorsYaml))
if err != nil {
return nil, err
}
return []helmcli.ValuesApplier{stringPathApplier, labelsApplier, nodeSelectorsApplier}, nil
}
6 changes: 5 additions & 1 deletion virtualcluster/nodes_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ func CreateNodepool(ctx context.Context, kubeconfigPath string, nodepoolName str
return fmt.Errorf("failed to load virtual node chart: %w", err)
}

cfgValues, err := cfg.toHelmValuesAppliers(nodepoolName)
if err != nil {
return fmt.Errorf("failed to convert to helm values: %w", err)
}
releaseCli, err := helmcli.NewReleaseCli(
kubeconfigPath,
virtualnodeReleaseNamespace,
nodepoolName,
ch,
virtualnodeReleaseLabels,
cfg.toHelmValuesAppliers(nodepoolName)...,
cfgValues...,
)
if err != nil {
return fmt.Errorf("failed to create helm release client: %w", err)
Expand Down

0 comments on commit e790153

Please sign in to comment.