From 84e61c03912af7519f4a60cd50f7c5680a313565 Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Tue, 26 Jan 2016 17:46:58 -0700 Subject: [PATCH 1/9] Add chart file support. Also added a log abstraction. --- chart/chart.go | 107 ++++++++++++++++++++++++++++++++++++ chart/chart_test.go | 117 ++++++++++++++++++++++++++++++++++++++++ chart/chartfile.go | 79 +++++++++++++++++++++++++++ chart/chartfile_test.go | 60 +++++++++++++++++++++ glide.lock | 51 ++++++++++++++++++ glide.yaml | 11 ++++ log/log.go | 51 ++++++++++++++++++ log/log_test.go | 49 +++++++++++++++++ 8 files changed, 525 insertions(+) create mode 100644 chart/chart.go create mode 100644 chart/chart_test.go create mode 100644 chart/chartfile.go create mode 100644 chart/chartfile_test.go create mode 100644 glide.lock create mode 100644 glide.yaml create mode 100644 log/log.go create mode 100644 log/log_test.go diff --git a/chart/chart.go b/chart/chart.go new file mode 100644 index 000000000..608f98769 --- /dev/null +++ b/chart/chart.go @@ -0,0 +1,107 @@ +package chart + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/helm/helm/manifest" +) + +// Chart represents a complete chart. +// +// A chart consists of the following parts: +// +// - Chart.yaml: In code, we refer to this as the Chartfile +// - manifests/*.yaml: The Kubernetes manifests +// +// On the Chart object, the manifests are sorted by type into a handful of +// recognized Kubernetes API v1 objects. +// +// TODO: Investigate treating these as unversioned. +type Chart struct { + Chartfile *Chartfile + + // Kind is a map of Kind to an array of manifests. + // + // For example, Kind["Pod"] has an array of Pod manifests. + Kind map[string][]*manifest.Manifest + + // Manifests is an array of Manifest objects. + Manifests []*manifest.Manifest +} + +// Load loads an entire chart. +// +// This includes the Chart.yaml (*Chartfile) and all of the manifests. +// +// If you are just reading the Chart.yaml file, it is substantially more +// performant to use LoadChartfile. +func Load(chart string) (*Chart, error) { + if fi, err := os.Stat(chart); err != nil { + return nil, err + } else if !fi.IsDir() { + return nil, fmt.Errorf("Chart %s is not a directory.", chart) + } + + cf, err := LoadChartfile(filepath.Join(chart, "Chart.yaml")) + if err != nil { + return nil, err + } + + c := &Chart{ + Chartfile: cf, + Kind: map[string][]*manifest.Manifest{}, + } + + ms, err := manifest.ParseDir(chart) + if err != nil { + return c, err + } + + c.attachManifests(ms) + + return c, nil +} + +const ( + // AnnFile is the annotation key for a file's origin. + AnnFile = "chart.helm.sh/file" + + // AnnChartVersion is the annotation key for a chart's version. + AnnChartVersion = "chart.helm.sh/version" + + // AnnChartDesc is the annotation key for a chart's description. + AnnChartDesc = "chart.helm.sh/description" + + // AnnChartName is the annotation key for a chart name. + AnnChartName = "chart.helm.sh/name" +) + +// attachManifests sorts manifests into their respective categories, adding to the Chart. +func (c *Chart) attachManifests(manifests []*manifest.Manifest) { + c.Manifests = manifests + for _, m := range manifests { + c.Kind[m.Kind] = append(c.Kind[m.Kind], m) + } +} + +// UnknownKinds returns a list of kinds that this chart contains, but which were not in the passed in array. +// +// A Chart will store all kinds that are given to it. This makes it possible to get a list of kinds that are not +// known beforehand. +func (c *Chart) UnknownKinds(known []string) []string { + lookup := make(map[string]bool, len(known)) + for _, k := range known { + lookup[k] = true + } + + u := []string{} + for n := range c.Kind { + if _, ok := lookup[n]; !ok { + u = append(u, n) + } + } + + return u +} diff --git a/chart/chart_test.go b/chart/chart_test.go new file mode 100644 index 000000000..c914ee8f3 --- /dev/null +++ b/chart/chart_test.go @@ -0,0 +1,117 @@ +package chart + +import ( + "testing" +) + +const testfile = "../testdata/test-Chart.yaml" +const testchart = "../testdata/charts/kitchensink" + +func TestLoad(t *testing.T) { + c, err := Load(testchart) + if err != nil { + t.Errorf("Failed to load chart: %s", err) + } + + if c.Chartfile.Name != "kitchensink" { + t.Errorf("Expected chart name to be 'kitchensink'. Got '%s'.", c.Chartfile.Name) + } + if c.Chartfile.Dependencies[0].Version != "~10.21" { + d := c.Chartfile.Dependencies[0].Version + t.Errorf("Expected dependency 0 to have version '~10.21'. Got '%s'.", d) + } + + if len(c.Kind["Pod"]) != 3 { + t.Errorf("Expected 3 pods, got %d", len(c.Kind["Pod"])) + } + + if len(c.Kind["ReplicationController"]) == 0 { + t.Error("No RCs found") + } + if len(c.Kind["Namespace"]) == 0 { + t.Errorf("No namespaces found") + } + + if len(c.Kind["Secret"]) == 0 { + t.Error("Is it secret? Is it safe? NO!") + } + + if len(c.Kind["PersistentVolume"]) == 0 { + t.Errorf("No volumes.") + } + + if len(c.Kind["Service"]) == 0 { + t.Error("No service. Just like [insert mobile provider name here]") + } +} + +func TestLoadChart(t *testing.T) { + f, err := LoadChartfile(testfile) + if err != nil { + t.Errorf("Error loading %s: %s", testfile, err) + } + + if f.Name != "alpine-pod" { + t.Errorf("Expected alpine-pod, got %s", f.Name) + } + + if len(f.Maintainers) != 2 { + t.Errorf("Expected 2 maintainers, got %d", len(f.Maintainers)) + } + + if len(f.Dependencies) != 2 { + t.Errorf("Expected 2 dependencies, got %d", len(f.Dependencies)) + } + + if f.Dependencies[1].Name != "bar" { + t.Errorf("Expected second dependency to be bar: %q", f.Dependencies[1].Name) + } + + if f.PreInstall["mykeys"] != "generate-keypair foo" { + t.Errorf("Expected map value for mykeys.") + } + + if f.Source[0] != "https://example.com/helm" { + t.Errorf("Expected https://example.com/helm, got %s", f.Source) + } +} + +func TestVersionOK(t *testing.T) { + f, err := LoadChartfile(testfile) + if err != nil { + t.Errorf("Error loading %s: %s", testfile, err) + } + + // These are canaries. The SemVer package exhuastively tests the + // various permutations. This will alert us if we wired it up + // incorrectly. + + d := f.Dependencies[1] + if d.VersionOK("1.0.0") { + t.Errorf("1.0.0 should have been marked out of range") + } + + if !d.VersionOK("1.2.3") { + t.Errorf("Version 1.2.3 should have been marked in-range") + } + +} + +func TestUnknownKinds(t *testing.T) { + known := []string{"Pod"} + c, err := Load(testchart) + if err != nil { + t.Errorf("Failed to load chart: %s", err) + } + + unknown := c.UnknownKinds(known) + if len(unknown) < 5 { + t.Errorf("Expected at least 5 unknown chart types, got %d.", len(unknown)) + } + + for _, k := range unknown { + if k == "Pod" { + t.Errorf("Pod is not an unknown kind.") + } + } +} diff --git a/chart/chartfile.go b/chart/chartfile.go new file mode 100644 index 000000000..821f941be --- /dev/null +++ b/chart/chartfile.go @@ -0,0 +1,79 @@ +package chart + +import ( + "io/ioutil" + + "github.com/Masterminds/semver" + "gopkg.in/yaml.v2" +) + +// Chartfile describes a Helm Chart (e.g. Chart.yaml) +type Chartfile struct { + Name string `yaml:"name"` + Description string `yaml:"description"` + Version string `yaml:"version"` + Keywords []string `yaml:"keywords,omitempty"` + Maintainers []*Maintainer `yaml:"maintainers,omitempty"` + Source []string `yaml:"source,omitempty"` + Home string `yaml:"home"` + Dependencies []*Dependency `yaml:"dependencies,omitempty"` + Environment []*EnvConstraint `yaml:"environment,omitempty"` +} + +// Maintainer describes a chart maintainer. +type Maintainer struct { + Name string `yaml:"name"` + Email string `yaml:"email,omitempty"` +} + +// Dependency describes a specific dependency. +type Dependency struct { + Name string `yaml:"name,omitempty"` + Version string `yaml:"version"` + Location string `yaml:"location"` +} + +// Specify environmental constraints. +type EnvConstraint struct { + Name string `yaml:"name"` + Version string `yaml:"version"` + Extensions []string `yaml:"extensions,omitempty"` + APIGroups []string `yaml:"apiGroups,omitempty"` +} + +// LoadChartfile loads a Chart.yaml file into a *Chart. +func LoadChartfile(filename string) (*Chartfile, error) { + b, err := ioutil.ReadFile(filename) + if err != nil { + return nil, err + } + var y Chartfile + return &y, yaml.Unmarshal(b, &y) +} + +// Save saves a Chart.yaml file +func (c *Chartfile) Save(filename string) error { + b, err := yaml.Marshal(c) + if err != nil { + return err + } + + return ioutil.WriteFile(filename, b, 0644) +} + +// VersionOK returns true if the given version meets the constraints. +// +// It returns false if the version string or constraint is unparsable or if the +// version does not meet the constraint. +func (d *Dependency) VersionOK(version string) bool { + c, err := semver.NewConstraint(d.Version) + if err != nil { + return false + } + v, err := semver.NewVersion(version) + if err != nil { + return false + } + + return c.Check(v) +} diff --git a/chart/chartfile_test.go b/chart/chartfile_test.go new file mode 100644 index 000000000..78413fd86 --- /dev/null +++ b/chart/chartfile_test.go @@ -0,0 +1,60 @@ +package chart + +import ( + "io/ioutil" + "os" + "testing" +) + +var testChart = `#helm:generate foo +name: frobniz +description: This is a frobniz. +version: 1.2.3-alpha.1+12345 +keywords: + - frobnitz + - sprocket + - dodad +maintainers: + - name: The Helm Team + email: helm@example.com + - name: Someone Else + email: nobody@example.com +source: https://example.com/foo/bar +home: http://example.com +dependencies: + - name: thingerbob + location: https://example.com/charts/thingerbob-3.2.1.tgz + version: ^3 +environment: + - name: Kubernetes + version: ~1.1 + extensions: + - extensions/v1beta1 + - extensions/v1beta1/daemonset + apiGroups: + - 3rdParty +` + +func TestLoadChartfile(t *testing.T) { + out, err := ioutil.TempFile("", "chartfile-") + if err != nil { + t.Fatal(err) + } + tname := out.Name() + defer func() { + os.Remove(tname) + }() + + out.Write([]byte(testChart)) + out.Close() + + c, err := LoadChartfile(tname) + if err != nil { + t.Errorf("Failed to open %s: %s", tname, err) + return + } + + if len(c.Environment[0].Extensions) != 2 { + t.Errorf("Expected two extensions, got %d", len(c.Environment[0].Extensions)) + } +} diff --git a/glide.lock b/glide.lock new file mode 100644 index 000000000..2e98b0206 --- /dev/null +++ b/glide.lock @@ -0,0 +1,51 @@ +hash: db55a031aaa2f352fa5e9e4fda871039afb80e383a57fc77e4b35114d47cca8a +updated: 2016-01-26T17:30:54.243252416-07:00 +imports: +- name: github.com/emicklei/go-restful + version: b86acf97a74ed7603ac78d012f5535b4d587b156 +- name: github.com/ghodss/yaml + version: 73d445a93680fa1a78ae23a5839bad48f32ba1ee +- name: github.com/golang/glog + version: 23def4e6c14b4da8ac2ed8007337bc5eb5007998 +- name: github.com/golang/protobuf + version: 6aaa8d47701fa6cf07e914ec01fde3d4a1fe79c3 +- name: github.com/google/go-github + version: b8b4ac742977310ff6e75140a403a38dab109977 + subpackages: + - /github +- name: github.com/google/go-querystring + version: 2a60fc2ba6c19de80291203597d752e9ba58e4c0 +- name: github.com/gorilla/context + version: 1c83b3eabd45b6d76072b66b746c20815fb2872d +- name: github.com/gorilla/handlers + version: 8f2758070a82adb7a3ad6b223a0b91878f32d400 +- name: github.com/gorilla/mux + version: 26a6070f849969ba72b72256e9f14cf519751690 +- name: github.com/gorilla/schema + version: 14c555599c2a4f493c1e13fd1ea6fdf721739028 +- name: github.com/Masterminds/semver + version: c4f7ef0702f269161a60489ccbbc9f1241ad1265 +- name: github.com/mjibson/appstats + version: 0542d5f0e87ea3a8fa4174322b9532f5d04f9fa8 +- name: golang.org/x/crypto + version: 1f22c0103821b9390939b6776727195525381532 +- name: golang.org/x/net + version: 04b9de9b512f58addf28c9853d50ebef61c3953e +- name: golang.org/x/oauth2 + version: 8a57ed94ffd43444c0879fe75701732a38afc985 +- name: golang.org/x/text + version: 6d3c22c4525a4da167968fa2479be5524d2e8bd0 +- name: google.golang.com/appengine + version: "" + repo: https://google.golang.com/appengine +- name: google.golang.org/api + version: 0caa37974a5f5ae67172acf68b4970f7864f994c +- name: google.golang.org/appengine + version: 6bde959377a90acb53366051d7d587bfd7171354 +- name: google.golang.org/cloud + version: fb10e8da373d97f6ba5e648299a10b3b91f14cd5 +- name: google.golang.org/grpc + version: e29d659177655e589850ba7d3d83f7ce12ef23dd +- name: gopkg.in/yaml.v2 + version: f7716cbe52baa25d2e9b0d0da546fcf909fc16b4 +devImports: [] diff --git a/glide.yaml b/glide.yaml new file mode 100644 index 000000000..be7d7dc53 --- /dev/null +++ b/glide.yaml @@ -0,0 +1,11 @@ +package: github.com/kubernetes/deployment-manager +import: +- package: github.com/emicklei/go-restful +- package: github.com/ghodss/yaml +- package: github.com/google/go-github + subpackages: + - /github +- package: github.com/gorilla/handlers +- package: github.com/gorilla/mux +- package: gopkg.in/yaml.v2 +- package: github.com/Masterminds/semver diff --git a/log/log.go b/log/log.go new file mode 100644 index 000000000..8d74d4878 --- /dev/null +++ b/log/log.go @@ -0,0 +1,51 @@ +/* Package log provides simple convenience wrappers for logging. + +Following convention, this provides functions for logging warnings, errors, information +and debugging. +*/ +package log + +import ( + "log" + "os" +) + +// LogReceiver can receive log messages from this package. +type LogReceiver interface { + Printf(format string, v ...interface{}) +} + +// Logger is the destination for this package. +// +// The logger that this prints to. +var Logger LogReceiver = log.New(os.Stderr, "", log.LstdFlags) + +// IsDebugging controls debugging output. +// +// If this is true, debugging messages will be printed. Expensive debugging +// operations can be wrapped in `if log.IsDebugging {}`. +var IsDebugging bool = false + +// Err prints an error of severity ERROR to the log. +func Err(msg string, v ...interface{}) { + Logger.Printf("[ERROR] "+msg+"\n", v...) +} + +// Warn prints an error severity WARN to the log. +func Warn(msg string, v ...interface{}) { + Logger.Printf("[WARN] "+msg+"\n", v...) +} + +// Info prints an error of severity INFO to the log. +func Info(msg string, v ...interface{}) { + Logger.Printf("[INFO] "+msg+"\n", v...) +} + +// Debug prints an error severity DEBUG to the log. +// +// Debug will only print if IsDebugging is true. +func Debug(msg string, v ...interface{}) { + if IsDebugging { + Logger.Printf("[DEBUG] "+msg+"\n", v...) + } +} diff --git a/log/log_test.go b/log/log_test.go new file mode 100644 index 000000000..3a10b8d2a --- /dev/null +++ b/log/log_test.go @@ -0,0 +1,49 @@ +package log + +import ( + "bytes" + "fmt" + "testing" +) + +type LoggerMock struct { + b bytes.Buffer +} + +func (l *LoggerMock) Printf(m string, v ...interface{}) { + l.b.Write([]byte(fmt.Sprintf(m, v...))) +} + +func TestLogger(t *testing.T) { + l := &LoggerMock{} + Logger = l + IsDebugging = true + + Err("%s%s%s", "a", "b", "c") + expect := "[ERROR] abc\n" + if l.b.String() != expect { + t.Errorf("Expected %q, got %q", expect, l.b.String()) + } + l.b.Reset() + + tests := map[string]func(string, ...interface{}){ + "[WARN] test\n": Warn, + "[INFO] test\n": Info, + "[DEBUG] test\n": Debug, + } + + for expect, f := range tests { + f("test") + if l.b.String() != expect { + t.Errorf("Expected %q, got %q", expect, l.b.String()) + } + l.b.Reset() + } + + IsDebugging = false + Debug("HELLO") + if l.b.String() != "" { + t.Errorf("Expected debugging to disable. Got %q", l.b.String()) + } + l.b.Reset() +} From 90f1d3d567fe9c7e3529d288af0b8d9930107596 Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Wed, 27 Jan 2016 18:01:34 -0700 Subject: [PATCH 2/9] Add Chart tar reader, dir reader, tests. --- chart/chart.go | 202 +++++++++++++----- chart/chart_test.go | 119 +++-------- chart/chartfile_test.go | 86 ++++---- chart/doc.go | 6 + chart/testdata/README.md | 9 + chart/testdata/frobnitz-0.0.1.tgz | Bin 0 -> 6074 bytes chart/testdata/frobnitz/Chart.yaml | 28 +++ chart/testdata/frobnitz/LICENSE | 202 ++++++++++++++++++ chart/testdata/frobnitz/README.md | 11 + chart/testdata/frobnitz/docs/README.md | 1 + chart/testdata/frobnitz/hooks/pre-install.py | 1 + .../templates/wordpress-resources.yaml | 12 ++ .../frobnitz/templates/wordpress.jinja | 72 +++++++ .../frobnitz/templates/wordpress.jinja.schema | 69 ++++++ .../frobnitz/templates/wordpress.yaml | 6 + chart/testdata/ill-1.2.3.tgz | Bin 0 -> 6187 bytes chart/testdata/ill/Chart.yaml | 28 +++ chart/testdata/ill/LICENSE | 202 ++++++++++++++++++ chart/testdata/ill/README.md | 11 + chart/testdata/ill/docs/README.md | 1 + chart/testdata/ill/hooks/pre-install.py | 1 + .../ill/templates/wordpress-resources.yaml | 12 ++ chart/testdata/ill/templates/wordpress.jinja | 72 +++++++ .../ill/templates/wordpress.jinja.schema | 69 ++++++ chart/testdata/ill/templates/wordpress.yaml | 6 + 25 files changed, 1041 insertions(+), 185 deletions(-) create mode 100644 chart/doc.go create mode 100644 chart/testdata/README.md create mode 100644 chart/testdata/frobnitz-0.0.1.tgz create mode 100644 chart/testdata/frobnitz/Chart.yaml create mode 100644 chart/testdata/frobnitz/LICENSE create mode 100644 chart/testdata/frobnitz/README.md create mode 100644 chart/testdata/frobnitz/docs/README.md create mode 100644 chart/testdata/frobnitz/hooks/pre-install.py create mode 100644 chart/testdata/frobnitz/templates/wordpress-resources.yaml create mode 100644 chart/testdata/frobnitz/templates/wordpress.jinja create mode 100644 chart/testdata/frobnitz/templates/wordpress.jinja.schema create mode 100644 chart/testdata/frobnitz/templates/wordpress.yaml create mode 100644 chart/testdata/ill-1.2.3.tgz create mode 100644 chart/testdata/ill/Chart.yaml create mode 100644 chart/testdata/ill/LICENSE create mode 100644 chart/testdata/ill/README.md create mode 100644 chart/testdata/ill/docs/README.md create mode 100644 chart/testdata/ill/hooks/pre-install.py create mode 100644 chart/testdata/ill/templates/wordpress-resources.yaml create mode 100644 chart/testdata/ill/templates/wordpress.jinja create mode 100644 chart/testdata/ill/templates/wordpress.jinja.schema create mode 100644 chart/testdata/ill/templates/wordpress.yaml diff --git a/chart/chart.go b/chart/chart.go index 608f98769..514a4001b 100644 --- a/chart/chart.go +++ b/chart/chart.go @@ -1,11 +1,23 @@ package chart import ( + "archive/tar" + "compress/gzip" + "errors" "fmt" + "io" + "io/ioutil" "os" "path/filepath" + "strings" - "github.com/helm/helm/manifest" + "github.com/kubernetes/deployment-manager/log" +) + +const ( + preTemplates string = "templates/" + preHooks string = "hooks/" + preDocs string = "docs/" ) // Chart represents a complete chart. @@ -13,31 +25,67 @@ import ( // A chart consists of the following parts: // // - Chart.yaml: In code, we refer to this as the Chartfile -// - manifests/*.yaml: The Kubernetes manifests +// - templates/*: The template directory +// - README.md: Optional README file +// - LICENSE: Optional license file +// - hooks/: Optional hooks registry +// - docs/: Optional docs directory // -// On the Chart object, the manifests are sorted by type into a handful of -// recognized Kubernetes API v1 objects. +// Packed charts are stored in gzipped tar archives (.tgz). Unpackaged charts +// are directories where the directory name is the Chartfile.Name. // -// TODO: Investigate treating these as unversioned. -type Chart struct { - Chartfile *Chartfile +// Optionally, a chart might also locate a provenance (.prov) file that it +// can use for cryptographic signing. +type Chart interface { + // Chartfile resturns a *Chartfile for this chart. + Chartfile() *Chartfile + // Dir returns a directory where the chart can be accessed. + Dir() string + + Close() error +} + +type dirChart struct { + chartfile *Chartfile +} + +func (d *dirChart) Chartfile() *Chartfile { + return d.chartfile +} + +func (d *dirChart) Dir() string { + return "." +} + +func (d *dirChart) Close() error { + return nil +} + +type tarChart struct { + chartfile *Chartfile + tmpDir string +} - // Kind is a map of Kind to an array of manifests. - // - // For example, Kind["Pod"] has an array of Pod manifests. - Kind map[string][]*manifest.Manifest +func (t *tarChart) Chartfile() *Chartfile { + return t.chartfile +} + +func (t *tarChart) Dir() string { + return "." +} - // Manifests is an array of Manifest objects. - Manifests []*manifest.Manifest +func (t *tarChart) Close() error { + // Remove the temp directory. + return os.RemoveAll(t.tmpDir) } -// Load loads an entire chart. +// LoadDir loads an entire chart from a directory. // // This includes the Chart.yaml (*Chartfile) and all of the manifests. // // If you are just reading the Chart.yaml file, it is substantially more // performant to use LoadChartfile. -func Load(chart string) (*Chart, error) { +func LoadDir(chart string) (Chart, error) { if fi, err := os.Stat(chart); err != nil { return nil, err } else if !fi.IsDir() { @@ -49,59 +97,103 @@ func Load(chart string) (*Chart, error) { return nil, err } - c := &Chart{ - Chartfile: cf, - Kind: map[string][]*manifest.Manifest{}, + c := &dirChart{ + chartfile: cf, } - ms, err := manifest.ParseDir(chart) - if err != nil { - return c, err - } - - c.attachManifests(ms) - return c, nil } -const ( - // AnnFile is the annotation key for a file's origin. - AnnFile = "chart.helm.sh/file" - - // AnnChartVersion is the annotation key for a chart's version. - AnnChartVersion = "chart.helm.sh/version" - - // AnnChartDesc is the annotation key for a chart's description. - AnnChartDesc = "chart.helm.sh/description" +// Load loads a chart from a chart archive. +// +// A chart archive is a gzipped tar archive that follows the Chart format +// specification. +func Load(archive string) (Chart, error) { + if fi, err := os.Stat(archive); err != nil { + return nil, err + } else if fi.IsDir() { + return nil, errors.New("cannot load a directory with chart.Load()") + } - // AnnChartName is the annotation key for a chart name. - AnnChartName = "chart.helm.sh/name" -) + raw, err := os.Open(archive) + if err != nil { + return nil, err + } + defer raw.Close() -// attachManifests sorts manifests into their respective categories, adding to the Chart. -func (c *Chart) attachManifests(manifests []*manifest.Manifest) { - c.Manifests = manifests - for _, m := range manifests { - c.Kind[m.Kind] = append(c.Kind[m.Kind], m) + unzipped, err := gzip.NewReader(raw) + if err != nil { + return nil, err } + defer unzipped.Close() + + untarred := tar.NewReader(unzipped) + return loadTar(untarred) } -// UnknownKinds returns a list of kinds that this chart contains, but which were not in the passed in array. -// -// A Chart will store all kinds that are given to it. This makes it possible to get a list of kinds that are not -// known beforehand. -func (c *Chart) UnknownKinds(known []string) []string { - lookup := make(map[string]bool, len(known)) - for _, k := range known { - lookup[k] = true +func loadTar(r *tar.Reader) (Chart, error) { + td, err := ioutil.TempDir("", "chart-") + if err != nil { + return nil, err + } + c := &tarChart{ + chartfile: &Chartfile{}, + tmpDir: td, } - u := []string{} - for n := range c.Kind { - if _, ok := lookup[n]; !ok { - u = append(u, n) + firstDir := "" + + hdr, err := r.Next() + for err == nil { + log.Debug("Reading %s", hdr.Name) + + // This is to prevent malformed tar attacks. + hdr.Name = filepath.Clean(hdr.Name) + + if firstDir == "" { + fi := hdr.FileInfo() + if fi.IsDir() { + log.Debug("Discovered app named %s", hdr.Name) + firstDir = hdr.Name + } else { + log.Warn("Unexpected file at root of archive: %s", hdr.Name) + } + } else if strings.HasPrefix(hdr.Name, firstDir) { + log.Debug("Extracting %s to %s", hdr.Name, c.tmpDir) + + // We know this has the prefix, so we know there won't be an error. + rel, _ := filepath.Rel(firstDir, hdr.Name) + + // If tar record is a directory, create one in the tmpdir and return. + if hdr.FileInfo().IsDir() { + os.MkdirAll(filepath.Join(c.tmpDir, rel), 0755) + hdr, err = r.Next() + continue + } + + dest := filepath.Join(c.tmpDir, rel) + f, err := os.Create(filepath.Join(c.tmpDir, rel)) + if err != nil { + log.Warn("Could not create %s: %s", dest, err) + hdr, err = r.Next() + continue + } + if _, err := io.Copy(f, r); err != nil { + log.Warn("Failed to copy %s: %s", dest, err) + } + f.Close() + } else { + log.Warn("Unexpected file outside of chart: %s", hdr.Name) } + hdr, err = r.Next() } - return u + if err != nil && err != io.EOF { + log.Warn("Unexpected error reading tar: %s", err) + c.Close() + return c, err + } + log.Info("Reached end of Tar file") + + return c, nil } diff --git a/chart/chart_test.go b/chart/chart_test.go index c914ee8f3..91f020f3e 100644 --- a/chart/chart_test.go +++ b/chart/chart_test.go @@ -2,116 +2,63 @@ package chart import ( "testing" -) - -const testfile = "../testdata/test-Chart.yaml" -const testchart = "../testdata/charts/kitchensink" -func TestLoad(t *testing.T) { - c, err := Load(testchart) - if err != nil { - t.Errorf("Failed to load chart: %s", err) - } - - if c.Chartfile.Name != "kitchensink" { - t.Errorf("Expected chart name to be 'kitchensink'. Got '%s'.", c.Chartfile.Name) - } - if c.Chartfile.Dependencies[0].Version != "~10.21" { - d := c.Chartfile.Dependencies[0].Version - t.Errorf("Expected dependency 0 to have version '~10.21'. Got '%s'.", d) - } - - if len(c.Kind["Pod"]) != 3 { - t.Errorf("Expected 3 pods, got %d", len(c.Kind["Pod"])) - } - - if len(c.Kind["ReplicationController"]) == 0 { - t.Error("No RCs found") - } - if len(c.Kind["Namespace"]) == 0 { - t.Errorf("No namespaces found") - } - - if len(c.Kind["Secret"]) == 0 { - t.Error("Is it secret? Is it safe? NO!") - } + "github.com/kubernetes/deployment-manager/log" +) - if len(c.Kind["PersistentVolume"]) == 0 { - t.Errorf("No volumes.") - } +const testfile = "testdata/frobnitz/Chart.yaml" +const testdir = "testdata/frobnitz/" +const testarchive = "testdata/frobnitz-0.0.1.tgz" +const testill = "testdata/ill-1.2.3.tgz" - if len(c.Kind["Service"]) == 0 { - t.Error("No service. Just like [insert mobile provider name here]") - } +func init() { + log.IsDebugging = true } -func TestLoadChart(t *testing.T) { - f, err := LoadChartfile(testfile) +func TestLoadDir(t *testing.T) { + c, err := LoadDir(testdir) if err != nil { - t.Errorf("Error loading %s: %s", testfile, err) - } - - if f.Name != "alpine-pod" { - t.Errorf("Expected alpine-pod, got %s", f.Name) - } - - if len(f.Maintainers) != 2 { - t.Errorf("Expected 2 maintainers, got %d", len(f.Maintainers)) - } - - if len(f.Dependencies) != 2 { - t.Errorf("Expected 2 dependencies, got %d", len(f.Dependencies)) - } - - if f.Dependencies[1].Name != "bar" { - t.Errorf("Expected second dependency to be bar: %q", f.Dependencies[1].Name) + t.Errorf("Failed to load chart: %s", err) } - if f.PreInstall["mykeys"] != "generate-keypair foo" { - t.Errorf("Expected map value for mykeys.") + if c.Chartfile().Name != "frobnitz" { + t.Errorf("Expected chart name to be 'frobnitz'. Got '%s'.", c.Chartfile().Name) } - if f.Source[0] != "https://example.com/helm" { - t.Errorf("Expected https://example.com/helm, got %s", f.Source) + if c.Chartfile().Dependencies[0].Version != "^3" { + d := c.Chartfile().Dependencies[0].Version + t.Errorf("Expected dependency 0 to have version '^3'. Got '%s'.", d) } } -func TestVersionOK(t *testing.T) { - f, err := LoadChartfile(testfile) +func TestLoad(t *testing.T) { + c, err := Load(testarchive) if err != nil { - t.Errorf("Error loading %s: %s", testfile, err) + t.Errorf("Failed to load chart: %s", err) + return } + defer c.Close() - // These are canaries. The SemVer package exhuastively tests the - // various permutations. This will alert us if we wired it up - // incorrectly. - - d := f.Dependencies[1] - if d.VersionOK("1.0.0") { - t.Errorf("1.0.0 should have been marked out of range") + if c.Chartfile() == nil { + t.Error("No chartfile was loaded.") + return } - if !d.VersionOK("1.2.3") { - t.Errorf("Version 1.2.3 should have been marked in-range") + if c.Chartfile().Name != "frobnitz" { + t.Errorf("Expected name to be frobnitz, got %q", c.Chartfile().Name) } - } -func TestUnknownKinds(t *testing.T) { - known := []string{"Pod"} - c, err := Load(testchart) +func TestLoadIll(t *testing.T) { + c, err := Load(testill) if err != nil { t.Errorf("Failed to load chart: %s", err) + return } + defer c.Close() - unknown := c.UnknownKinds(known) - if len(unknown) < 5 { - t.Errorf("Expected at least 5 unknown chart types, got %d.", len(unknown)) - } - - for _, k := range unknown { - if k == "Pod" { - t.Errorf("Pod is not an unknown kind.") - } + if c.Chartfile() == nil { + t.Error("No chartfile was loaded.") + return } } diff --git a/chart/chartfile_test.go b/chart/chartfile_test.go index 78413fd86..92781d3fa 100644 --- a/chart/chartfile_test.go +++ b/chart/chartfile_test.go @@ -1,60 +1,58 @@ package chart import ( - "io/ioutil" - "os" "testing" ) -var testChart = `#helm:generate foo -name: frobniz -description: This is a frobniz. -version: 1.2.3-alpha.1+12345 -keywords: - - frobnitz - - sprocket - - dodad -maintainers: - - name: The Helm Team - email: helm@example.com - - name: Someone Else - email: nobody@example.com -source: https://example.com/foo/bar -home: http://example.com -dependencies: - - name: thingerbob - location: https://example.com/charts/thingerbob-3.2.1.tgz - version: ^3 -environment: - - name: Kubernetes - version: ~1.1 - extensions: - - extensions/v1beta1 - - extensions/v1beta1/daemonset - apiGroups: - - 3rdParty -` - func TestLoadChartfile(t *testing.T) { - out, err := ioutil.TempFile("", "chartfile-") + f, err := LoadChartfile(testfile) if err != nil { - t.Fatal(err) + t.Errorf("Failed to open %s: %s", testfile, err) + return + } + + if len(f.Environment[0].Extensions) != 2 { + t.Errorf("Expected two extensions, got %d", len(f.Environment[0].Extensions)) + } + + if f.Name != "frobnitz" { + t.Errorf("Expected frobnitz, got %s", f.Name) } - tname := out.Name() - defer func() { - os.Remove(tname) - }() - out.Write([]byte(testChart)) - out.Close() + if len(f.Maintainers) != 2 { + t.Errorf("Expected 2 maintainers, got %d", len(f.Maintainers)) + } - c, err := LoadChartfile(tname) + if len(f.Dependencies) != 1 { + t.Errorf("Expected 2 dependencies, got %d", len(f.Dependencies)) + } + + if f.Dependencies[0].Name != "thingerbob" { + t.Errorf("Expected second dependency to be thingerbob: %q", f.Dependencies[0].Name) + } + + if f.Source[0] != "https://example.com/helm" { + t.Errorf("Expected https://example.com/helm, got %s", f.Source) + } +} + +func TestVersionOK(t *testing.T) { + f, err := LoadChartfile(testfile) if err != nil { - t.Errorf("Failed to open %s: %s", tname, err) - return + t.Errorf("Error loading %s: %s", testfile, err) } - if len(c.Environment[0].Extensions) != 2 { - t.Errorf("Expected two extensions, got %d", len(c.Environment[0].Extensions)) + // These are canaries. The SemVer package exhuastively tests the + // various permutations. This will alert us if we wired it up + // incorrectly. + + d := f.Dependencies[0] + if d.VersionOK("1.0.0") { + t.Errorf("1.0.0 should have been marked out of range") + } + + if !d.VersionOK("3.2.3") { + t.Errorf("Version 3.2.3 should have been marked in-range") } + } diff --git a/chart/doc.go b/chart/doc.go new file mode 100644 index 000000000..521446e07 --- /dev/null +++ b/chart/doc.go @@ -0,0 +1,6 @@ +/* Package chart implements the Chart format. + +This package provides tools for working with the Chart format, including the +Chartfile (chart.yaml) and compressed chart archives. +*/ +package chart diff --git a/chart/testdata/README.md b/chart/testdata/README.md new file mode 100644 index 000000000..6ddc704f5 --- /dev/null +++ b/chart/testdata/README.md @@ -0,0 +1,9 @@ +The testdata directory here holds charts that match the specification. + +The `fromnitz/` directory contains a chart that matches the chart +specification. + +The `frobnitz-0.0.1.tgz` file is an archive of the `frobnitz` directory. + +The `ill` chart and directory is a chart that is not 100% compatible, +but which should still be parseable. diff --git a/chart/testdata/frobnitz-0.0.1.tgz b/chart/testdata/frobnitz-0.0.1.tgz new file mode 100644 index 0000000000000000000000000000000000000000..79d611a5d98468e7ee84bf8dbddbb3abe2a58c71 GIT binary patch literal 6074 zcmV;r7e(kFiwFRzMX6Q*1MNL)a~n63`K(`ol`Cbbj-vRG}0kNRuCh&Upr;Bs|YYMLwR1 zY~?(dHksT4$L_=b{ZBF`PX4Dep5@I2PvXyx1Nd=taIjYX?RK|YlmAh-bMOsoeRfJ# zjDK}>sDiJCbtQTt$E=ko$MT^QJ zt%__c48mES&-L;Ce(-!BxUxUu+15-UV4P8%06dzDG!p4piVEHNOr}$jjns%Jh}D=I zKCZ$tMhn#Y<(j=NFtpvwryppwXY_w{w?uj^GnFPH%|m=Y6(fU#OsRKYNC7@c{s$eD|6yk%{|6af{xABcz4Kx3 zOW?RY|AUmTd*tNbZa-V1wS};NtAnDSq9cS!ey>)u8|5EBuB4w43ZqOeAPICNj+}OU<_3(KgdF9>**Z zJcWc!uv}ybWVmU>#wv}>DoBhoR%p>+nSiV{D#rMA!-B%Ok<@u6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ZjdD7ba{?CI9Y{jF+=Q5Ac?>O0SF;@BE$Nwg;K)<6yuW0y*>_&{Vgwk=e zuV2xKaQ4ddQ?bQd^F)w0>DAmf=J`4DFX~6iO zfJC&h;)Y6TFJfgX!NXZs@=EiYln$*be>b8YDkuDejqxbU73xtS!dH){0N!jnK0X}WHmtbyTRFRC#J!J>cUk<3)7*bfV)V~s? zD_zJZt1tEu?oi*g$XHh33j--4HC4lX*>i95up4m?@+uxPB#V&QuEc#6yxPlHk3CcP zlAxXDl)UD-?|Xc~Z;&+TG3X zKR(Pbzqmk+K~pK#vj3IM!>l3;xpm6%l=#K zC)MBo?X*`-_RDb{d%UlAz)BX?Jwcmd~I%;q3|2)Vj z{|`Opr5`YF!i7K8u<;C=u^F4O8Jn>go3R<2u^F4O8Jn>g|HR{e0at-xcK~<*0D`W? A3IG5A literal 0 HcmV?d00001 diff --git a/chart/testdata/frobnitz/Chart.yaml b/chart/testdata/frobnitz/Chart.yaml new file mode 100644 index 000000000..9572f010c --- /dev/null +++ b/chart/testdata/frobnitz/Chart.yaml @@ -0,0 +1,28 @@ +#helm:generate foo +name: frobnitz +description: This is a frobniz. +version: "1.2.3-alpha.1+12345" +keywords: + - frobnitz + - sprocket + - dodad +maintainers: + - name: The Helm Team + email: helm@example.com + - name: Someone Else + email: nobody@example.com +source: + - https://example.com/foo/bar +home: http://example.com +dependencies: + - name: thingerbob + location: https://example.com/charts/thingerbob-3.2.1.tgz + version: ^3 +environment: + - name: Kubernetes + version: ~1.1 + extensions: + - extensions/v1beta1 + - extensions/v1beta1/daemonset + apiGroups: + - 3rdParty diff --git a/chart/testdata/frobnitz/LICENSE b/chart/testdata/frobnitz/LICENSE new file mode 100644 index 000000000..d64569567 --- /dev/null +++ b/chart/testdata/frobnitz/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/chart/testdata/frobnitz/README.md b/chart/testdata/frobnitz/README.md new file mode 100644 index 000000000..8cf4cc3d7 --- /dev/null +++ b/chart/testdata/frobnitz/README.md @@ -0,0 +1,11 @@ +# Frobnitz + +This is an example chart. + +## Usage + +This is an example. It has no usage. + +## Development + +For developer info, see the top-level repository. diff --git a/chart/testdata/frobnitz/docs/README.md b/chart/testdata/frobnitz/docs/README.md new file mode 100644 index 000000000..d40747caf --- /dev/null +++ b/chart/testdata/frobnitz/docs/README.md @@ -0,0 +1 @@ +This is a placeholder for documentation. diff --git a/chart/testdata/frobnitz/hooks/pre-install.py b/chart/testdata/frobnitz/hooks/pre-install.py new file mode 100644 index 000000000..c9b0d0a92 --- /dev/null +++ b/chart/testdata/frobnitz/hooks/pre-install.py @@ -0,0 +1 @@ +# Placeholder. diff --git a/chart/testdata/frobnitz/templates/wordpress-resources.yaml b/chart/testdata/frobnitz/templates/wordpress-resources.yaml new file mode 100644 index 000000000..00f709de0 --- /dev/null +++ b/chart/testdata/frobnitz/templates/wordpress-resources.yaml @@ -0,0 +1,12 @@ +# Google Cloud Deployment Manager template +resources: +- name: nfs-disk + type: compute.v1.disk + properties: + zone: us-central1-b + sizeGb: 200 +- name: mysql-disk + type: compute.v1.disk + properties: + zone: us-central1-b + sizeGb: 200 diff --git a/chart/testdata/frobnitz/templates/wordpress.jinja b/chart/testdata/frobnitz/templates/wordpress.jinja new file mode 100644 index 000000000..f34e4fec9 --- /dev/null +++ b/chart/testdata/frobnitz/templates/wordpress.jinja @@ -0,0 +1,72 @@ +#helm:generate dm_template +{% set PROPERTIES = properties or {} %} +{% set PROJECT = PROPERTIES['project'] or 'dm-k8s-testing' %} +{% set NFS_SERVER = PROPERTIES['nfs-server'] or {} %} +{% set NFS_SERVER_IP = NFS_SERVER['ip'] or '10.0.253.247' %} +{% set NFS_SERVER_PORT = NFS_SERVER['port'] or 2049 %} +{% set NFS_SERVER_DISK = NFS_SERVER['disk'] or 'nfs-disk' %} +{% set NFS_SERVER_DISK_FSTYPE = NFS_SERVER['fstype'] or 'ext4' %} +{% set NGINX = PROPERTIES['nginx'] or {} %} +{% set NGINX_PORT = 80 %} +{% set NGINX_REPLICAS = NGINX['replicas'] or 2 %} +{% set WORDPRESS_PHP = PROPERTIES['wordpress-php'] or {} %} +{% set WORDPRESS_PHP_REPLICAS = WORDPRESS_PHP['replicas'] or 2 %} +{% set WORDPRESS_PHP_PORT = WORDPRESS_PHP['port'] or 9000 %} +{% set MYSQL = PROPERTIES['mysql'] or {} %} +{% set MYSQL_PORT = MYSQL['port'] or 3306 %} +{% set MYSQL_PASSWORD = MYSQL['password'] or 'mysql-password' %} +{% set MYSQL_DISK = MYSQL['disk'] or 'mysql-disk' %} +{% set MYSQL_DISK_FSTYPE = MYSQL['fstype'] or 'ext4' %} + +resources: +- name: nfs + type: github.com/kubernetes/application-dm-templates/storage/nfs:v1 + properties: + ip: {{ NFS_SERVER_IP }} + port: {{ NFS_SERVER_PORT }} + disk: {{ NFS_SERVER_DISK }} + fstype: {{NFS_SERVER_DISK_FSTYPE }} +- name: nginx + type: github.com/kubernetes/application-dm-templates/common/replicatedservice:v2 + properties: + service_port: {{ NGINX_PORT }} + container_port: {{ NGINX_PORT }} + replicas: {{ NGINX_REPLICAS }} + external_service: true + image: gcr.io/{{ PROJECT }}/nginx:latest + volumes: + - mount_path: /var/www/html + persistentVolumeClaim: + claimName: nfs +- name: mysql + type: github.com/kubernetes/application-dm-templates/common/replicatedservice:v2 + properties: + service_port: {{ MYSQL_PORT }} + container_port: {{ MYSQL_PORT }} + replicas: 1 + image: mysql:5.6 + env: + - name: MYSQL_ROOT_PASSWORD + value: {{ MYSQL_PASSWORD }} + volumes: + - mount_path: /var/lib/mysql + gcePersistentDisk: + pdName: {{ MYSQL_DISK }} + fsType: {{ MYSQL_DISK_FSTYPE }} +- name: wordpress-php + type: github.com/kubernetes/application-dm-templates/common/replicatedservice:v2 + properties: + service_name: wordpress-php + service_port: {{ WORDPRESS_PHP_PORT }} + container_port: {{ WORDPRESS_PHP_PORT }} + replicas: 2 + image: wordpress:fpm + env: + - name: WORDPRESS_DB_PASSWORD + value: {{ MYSQL_PASSWORD }} + - name: WORDPRESS_DB_HOST + value: mysql-service + volumes: + - mount_path: /var/www/html + persistentVolumeClaim: + claimName: nfs diff --git a/chart/testdata/frobnitz/templates/wordpress.jinja.schema b/chart/testdata/frobnitz/templates/wordpress.jinja.schema new file mode 100644 index 000000000..215b47e1e --- /dev/null +++ b/chart/testdata/frobnitz/templates/wordpress.jinja.schema @@ -0,0 +1,69 @@ +info: + title: Wordpress + description: | + Defines a Wordpress website by defining four replicated services: an NFS service, an nginx service, a wordpress-php service, and a MySQL service. + + The nginx service and the Wordpress-php service both use NFS to share files. + +properties: + project: + type: string + default: dm-k8s-testing + description: Project location to load the images from. + nfs-service: + type: object + properties: + ip: + type: string + default: 10.0.253.247 + description: The IP of the NFS service. + port: + type: int + default: 2049 + description: The port of the NFS service. + disk: + type: string + default: nfs-disk + description: The name of the persistent disk the NFS service uses. + fstype: + type: string + default: ext4 + description: The filesystem the disk of the NFS service uses. + nginx: + type: object + properties: + replicas: + type: int + default: 2 + description: The number of replicas for the nginx service. + wordpress-php: + type: object + properties: + replicas: + type: int + default: 2 + description: The number of replicas for the wordpress-php service. + port: + type: int + default: 9000 + description: The port the wordpress-php service runs on. + mysql: + type: object + properties: + port: + type: int + default: 3306 + description: The port the MySQL service runs on. + password: + type: string + default: mysql-password + description: The root password of the MySQL service. + disk: + type: string + default: mysql-disk + description: The name of the persistent disk the MySQL service uses. + fstype: + type: string + default: ext4 + description: The filesystem the disk of the MySQL service uses. + diff --git a/chart/testdata/frobnitz/templates/wordpress.yaml b/chart/testdata/frobnitz/templates/wordpress.yaml new file mode 100644 index 000000000..b401897ab --- /dev/null +++ b/chart/testdata/frobnitz/templates/wordpress.yaml @@ -0,0 +1,6 @@ +imports: +- path: wordpress.jinja + +resources: +- name: wordpress + type: wordpress.jinja diff --git a/chart/testdata/ill-1.2.3.tgz b/chart/testdata/ill-1.2.3.tgz new file mode 100644 index 0000000000000000000000000000000000000000..bb4d5d49924dff85cf6ef2b1e0c3784a4a5c72db GIT binary patch literal 6187 zcmV+`7}VzR>%CNYj1y#wL6Cgoz8w6|J`cu!}G6L>j7!**2Y0hP;88@H;NJ0}Vy1lUq;t(P3 zw3@AEyO~cu{=&vG7EA)Xl<{mdl~;nPF#+_%Z!)(*xa}hnWkr(?PjOB zx5MMvlsDV|+3tWGH#a_r`L)U-eYC;Y4gti!^(<4P4tPiC=bj#LuN8K^|4RIqNWMK~o@Llw;{le8+bkuV6S zc|OxeySu^jUEsy;kY^iHg@AEHbpWtuCelcxBPl9$=Tn(ZL^f1Iq99f#rX4NCV#4cg zIcH}NcuAp-W_uq0@7{(;uVkjuM5K9$?}uV2vQ$7Aug>^eyV*88xXDF|_lQiqc7l7m zSM8z5dHa?}yAc-&{3fX}#%J#{Ydt1J?3?j0@%es&~>o8+0EG$L;xF=ditZ=D>*Sc-ig09LTPKOR&2TVe29? zvmi0fSfNFOWdicos2Jhxh6ROVBdPODf(9W6xMmTe6cHQF*+7g8kT$@FylFCJ-&OkG2+%zz}ADDyevMLt!T{7CpLoMkhp^#VvI8Hb7K zga%nY1O$i)k6D*MFA!0rxIo)*;Uj|Lr~o!_%JT72Fu)Qhh3y|p<>2iRI9NZGn3o-yPWkNfM z0CEU5-B^w!h_V7bptBVDPpa5tTQC>@m2GZ^QingtlPeh&2rFZuepmqF2B?)92?4qj zsi}N0dS;A(#Im4j1FFF`(Y%tnrp+=j7Fh<<>FJm_{sB=WD)O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r1T zTu#6eQ7;J+t3|$ga4GsuD0)x8(Rh}u5`PKx^!v|OfE93Fo)0eTFidE$EO;z?{?f_n z`ufAKTkF53hH@zRhs(e_>c73icB|I^X&>&bzyI?n7q;Tj;&YkD=y#j~Y%o^&`s4qR zSD^3c&?_2#AiEYrEunNA&h2~H_`wU@y)WIobJgHdd5W2xyFp(Rpv)w=Q+4&>&f7&oRO65>V2xKaQ4e=&?a>EV zF)w0>DAmf=J`4DFVZiv0K_c2%aYLoF2eC4h;O49=d8PR}r9-RA??%)^CFR`SZ!b_; zp)@RqgQt}*eF_E{yF1TXip!l|rXX5FC)0cp7naVeaUhsmutb$?=JrJejmQNRM;M10S3Fa0?70J+? zQ+5#j;ed*bA%zu7{WDRz)P;Pu`eGm94)tA$jAaGBFpwfrQ#IU|J@+OLyAk&wui`O7 zvIv>&O59h$tG$fX*fWJM3EE*!$!osbPW=osuWSgc(uz9_y~>U+VCu^V_+qK`s{HSd z{2KUO`+x1uvs(TKkJtI%qg>{T3)C1im13;~SlT?i4zfgJB=uQedRgo1o>%$(Ul898 zo@wTB+?>t-NE~yorCrN|K(AxC+q@GUE?u%k*!Z%{o7ss`|l5U2=q?- zKWNv!|9)_=w?6;#C|CJ^^_Z1@z`O|;eyU;Z8P?bOT3_pHeXXzcwZ7KZ`uZ2R{thaP Jq#yuz002%l0}B8E literal 0 HcmV?d00001 diff --git a/chart/testdata/ill/Chart.yaml b/chart/testdata/ill/Chart.yaml new file mode 100644 index 000000000..1256cea32 --- /dev/null +++ b/chart/testdata/ill/Chart.yaml @@ -0,0 +1,28 @@ +#helm:generate foo +name: ill +description: This is a frobniz. +version: "1.2.3-alpha.1+12345" +keywords: + - ill + - sprocket + - dodad +maintainers: + - name: The Helm Team + email: helm@example.com + - name: Someone Else + email: nobody@example.com +source: + - https://example.com/foo/bar +home: http://example.com +dependencies: + - name: thingerbob + location: https://example.com/charts/thingerbob-3.2.1.tgz + version: ^3 +environment: + - name: Kubernetes + version: ~1.1 + extensions: + - extensions/v1beta1 + - extensions/v1beta1/daemonset + apiGroups: + - 3rdParty diff --git a/chart/testdata/ill/LICENSE b/chart/testdata/ill/LICENSE new file mode 100644 index 000000000..d64569567 --- /dev/null +++ b/chart/testdata/ill/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/chart/testdata/ill/README.md b/chart/testdata/ill/README.md new file mode 100644 index 000000000..8cf4cc3d7 --- /dev/null +++ b/chart/testdata/ill/README.md @@ -0,0 +1,11 @@ +# Frobnitz + +This is an example chart. + +## Usage + +This is an example. It has no usage. + +## Development + +For developer info, see the top-level repository. diff --git a/chart/testdata/ill/docs/README.md b/chart/testdata/ill/docs/README.md new file mode 100644 index 000000000..d40747caf --- /dev/null +++ b/chart/testdata/ill/docs/README.md @@ -0,0 +1 @@ +This is a placeholder for documentation. diff --git a/chart/testdata/ill/hooks/pre-install.py b/chart/testdata/ill/hooks/pre-install.py new file mode 100644 index 000000000..c9b0d0a92 --- /dev/null +++ b/chart/testdata/ill/hooks/pre-install.py @@ -0,0 +1 @@ +# Placeholder. diff --git a/chart/testdata/ill/templates/wordpress-resources.yaml b/chart/testdata/ill/templates/wordpress-resources.yaml new file mode 100644 index 000000000..00f709de0 --- /dev/null +++ b/chart/testdata/ill/templates/wordpress-resources.yaml @@ -0,0 +1,12 @@ +# Google Cloud Deployment Manager template +resources: +- name: nfs-disk + type: compute.v1.disk + properties: + zone: us-central1-b + sizeGb: 200 +- name: mysql-disk + type: compute.v1.disk + properties: + zone: us-central1-b + sizeGb: 200 diff --git a/chart/testdata/ill/templates/wordpress.jinja b/chart/testdata/ill/templates/wordpress.jinja new file mode 100644 index 000000000..f34e4fec9 --- /dev/null +++ b/chart/testdata/ill/templates/wordpress.jinja @@ -0,0 +1,72 @@ +#helm:generate dm_template +{% set PROPERTIES = properties or {} %} +{% set PROJECT = PROPERTIES['project'] or 'dm-k8s-testing' %} +{% set NFS_SERVER = PROPERTIES['nfs-server'] or {} %} +{% set NFS_SERVER_IP = NFS_SERVER['ip'] or '10.0.253.247' %} +{% set NFS_SERVER_PORT = NFS_SERVER['port'] or 2049 %} +{% set NFS_SERVER_DISK = NFS_SERVER['disk'] or 'nfs-disk' %} +{% set NFS_SERVER_DISK_FSTYPE = NFS_SERVER['fstype'] or 'ext4' %} +{% set NGINX = PROPERTIES['nginx'] or {} %} +{% set NGINX_PORT = 80 %} +{% set NGINX_REPLICAS = NGINX['replicas'] or 2 %} +{% set WORDPRESS_PHP = PROPERTIES['wordpress-php'] or {} %} +{% set WORDPRESS_PHP_REPLICAS = WORDPRESS_PHP['replicas'] or 2 %} +{% set WORDPRESS_PHP_PORT = WORDPRESS_PHP['port'] or 9000 %} +{% set MYSQL = PROPERTIES['mysql'] or {} %} +{% set MYSQL_PORT = MYSQL['port'] or 3306 %} +{% set MYSQL_PASSWORD = MYSQL['password'] or 'mysql-password' %} +{% set MYSQL_DISK = MYSQL['disk'] or 'mysql-disk' %} +{% set MYSQL_DISK_FSTYPE = MYSQL['fstype'] or 'ext4' %} + +resources: +- name: nfs + type: github.com/kubernetes/application-dm-templates/storage/nfs:v1 + properties: + ip: {{ NFS_SERVER_IP }} + port: {{ NFS_SERVER_PORT }} + disk: {{ NFS_SERVER_DISK }} + fstype: {{NFS_SERVER_DISK_FSTYPE }} +- name: nginx + type: github.com/kubernetes/application-dm-templates/common/replicatedservice:v2 + properties: + service_port: {{ NGINX_PORT }} + container_port: {{ NGINX_PORT }} + replicas: {{ NGINX_REPLICAS }} + external_service: true + image: gcr.io/{{ PROJECT }}/nginx:latest + volumes: + - mount_path: /var/www/html + persistentVolumeClaim: + claimName: nfs +- name: mysql + type: github.com/kubernetes/application-dm-templates/common/replicatedservice:v2 + properties: + service_port: {{ MYSQL_PORT }} + container_port: {{ MYSQL_PORT }} + replicas: 1 + image: mysql:5.6 + env: + - name: MYSQL_ROOT_PASSWORD + value: {{ MYSQL_PASSWORD }} + volumes: + - mount_path: /var/lib/mysql + gcePersistentDisk: + pdName: {{ MYSQL_DISK }} + fsType: {{ MYSQL_DISK_FSTYPE }} +- name: wordpress-php + type: github.com/kubernetes/application-dm-templates/common/replicatedservice:v2 + properties: + service_name: wordpress-php + service_port: {{ WORDPRESS_PHP_PORT }} + container_port: {{ WORDPRESS_PHP_PORT }} + replicas: 2 + image: wordpress:fpm + env: + - name: WORDPRESS_DB_PASSWORD + value: {{ MYSQL_PASSWORD }} + - name: WORDPRESS_DB_HOST + value: mysql-service + volumes: + - mount_path: /var/www/html + persistentVolumeClaim: + claimName: nfs diff --git a/chart/testdata/ill/templates/wordpress.jinja.schema b/chart/testdata/ill/templates/wordpress.jinja.schema new file mode 100644 index 000000000..215b47e1e --- /dev/null +++ b/chart/testdata/ill/templates/wordpress.jinja.schema @@ -0,0 +1,69 @@ +info: + title: Wordpress + description: | + Defines a Wordpress website by defining four replicated services: an NFS service, an nginx service, a wordpress-php service, and a MySQL service. + + The nginx service and the Wordpress-php service both use NFS to share files. + +properties: + project: + type: string + default: dm-k8s-testing + description: Project location to load the images from. + nfs-service: + type: object + properties: + ip: + type: string + default: 10.0.253.247 + description: The IP of the NFS service. + port: + type: int + default: 2049 + description: The port of the NFS service. + disk: + type: string + default: nfs-disk + description: The name of the persistent disk the NFS service uses. + fstype: + type: string + default: ext4 + description: The filesystem the disk of the NFS service uses. + nginx: + type: object + properties: + replicas: + type: int + default: 2 + description: The number of replicas for the nginx service. + wordpress-php: + type: object + properties: + replicas: + type: int + default: 2 + description: The number of replicas for the wordpress-php service. + port: + type: int + default: 9000 + description: The port the wordpress-php service runs on. + mysql: + type: object + properties: + port: + type: int + default: 3306 + description: The port the MySQL service runs on. + password: + type: string + default: mysql-password + description: The root password of the MySQL service. + disk: + type: string + default: mysql-disk + description: The name of the persistent disk the MySQL service uses. + fstype: + type: string + default: ext4 + description: The filesystem the disk of the MySQL service uses. + diff --git a/chart/testdata/ill/templates/wordpress.yaml b/chart/testdata/ill/templates/wordpress.yaml new file mode 100644 index 000000000..b401897ab --- /dev/null +++ b/chart/testdata/ill/templates/wordpress.yaml @@ -0,0 +1,6 @@ +imports: +- path: wordpress.jinja + +resources: +- name: wordpress + type: wordpress.jinja From ffe15f15856a7fac4e4dc3c06c70500ae6c32af0 Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Thu, 28 Jan 2016 12:21:56 -0700 Subject: [PATCH 3/9] Load chartfile into chart on chart file load. Best commit message ever. --- chart/chart.go | 19 +++++++++++++++++-- chart/chart_test.go | 18 ++++++++++++++---- chart/chartfile_test.go | 4 ++-- chart/testdata/nochart.tgz | Bin 0 -> 325 bytes 4 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 chart/testdata/nochart.tgz diff --git a/chart/chart.go b/chart/chart.go index 514a4001b..2f0a35c42 100644 --- a/chart/chart.go +++ b/chart/chart.go @@ -14,6 +14,8 @@ import ( "github.com/kubernetes/deployment-manager/log" ) +const ChartfileName string = "Chart.yaml" + const ( preTemplates string = "templates/" preHooks string = "hooks/" @@ -42,11 +44,13 @@ type Chart interface { // Dir returns a directory where the chart can be accessed. Dir() string + // Close cleans up a chart. Close() error } type dirChart struct { chartfile *Chartfile + dir string } func (d *dirChart) Chartfile() *Chartfile { @@ -99,6 +103,7 @@ func LoadDir(chart string) (Chart, error) { c := &dirChart{ chartfile: cf, + dir: chart, } return c, nil @@ -128,10 +133,20 @@ func Load(archive string) (Chart, error) { defer unzipped.Close() untarred := tar.NewReader(unzipped) - return loadTar(untarred) + c, err := loadTar(untarred) + if err != nil { + return c, err + } + + cf, err := LoadChartfile(filepath.Join(c.tmpDir, ChartfileName)) + if err != nil { + return c, err + } + c.chartfile = cf + return c, nil } -func loadTar(r *tar.Reader) (Chart, error) { +func loadTar(r *tar.Reader) (*tarChart, error) { td, err := ioutil.TempDir("", "chart-") if err != nil { return nil, err diff --git a/chart/chart_test.go b/chart/chart_test.go index 91f020f3e..5a16dc6bf 100644 --- a/chart/chart_test.go +++ b/chart/chart_test.go @@ -6,10 +6,13 @@ import ( "github.com/kubernetes/deployment-manager/log" ) -const testfile = "testdata/frobnitz/Chart.yaml" -const testdir = "testdata/frobnitz/" -const testarchive = "testdata/frobnitz-0.0.1.tgz" -const testill = "testdata/ill-1.2.3.tgz" +const ( + testfile = "testdata/frobnitz/Chart.yaml" + testdir = "testdata/frobnitz/" + testarchive = "testdata/frobnitz-0.0.1.tgz" + testill = "testdata/ill-1.2.3.tgz" + testnochart = "testdata/nochart.tgz" +) func init() { log.IsDebugging = true @@ -62,3 +65,10 @@ func TestLoadIll(t *testing.T) { return } } + +func TestLoadNochart(t *testing.T) { + _, err := Load(testnochart) + if err == nil { + t.Error("Nochart should not have loaded at all.") + } +} diff --git a/chart/chartfile_test.go b/chart/chartfile_test.go index 92781d3fa..d1d21d3f3 100644 --- a/chart/chartfile_test.go +++ b/chart/chartfile_test.go @@ -31,8 +31,8 @@ func TestLoadChartfile(t *testing.T) { t.Errorf("Expected second dependency to be thingerbob: %q", f.Dependencies[0].Name) } - if f.Source[0] != "https://example.com/helm" { - t.Errorf("Expected https://example.com/helm, got %s", f.Source) + if f.Source[0] != "https://example.com/foo/bar" { + t.Errorf("Expected https://example.com/foo/bar, got %s", f.Source) } } diff --git a/chart/testdata/nochart.tgz b/chart/testdata/nochart.tgz new file mode 100644 index 0000000000000000000000000000000000000000..f5a235537aa99fd1b58faec20e3e3e871f5b2ab1 GIT binary patch literal 325 zcmV-L0lNMliwFP@XsT8K1MSj5PlGTR2H>6f72n01Vqt@wm$;)B;}4*eVw*s_wBI!D z*V{2&h*58gF}}~Glze%43p{K$clX=u_Jgs4|5IWZ+0_BvX1x%A`w8ai^8-t&(iL%d_m5)AYfQ&hp{5nMEn@SXviJo}y++ zI!;;B?d-e6_Lz7ha*-D~KOGwC?V!n~JV!DpW1ltoehNB|&z}F0WQU=7FCbkv>t{bT zOMB;;3L&mi_i>l{w1d9R($7IJ#-{T} Date: Fri, 29 Jan 2016 15:51:06 -0700 Subject: [PATCH 4/9] Add short/long/local names according to new spec. The url package adds short, long, and local URL parsing. It also supports switching between short and long names. --- url/url.go | 189 ++++++++++++++++++++++++++++++++++++++++++++++++ url/url_test.go | 177 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 366 insertions(+) create mode 100644 url/url.go create mode 100644 url/url_test.go diff --git a/url/url.go b/url/url.go new file mode 100644 index 000000000..b604cf96d --- /dev/null +++ b/url/url.go @@ -0,0 +1,189 @@ +/* package URL handles Helm-DM URLs + +Helm uses three kinds of URLs: + + - Fully qualified (long) names: https://example.com/foo/bar-1.2.3.tgz + - Short names: helm:example.com/foo/bar#1.2.3 + - Local names: file:///foo/bar + +This package provides utilities for working with this type of URL. +*/ +package url + +import ( + "errors" + "fmt" + "net/url" + "path/filepath" + "regexp" + "strings" +) + +const ( + SchemeHTTP = "http" + SchemeHTTPS = "https" + SchemeHelm = "helm" + SchemeFile = "file" +) + +// TarNameRegex parses the name component of a URI and breaks it into a name and version. +// +// This borrows liberally from github.com/Masterminds/semver. +const TarNameRegex = `([0-9A-Za-z\-_/]+)-(v?([0-9]+)(\.[0-9]+)?(\.[0-9]+)?` + + `(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?` + + `(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?)(.tgz)?` + +var tnregexp *regexp.Regexp + +func init() { + tnregexp = regexp.MustCompile("^" + TarNameRegex + "$") +} + +type URL struct { + // The scheme of the URL. Typically one of http, https, helm, or file. + Scheme string + // The host information, if applicable. + Host string + // The bucket name + Bucket string + // The chart name + Name string + // The version or version range. + Version string + + // If this is a local chart, the path to the chart. + LocalRef string + + isLocal bool + + original string +} + +func Parse(path string) (*URL, error) { + + // Check for absolute or relative path. + if path[0] == '.' || path[0] == '/' { + return &URL{ + LocalRef: path, + isLocal: true, + original: path, + }, nil + } + + // TODO: Do we want to support file:///foo/bar.tgz? + if strings.HasPrefix(path, SchemeFile+":") { + path := strings.TrimPrefix(path, SchemeFile+":") + return &URL{ + LocalRef: filepath.Clean(path), + isLocal: true, + original: path, + }, nil + } + + u, err := url.Parse(path) + if err != nil { + return nil, err + } + // Short name + if u.Scheme == SchemeHelm { + parts := strings.SplitN(u.Opaque, "/", 3) + if len(parts) < 3 { + return nil, fmt.Errorf("both bucket and chart name are required in %s: %s", path, u.Path) + } + // Need to parse opaque data into bucket and chart. + return &URL{ + Scheme: u.Scheme, + Host: parts[0], + Bucket: parts[1], + Name: parts[2], + Version: u.Fragment, + original: path, + }, nil + } + + // Long name + parts := strings.SplitN(u.Path, "/", 3) + if len(parts) < 3 { + return nil, fmt.Errorf("both bucket and chart name are required in %s", path) + } + + name, version, err := parseTarName(parts[2]) + if err != nil { + return nil, err + } + + return &URL{ + Scheme: u.Scheme, + Host: u.Host, + Bucket: parts[1], + Name: name, + Version: version, + original: path, + }, nil +} + +// IsLocal returns true if this is a local path. +func (u *URL) IsLocal() bool { + return u.isLocal +} + +// Local returns a local version of the path. +// +// This will return an error if the URL does not reference a local chart. +func (u *URL) Local() (string, error) { + return u.LocalRef, nil +} + +var ErrLocal = errors.New("cannot use local URL as remote") +var ErrRemote = errors.New("cannot use remote URL as local") + +// Short returns a short form URL. +// +// This will return an error if the URL references a local chart. +func (u *URL) Short() (string, error) { + if u.IsLocal() { + return "", ErrLocal + } + fname := fmt.Sprintf("%s/%s/%s", u.Host, u.Bucket, u.Name) + return (&url.URL{ + Scheme: SchemeHelm, + Opaque: fname, + Fragment: u.Version, + }).String(), nil +} + +// Long returns a long-form URL. +// +// If secure is true, this will return an HTTPS URL, otherwise HTTP. +// +// This will return an error if the URL references a local chart. +func (u *URL) Long(secure bool) (string, error) { + if u.IsLocal() { + return "", ErrLocal + } + + scheme := SchemeHTTPS + if !secure { + scheme = SchemeHTTP + } + fname := fmt.Sprintf("%s/%s-%s.tgz", u.Bucket, u.Name, u.Version) + + return (&url.URL{ + Scheme: scheme, + Host: u.Host, + Path: fname, + }).String(), nil + +} + +// parseTarName parses a long-form tarfile name. +func parseTarName(name string) (string, string, error) { + if strings.HasSuffix(name, ".tgz") { + name = strings.TrimSuffix(name, ".tgz") + } + v := tnregexp.FindStringSubmatch(name) + if v == nil { + return name, "", fmt.Errorf("invalid name %s", name) + } + return v[1], v[2], nil +} diff --git a/url/url_test.go b/url/url_test.go new file mode 100644 index 000000000..aeea98c0c --- /dev/null +++ b/url/url_test.go @@ -0,0 +1,177 @@ +package url + +import ( + "testing" +) + +func TestParse(t *testing.T) { + tests := map[string]URL{ + "helm:host/bucket/name#1.2.3": URL{Scheme: "helm", Host: "host", Bucket: "bucket", Name: "name", Version: "1.2.3"}, + "https://host/bucket/name-1.2.3.tgz": URL{Scheme: "https", Host: "host", Bucket: "bucket", Name: "name", Version: "1.2.3"}, + "http://host/bucket/name-1.2.3.tgz": URL{Scheme: "http", Host: "host", Bucket: "bucket", Name: "name", Version: "1.2.3"}, + } + + for start, expect := range tests { + u, err := Parse(start) + if err != nil { + t.Errorf("Failed parsing %s: %s", start, err) + } + + if expect.Scheme != u.Scheme { + t.Errorf("Unexpected scheme: %s", u.Scheme) + } + + if expect.Host != u.Host { + t.Errorf("Unexpected host: %q", u.Host) + } + + if expect.Bucket != u.Bucket { + t.Errorf("Unexpected bucket: %q", u.Bucket) + } + + if expect.Name != u.Name { + t.Errorf("Unexpected name: %q", u.Name) + } + + if expect.Version != u.Version { + t.Errorf("Unexpected version: %q", u.Version) + } + + if expect.LocalRef != u.LocalRef { + t.Errorf("Unexpected local dir: %q", u.LocalRef) + } + + } +} + +func TestShort(t *testing.T) { + tests := map[string]string{ + "https://example.com/foo/bar-1.2.3.tgz": "helm:example.com/foo/bar#1.2.3", + "http://example.com/foo/bar-1.2.3.tgz": "helm:example.com/foo/bar#1.2.3", + "helm:example.com/foo/bar#1.2.3": "helm:example.com/foo/bar#1.2.3", + "helm:example.com/foo/bar#>1.2.3": "helm:example.com/foo/bar#%3E1.2.3", + } + + for start, expect := range tests { + u, err := Parse(start) + if err != nil { + t.Errorf("Failed to parse: %s", err) + continue + } + short, err := u.Short() + if err != nil { + t.Errorf("Failed to generate short: %s", err) + continue + } + + if short != expect { + t.Errorf("Expected %q, got %q", expect, short) + } + } + + fails := []string{"./this/is/local", "file:///this/is/local"} + for _, f := range fails { + u, err := Parse(f) + if err != nil { + t.Errorf("Failed to parse: %s", err) + continue + } + + if _, err := u.Short(); err == nil { + t.Errorf("%q should have caused an error for Short()", f) + } + } +} + +func TestLong(t *testing.T) { + tests := map[string]string{ + "https://example.com/foo/bar-1.2.3.tgz": "https://example.com/foo/bar-1.2.3.tgz", + "http://example.com/foo/bar-1.2.3.tgz": "https://example.com/foo/bar-1.2.3.tgz", + "helm:example.com/foo/bar#1.2.3": "https://example.com/foo/bar-1.2.3.tgz", + "helm:example.com/foo/bar#>1.2.3": "https://example.com/foo/bar-%3E1.2.3.tgz", + } + + for start, expect := range tests { + t.Logf("Parsing %s", start) + u, err := Parse(start) + if err != nil { + t.Errorf("Failed to parse: %s", err) + continue + } + long, err := u.Long(true) + if err != nil { + t.Errorf("Failed to generate long: %s", err) + continue + } + + if long != expect { + t.Errorf("Expected %q, got %q", expect, long) + } + } + + fails := []string{"./this/is/local", "file:///this/is/local"} + for _, f := range fails { + u, err := Parse(f) + if err != nil { + t.Errorf("Failed to parse: %s", err) + continue + } + + if _, err := u.Long(false); err == nil { + t.Errorf("%q should have caused an error for Long()", f) + } + } +} + +func TestLocal(t *testing.T) { + tests := map[string]string{ + "file:///foo/bar-1.2.3.tgz": "/foo/bar-1.2.3.tgz", + "file:///foo/bar": "/foo/bar", + "./foo/bar": "./foo/bar", + "/foo/bar": "/foo/bar", + } + + for start, expect := range tests { + u, err := Parse(start) + if err != nil { + t.Errorf("Failed parse: %s", err) + continue + } + + fin, err := u.Local() + if err != nil { + t.Errorf("Failed Local(): %s", err) + continue + } + + if fin != expect { + t.Errorf("Expected %q, got %q", expect, fin) + } + } + +} + +func TestParseTarName(t *testing.T) { + tests := []struct{ start, name, version string }{ + {"butcher-1.2.3", "butcher", "1.2.3"}, + {"butcher-1.2.3.tgz", "butcher", "1.2.3"}, + {"butcher-1.2.3-beta1+1234", "butcher", "1.2.3-beta1+1234"}, + {"butcher-1.2.3-beta1+1234.tgz", "butcher", "1.2.3-beta1+1234"}, + {"foo/butcher-1.2.3.tgz", "foo/butcher", "1.2.3"}, + } + + for _, tt := range tests { + n, v, e := parseTarName(tt.start) + if e != nil { + t.Errorf("Error parsing %s: %s", tt.start, e) + continue + } + if n != tt.name { + t.Errorf("Expected name %q, got %q", tt.name, n) + } + + if v != tt.version { + t.Errorf("Expected version %q, got %q", tt.version, v) + } + } +} From a293f94e865e5be856cf76ed773e261d0164b0af Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Mon, 1 Feb 2016 11:55:29 -0700 Subject: [PATCH 5/9] Streamline local file handling, test Windows files --- url/url.go | 109 ++++++++++++++++++++++++++++++------------------ url/url_test.go | 27 ++++++++++-- 2 files changed, 91 insertions(+), 45 deletions(-) diff --git a/url/url.go b/url/url.go index b604cf96d..37d2f9f4a 100644 --- a/url/url.go +++ b/url/url.go @@ -1,3 +1,19 @@ +/* +Copyright 2015 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + /* package URL handles Helm-DM URLs Helm uses three kinds of URLs: @@ -14,11 +30,16 @@ import ( "errors" "fmt" "net/url" - "path/filepath" "regexp" "strings" ) +// ErrLocal indicates that a local URL was used as a remote URL. +var ErrLocal = errors.New("cannot use local URL as remote") + +// ErrRemote indicates that a remote URL was used as a local URL. +var ErrRemote = errors.New("cannot use remote URL as local") + const ( SchemeHTTP = "http" SchemeHTTPS = "https" @@ -61,31 +82,23 @@ type URL struct { func Parse(path string) (*URL, error) { - // Check for absolute or relative path. - if path[0] == '.' || path[0] == '/' { - return &URL{ - LocalRef: path, - isLocal: true, - original: path, - }, nil - } - // TODO: Do we want to support file:///foo/bar.tgz? - if strings.HasPrefix(path, SchemeFile+":") { - path := strings.TrimPrefix(path, SchemeFile+":") - return &URL{ - LocalRef: filepath.Clean(path), - isLocal: true, - original: path, - }, nil - } + //if strings.HasPrefix(path, SchemeFile+":") { + //path := strings.TrimPrefix(path, SchemeFile+":") + //return &URL{ + //LocalRef: filepath.Clean(path), + //isLocal: true, + //original: path, + //}, nil + //} u, err := url.Parse(path) if err != nil { return nil, err } - // Short name - if u.Scheme == SchemeHelm { + + switch u.Scheme { + case SchemeHelm: parts := strings.SplitN(u.Opaque, "/", 3) if len(parts) < 3 { return nil, fmt.Errorf("both bucket and chart name are required in %s: %s", path, u.Path) @@ -99,27 +112,44 @@ func Parse(path string) (*URL, error) { Version: u.Fragment, original: path, }, nil - } - // Long name - parts := strings.SplitN(u.Path, "/", 3) - if len(parts) < 3 { - return nil, fmt.Errorf("both bucket and chart name are required in %s", path) - } + case SchemeHTTP, SchemeHTTPS: + // Long name + parts := strings.SplitN(u.Path, "/", 3) + if len(parts) < 3 { + return nil, fmt.Errorf("both bucket and chart name are required in %s", path) + } - name, version, err := parseTarName(parts[2]) - if err != nil { - return nil, err - } + name, version, err := parseTarName(parts[2]) + if err != nil { + return nil, err + } + + return &URL{ + Scheme: u.Scheme, + Host: u.Host, + Bucket: parts[1], + Name: name, + Version: version, + original: path, + }, nil + case SchemeFile: + return &URL{ + LocalRef: u.Path, + isLocal: true, + original: path, + }, nil + default: + // In this case... + // - if the path is relative or absolute, return it as-is. + // - if it's a URL of an unknown scheme, return it as is. + return &URL{ + LocalRef: path, + isLocal: true, + original: path, + }, nil - return &URL{ - Scheme: u.Scheme, - Host: u.Host, - Bucket: parts[1], - Name: name, - Version: version, - original: path, - }, nil + } } // IsLocal returns true if this is a local path. @@ -134,9 +164,6 @@ func (u *URL) Local() (string, error) { return u.LocalRef, nil } -var ErrLocal = errors.New("cannot use local URL as remote") -var ErrRemote = errors.New("cannot use remote URL as local") - // Short returns a short form URL. // // This will return an error if the URL references a local chart. diff --git a/url/url_test.go b/url/url_test.go index aeea98c0c..b93ea47c6 100644 --- a/url/url_test.go +++ b/url/url_test.go @@ -1,3 +1,19 @@ +/* +Copyright 2015 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package url import ( @@ -125,10 +141,13 @@ func TestLong(t *testing.T) { func TestLocal(t *testing.T) { tests := map[string]string{ - "file:///foo/bar-1.2.3.tgz": "/foo/bar-1.2.3.tgz", - "file:///foo/bar": "/foo/bar", - "./foo/bar": "./foo/bar", - "/foo/bar": "/foo/bar", + "file:///foo/bar-1.2.3.tgz": "/foo/bar-1.2.3.tgz", + "file:///foo/bar": "/foo/bar", + "./foo/bar": "./foo/bar", + "/foo/bar": "/foo/bar", + "file://localhost/etc/fstab": "/etc/fstab", + // https://blogs.msdn.microsoft.com/ie/2006/12/06/file-uris-in-windows/ + "file:///C:/WINDOWS/clock.avi": "/C:/WINDOWS/clock.avi", } for start, expect := range tests { From 31c30896098318e93ea575656c57946d52d56f64 Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Mon, 1 Feb 2016 16:40:39 -0700 Subject: [PATCH 6/9] Added Locator, added license blocks. --- chart/chart.go | 16 ++++++++ chart/chart_test.go | 16 ++++++++ chart/chartfile.go | 16 ++++++++ chart/chartfile_test.go | 16 ++++++++ chart/doc.go | 16 ++++++++ url/url.go => chart/locator.go | 47 +++++++----------------- url/url_test.go => chart/locator_test.go | 10 ++--- 7 files changed, 98 insertions(+), 39 deletions(-) rename url/url.go => chart/locator.go (81%) rename url/url_test.go => chart/locator_test.go (91%) diff --git a/chart/chart.go b/chart/chart.go index 2f0a35c42..0c8f377d6 100644 --- a/chart/chart.go +++ b/chart/chart.go @@ -1,3 +1,19 @@ +/* +Copyright 2015 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package chart import ( diff --git a/chart/chart_test.go b/chart/chart_test.go index 5a16dc6bf..83289c916 100644 --- a/chart/chart_test.go +++ b/chart/chart_test.go @@ -1,3 +1,19 @@ +/* +Copyright 2015 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package chart import ( diff --git a/chart/chartfile.go b/chart/chartfile.go index 821f941be..e3b0671c6 100644 --- a/chart/chartfile.go +++ b/chart/chartfile.go @@ -1,3 +1,19 @@ +/* +Copyright 2015 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package chart import ( diff --git a/chart/chartfile_test.go b/chart/chartfile_test.go index d1d21d3f3..b5e46dfa6 100644 --- a/chart/chartfile_test.go +++ b/chart/chartfile_test.go @@ -1,3 +1,19 @@ +/* +Copyright 2015 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package chart import ( diff --git a/chart/doc.go b/chart/doc.go index 521446e07..bc0ba2ca2 100644 --- a/chart/doc.go +++ b/chart/doc.go @@ -1,3 +1,19 @@ +/* +Copyright 2015 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + /* Package chart implements the Chart format. This package provides tools for working with the Chart format, including the diff --git a/url/url.go b/chart/locator.go similarity index 81% rename from url/url.go rename to chart/locator.go index 37d2f9f4a..7619ec31e 100644 --- a/url/url.go +++ b/chart/locator.go @@ -14,17 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -/* package URL handles Helm-DM URLs - -Helm uses three kinds of URLs: - - - Fully qualified (long) names: https://example.com/foo/bar-1.2.3.tgz - - Short names: helm:example.com/foo/bar#1.2.3 - - Local names: file:///foo/bar - -This package provides utilities for working with this type of URL. -*/ -package url +package chart import ( "errors" @@ -35,10 +25,10 @@ import ( ) // ErrLocal indicates that a local URL was used as a remote URL. -var ErrLocal = errors.New("cannot use local URL as remote") +var ErrLocal = errors.New("cannot use local Locator as remote") // ErrRemote indicates that a remote URL was used as a local URL. -var ErrRemote = errors.New("cannot use remote URL as local") +var ErrRemote = errors.New("cannot use remote Locator as local") const ( SchemeHTTP = "http" @@ -60,7 +50,7 @@ func init() { tnregexp = regexp.MustCompile("^" + TarNameRegex + "$") } -type URL struct { +type Locator struct { // The scheme of the URL. Typically one of http, https, helm, or file. Scheme string // The host information, if applicable. @@ -80,18 +70,7 @@ type URL struct { original string } -func Parse(path string) (*URL, error) { - - // TODO: Do we want to support file:///foo/bar.tgz? - //if strings.HasPrefix(path, SchemeFile+":") { - //path := strings.TrimPrefix(path, SchemeFile+":") - //return &URL{ - //LocalRef: filepath.Clean(path), - //isLocal: true, - //original: path, - //}, nil - //} - +func Parse(path string) (*Locator, error) { u, err := url.Parse(path) if err != nil { return nil, err @@ -104,7 +83,7 @@ func Parse(path string) (*URL, error) { return nil, fmt.Errorf("both bucket and chart name are required in %s: %s", path, u.Path) } // Need to parse opaque data into bucket and chart. - return &URL{ + return &Locator{ Scheme: u.Scheme, Host: parts[0], Bucket: parts[1], @@ -125,7 +104,7 @@ func Parse(path string) (*URL, error) { return nil, err } - return &URL{ + return &Locator{ Scheme: u.Scheme, Host: u.Host, Bucket: parts[1], @@ -134,7 +113,7 @@ func Parse(path string) (*URL, error) { original: path, }, nil case SchemeFile: - return &URL{ + return &Locator{ LocalRef: u.Path, isLocal: true, original: path, @@ -143,7 +122,7 @@ func Parse(path string) (*URL, error) { // In this case... // - if the path is relative or absolute, return it as-is. // - if it's a URL of an unknown scheme, return it as is. - return &URL{ + return &Locator{ LocalRef: path, isLocal: true, original: path, @@ -153,21 +132,21 @@ func Parse(path string) (*URL, error) { } // IsLocal returns true if this is a local path. -func (u *URL) IsLocal() bool { +func (u *Locator) IsLocal() bool { return u.isLocal } // Local returns a local version of the path. // // This will return an error if the URL does not reference a local chart. -func (u *URL) Local() (string, error) { +func (u *Locator) Local() (string, error) { return u.LocalRef, nil } // Short returns a short form URL. // // This will return an error if the URL references a local chart. -func (u *URL) Short() (string, error) { +func (u *Locator) Short() (string, error) { if u.IsLocal() { return "", ErrLocal } @@ -184,7 +163,7 @@ func (u *URL) Short() (string, error) { // If secure is true, this will return an HTTPS URL, otherwise HTTP. // // This will return an error if the URL references a local chart. -func (u *URL) Long(secure bool) (string, error) { +func (u *Locator) Long(secure bool) (string, error) { if u.IsLocal() { return "", ErrLocal } diff --git a/url/url_test.go b/chart/locator_test.go similarity index 91% rename from url/url_test.go rename to chart/locator_test.go index b93ea47c6..cf87d6eb4 100644 --- a/url/url_test.go +++ b/chart/locator_test.go @@ -14,17 +14,17 @@ See the License for the specific language governing permissions and limitations under the License. */ -package url +package chart import ( "testing" ) func TestParse(t *testing.T) { - tests := map[string]URL{ - "helm:host/bucket/name#1.2.3": URL{Scheme: "helm", Host: "host", Bucket: "bucket", Name: "name", Version: "1.2.3"}, - "https://host/bucket/name-1.2.3.tgz": URL{Scheme: "https", Host: "host", Bucket: "bucket", Name: "name", Version: "1.2.3"}, - "http://host/bucket/name-1.2.3.tgz": URL{Scheme: "http", Host: "host", Bucket: "bucket", Name: "name", Version: "1.2.3"}, + tests := map[string]Locator{ + "helm:host/bucket/name#1.2.3": Locator{Scheme: "helm", Host: "host", Bucket: "bucket", Name: "name", Version: "1.2.3"}, + "https://host/bucket/name-1.2.3.tgz": Locator{Scheme: "https", Host: "host", Bucket: "bucket", Name: "name", Version: "1.2.3"}, + "http://host/bucket/name-1.2.3.tgz": Locator{Scheme: "http", Host: "host", Bucket: "bucket", Name: "name", Version: "1.2.3"}, } for start, expect := range tests { From 57a562fb2be9096d6b696fcc5e97de71f65693c8 Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Mon, 1 Feb 2016 16:42:25 -0700 Subject: [PATCH 7/9] Add vendor/ to gitignore. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 8583018ea..1a3ab219d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ resourcifier/pkg/* resourcifier/bin/* manager/pkg/* .project +vendor/* From cfaca7ffb58c6544d1e0a4c579ca9966362bdfd7 Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Mon, 1 Feb 2016 18:04:47 -0700 Subject: [PATCH 8/9] Refactor Chart to be a struct, with more methods. This refactors the Chart to be a struct that can have numerous properties and methods. Now, a chartLoader implements the loading and closing of charts. --- chart/chart.go | 107 ++++++++++++++++++++++-------- chart/chart_test.go | 51 ++++++++++++++ chart/testdata/frobnitz-0.0.1.tgz | Bin 6074 -> 6246 bytes chart/testdata/frobnitz/icon.svg | 8 +++ 4 files changed, 137 insertions(+), 29 deletions(-) create mode 100644 chart/testdata/frobnitz/icon.svg diff --git a/chart/chart.go b/chart/chart.go index 0c8f377d6..e386c1ece 100644 --- a/chart/chart.go +++ b/chart/chart.go @@ -36,6 +36,7 @@ const ( preTemplates string = "templates/" preHooks string = "hooks/" preDocs string = "docs/" + preIcon string = "icon.svg" ) // Chart represents a complete chart. @@ -54,47 +55,93 @@ const ( // // Optionally, a chart might also locate a provenance (.prov) file that it // can use for cryptographic signing. -type Chart interface { +type Chart struct { + loader chartLoader +} + +// Close the chart. +// +// Charts should always be closed when no longer needed. +func (c *Chart) Close() error { + return c.loader.close() +} + +// Chartfile gets the Chartfile (Chart.yaml) for this chart. +func (c *Chart) Chartfile() *Chartfile { + return c.loader.chartfile() +} + +// Dir() returns the directory where the charts are located. +func (c *Chart) Dir() string { + return c.loader.dir() +} + +// DocsDir returns the directory where the chart's documentation is stored. +func (c *Chart) DocsDir() string { + return filepath.Join(c.loader.dir(), preDocs) +} + +// HooksDir returns the directory where the hooks are stored. +func (c *Chart) HooksDir() string { + return filepath.Join(c.loader.dir(), preHooks) +} + +// TemplatesDir returns the directory where the templates are stored. +func (c *Chart) TemplatesDir() string { + return filepath.Join(c.loader.dir(), preTemplates) +} + +// Icon returns the path to the icon.svg file. +// +// If an icon is not found in the chart, this will return an error. +func (c *Chart) Icon() (string, error) { + i := filepath.Join(c.Dir(), preIcon) + _, err := os.Stat(i) + return i, err +} + +// chartLoader provides load, close, and save implementations for a chart. +type chartLoader interface { // Chartfile resturns a *Chartfile for this chart. - Chartfile() *Chartfile + chartfile() *Chartfile // Dir returns a directory where the chart can be accessed. - Dir() string + dir() string // Close cleans up a chart. - Close() error + close() error } type dirChart struct { - chartfile *Chartfile - dir string + chartyaml *Chartfile + chartdir string } -func (d *dirChart) Chartfile() *Chartfile { - return d.chartfile +func (d *dirChart) chartfile() *Chartfile { + return d.chartyaml } -func (d *dirChart) Dir() string { - return "." +func (d *dirChart) dir() string { + return d.chartdir } -func (d *dirChart) Close() error { +func (d *dirChart) close() error { return nil } type tarChart struct { - chartfile *Chartfile + chartyaml *Chartfile tmpDir string } -func (t *tarChart) Chartfile() *Chartfile { - return t.chartfile +func (t *tarChart) chartfile() *Chartfile { + return t.chartyaml } -func (t *tarChart) Dir() string { - return "." +func (t *tarChart) dir() string { + return t.tmpDir } -func (t *tarChart) Close() error { +func (t *tarChart) close() error { // Remove the temp directory. return os.RemoveAll(t.tmpDir) } @@ -105,7 +152,7 @@ func (t *tarChart) Close() error { // // If you are just reading the Chart.yaml file, it is substantially more // performant to use LoadChartfile. -func LoadDir(chart string) (Chart, error) { +func LoadDir(chart string) (*Chart, error) { if fi, err := os.Stat(chart); err != nil { return nil, err } else if !fi.IsDir() { @@ -117,19 +164,21 @@ func LoadDir(chart string) (Chart, error) { return nil, err } - c := &dirChart{ - chartfile: cf, - dir: chart, + cl := &dirChart{ + chartyaml: cf, + chartdir: chart, } - return c, nil + return &Chart{ + loader: cl, + }, nil } // Load loads a chart from a chart archive. // // A chart archive is a gzipped tar archive that follows the Chart format // specification. -func Load(archive string) (Chart, error) { +func Load(archive string) (*Chart, error) { if fi, err := os.Stat(archive); err != nil { return nil, err } else if fi.IsDir() { @@ -151,15 +200,15 @@ func Load(archive string) (Chart, error) { untarred := tar.NewReader(unzipped) c, err := loadTar(untarred) if err != nil { - return c, err + return nil, err } cf, err := LoadChartfile(filepath.Join(c.tmpDir, ChartfileName)) if err != nil { - return c, err + return nil, err } - c.chartfile = cf - return c, nil + c.chartyaml = cf + return &Chart{loader: c}, nil } func loadTar(r *tar.Reader) (*tarChart, error) { @@ -168,7 +217,7 @@ func loadTar(r *tar.Reader) (*tarChart, error) { return nil, err } c := &tarChart{ - chartfile: &Chartfile{}, + chartyaml: &Chartfile{}, tmpDir: td, } @@ -221,7 +270,7 @@ func loadTar(r *tar.Reader) (*tarChart, error) { if err != nil && err != io.EOF { log.Warn("Unexpected error reading tar: %s", err) - c.Close() + c.close() return c, err } log.Info("Reached end of Tar file") diff --git a/chart/chart_test.go b/chart/chart_test.go index 83289c916..542664729 100644 --- a/chart/chart_test.go +++ b/chart/chart_test.go @@ -17,6 +17,7 @@ limitations under the License. package chart import ( + "path/filepath" "testing" "github.com/kubernetes/deployment-manager/log" @@ -30,11 +31,16 @@ const ( testnochart = "testdata/nochart.tgz" ) +// Type canaries. If these fail, they will fail at compile time. +var _ chartLoader = &dirChart{} +var _ chartLoader = &tarChart{} + func init() { log.IsDebugging = true } func TestLoadDir(t *testing.T) { + c, err := LoadDir(testdir) if err != nil { t.Errorf("Failed to load chart: %s", err) @@ -51,6 +57,7 @@ func TestLoadDir(t *testing.T) { } func TestLoad(t *testing.T) { + c, err := Load(testarchive) if err != nil { t.Errorf("Failed to load chart: %s", err) @@ -80,6 +87,11 @@ func TestLoadIll(t *testing.T) { t.Error("No chartfile was loaded.") return } + + // Ill does not have an icon. + if i, err := c.Icon(); err == nil { + t.Errorf("Expected icon to be missing. Got %s", i) + } } func TestLoadNochart(t *testing.T) { @@ -88,3 +100,42 @@ func TestLoadNochart(t *testing.T) { t.Error("Nochart should not have loaded at all.") } } + +func TestChart(t *testing.T) { + c, err := LoadDir(testdir) + if err != nil { + t.Errorf("Failed to load chart: %s", err) + } + defer c.Close() + + if c.Dir() != c.loader.dir() { + t.Errorf("Unexpected location for directory: %s", c.Dir()) + } + + if c.Chartfile().Name != c.loader.chartfile().Name { + t.Errorf("Unexpected chart file name: %s", c.Chartfile().Name) + } + + d := c.DocsDir() + if d != filepath.Join(testdir, preDocs) { + t.Errorf("Unexpectedly, docs are in %s", d) + } + + d = c.TemplatesDir() + if d != filepath.Join(testdir, preTemplates) { + t.Errorf("Unexpectedly, templates are in %s", d) + } + + d = c.HooksDir() + if d != filepath.Join(testdir, preHooks) { + t.Errorf("Unexpectedly, hooks are in %s", d) + } + + i, err := c.Icon() + if err != nil { + t.Errorf("No icon found in test chart: %s", err) + } + if i != filepath.Join(testdir, preIcon) { + t.Errorf("Unexpectedly, icon is in %s", i) + } +} diff --git a/chart/testdata/frobnitz-0.0.1.tgz b/chart/testdata/frobnitz-0.0.1.tgz index 79d611a5d98468e7ee84bf8dbddbb3abe2a58c71..78fb3bc176af80192b52fb4fec619249905dda80 100644 GIT binary patch literal 6246 zcmV-s7@6lEiwFQ3{I6C31MNL)bK6Fe`K(_tN7t66I*JlSNsg5`yNX5I+}M(il;cgg za;_mUBqs(i3uG z&fZsS_mfky;`%q2|AVQBlcR}9MaFZ%#!79ZJP}9CDaJ-5^eB_FT&nblT}-8B@DI1o zKeRS3MW*RlquttTb+&mtoAOrsKihkq{e#BFd$E|SEYe3CjBS@t@rRyeYV=;@^dnLc zk2Vr6(;WT~DJDiJCbtQ%_) zPD#~JMT^QLt%__U48m!i&-BsGPVjsOxUw_k*~U~MU>s2$06dzBG!p4ZiVEHNRHhS= z4b_k+h}DQ2KCZ$tLJQP8<(%yfFtpvuCm(3GXY_w}HbigTlWa70Q+}pWq4@J(~H$2*jxJckPNsTc+lg~3%%mQ$oEINmUEH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svg b/chart/testdata/frobnitz/icon.svg new file mode 100644 index 000000000..892130606 --- /dev/null +++ b/chart/testdata/frobnitz/icon.svg @@ -0,0 +1,8 @@ + + + Example icon + + + From 4fe37ea6342991e4d0519e48d1fd6fca061baf5c Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Tue, 2 Feb 2016 16:35:04 -0700 Subject: [PATCH 9/9] Add Save and Create functions. --- chart/chart.go | 65 +++++++++ chart/chartfile.go | 7 +- chart/save.go | 124 ++++++++++++++++++ chart/save_test.go | 123 +++++++++++++++++ chart/testdata/sprocket/Chart.yaml | 4 + chart/testdata/sprocket/LICENSE | 1 + chart/testdata/sprocket/README.md | 3 + chart/testdata/sprocket/docs/README.md | 1 + chart/testdata/sprocket/hooks/pre-install.py | 1 + chart/testdata/sprocket/icon.svg | 8 ++ .../sprocket/templates/placeholder.txt | 1 + 11 files changed, 337 insertions(+), 1 deletion(-) create mode 100644 chart/save.go create mode 100644 chart/save_test.go create mode 100644 chart/testdata/sprocket/Chart.yaml create mode 100644 chart/testdata/sprocket/LICENSE create mode 100644 chart/testdata/sprocket/README.md create mode 100644 chart/testdata/sprocket/docs/README.md create mode 100644 chart/testdata/sprocket/hooks/pre-install.py create mode 100644 chart/testdata/sprocket/icon.svg create mode 100644 chart/testdata/sprocket/templates/placeholder.txt diff --git a/chart/chart.go b/chart/chart.go index e386c1ece..2bed1f6c5 100644 --- a/chart/chart.go +++ b/chart/chart.go @@ -39,6 +39,8 @@ const ( preIcon string = "icon.svg" ) +var headerBytes = []byte("+aHR0cHM6Ly95b3V0dS5iZS96OVV6MWljandyTQo=") + // Chart represents a complete chart. // // A chart consists of the following parts: @@ -146,6 +148,69 @@ func (t *tarChart) close() error { return os.RemoveAll(t.tmpDir) } +// New creates a new chart in a directory. +// +// Inside of dir, this will create a directory based on the name of +// chartfile.Name. It will then write the Chart.yaml into this directory and +// create the (empty) appropriate directories. +// +// The returned *Chart will point to the newly created directory. +// +// If dir does not exist, this will return an error. +// If Chart.yaml or any directories cannot be created, this will return an +// error. In such a case, this will attempt to clean up by removing the +// new chart directory. +func Create(chartfile *Chartfile, dir string) (*Chart, error) { + path, err := filepath.Abs(dir) + if err != nil { + return nil, err + } + + if fi, err := os.Stat(path); err != nil { + return nil, err + } else if !fi.IsDir() { + return nil, fmt.Errorf("no such directory %s", path) + } + + n := fname(chartfile.Name) + cdir := filepath.Join(path, n) + if _, err := os.Stat(cdir); err == nil { + return nil, fmt.Errorf("directory already exists: %s", cdir) + } + if err := os.MkdirAll(cdir, 0755); err != nil { + return nil, err + } + + rollback := func() { + // TODO: Should we log failures here? + os.RemoveAll(cdir) + } + + if err := chartfile.Save(filepath.Join(cdir, ChartfileName)); err != nil { + rollback() + return nil, err + } + + for _, d := range []string{preHooks, preDocs, preTemplates} { + if err := os.MkdirAll(filepath.Join(cdir, d), 0755); err != nil { + rollback() + return nil, err + } + } + + return &Chart{ + loader: &dirChart{chartyaml: chartfile, chartdir: cdir}, + }, nil +} + +// fname prepares names for the filesystem +func fname(name string) string { + // Right now, we don't do anything. Do we need to encode any particular + // characters? What characters are legal in a chart name, but not in file + // names on Windows, Linux, or OSX. + return name +} + // LoadDir loads an entire chart from a directory. // // This includes the Chart.yaml (*Chartfile) and all of the manifests. diff --git a/chart/chartfile.go b/chart/chartfile.go index e3b0671c6..a7fbd0985 100644 --- a/chart/chartfile.go +++ b/chart/chartfile.go @@ -69,7 +69,7 @@ func LoadChartfile(filename string) (*Chartfile, error) { // Save saves a Chart.yaml file func (c *Chartfile) Save(filename string) error { - b, err := yaml.Marshal(c) + b, err := c.Marshal() if err != nil { return err } @@ -77,6 +77,11 @@ func (c *Chartfile) Save(filename string) error { return ioutil.WriteFile(filename, b, 0644) } +// Marshal encodes the chart file into YAML. +func (c *Chartfile) Marshal() ([]byte, error) { + return yaml.Marshal(c) +} + // VersionOK returns true if the given version meets the constraints. // // It returns false if the version string or constraint is unparsable or if the diff --git a/chart/save.go b/chart/save.go new file mode 100644 index 000000000..9d4302e14 --- /dev/null +++ b/chart/save.go @@ -0,0 +1,124 @@ +/* +Copyright 2015 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package chart + +import ( + "archive/tar" + "compress/gzip" + "fmt" + "io" + "os" + "path/filepath" + + "github.com/kubernetes/deployment-manager/log" +) + +// Save creates an archived chart to the given directory. +// +// This takes an existing chart and a destination directory. +// +// If the directory is /foo, and the chart is named bar, with version 1.0.0, this +// will generate /foo/bar-1.0.0.tgz. +// +// This returns the absolute path to the chart archive file. +func Save(c *Chart, outDir string) (string, error) { + // Create archive + if fi, err := os.Stat(outDir); err != nil { + return "", err + } else if !fi.IsDir() { + return "", fmt.Errorf("location %s is not a directory", outDir) + } + + cfile := c.Chartfile() + dir := c.Dir() + basename := filepath.Base(dir) + pdir := filepath.Dir(dir) + if basename == "." { + basename = fname(cfile.Name) + } + filename := fmt.Sprintf("%s-%s.tgz", fname(cfile.Name), cfile.Version) + filename = filepath.Join(outDir, filename) + + // Fail early if the YAML is borked. + if err := cfile.Save(filepath.Join(dir, ChartfileName)); err != nil { + return "", err + } + + // Create file. + f, err := os.Create(filename) + if err != nil { + return "", err + } + + // Wrap in gzip writer + zipper := gzip.NewWriter(f) + zipper.Header.Extra = headerBytes + zipper.Header.Comment = "Helm" + + // Wrap in tar writer + twriter := tar.NewWriter(zipper) + rollback := false + defer func() { + twriter.Close() + zipper.Close() + f.Close() + if rollback { + log.Warn("Removing incomplete archive %s", filename) + os.Remove(filename) + } + }() + + err = filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error { + if err != nil { + return err + } + hdr, err := tar.FileInfoHeader(fi, ".") + if err != nil { + return err + } + + relpath, err := filepath.Rel(pdir, path) + if err != nil { + return err + } + hdr.Name = relpath + + twriter.WriteHeader(hdr) + + // Skip directories. + if fi.IsDir() { + return nil + } + + in, err := os.Open(path) + if err != nil { + return err + } + _, err = io.Copy(twriter, in) + in.Close() + if err != nil { + return err + } + + return nil + }) + if err != nil { + rollback = true + return filename, err + } + return filename, nil +} diff --git a/chart/save_test.go b/chart/save_test.go new file mode 100644 index 000000000..56979c6a1 --- /dev/null +++ b/chart/save_test.go @@ -0,0 +1,123 @@ +/* +Copyright 2015 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package chart + +import ( + "archive/tar" + "compress/gzip" + "fmt" + "io/ioutil" + "os" + "path/filepath" + "testing" +) + +const sprocketdir = "testdata/sprocket" + +func TestSave(t *testing.T) { + + tmpdir, err := ioutil.TempDir("", "helm-") + if err != nil { + t.Fatal("Could not create temp directory") + } + t.Logf("Temp: %s", tmpdir) + // Because of the defer, don't call t.Fatal in the remainder of this + // function. + defer os.RemoveAll(tmpdir) + + c, err := LoadDir(sprocketdir) + if err != nil { + t.Errorf("Failed to load %s: %s", sprocketdir, err) + return + } + + tfile, err := Save(c, tmpdir) + if err != nil { + t.Errorf("Failed to save %s to %s: %s", c.Chartfile().Name, tmpdir, err) + return + } + + b := filepath.Base(tfile) + expectname := "sprocket-1.2.3-alpha.1+12345.tgz" + if b != expectname { + t.Errorf("Expected %q, got %q", expectname, b) + } + + files, err := getAllFiles(tfile) + if err != nil { + t.Errorf("Could not extract files: %s", err) + } + + // Files should come back in order. + expect := []string{ + "sprocket", + "sprocket/Chart.yaml", + "sprocket/LICENSE", + "sprocket/README.md", + "sprocket/docs", + "sprocket/docs/README.md", + "sprocket/hooks", + "sprocket/hooks/pre-install.py", + "sprocket/icon.svg", + "sprocket/templates", + "sprocket/templates/placeholder.txt", + } + if len(expect) != len(files) { + t.Errorf("Expected %d files, found %d", len(expect), len(files)) + return + } + for i := 0; i < len(expect); i++ { + if expect[i] != files[i] { + t.Errorf("Expected file %q, got %q", expect[i], files[i]) + } + } +} + +func getAllFiles(tfile string) ([]string, error) { + f1, err := os.Open(tfile) + if err != nil { + return []string{}, err + } + f2, err := gzip.NewReader(f1) + if err != nil { + f1.Close() + return []string{}, err + } + + if f2.Header.Comment != "Helm" { + return []string{}, fmt.Errorf("Expected header Helm. Got %s", f2.Header.Comment) + } + if string(f2.Header.Extra) != string(headerBytes) { + return []string{}, fmt.Errorf("Expected header signature. Got %v", f2.Header.Extra) + } + + f3 := tar.NewReader(f2) + + files := []string{} + var e error + var hdr *tar.Header + for e == nil { + hdr, e = f3.Next() + if e == nil { + files = append(files, hdr.Name) + } + } + + f2.Close() + f1.Close() + return files, nil +} diff --git a/chart/testdata/sprocket/Chart.yaml b/chart/testdata/sprocket/Chart.yaml new file mode 100644 index 000000000..771ea87cb --- /dev/null +++ b/chart/testdata/sprocket/Chart.yaml @@ -0,0 +1,4 @@ +name: sprocket +description: This is a sprocket. +version: 1.2.3-alpha.1+12345 +home: "" diff --git a/chart/testdata/sprocket/LICENSE b/chart/testdata/sprocket/LICENSE new file mode 100644 index 000000000..409cf6037 --- /dev/null +++ b/chart/testdata/sprocket/LICENSE @@ -0,0 +1 @@ +Placeholder for license. diff --git a/chart/testdata/sprocket/README.md b/chart/testdata/sprocket/README.md new file mode 100644 index 000000000..d1daac8da --- /dev/null +++ b/chart/testdata/sprocket/README.md @@ -0,0 +1,3 @@ +# Sprocket + +This is an example chart. diff --git a/chart/testdata/sprocket/docs/README.md b/chart/testdata/sprocket/docs/README.md new file mode 100644 index 000000000..d40747caf --- /dev/null +++ b/chart/testdata/sprocket/docs/README.md @@ -0,0 +1 @@ +This is a placeholder for documentation. diff --git a/chart/testdata/sprocket/hooks/pre-install.py b/chart/testdata/sprocket/hooks/pre-install.py new file mode 100644 index 000000000..c9b0d0a92 --- /dev/null +++ b/chart/testdata/sprocket/hooks/pre-install.py @@ -0,0 +1 @@ +# Placeholder. diff --git a/chart/testdata/sprocket/icon.svg b/chart/testdata/sprocket/icon.svg new file mode 100644 index 000000000..892130606 --- /dev/null +++ b/chart/testdata/sprocket/icon.svg @@ -0,0 +1,8 @@ + + + Example icon + + + diff --git a/chart/testdata/sprocket/templates/placeholder.txt b/chart/testdata/sprocket/templates/placeholder.txt new file mode 100644 index 000000000..ef9fb20a7 --- /dev/null +++ b/chart/testdata/sprocket/templates/placeholder.txt @@ -0,0 +1 @@ +This is a placeholder.