Skip to content

Commit

Permalink
support rad install kubernetes --set values (#5243)
Browse files Browse the repository at this point in the history
# Description

Introduce rad install kubernetes --set
which can set single or multiple comma separated values for helm chart. 

ex: 
rad install kubernetes --set
global.ucp.image=radius.azurecr.io/ucpd,global.ucp.tag=latest

rad install kubernetes --set
global.tracerProvider.zipkin.url=http://jaeger-collector.radius-monitoring.svc.cluster.local:9411/api/v2/spans

## Issue reference


Partially Fixes: #5173 based on discussion here :
https://github.com/project-radius/radius/issues/5173#issuecomment-1452655212

## Checklist

Please make sure you've completed the relevant tasks for this PR, out of
the following list:

* [ ] Code compiles correctly
* [ ] Adds necessary unit tests for change
* [ ] Adds necessary E2E tests for change
* [ ] Unit tests passing
* [ ] Extended the documentation / Created issue for it
  • Loading branch information
nithyatsu authored Mar 20, 2023
1 parent 613419a commit 5918d73
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmd/rad/cmd/installKubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func installKubernetes(cmd *cobra.Command, args []string) error {
AppCoreImage: chartArgs.AppCoreImage,
AppCoreTag: chartArgs.AppCoreTag,
PublicEndpointOverride: chartArgs.PublicEndpointOverride,
Values: chartArgs.Values,
},
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/cli/helm/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ func PopulateDefaultClusterOptions(cliOptions CLIClusterOptions) ClusterOptions
if cliOptions.Radius.AWSProvider != nil {
options.Radius.AWSProvider = cliOptions.Radius.AWSProvider
}

if len(cliOptions.Radius.Values) > 0 {
options.Radius.Values = cliOptions.Radius.Values
}
return options
}

Expand Down
12 changes: 10 additions & 2 deletions pkg/cli/helm/radiusclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/storage/driver"
"helm.sh/helm/v3/pkg/strvals"
"k8s.io/cli-runtime/pkg/genericclioptions"

"github.com/project-radius/radius/pkg/cli/aws"
Expand Down Expand Up @@ -44,6 +45,7 @@ type RadiusOptions struct {
PublicEndpointOverride string
AzureProvider *azure.Provider
AWSProvider *aws.Provider
Values string
}

func ApplyRadiusHelmChart(options RadiusOptions, kubeContext string) (bool, error) {
Expand Down Expand Up @@ -72,7 +74,8 @@ func ApplyRadiusHelmChart(options RadiusOptions, kubeContext string) (bool, erro
return false, fmt.Errorf("failed to load helm chart, err: %w, helm output: %s", err, helmOutput.String())
}

err = addRadiusValues(helmChart, &options)
// TODO: refactor this to use the addChartValues function
err = AddRadiusValues(helmChart, &options)
if err != nil {
return false, fmt.Errorf("failed to add radius values, err: %w, helm output: %s", err, helmOutput.String())
}
Expand Down Expand Up @@ -213,7 +216,7 @@ func runRadiusHelmUpgrade(helmConf *helm.Configuration, releaseName string, helm
return runUpgrade(installClient, releaseName, helmChart)
}

func addRadiusValues(helmChart *chart.Chart, options *RadiusOptions) error {
func AddRadiusValues(helmChart *chart.Chart, options *RadiusOptions) error {
values := helmChart.Values

_, ok := values["global"]
Expand Down Expand Up @@ -284,6 +287,11 @@ func addRadiusValues(helmChart *chart.Chart, options *RadiusOptions) error {
de["tag"] = options.DETag
}

err := strvals.ParseInto(options.Values, values)
if err != nil {
return err
}

return nil
}

Expand Down
53 changes: 53 additions & 0 deletions pkg/cli/helm/radiusclient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// ------------------------------------------------------------
package helm

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"helm.sh/helm/v3/pkg/chart"
)

func Test_AddRadiusValues(t *testing.T) {
var helmChart chart.Chart
helmChart.Values = map[string]any{}
options := &RadiusOptions{
Image: "image",
Values: "global.de.tag=de-tag,global.ucp.tag=ucp-tag,",
}
err := AddRadiusValues(&helmChart, options)
values := helmChart.Values
require.Equal(t, err, nil)

_, ok := values["global"]
assert.True(t, ok)
global := values["global"].(map[string]any)
_, ok = global["rp"]
assert.True(t, ok)
rp := global["rp"].(map[string]any)
_, ok = rp["container"]
assert.True(t, ok)
assert.Equal(t, rp["container"], "image")

_, ok = values["global"]
assert.True(t, ok)
global = values["global"].(map[string]any)
_, ok = global["de"]
assert.True(t, ok)
de := global["de"].(map[string]any)
_, ok = de["tag"]
assert.True(t, ok)
assert.Equal(t, de["tag"], "de-tag")

_, ok = global["ucp"]
assert.True(t, ok)
ucp := global["ucp"].(map[string]any)
_, ok = ucp["tag"]
assert.True(t, ok)
assert.Equal(t, ucp["tag"], "ucp-tag")

}
10 changes: 10 additions & 0 deletions pkg/cli/setup/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type ChartArgs struct {
// for display purposes. This is useful when the the actual public IP address of a cluster's ingress
// is not a routable IP. This comes up all of the time for a local cluster.
PublicEndpointOverride string

// Values is a string consisting of list of values to pass to the Helm chart which would be used to override values in the chart.
Values string
}

// RegisterPersistentChartArgs registers the CLI arguments used for our Helm chart.
Expand All @@ -37,6 +40,7 @@ func RegisterPersistentChartArgs(cmd *cobra.Command) {
cmd.PersistentFlags().String("ucp-image", "", "Specify the UCP image to use")
cmd.PersistentFlags().String("ucp-tag", "", "Specify the UCP tag to use")
cmd.PersistentFlags().String("public-endpoint-override", "", "Specify the public IP address or hostname of the Kubernetes cluster. It must be in the format: <hostname>[:<port>]. Ex: 'localhost:9000'")
cmd.PersistentFlags().String("set", "", "Set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
}

// ParseChartArgs the arguments we provide for installation of the Helm chart.
Expand Down Expand Up @@ -65,6 +69,11 @@ func ParseChartArgs(cmd *cobra.Command) (*ChartArgs, error) {
if err != nil {
return nil, err
}
values, err := cmd.Flags().GetString("set")
if err != nil {
return nil, err
}

publicEndpointOverride, err := cmd.Flags().GetString("public-endpoint-override")
if err != nil {
return nil, err
Expand All @@ -80,5 +89,6 @@ func ParseChartArgs(cmd *cobra.Command) (*ChartArgs, error) {
UcpImage: ucpImage,
UcpTag: ucpTag,
PublicEndpointOverride: publicEndpointOverride,
Values: values,
}, nil
}

0 comments on commit 5918d73

Please sign in to comment.