Skip to content

Commit

Permalink
Use helm's merging of vaulefiles and files
Browse files Browse the repository at this point in the history
  • Loading branch information
Ole Markus With committed Dec 16, 2020
1 parent b6a07c1 commit 24c9d03
Show file tree
Hide file tree
Showing 31 changed files with 1,721 additions and 31 deletions.
2 changes: 2 additions & 0 deletions cmd/kops/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ go_library(
"//vendor/github.com/spf13/cobra/doc:go_default_library",
"//vendor/github.com/spf13/viper:go_default_library",
"//vendor/golang.org/x/crypto/ssh:go_default_library",
"//vendor/helm.sh/helm/v3/pkg/cli/values:go_default_library",
"//vendor/helm.sh/helm/v3/pkg/strvals:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
Expand Down Expand Up @@ -156,6 +157,7 @@ go_test(
"//channels:channeldata", # keep
"//tests/integration/create_cluster:exported_testdata", # keep
"//tests/integration/update_cluster:exported_testdata", # keep
"test/values.yaml",
],
embed = [":go_default_library"],
shard_count = 10,
Expand Down
42 changes: 12 additions & 30 deletions cmd/kops/toolbox_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"k8s.io/kubectl/pkg/util/templates"
"sigs.k8s.io/yaml"

helmvalues "helm.sh/helm/v3/pkg/cli/values"

"k8s.io/kops/cmd/kops/util"
kopsapi "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/try"
Expand Down Expand Up @@ -250,7 +252,16 @@ func newTemplateContext(files []string, values []string, stringValues []string)
return nil, fmt.Errorf("unable decode the configuration file: %s, error: %v", j, err)
}

context = mergeValues(context, ctx)
valueOpts := &helmvalues.Options{
Values: values,
ValueFiles: files,
StringValues: stringValues,
}

context, err = valueOpts.MergeValues(nil)
if err != nil {
return nil, err
}
}
}

Expand All @@ -271,35 +282,6 @@ func newTemplateContext(files []string, values []string, stringValues []string)
return context, nil
}

// Merges source and destination map, preferring values from the source map
// Copied from the Helm (https://github.com/kubernetes/helm) project:
// https://github.com/kubernetes/helm/blob/282984e75fd115a0765730efe09d8257c72fa56d/cmd/helm/install.go#L302
func mergeValues(dest map[string]interface{}, src map[string]interface{}) map[string]interface{} {
for k, v := range src {
// If the key doesn't exist already, then just set the key to that value
if _, exists := dest[k]; !exists {
dest[k] = v
continue
}
nextMap, ok := v.(map[string]interface{})
// If it isn't another map, overwrite the value
if !ok {
dest[k] = v
continue
}
// Edge case: If the key exists in the destination, but isn't a map
destMap, isMap := dest[k].(map[string]interface{})
// If the source map has a map for this key, prefer it
if !isMap {
dest[k] = v
continue
}
// If we got to this point, it is a map in both, so merge them
dest[k] = mergeValues(destMap, nextMap)
}
return dest
}

// expandFiles is responsible for resolving any references to directories
func expandFiles(path string) ([]string, error) {
// @check if the path is a directory, if not we can return straight away
Expand Down
2 changes: 1 addition & 1 deletion docs/releases/1.20-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

* The helm functions used to parse `--set` flags on `kops toolbox template` has been updated to helm3, which has a slightly different behaviour from helm3.
* kOps now use helm3 functions for merging template `--set` and `--values` arguments. This has slightly different behaviour than previous helm2-like logic.

# Breaking changes

Expand Down
13 changes: 13 additions & 0 deletions vendor/helm.sh/helm/v3/internal/tlsutil/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = [
"cfg.go",
"tls.go",
],
importmap = "k8s.io/kops/vendor/helm.sh/helm/v3/internal/tlsutil",
importpath = "helm.sh/helm/v3/internal/tlsutil",
visibility = ["//vendor/helm.sh/helm/v3:__subpackages__"],
deps = ["//vendor/github.com/pkg/errors:go_default_library"],
)
58 changes: 58 additions & 0 deletions vendor/helm.sh/helm/v3/internal/tlsutil/cfg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
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 tlsutil

import (
"crypto/tls"
"crypto/x509"
"os"

"github.com/pkg/errors"
)

// Options represents configurable options used to create client and server TLS configurations.
type Options struct {
CaCertFile string
// If either the KeyFile or CertFile is empty, ClientConfig() will not load them.
KeyFile string
CertFile string
// Client-only options
InsecureSkipVerify bool
}

// ClientConfig returns a TLS configuration for use by a Helm client.
func ClientConfig(opts Options) (cfg *tls.Config, err error) {
var cert *tls.Certificate
var pool *x509.CertPool

if opts.CertFile != "" || opts.KeyFile != "" {
if cert, err = CertFromFilePair(opts.CertFile, opts.KeyFile); err != nil {
if os.IsNotExist(err) {
return nil, errors.Wrapf(err, "could not load x509 key pair (cert: %q, key: %q)", opts.CertFile, opts.KeyFile)
}
return nil, errors.Wrapf(err, "could not read x509 key pair (cert: %q, key: %q)", opts.CertFile, opts.KeyFile)
}
}
if !opts.InsecureSkipVerify && opts.CaCertFile != "" {
if pool, err = CertPoolFromFile(opts.CaCertFile); err != nil {
return nil, err
}
}

cfg = &tls.Config{InsecureSkipVerify: opts.InsecureSkipVerify, Certificates: []tls.Certificate{*cert}, RootCAs: pool}
return cfg, nil
}
76 changes: 76 additions & 0 deletions vendor/helm.sh/helm/v3/internal/tlsutil/tls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
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 tlsutil

import (
"crypto/tls"
"crypto/x509"
"io/ioutil"

"github.com/pkg/errors"
)

// NewClientTLS returns tls.Config appropriate for client auth.
func NewClientTLS(certFile, keyFile, caFile string) (*tls.Config, error) {
config := tls.Config{}

if certFile != "" && keyFile != "" {
cert, err := CertFromFilePair(certFile, keyFile)
if err != nil {
return nil, err
}
config.Certificates = []tls.Certificate{*cert}
}

if caFile != "" {
cp, err := CertPoolFromFile(caFile)
if err != nil {
return nil, err
}
config.RootCAs = cp
}

return &config, nil
}

// CertPoolFromFile returns an x509.CertPool containing the certificates
// in the given PEM-encoded file.
// Returns an error if the file could not be read, a certificate could not
// be parsed, or if the file does not contain any certificates
func CertPoolFromFile(filename string) (*x509.CertPool, error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, errors.Errorf("can't read CA file: %v", filename)
}
cp := x509.NewCertPool()
if !cp.AppendCertsFromPEM(b) {
return nil, errors.Errorf("failed to append certificates from file: %s", filename)
}
return cp, nil
}

// CertFromFilePair returns an tls.Certificate containing the
// certificates public/private key pair from a pair of given PEM-encoded files.
// Returns an error if the file could not be read, a certificate could not
// be parsed, or if the file does not contain any certificates
func CertFromFilePair(certFile, keyFile string) (*tls.Certificate, error) {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, errors.Wrapf(err, "can't load key pair from cert %s and key %s", certFile, keyFile)
}
return &cert, err
}
9 changes: 9 additions & 0 deletions vendor/helm.sh/helm/v3/internal/urlutil/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["urlutil.go"],
importmap = "k8s.io/kops/vendor/helm.sh/helm/v3/internal/urlutil",
importpath = "helm.sh/helm/v3/internal/urlutil",
visibility = ["//vendor/helm.sh/helm/v3:__subpackages__"],
)
73 changes: 73 additions & 0 deletions vendor/helm.sh/helm/v3/internal/urlutil/urlutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
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 urlutil

import (
"net/url"
"path"
"path/filepath"
)

// URLJoin joins a base URL to one or more path components.
//
// It's like filepath.Join for URLs. If the baseURL is pathish, this will still
// perform a join.
//
// If the URL is unparsable, this returns an error.
func URLJoin(baseURL string, paths ...string) (string, error) {
u, err := url.Parse(baseURL)
if err != nil {
return "", err
}
// We want path instead of filepath because path always uses /.
all := []string{u.Path}
all = append(all, paths...)
u.Path = path.Join(all...)
return u.String(), nil
}

// Equal normalizes two URLs and then compares for equality.
func Equal(a, b string) bool {
au, err := url.Parse(a)
if err != nil {
a = filepath.Clean(a)
b = filepath.Clean(b)
// If urls are paths, return true only if they are an exact match
return a == b
}
bu, err := url.Parse(b)
if err != nil {
return false
}

for _, u := range []*url.URL{au, bu} {
if u.Path == "" {
u.Path = "/"
}
u.Path = filepath.Clean(u.Path)
}
return au.String() == bu.String()
}

// ExtractHostname returns hostname from URL
func ExtractHostname(addr string) (string, error) {
u, err := url.Parse(addr)
if err != nil {
return "", err
}
return u.Hostname(), nil
}
9 changes: 9 additions & 0 deletions vendor/helm.sh/helm/v3/internal/version/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["version.go"],
importmap = "k8s.io/kops/vendor/helm.sh/helm/v3/internal/version",
importpath = "helm.sh/helm/v3/internal/version",
visibility = ["//vendor/helm.sh/helm/v3:__subpackages__"],
)
Loading

0 comments on commit 24c9d03

Please sign in to comment.