Skip to content

Commit

Permalink
Upgrade to Helm v3 sdk (linkerd#4878)
Browse files Browse the repository at this point in the history
Fixes linkerd#4874

This branch upgrades Helm sdk from v2 to v3 *without any functionaly
changes*, just replacing types with newer API's.

This should not effect our current support for Helm v2 as we did not
change any of the underlying tempaltes(which work with Helm v2). This
works becuase we did not use any of the API's that read the Chart
metadata (which are the only ones changed from v2 to v3) and currently
manually load files and pass ito the sdk.

This PR should provide a great point to start more of the newer Helm v3
API's including for the upgrade workflow thus allowing us to make
Linkerd CLI more simpler.

Signed-off-by: Tarun Pothulapati <tarunpothulapati@outlook.com>
  • Loading branch information
Pothulapati authored Nov 23, 2020
1 parent 92eb174 commit fafeee3
Show file tree
Hide file tree
Showing 19 changed files with 546 additions and 306 deletions.
9 changes: 4 additions & 5 deletions cli/cmd/install-cni-plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (
"os"
"strings"

cnicharts "github.com/linkerd/linkerd2/pkg/charts/cni"

"github.com/linkerd/linkerd2/pkg/charts"
"k8s.io/helm/pkg/chartutil"

cnicharts "github.com/linkerd/linkerd2/pkg/charts/cni"
"github.com/linkerd/linkerd2/pkg/version"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -207,7 +206,7 @@ func renderCNIPlugin(w io.Writer, config *cniPluginOptions) error {
return err
}

files := []*chartutil.BufferedFile{
files := []*loader.BufferedFile{
{Name: chartutil.ChartfileName},
{Name: "templates/cni-plugin.yaml"},
}
Expand Down
12 changes: 7 additions & 5 deletions cli/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import (
"github.com/linkerd/linkerd2/pkg/k8s"
"github.com/linkerd/linkerd2/pkg/tree"
"github.com/spf13/cobra"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/helm/pkg/chartutil"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -278,6 +279,7 @@ func install(ctx context.Context, w io.Writer, values *l5dcharts.Values, flags [
fmt.Fprintf(os.Stderr, errMsgLinkerdConfigResourceConflict, controlPlaneNamespace, "Secret/linkerd-config-overrides already exists")
os.Exit(1)
}

}

err = initializeIssuerCredentials(ctx, k8sAPI, values)
Expand All @@ -304,7 +306,7 @@ func render(w io.Writer, values *l5dcharts.Values, stage string) error {
return err
}

files := []*chartutil.BufferedFile{
files := []*loader.BufferedFile{
{Name: chartutil.ChartfileName},
}

Expand All @@ -321,7 +323,7 @@ func render(w io.Writer, values *l5dcharts.Values, stage string) error {
Dir: addOnChartsPath + "/" + addOn.Name(),
Namespace: controlPlaneNamespace,
RawValues: append(addOn.Values(), rawValues...),
Files: []*chartutil.BufferedFile{
Files: []*loader.BufferedFile{
{
Name: chartutil.ChartfileName,
},
Expand All @@ -335,7 +337,7 @@ func render(w io.Writer, values *l5dcharts.Values, stage string) error {
if stage == "" || stage == configStage {
for _, template := range templatesConfigStage {
files = append(files,
&chartutil.BufferedFile{Name: template},
&loader.BufferedFile{Name: template},
)
}

Expand All @@ -348,7 +350,7 @@ func render(w io.Writer, values *l5dcharts.Values, stage string) error {
if stage == "" || stage == controlPlaneStage {
for _, template := range templatesControlPlaneStage {
files = append(files,
&chartutil.BufferedFile{Name: template},
&loader.BufferedFile{Name: template},
)
}

Expand Down
67 changes: 38 additions & 29 deletions cli/cmd/install_cni_helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"testing"

cnicharts "github.com/linkerd/linkerd2/pkg/charts/cni"
"k8s.io/helm/pkg/chartutil"
pb "k8s.io/helm/pkg/proto/hapi/chart"
"k8s.io/helm/pkg/renderutil"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/engine"
"sigs.k8s.io/yaml"
)

Expand All @@ -21,7 +21,7 @@ func TestRenderCniHelm(t *testing.T) {

t.Run("Cni Install with defaults", func(t *testing.T) {
chartCni := chartCniPlugin(t)
testRenderCniHelm(t, chartCni, &pb.Config{}, "install_cni_helm_default_output.golden")
testRenderCniHelm(t, chartCni, &chartutil.Values{}, "install_cni_helm_default_output.golden")
})

t.Run("Cni Install with overridden values", func(t *testing.T) {
Expand All @@ -44,28 +44,35 @@ func TestRenderCniHelm(t *testing.T) {
"priorityClassName": "system-node-critical"
}`

overrideConfig := &pb.Config{Raw: overrideJSON}
testRenderCniHelm(t, chartCni, overrideConfig, "install_cni_helm_override_output.golden")
var overrideConfig chartutil.Values
err := yaml.Unmarshal([]byte(overrideJSON), &overrideConfig)
if err != nil {
t.Fatal("Unexpected error", err)
}
testRenderCniHelm(t, chartCni, &overrideConfig, "install_cni_helm_override_output.golden")
})

}

func testRenderCniHelm(t *testing.T, chart *pb.Chart, overrideConfig *pb.Config, goldenFileName string) {
func testRenderCniHelm(t *testing.T, chart *chart.Chart, overrideConfig *chartutil.Values, goldenFileName string) {
var (
chartName = "linkerd2-cni"
namespace = "linkerd-test"
)

releaseOptions := renderutil.Options{
ReleaseOptions: chartutil.ReleaseOptions{
Name: chartName,
Namespace: namespace,
IsUpgrade: false,
IsInstall: true,
},
releaseOptions := chartutil.ReleaseOptions{
Name: chartName,
Namespace: namespace,
IsUpgrade: false,
IsInstall: true,
}

rendered, err := renderutil.Render(chart, overrideConfig, releaseOptions)
valuesToRender, err := chartutil.ToRenderValues(chart, *overrideConfig, releaseOptions, nil)
if err != nil {
t.Fatal("Unexpected error", err)
}

rendered, err := engine.Render(chart, valuesToRender)
if err != nil {
t.Fatal("Unexpected error", err)
}
Expand All @@ -85,39 +92,41 @@ func testRenderCniHelm(t *testing.T, chart *pb.Chart, overrideConfig *pb.Config,
diffTestdata(t, goldenFileName, buf.String())
}

func chartCniPlugin(t *testing.T) *pb.Chart {
values, err := readCniTestValues(t)
func chartCniPlugin(t *testing.T) *chart.Chart {
rawValues, err := readCniTestValues(t)
if err != nil {
t.Fatal("Unexpected error", err)
}

var values chartutil.Values
err = yaml.Unmarshal(rawValues, &values)
if err != nil {
t.Fatal("Unexpected error", err)
}
chartPartials := chartPartials(t, []string{"templates/_helpers.tpl"})

chart := &pb.Chart{
Metadata: &pb.Metadata{
cniChart := &chart.Chart{
Metadata: &chart.Metadata{
Name: helmCNIDefaultChartName,
Sources: []string{
filepath.Join("..", "..", "..", "charts", "linkerd2-cni"),
},
},
Dependencies: []*pb.Chart{
chartPartials,
},
Values: &pb.Config{
Raw: string(values),
},
Values: values,
}

chart.Templates = append(chart.Templates, &pb.Template{
cniChart.AddDependency(chartPartials)

cniChart.Templates = append(cniChart.Templates, &chart.File{
Name: "templates/cni-plugin.yaml",
})

for _, template := range chart.Templates {
filepath := filepath.Join(chart.Metadata.Sources[0], template.Name)
for _, template := range cniChart.Templates {
filepath := filepath.Join(cniChart.Metadata.Sources[0], template.Name)
template.Data = []byte(readTestdata(t, filepath))
}

return chart
return cniChart
}

func readCniTestValues(t *testing.T) ([]byte, error) {
Expand Down
Loading

0 comments on commit fafeee3

Please sign in to comment.