mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Fix cluster of silly Go nits.
This commit is contained in:
@@ -18,14 +18,8 @@ limitations under the License.
|
||||
package configmapandsecret
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
cutil "github.com/kubernetes-sigs/kustomize/pkg/configmapandsecret/util"
|
||||
"github.com/kubernetes-sigs/kustomize/pkg/hash"
|
||||
"github.com/kubernetes-sigs/kustomize/pkg/types"
|
||||
@@ -49,21 +43,6 @@ func MakeConfigmapAndGenerateName(cm types.ConfigMapArgs) (*unstructured.Unstruc
|
||||
return unstructuredCM, nameWithHash, err
|
||||
}
|
||||
|
||||
// MakeSecretAndGenerateName returns a secret with the name appended with a hash.
|
||||
func MakeSecretAndGenerateName(secret types.SecretArgs, path string) (*unstructured.Unstructured, string, error) {
|
||||
corev1Secret, err := makeSecret(secret, path)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
h, err := hash.SecretHash(corev1Secret)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
nameWithHash := fmt.Sprintf("%s-%s", secret.Name, h)
|
||||
unstructuredCM, err := objectToUnstructured(corev1Secret)
|
||||
return unstructuredCM, nameWithHash, err
|
||||
}
|
||||
|
||||
func objectToUnstructured(in runtime.Object) (*unstructured.Unstructured, error) {
|
||||
marshaled, err := json.Marshal(in)
|
||||
if err != nil {
|
||||
@@ -99,38 +78,3 @@ func makeConfigMap(cm types.ConfigMapArgs) (*corev1.ConfigMap, error) {
|
||||
|
||||
return corev1cm, nil
|
||||
}
|
||||
|
||||
func makeSecret(secret types.SecretArgs, path string) (*corev1.Secret, error) {
|
||||
corev1secret := &corev1.Secret{}
|
||||
corev1secret.APIVersion = "v1"
|
||||
corev1secret.Kind = "Secret"
|
||||
corev1secret.Name = secret.Name
|
||||
corev1secret.Type = corev1.SecretType(secret.Type)
|
||||
if corev1secret.Type == "" {
|
||||
corev1secret.Type = corev1.SecretTypeOpaque
|
||||
}
|
||||
corev1secret.Data = map[string][]byte{}
|
||||
|
||||
for k, v := range secret.Commands {
|
||||
out, err := createSecretKey(path, v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
corev1secret.Data[k] = out
|
||||
}
|
||||
|
||||
return corev1secret, nil
|
||||
}
|
||||
|
||||
func createSecretKey(wd string, command string) ([]byte, error) {
|
||||
fi, err := os.Stat(wd)
|
||||
if err != nil || !fi.IsDir() {
|
||||
wd = filepath.Dir(wd)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "sh", "-c", command)
|
||||
cmd.Dir = wd
|
||||
|
||||
return cmd.Output()
|
||||
}
|
||||
|
||||
@@ -17,10 +17,15 @@ limitations under the License.
|
||||
package configmapandsecret
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/kubernetes-sigs/kustomize/pkg/types"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -110,24 +115,6 @@ func makeTestSecret(name string) *corev1.Secret {
|
||||
}
|
||||
}
|
||||
|
||||
func makeUnstructuredSecret(name string) *unstructured.Unstructured {
|
||||
return &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"apiVersion": "v1",
|
||||
"kind": "Secret",
|
||||
"metadata": map[string]interface{}{
|
||||
"name": name,
|
||||
"creationTimestamp": nil,
|
||||
},
|
||||
"type": string(corev1.SecretTypeOpaque),
|
||||
"data": map[string]interface{}{
|
||||
"DB_USERNAME": base64.StdEncoding.EncodeToString([]byte("admin")),
|
||||
"DB_PASSWORD": base64.StdEncoding.EncodeToString([]byte("somepw")),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestConstructConfigMap(t *testing.T) {
|
||||
type testCase struct {
|
||||
description string
|
||||
@@ -198,6 +185,41 @@ func TestConstructSecret(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func makeSecret(secret types.SecretArgs, path string) (*corev1.Secret, error) {
|
||||
corev1secret := &corev1.Secret{}
|
||||
corev1secret.APIVersion = "v1"
|
||||
corev1secret.Kind = "Secret"
|
||||
corev1secret.Name = secret.Name
|
||||
corev1secret.Type = corev1.SecretType(secret.Type)
|
||||
if corev1secret.Type == "" {
|
||||
corev1secret.Type = corev1.SecretTypeOpaque
|
||||
}
|
||||
corev1secret.Data = map[string][]byte{}
|
||||
|
||||
for k, v := range secret.Commands {
|
||||
out, err := createSecretKey(path, v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
corev1secret.Data[k] = out
|
||||
}
|
||||
|
||||
return corev1secret, nil
|
||||
}
|
||||
|
||||
func createSecretKey(wd string, command string) ([]byte, error) {
|
||||
fi, err := os.Stat(wd)
|
||||
if err != nil || !fi.IsDir() {
|
||||
wd = filepath.Dir(wd)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "sh", "-c", command)
|
||||
cmd.Dir = wd
|
||||
|
||||
return cmd.Output()
|
||||
}
|
||||
|
||||
func TestFailConstructSecret(t *testing.T) {
|
||||
secret := types.SecretArgs{
|
||||
Name: "secret",
|
||||
|
||||
@@ -62,7 +62,7 @@ func HandleConfigMapFromFileSources(configMap *v1.ConfigMap, fileSources []strin
|
||||
}
|
||||
if info.IsDir() {
|
||||
if strings.Contains(fileSource, "=") {
|
||||
return fmt.Errorf("cannot give a key name for a directory path.")
|
||||
return fmt.Errorf("cannot give a key name for a directory path")
|
||||
}
|
||||
fileList, err := ioutil.ReadDir(filePath)
|
||||
if err != nil {
|
||||
@@ -127,7 +127,7 @@ func addKeyFromLiteralToConfigMap(configMap *v1.ConfigMap, keyName, data string)
|
||||
return fmt.Errorf("%q is not a valid key name for a ConfigMap: %s", keyName, strings.Join(errs, ";"))
|
||||
}
|
||||
if _, entryExists := configMap.Data[keyName]; entryExists {
|
||||
return fmt.Errorf("cannot add key %s, another key by that name already exists: %v.", keyName, configMap.Data)
|
||||
return fmt.Errorf("cannot add key %s, another key by that name already exists: %v", keyName, configMap.Data)
|
||||
}
|
||||
configMap.Data[keyName] = data
|
||||
return nil
|
||||
|
||||
@@ -59,7 +59,7 @@ func HandleFromFileSources(secret *v1.Secret, fileSources []string) error {
|
||||
}
|
||||
if info.IsDir() {
|
||||
if strings.Contains(fileSource, "=") {
|
||||
return fmt.Errorf("cannot give a key name for a directory path.")
|
||||
return fmt.Errorf("cannot give a key name for a directory path")
|
||||
}
|
||||
fileList, err := ioutil.ReadDir(filePath)
|
||||
if err != nil {
|
||||
@@ -119,7 +119,7 @@ func addKeyFromLiteralToSecret(secret *v1.Secret, keyName string, data []byte) e
|
||||
}
|
||||
|
||||
if _, entryExists := secret.Data[keyName]; entryExists {
|
||||
return fmt.Errorf("cannot add key %s, another key by that name already exists: %v.", keyName, secret.Data)
|
||||
return fmt.Errorf("cannot add key %s, another key by that name already exists: %v", keyName, secret.Data)
|
||||
}
|
||||
secret.Data[keyName] = data
|
||||
return nil
|
||||
|
||||
@@ -64,11 +64,11 @@ func ParseFileSource(source string) (keyName, filePath string, err error) {
|
||||
case numSeparators == 0:
|
||||
return path.Base(source), source, nil
|
||||
case numSeparators == 1 && strings.HasPrefix(source, "="):
|
||||
return "", "", fmt.Errorf("key name for file path %v missing.", strings.TrimPrefix(source, "="))
|
||||
return "", "", fmt.Errorf("key name for file path %v missing", strings.TrimPrefix(source, "="))
|
||||
case numSeparators == 1 && strings.HasSuffix(source, "="):
|
||||
return "", "", fmt.Errorf("file path for key name %v missing.", strings.TrimSuffix(source, "="))
|
||||
return "", "", fmt.Errorf("file path for key name %v missing", strings.TrimSuffix(source, "="))
|
||||
case numSeparators > 1:
|
||||
return "", "", errors.New("Key names or file paths cannot contain '='.")
|
||||
return "", "", errors.New("key names or file paths cannot contain '='")
|
||||
default:
|
||||
components := strings.Split(source, "=")
|
||||
return components[0], components[1], nil
|
||||
|
||||
Reference in New Issue
Block a user