mirror of https://github.com/helm/helm
Signed-off-by: Zsolt Zadory <zsolt.zadory@nokia.com>pull/5214/head
parent
dd4afa6af0
commit
6127bbeb49
@ -0,0 +1,23 @@
|
||||
# Chart and template weights
|
||||
|
||||
This part of the Best Practices Guide discusses how to manipulate the creation order of kubernetes resources
|
||||
|
||||
The weight of an object can be given at two levels:
|
||||
|
||||
- Chart: It can be defined in `Chart.yaml`. All templates directly defined in this chart is affected. It takes precedence over template weight.
|
||||
- Template: A `helm.sh/order-weight` annotation shall be declared in the uppermost metadata section of a kubernetes object.
|
||||
|
||||
## Precedence
|
||||
|
||||
Every object will get a tuple value of (chart weight, object weight). Default is 0 for both.
|
||||
Chart weight takes precedence over individual objects. Higher weight means it will be handled earlier.
|
||||
|
||||
## Scope
|
||||
|
||||
Weights have a global scope, so if subchart has higher weight then it will be handled sooner regardless of the inclusion order.
|
||||
|
||||
## How it works
|
||||
|
||||
Without the `--wait` flag it works like kind ordering - all objects will be sent to apiserver in one big manifest and objects are sorted accordingly (sortByWeight(sortByKind(templates))).
|
||||
|
||||
With the `--wait` flag equally weighted objects are grouped together, and these groups are then installed one-by-one. Helm waits for each group's resources individually for the given `--timeout` period.
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
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 manifest
|
||||
|
||||
// Equalto implements = operator for weights
|
||||
//
|
||||
// Weights are equal if both Chart and Manifest attributes are equal
|
||||
func (mw *Weight) Equalto(other *Weight) bool {
|
||||
if mw == nil || other == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if mw.Chart == other.Chart && mw.Manifest == other.Manifest {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// LessThan implements < operator for weights
|
||||
//
|
||||
// Precedence is Weight.Chart > Weight.Manifest
|
||||
func (mw *Weight) LessThan(other *Weight) bool {
|
||||
if mw == nil || other == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if mw.Chart == other.Chart {
|
||||
return mw.Manifest < other.Manifest
|
||||
}
|
||||
|
||||
return mw.Chart < other.Chart
|
||||
}
|
||||
|
||||
// GreaterThan implements > operator for weights
|
||||
//
|
||||
// Precedence is Weight.Chart > Weight.Manifest
|
||||
func (mw *Weight) GreaterThan(other *Weight) bool {
|
||||
if mw == nil || other == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if mw.Chart == other.Chart {
|
||||
return mw.Manifest > other.Manifest
|
||||
}
|
||||
|
||||
return mw.Chart > other.Chart
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
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 tiller
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
// sortByWeight does an in-place sort of manifests by Kind.
|
||||
//
|
||||
// Results are sorted by weight
|
||||
func sortByWeight(manifests []Manifest, st SortType) []Manifest {
|
||||
ws := newWeightSorter(manifests, st)
|
||||
sort.Stable(ws)
|
||||
return ws.manifests
|
||||
}
|
||||
|
||||
type weightSorter struct {
|
||||
manifests []Manifest
|
||||
stype SortType
|
||||
}
|
||||
|
||||
func newWeightSorter(m []Manifest, t SortType) *weightSorter {
|
||||
return &weightSorter{
|
||||
manifests: m,
|
||||
stype: t,
|
||||
}
|
||||
}
|
||||
|
||||
func (w *weightSorter) Len() int { return len(w.manifests) }
|
||||
|
||||
func (w *weightSorter) Swap(i, j int) { w.manifests[i], w.manifests[j] = w.manifests[j], w.manifests[i] }
|
||||
|
||||
func (w *weightSorter) Less(i, j int) bool {
|
||||
a := w.manifests[i]
|
||||
b := w.manifests[j]
|
||||
|
||||
if w.stype == SortInstall {
|
||||
return a.Weight.GreaterThan(b.Weight)
|
||||
}
|
||||
return a.Weight.LessThan(b.Weight)
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
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 tiller
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"k8s.io/helm/pkg/manifest"
|
||||
)
|
||||
|
||||
func TestWeightSorter(t *testing.T) {
|
||||
manifests := []Manifest{
|
||||
{
|
||||
Name: "a",
|
||||
Weight: &manifest.Weight{
|
||||
Chart: 1,
|
||||
Manifest: 5,
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "u",
|
||||
Weight: &manifest.Weight{
|
||||
Chart: 0,
|
||||
Manifest: 2,
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "d",
|
||||
Weight: &manifest.Weight{
|
||||
Chart: 1,
|
||||
Manifest: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "e",
|
||||
Weight: &manifest.Weight{
|
||||
Chart: 2,
|
||||
Manifest: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "t",
|
||||
Weight: &manifest.Weight{
|
||||
Chart: 10,
|
||||
Manifest: 4294967295,
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "b",
|
||||
Weight: &manifest.Weight{
|
||||
Chart: 0,
|
||||
Manifest: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "p",
|
||||
Weight: &manifest.Weight{
|
||||
Chart: 1,
|
||||
Manifest: 5,
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "s",
|
||||
Weight: &manifest.Weight{
|
||||
Chart: 10,
|
||||
Manifest: 10,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range []struct {
|
||||
description string
|
||||
order SortType
|
||||
expected string
|
||||
}{
|
||||
{"install", SortInstall, "tseapdub"},
|
||||
{"uninstall", SortUninstall, "budapest"},
|
||||
} {
|
||||
var buf bytes.Buffer
|
||||
t.Run(test.description, func(t *testing.T) {
|
||||
if got, want := len(test.expected), len(manifests); got != want {
|
||||
t.Fatalf("Expected %d names in order, got %d", want, got)
|
||||
}
|
||||
defer buf.Reset()
|
||||
for _, r := range sortByWeight(manifests, test.order) {
|
||||
buf.WriteString(r.Name)
|
||||
}
|
||||
if got := buf.String(); got != test.expected {
|
||||
t.Errorf("Expected %q, got %q", test.expected, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in new issue