Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template functions for recommended kubernetes versions #10369

Merged
merged 3 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion cmd/kops/toolbox_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"k8s.io/kubectl/pkg/util/templates"

"k8s.io/kops/cmd/kops/util"
kopsapi "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/try"
"k8s.io/kops/pkg/util/templater"
"k8s.io/kops/upup/pkg/fi/utils"
Expand Down Expand Up @@ -68,6 +69,7 @@ type toolboxTemplateOption struct {
templatePath []string
values []string
stringValues []string
channel string
}

// NewCmdToolboxTemplate returns a new templating command
Expand Down Expand Up @@ -96,6 +98,7 @@ func NewCmdToolboxTemplate(f *util.Factory, out io.Writer) *cobra.Command {
cmd.Flags().StringArrayVar(&options.stringValues, "set-string", options.stringValues, "Set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
cmd.Flags().StringSliceVar(&options.templatePath, "template", options.templatePath, "Path to template file or directory of templates to render")
cmd.Flags().StringSliceVar(&options.snippetsPath, "snippets", options.snippetsPath, "Path to directory containing snippets used for templating")
cmd.Flags().StringVar(&options.channel, "channel", options.channel, "Channel to use for the channel* functions")
cmd.Flags().StringVar(&options.outputPath, "output", options.outputPath, "Path to output file, otherwise defaults to stdout")
cmd.Flags().StringVar(&options.configValue, "config-value", "", "Show the value of a specific configuration value")
cmd.Flags().BoolVar(&options.failOnMissing, "fail-on-missing", true, "Fail on referencing unset variables in templates")
Expand Down Expand Up @@ -158,8 +161,18 @@ func runToolBoxTemplate(f *util.Factory, out io.Writer, options *toolboxTemplate
}
}

channelLocation := ""
if channelLocation == "" {
channelLocation = kopsapi.DefaultChannel
}

channel, err := kopsapi.LoadChannel(channelLocation)
if err != nil {
return fmt.Errorf("error loading channel %q: %v", channelLocation, err)
}

// @step: render each of the templates, splitting on the documents
r := templater.NewTemplater()
r := templater.NewTemplater(channel)
var documents []string
for _, x := range templates {
content, err := ioutil.ReadFile(x)
Expand Down
1 change: 1 addition & 0 deletions docs/cli/kops_toolbox_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ kops toolbox template [flags]
### Options

```
--channel string Channel to use for the channel* functions
--config-value string Show the value of a specific configuration value
--fail-on-missing Fail on referencing unset variables in templates (default true)
--format-yaml Attempt to format the generated yaml content before output
Expand Down
12 changes: 12 additions & 0 deletions docs/operations/cluster_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ spec:

### 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`;

```YAML
Expand Down
4 changes: 4 additions & 0 deletions docs/releases/1.20-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

# Significant changes

* Added [template funtions](https://kops.sigs.k8s.io/operations/cluster_template/#template-functions) for kubernetes version based on channel data.

# Breaking changes

* Support for Kubernetes 1.11 and 1.12 has been removed.
Expand All @@ -14,6 +16,8 @@

# 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

* Support for Kubernetes versions 1.13 and 1.14 are deprecated and will be removed in kOps 1.21.
Expand Down
13 changes: 11 additions & 2 deletions pkg/util/templater/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["templater.go"],
srcs = [
"template_functions.go",
"templater.go",
],
importpath = "k8s.io/kops/pkg/util/templater",
visibility = ["//visibility:public"],
deps = ["//vendor/github.com/Masterminds/sprig/v3:go_default_library"],
deps = [
"//: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",
],
)

go_test(
Expand All @@ -15,6 +23,7 @@ go_test(
embed = [":go_default_library"],
deps = [
"//pkg/diff:go_default_library",
"//tests/integration/channel/simple:go_default_library",
"//vendor/gopkg.in/yaml.v2:go_default_library",
],
)
66 changes: 66 additions & 0 deletions pkg/util/templater/template_functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2020 The Kubernetes 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 templater

import (
"text/template"

"github.com/Masterminds/sprig/v3"
"k8s.io/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
func (r *Templater) templateFuncsMap(tm *template.Template) template.FuncMap {
// grab the template functions from sprig which are pretty awesome
funcs := sprig.TxtFuncMap()

funcs["indent"] = indentContent
// @step: as far as i can see there's no native way in sprig in include external snippets of code
funcs["include"] = func(name string, context map[string]interface{}) string {
content, err := includeSnippet(tm, name, context)
if err != nil {
panic(err.Error())
}

return content
}

funcs["ChannelRecommendedKubernetesUpgradeVersion"] = func(version string) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These function names are pretty long.

This is essentially the recommended patch version for a given kubernetes version and the function below is the recommended minor+patch version for a given kops version.

How about:

ChannelRecommendedKubernetesPatchVersion
ChannelRecommendedKubernetesMinorVersion

or even just

ChannelKubernetesPatchVersion
ChannelKubernetesMinorVersion

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the easiest functions to name :)

I mainly based the function names after the primary underlying function. I don't mind long function names; it boils down to what most intuitive for users.


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 funcs
}
31 changes: 8 additions & 23 deletions pkg/util/templater/templater.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,23 @@ import (
"strings"
"text/template"

"github.com/Masterminds/sprig/v3"
"k8s.io/kops/pkg/apis/kops"
)

const (
templateName = "mainTemplate"
)

// Templater is golang template renders
type Templater struct{}
type Templater struct {
channel *kops.Channel
}

// NewTemplater returns a new renderer implementation
func NewTemplater() *Templater {
return &Templater{}
func NewTemplater(channel *kops.Channel) *Templater {
return &Templater{
channel: channel,
}
}

// Render is responsible for actually rendering the template
Expand Down Expand Up @@ -72,25 +76,6 @@ func (r *Templater) Render(content string, context map[string]interface{}, snipp
return writer.String(), nil
}

// templateFuncsMap returns a map if the template functions for this template
func (r *Templater) templateFuncsMap(tm *template.Template) template.FuncMap {
// grab the template functions from sprig which are pretty awesome
funcs := sprig.TxtFuncMap()

funcs["indent"] = indentContent
// @step: as far as i can see there's no native way in sprig in include external snippets of code
funcs["include"] = func(name string, context map[string]interface{}) string {
content, err := includeSnippet(tm, name, context)
if err != nil {
panic(err.Error())
}

return content
}

return funcs
}

// indentContent is responsible for indenting the string content
func indentContent(indent int, content string) string {
var b bytes.Buffer
Expand Down
28 changes: 27 additions & 1 deletion pkg/util/templater/templater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package templater

import (
"io/ioutil"
"os"
"testing"

"k8s.io/kops/pkg/diff"
"k8s.io/kops/tests/integration/channel/simple"

yaml "gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -81,6 +83,22 @@ func TestRenderIndent(t *testing.T) {
makeRenderTests(t, cases)
}

func TestRenderChannelFunctions(t *testing.T) {
cases := []renderTest{
{
Context: map[string]interface{}{},
Template: `{{ ChannelRecommendedKopsKubernetesVersion }}`,
Expected: "1.5.2",
},
{
Context: map[string]interface{}{},
Template: `{{ ChannelRecommendedKubernetesUpgradeVersion "1.4.2" }}`,
Expected: "1.4.8",
},
}
makeRenderTests(t, cases)
}

func TestRenderSnippet(t *testing.T) {
cases := []renderTest{
{
Expand Down Expand Up @@ -182,7 +200,15 @@ type renderTest struct {
}

func makeRenderTests(t *testing.T, tests []renderTest) {
r := NewTemplater()

sourcePath := "../../../tests/integration/channel/simple/channel.yaml"
s, _ := os.Getwd()

channel, err := simple.NewMockChannel(sourcePath)
if err != nil {
t.Fatalf("could not load channel: %v, %s", err, s)
}
r := NewTemplater(channel)
for i, x := range tests {
render, err := r.Render(x.Template, x.Context, x.Snippets, !x.DisableMissing)
if x.NotOK {
Expand Down
8 changes: 1 addition & 7 deletions tests/integration/channel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@ go_test(
name = "go_default_test",
srcs = ["integration_test.go"],
data = [
"exported_testdata", # keep
"//channels:channeldata", # keep
],
deps = [
"//pkg/apis/kops:go_default_library",
"//tests/integration/channel/simple:go_default_library",
"//vendor/github.com/blang/semver/v4:go_default_library",
],
)

filegroup(
name = "exported_testdata",
srcs = glob(["simple/**"]),
visibility = ["//visibility:public"],
)
12 changes: 4 additions & 8 deletions tests/integration/channel/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,16 @@ import (

"github.com/blang/semver/v4"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/tests/integration/channel/simple"
)

// TestKopsUpgrades tests the version logic for kops versions
func TestKopsUpgrades(t *testing.T) {
srcDir := "simple"
sourcePath := path.Join(srcDir, "channel.yaml")
sourceBytes, err := ioutil.ReadFile(sourcePath)
if err != nil {
t.Fatalf("unexpected error reading sourcePath %q: %v", sourcePath, err)
}
sourcePath := "simple/channel.yaml"

channel, err := kops.ParseChannel(sourceBytes)
channel, err := simple.NewMockChannel(sourcePath)
if err != nil {
t.Fatalf("failed to parse channel: %v", err)
t.Fatalf("failed to create channel: %v", err)
}

grid := []struct {
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/channel/simple/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["mock_channel.go"],
data = ["channel.yaml"],
importpath = "k8s.io/kops/tests/integration/channel/simple",
visibility = ["//visibility:public"],
deps = ["//pkg/apis/kops:go_default_library"],
)
38 changes: 38 additions & 0 deletions tests/integration/channel/simple/mock_channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2020 The Kubernetes 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 simple

import (
"fmt"
"io/ioutil"

"k8s.io/kops/pkg/apis/kops"
)

func NewMockChannel(sourcePath string) (*kops.Channel, error) {
sourceBytes, err := ioutil.ReadFile(sourcePath)
if err != nil {
return nil, fmt.Errorf("unexpected error reading sourcePath %q: %v", sourcePath, err)
}

channel, err := kops.ParseChannel(sourceBytes)
if err != nil {
return nil, fmt.Errorf("failed to parse channel: %v", err)
}
return channel, nil

}