Add template function for upgrade version

This commit is contained in:
Ole Markus With 2020-12-05 18:43:50 +01:00
parent 5fe948bb5c
commit d89a7a55ce
7 changed files with 52 additions and 7 deletions

View File

@ -121,6 +121,18 @@ spec:
### Template Functions ### Template Functions
#### Kops specific functions
##### ChannelRecommendedKopsKubernetesVersion
This function returns the kubernetes version recommended for the running kops version.
##### ChannelRecommendedKopsUpgradeVersion <kuberneteVersion>
This function returns the kubernetes upgrade recommendation given that you run `<kubernetesVersion>`. Typically this is the latest minor version supported by the given channel.
#### Sprig functions
The entire set of https://github.com/Masterminds/sprig functions are available within the templates for you. Note if you want to use the 'defaults' functions switch off the verification check on the command line by `--fail-on-missing=false`; The entire set of https://github.com/Masterminds/sprig functions are available within the templates for you. Note if you want to use the 'defaults' functions switch off the verification check on the command line by `--fail-on-missing=false`;
```YAML ```YAML

View File

@ -4,6 +4,8 @@
# Significant changes # Significant changes
* Added [template funtions](https://kops.sigs.k8s.io/operations/cluster_template/#template-functions) for kubernetes version based on channel data.
# Breaking changes # Breaking changes
* Support for Kubernetes 1.11 and 1.12 has been removed. * Support for Kubernetes 1.11 and 1.12 has been removed.
@ -14,6 +16,8 @@
# Required Actions # Required Actions
* If you are running `kops toolbox template` in an airgapped environment, you have to set `--channel` to point to a local channel file.
# Deprecations # Deprecations
* Support for Kubernetes versions 1.13 and 1.14 are deprecated and will be removed in kOps 1.21. * Support for Kubernetes versions 1.13 and 1.14 are deprecated and will be removed in kOps 1.21.

View File

@ -11,6 +11,7 @@ go_library(
deps = [ deps = [
"//:go_default_library", "//:go_default_library",
"//pkg/apis/kops:go_default_library", "//pkg/apis/kops:go_default_library",
"//pkg/apis/kops/util:go_default_library",
"//vendor/github.com/Masterminds/sprig/v3:go_default_library", "//vendor/github.com/Masterminds/sprig/v3:go_default_library",
], ],
) )

View File

@ -22,6 +22,7 @@ import (
"github.com/Masterminds/sprig/v3" "github.com/Masterminds/sprig/v3"
"k8s.io/kops" "k8s.io/kops"
kopsapi "k8s.io/kops/pkg/apis/kops" kopsapi "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/apis/kops/util"
) )
// templateFuncsMap returns a map if the template functions for this template // templateFuncsMap returns a map if the template functions for this template
@ -40,8 +41,25 @@ func (r *Templater) templateFuncsMap(tm *template.Template) template.FuncMap {
return content return content
} }
funcs["ChannelRecommendedKubernetesVersion"] = func() string { funcs["ChannelRecommendedKubernetesUpgradeVersion"] = func(version string) string {
parsed, err := util.ParseKubernetesVersion(version)
if err != nil {
panic(err.Error())
}
versionInfo := kopsapi.FindKubernetesVersionSpec(r.channel.Spec.KubernetesVersions, *parsed)
recommended, err := versionInfo.FindRecommendedUpgrade(*parsed)
if err != nil {
panic(err.Error())
}
return recommended.String()
}
funcs["ChannelRecommendedKopsKubernetesVersion"] = func() string {
return kopsapi.RecommendedKubernetesVersion(r.channel, kops.Version).String() return kopsapi.RecommendedKubernetesVersion(r.channel, kops.Version).String()
} }
return funcs return funcs

View File

@ -18,6 +18,7 @@ package templater
import ( import (
"io/ioutil" "io/ioutil"
"os"
"testing" "testing"
"k8s.io/kops/pkg/diff" "k8s.io/kops/pkg/diff"
@ -86,9 +87,14 @@ func TestRenderChannelFunctions(t *testing.T) {
cases := []renderTest{ cases := []renderTest{
{ {
Context: map[string]interface{}{}, Context: map[string]interface{}{},
Template: `{{ ChannelRecommendedKubernetesVersion }}`, Template: `{{ ChannelRecommendedKopsKubernetesVersion }}`,
Expected: "1.5.2", Expected: "1.5.2",
}, },
{
Context: map[string]interface{}{},
Template: `{{ ChannelRecommendedKubernetesUpgradeVersion "1.4.2" }}`,
Expected: "1.4.8",
},
} }
makeRenderTests(t, cases) makeRenderTests(t, cases)
} }
@ -194,9 +200,13 @@ type renderTest struct {
} }
func makeRenderTests(t *testing.T, tests []renderTest) { func makeRenderTests(t *testing.T, tests []renderTest) {
channel, err := simple.NewMockChannel()
sourcePath := "../../../tests/integration/channel/simple/channel.yaml"
s, _ := os.Getwd()
channel, err := simple.NewMockChannel(sourcePath)
if err != nil { if err != nil {
t.Fatalf("could not load channel: %v", err) t.Fatalf("could not load channel: %v, %s", err, s)
} }
r := NewTemplater(channel) r := NewTemplater(channel)
for i, x := range tests { for i, x := range tests {

View File

@ -28,8 +28,9 @@ import (
// TestKopsUpgrades tests the version logic for kops versions // TestKopsUpgrades tests the version logic for kops versions
func TestKopsUpgrades(t *testing.T) { func TestKopsUpgrades(t *testing.T) {
sourcePath := "simple/channel.yaml"
channel, err := simple.NewMockChannel() channel, err := simple.NewMockChannel(sourcePath)
if err != nil { if err != nil {
t.Fatalf("failed to create channel: %v", err) t.Fatalf("failed to create channel: %v", err)
} }

View File

@ -23,8 +23,7 @@ import (
"k8s.io/kops/pkg/apis/kops" "k8s.io/kops/pkg/apis/kops"
) )
func NewMockChannel() (*kops.Channel, error) { func NewMockChannel(sourcePath string) (*kops.Channel, error) {
sourcePath := "simple/channel.yaml"
sourceBytes, err := ioutil.ReadFile(sourcePath) sourceBytes, err := ioutil.ReadFile(sourcePath)
if err != nil { if err != nil {
return nil, fmt.Errorf("unexpected error reading sourcePath %q: %v", sourcePath, err) return nil, fmt.Errorf("unexpected error reading sourcePath %q: %v", sourcePath, err)