mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 01:50:55 +00:00
Move some code to make it reusable without import cycles.
This commit is contained in:
@@ -4,7 +4,6 @@
|
||||
package generators
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
@@ -38,14 +37,9 @@ func MakeConfigMap(
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, k := range filtersutil.SortedMapKeys(m) {
|
||||
fldName, vrN := makeConfigMapValueRNode(m[k])
|
||||
if _, err = rn.Pipe(
|
||||
yaml.LookupCreate(yaml.MappingNode, fldName),
|
||||
yaml.SetField(k, vrN)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = rn.LoadMapIntoConfigMapData(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
copyLabelsAndAnnotations(rn, args.Options)
|
||||
return rn, err
|
||||
return rn, nil
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
package generators
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
@@ -51,14 +50,9 @@ func MakeSecret(
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, k := range filtersutil.SortedMapKeys(m) {
|
||||
vrN := makeSecretValueRNode(m[k])
|
||||
if _, err = rn.Pipe(
|
||||
yaml.LookupCreate(yaml.MappingNode, yaml.DataField),
|
||||
yaml.SetField(k, vrN)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = rn.LoadMapIntoSecretData(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
copyLabelsAndAnnotations(rn, args.Options)
|
||||
return rn, err
|
||||
return rn, nil
|
||||
}
|
||||
|
||||
@@ -4,13 +4,9 @@
|
||||
package generators
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/go-errors/errors"
|
||||
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
@@ -66,13 +62,13 @@ func copyLabelsAndAnnotations(
|
||||
if opts == nil {
|
||||
return nil
|
||||
}
|
||||
for _, k := range filtersutil.SortedMapKeys(opts.Labels) {
|
||||
for _, k := range yaml.SortedMapKeys(opts.Labels) {
|
||||
v := opts.Labels[k]
|
||||
if _, err := rn.Pipe(yaml.SetLabel(k, v)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, k := range filtersutil.SortedMapKeys(opts.Annotations) {
|
||||
for _, k := range yaml.SortedMapKeys(opts.Annotations) {
|
||||
v := opts.Annotations[k]
|
||||
if _, err := rn.Pipe(yaml.SetAnnotation(k, v)); err != nil {
|
||||
return err
|
||||
@@ -80,60 +76,3 @@ func copyLabelsAndAnnotations(
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// In a secret, all data is base64 encoded, regardless of its conformance
|
||||
// or lack thereof to UTF-8.
|
||||
func makeSecretValueRNode(s string) *yaml.RNode {
|
||||
yN := &yaml.Node{Kind: yaml.ScalarNode}
|
||||
// Purposely don't use YAML tags to identify the data as being plain text or
|
||||
// binary. It kubernetes Secrets the values in the `data` map are expected
|
||||
// to be base64 encoded, and in ConfigMaps that same can be said for the
|
||||
// values in the `binaryData` field.
|
||||
yN.Tag = yaml.NodeTagString
|
||||
yN.Value = encodeBase64(s)
|
||||
if strings.Contains(yN.Value, "\n") {
|
||||
yN.Style = yaml.LiteralStyle
|
||||
}
|
||||
return yaml.NewRNode(yN)
|
||||
}
|
||||
|
||||
func makeConfigMapValueRNode(s string) (field string, rN *yaml.RNode) {
|
||||
yN := &yaml.Node{Kind: yaml.ScalarNode}
|
||||
yN.Tag = yaml.NodeTagString
|
||||
if utf8.ValidString(s) {
|
||||
field = yaml.DataField
|
||||
yN.Value = s
|
||||
} else {
|
||||
field = yaml.BinaryDataField
|
||||
yN.Value = encodeBase64(s)
|
||||
}
|
||||
if strings.Contains(yN.Value, "\n") {
|
||||
yN.Style = yaml.LiteralStyle
|
||||
}
|
||||
return field, yaml.NewRNode(yN)
|
||||
}
|
||||
|
||||
// encodeBase64 encodes s as base64 that is broken up into multiple lines
|
||||
// as appropriate for the resulting length.
|
||||
func encodeBase64(s string) string {
|
||||
const lineLen = 70
|
||||
encLen := base64.StdEncoding.EncodedLen(len(s))
|
||||
lines := encLen/lineLen + 1
|
||||
buf := make([]byte, encLen*2+lines)
|
||||
in := buf[0:encLen]
|
||||
out := buf[encLen:]
|
||||
base64.StdEncoding.Encode(in, []byte(s))
|
||||
k := 0
|
||||
for i := 0; i < len(in); i += lineLen {
|
||||
j := i + lineLen
|
||||
if j > len(in) {
|
||||
j = len(in)
|
||||
}
|
||||
k += copy(out[k:], in[i:j])
|
||||
if lines > 1 {
|
||||
out[k] = '\n'
|
||||
k++
|
||||
}
|
||||
}
|
||||
return string(out[:k])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user