Skip to content

Instantly share code, notes, and snippets.

@ihcsim
Last active February 25, 2020 05:35
Show Gist options
  • Select an option

  • Save ihcsim/8cd8bcd631a518982a17a2c462fcf54b to your computer and use it in GitHub Desktop.

Select an option

Save ihcsim/8cd8bcd631a518982a17a2c462fcf54b to your computer and use it in GitHub Desktop.
diff --git a/charts/linkerd2/values.yaml b/charts/linkerd2/values.yaml
index e21c8997..7ba88ee9 100644
--- a/charts/linkerd2/values.yaml
+++ b/charts/linkerd2/values.yaml
@@ -172,7 +172,7 @@ nodeSelector:
# Configuration for Add-ons
tracing:
- enabled: false
+ enabled: true
collector:
name: linkerd-collector
image: omnition/opencensus-collector:0.1.10
diff --git a/cli/cmd/install.go b/cli/cmd/install.go
index dfd4390e..541db5a5 100644
--- a/cli/cmd/install.go
+++ b/cli/cmd/install.go
@@ -7,7 +7,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
- "reflect"
"strings"
"time"
@@ -97,6 +96,8 @@ const (
helmDefaultChartName = "linkerd2"
helmDefaultChartDir = "linkerd2"
+ tracingChartName = "tracing"
+
errMsgCannotInitializeClient = `Unable to install the Linkerd control plane. Cannot connect to the Kubernetes cluster:
%s
@@ -152,6 +153,12 @@ var (
"templates/tap.yaml",
"templates/linkerd-values.yaml",
}
+
+ tracingTemplates = []*chartutil.BufferedFile{
+ {Name: chartutil.ChartfileName},
+ {Name: "templates/tracing-rbac.yaml"},
+ {Name: "templates/tracing.yaml"},
+ }
)
// newInstallOptionsWithDefaults initializes install options with default
@@ -840,45 +847,33 @@ func render(w io.Writer, values *l5dcharts.Values) error {
return err
}
- dependencies, err := charts.LoadDependencies(helmDefaultChartName)
- if err != nil {
- return err
- }
-
- // Render for each add-on separately and attach
- for _, dep := range dependencies {
- chartName := dep.Metadata.Name
- if chartName == "partials" {
- continue
+ if values.Stage != configStage {
+ addons, err := parseAddOnValues(values)
+ if err != nil {
+ return err
}
- addOnValues, enabled := checkAddon(values, chartName)
- if enabled {
- files := []*chartutil.BufferedFile{
- {Name: chartutil.ChartfileName},
- }
-
- for _, template := range dep.Templates {
- files = append(files, &chartutil.BufferedFile{
- Name: template.Name,
- Data: template.Data,
- })
- }
+ for addon, values := range addons {
+ var chart *charts.Chart
+ switch addon {
+ case "tracing":
+ chart = &charts.Chart{
+ Name: tracingChartName,
+ Dir: filepath.Join(addOnChartsPath, tracingChartName),
+ Namespace: controlPlaneNamespace,
+ RawValues: append(rawValues, values...),
+ Files: tracingTemplates,
+ }
- subChart := &charts.Chart{
- Name: chartName,
- Dir: filepath.Join(addOnChartsPath, chartName),
- Namespace: controlPlaneNamespace,
- RawValues: append(rawValues, addOnValues...),
- Files: files,
- }
+ b, err := chart.Render()
+ if err != nil {
+ return err
+ }
- addOnBuf, err := subChart.Render()
- if err != nil {
- return err
+ if _, err := buf.WriteString(b.String()); err != nil {
+ return err
+ }
}
-
- buf.Write(addOnBuf.Bytes())
}
}
@@ -1231,18 +1226,20 @@ func toIdentityContext(idvals *identityWithAnchorsAndTrustDomain) *pb.IdentityCo
Scheme: idvals.Identity.Issuer.Scheme,
}
}
-func checkAddon(values *l5dcharts.Values, name string) (addonvalues []byte, enabled bool) {
- r := reflect.ValueOf(values)
+func parseAddOnValues(values *l5dcharts.Values) (map[string][]byte, error) {
+ addonValues := map[string][]byte{}
- if !reflect.Indirect(r).FieldByName(strings.Title(name)).IsNil() {
- if reflect.Indirect(r).FieldByName(strings.Title(name)).MapIndex(reflect.ValueOf("enabled")).Interface().(bool) {
- values, err := yaml.Marshal(reflect.Indirect(r).FieldByName(strings.Title(name)).Interface())
+ if values.Tracing != nil {
+ if values.Tracing["enabled"].(bool) {
+ data, err := yaml.Marshal(values.Tracing)
if err != nil {
- return nil, false
+ return nil, err
}
- return values, true
+
+ addonValues[tracingChartName] = data
}
}
- return nil, false
+
+ return addonValues, nil
}
diff --git a/pkg/charts/charts.go b/pkg/charts/charts.go
index 53d4f901..828d1a91 100644
--- a/pkg/charts/charts.go
+++ b/pkg/charts/charts.go
@@ -2,26 +2,18 @@ package charts
import (
"bytes"
- "io/ioutil"
- "net/http"
"path"
- fpath "path/filepath"
"strings"
"github.com/linkerd/linkerd2/pkg/charts/static"
"github.com/linkerd/linkerd2/pkg/version"
"k8s.io/helm/pkg/chartutil"
- "k8s.io/helm/pkg/proto/hapi/chart"
helmChart "k8s.io/helm/pkg/proto/hapi/chart"
"k8s.io/helm/pkg/renderutil"
"k8s.io/helm/pkg/timeconv"
)
-const (
- versionPlaceholder = "{version}"
- linkerdChartName = "linkerd2"
- linkerdChartRootDir = "linkerd2"
-)
+const versionPlaceholder = "{version}"
// Chart holds the necessary info to render a Helm chart
type Chart struct {
@@ -32,106 +24,6 @@ type Chart struct {
Files []*chartutil.BufferedFile
}
-// LoadChart returns a new Chart object that contains all the files of the
-// specified chart. The chart's files are loaded from the virtual filesystem
-// using the Helm's chartutil.LoadFiles() helper function.
-func LoadChart(chartName string) (*helmChart.Chart, error) {
- var vfiles []*chartutil.BufferedFile
-
- // retrieve all the files of a chart located at linkerdChartRootRid
- walkVFS := func() error {
- files, err := readVirtualFiles(linkerdChartRootDir, chartName)
- if err != nil {
- return err
- }
-
- for path, file := range files {
- data, err := ioutil.ReadAll(file)
- if err != nil {
- return err
- }
-
- filename := path
- if strings.HasPrefix(path, chartName) {
- filename = path[len(chartName)+1:]
- }
-
- vfiles = append(vfiles, &chartutil.BufferedFile{
- Name: filename,
- Data: data,
- })
- }
-
- return nil
- }
-
- if err := walkVFS(); err != nil {
- return nil, err
- }
-
- return chartutil.LoadFiles(vfiles)
-}
-
-// LoadDependencies loads all the dependent subcharts of the specified chart.
-// It relies on LoadChart to load the files and metadata of the chart from the
-// VFS.
-func LoadDependencies(chartName string) ([]*chart.Chart, error) {
- chart, err := LoadChart(chartName)
- if err != nil {
- return nil, err
- }
-
- return chart.Dependencies, nil
-}
-
-// readVirtualFiles read the content of a file from the VFS. If the file is
-// directory, it also loads the children files content, recursively.
-//
-// The result map is keyed off the full path of the files, which is needed
-// by the renderer. The http.File struct contains only the file basename.
-func readVirtualFiles(filename, root string) (map[string]http.File, error) {
- filepath := filename
- if !strings.HasPrefix(filepath, root) {
- filepath = fpath.Join(root, filename)
- }
-
- file, err := static.Templates.Open(filepath)
- if err != nil {
- return nil, err
- }
-
- fileInfo, err := file.Stat()
- if err != nil {
- return nil, err
- }
-
- if !fileInfo.IsDir() {
- return map[string]http.File{filepath: file}, nil
- }
-
- // file is a directory. read its children files.
- files := map[string]http.File{}
- filesInfo, err := file.Readdir(0)
- if err != nil {
- return nil, err
- }
-
- parent := fileInfo.Name()
- for _, fileInfo := range filesInfo {
- filename := fpath.Join(parent, fileInfo.Name())
- children, err := readVirtualFiles(filename, root)
- if err != nil {
- return nil, err
- }
-
- for path, file := range children {
- files[path] = file
- }
- }
-
- return files, nil
-}
-
func (chart *Chart) render(partialsFiles []*chartutil.BufferedFile) (bytes.Buffer, error) {
if err := FilesReader(chart.Dir+"/", chart.Files); err != nil {
return bytes.Buffer{}, err
diff --git a/pkg/charts/charts_test.go b/pkg/charts/charts_test.go
deleted file mode 100644
index 0a79cb5b..00000000
--- a/pkg/charts/charts_test.go
+++ /dev/null
@@ -1,92 +0,0 @@
-package charts
-
-import (
- "reflect"
- "testing"
-
- "k8s.io/helm/pkg/chartutil"
- "k8s.io/helm/pkg/proto/hapi/chart"
-)
-
-const testChartRootDir = "../../charts/linkerd2"
-
-func TestLoadChart(t *testing.T) {
- actual, err := LoadChart(linkerdChartName)
- if err != nil {
- t.Fatal("unexpected error: ", err)
- }
-
- expected, err := chartutil.Load(testChartRootDir)
- if err != nil {
- t.Fatal("unexpected error: ", err)
- }
-
- // compare the charts' metadata
- if !reflect.DeepEqual(expected.Metadata, actual.Metadata) {
- t.Errorf("chart metadata mismatch.\nexpected: %+v\n actual: %+v\n", expected.Metadata, actual.Metadata)
- }
-
- // check for missing templates
- missing := []*chart.Template{}
- for _, expected := range expected.Templates {
- expected := expected
-
- var found bool
- for _, actual := range actual.Templates {
- if reflect.DeepEqual(expected, actual) {
- found = true
- break
- }
- }
-
- if !found {
- missing = append(missing, expected)
- }
- }
-
- if len(missing) > 0 {
- err := "missing chart templates:"
- for _, m := range missing {
- err += m.Name + ", "
- }
- t.Errorf(err)
- }
-}
-
-func TestLoadDependencies(t *testing.T) {
- actual, err := LoadDependencies(linkerdChartName)
- if err != nil {
- t.Fatal("unexpected error: ", err)
- }
-
- expected, err := chartutil.Load(testChartRootDir)
- if err != nil {
- t.Fatal("unexpected error: ", err)
- }
-
- // check for missing dependencies
- missing := []string{}
- for _, expected := range expected.Dependencies {
- expected := expected
-
- var found bool
- for _, actual := range actual {
- if reflect.DeepEqual(expected.Metadata, actual.Metadata) {
- found = true
- break
- }
- }
-
- if !found {
- missing = append(missing, expected.Metadata.Name)
- }
- }
-
- if len(missing) > 0 {
- err := "missing dependencies: "
- for _, m := range missing {
- err += m + ", "
- }
- t.Errorf(err)
- }
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment