Skip to content

Commit

Permalink
modified tests and files
Browse files Browse the repository at this point in the history
Signed-off-by: jignyasamishra <[email protected]>
  • Loading branch information
jignyasamishra committed Feb 24, 2024
1 parent 8d2a1c9 commit 8708d7b
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 202 deletions.
362 changes: 201 additions & 161 deletions pkg/apis/kappctrl/v1alpha1/generated.pb.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pkg/apis/kappctrl/v1alpha1/generated.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/apis/kappctrl/v1alpha1/types_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type AppTemplateValuesSource struct {
// +k8s:openapi-gen=true
type AppTemplateValuesSourceRef struct {
Name string `json:"name,omitempty" protobuf:"bytes,1,opt,name=name"`
Key string `json:"key,omitempty" protobuf:"bytes,2,opt,name=key"`
}

// +k8s:openapi-gen=true
Expand Down
6 changes: 6 additions & 0 deletions pkg/apiserver/openapi/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 42 additions & 5 deletions pkg/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,43 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

func Test_AppTemplateValuesSourceRef_HandlesKeyField(t *testing.T) {
log := logf.Log.WithName("kc")

// Define an App that uses AppTemplateValuesSourceRef with a Key field
appWithKeyRefs := v1alpha1.App{
ObjectMeta: metav1.ObjectMeta{
Name: "app-with-key-refs",
Namespace: "default",
},
Spec: v1alpha1.AppSpec{
Template: []v1alpha1.AppTemplate{
{
Ytt: &v1alpha1.AppTemplateYtt{
ValuesFrom: []v1alpha1.AppTemplateValuesSource{
{
SecretRef: &v1alpha1.AppTemplateValuesSourceRef{
Name: "my-secret",
Key: "my-key",
},
},
},
},
},
},
},
}

k8scs := k8sfake.NewSimpleClientset()
fetchFac := fetch.NewFactory(k8scs, fetch.VendirOpts{}, exec.NewPlainCmdRunner())
tmpFac := template.NewFactory(k8scs, fetchFac, false, exec.NewPlainCmdRunner())
deployFac := deploy.NewFactory(k8scs, kubeconfig.NewKubeconfig(k8scs, log), nil, exec.NewPlainCmdRunner(), log)

app := apppkg.NewApp(appWithKeyRefs, apppkg.Hooks{}, fetchFac, tmpFac, deployFac, log, apppkg.Opts{}, metrics.NewMetrics(), FakeComponentInfo{})
assert.NotNil(t, app)

}

func Test_SecretRefs_RetrievesAllSecretRefs(t *testing.T) {
log := logf.Log.WithName("kc")
expected := map[reftracker.RefKey]struct{}{
Expand All @@ -45,15 +82,15 @@ func Test_SecretRefs_RetrievesAllSecretRefs(t *testing.T) {
},
Spec: v1alpha1.AppSpec{
Fetch: []v1alpha1.AppFetch{
v1alpha1.AppFetch{Inline: &v1alpha1.AppFetchInline{PathsFrom: []v1alpha1.AppFetchInlineSource{{SecretRef: &v1alpha1.AppFetchInlineSourceRef{"", "s"}}}}},
v1alpha1.AppFetch{Inline: &v1alpha1.AppFetchInline{PathsFrom: []v1alpha1.AppFetchInlineSource{{SecretRef: &v1alpha1.AppFetchInlineSourceRef{Name: "s"}}}}},
v1alpha1.AppFetch{Image: &v1alpha1.AppFetchImage{SecretRef: &v1alpha1.AppFetchLocalRef{Name: "s1"}}},
v1alpha1.AppFetch{HTTP: &v1alpha1.AppFetchHTTP{SecretRef: &v1alpha1.AppFetchLocalRef{Name: "s2"}}},
v1alpha1.AppFetch{Git: &v1alpha1.AppFetchGit{SecretRef: &v1alpha1.AppFetchLocalRef{Name: "s3"}}},
v1alpha1.AppFetch{HelmChart: &v1alpha1.AppFetchHelmChart{Repository: &v1alpha1.AppFetchHelmChartRepo{SecretRef: &v1alpha1.AppFetchLocalRef{Name: "s4"}}}},
v1alpha1.AppFetch{ImgpkgBundle: &v1alpha1.AppFetchImgpkgBundle{SecretRef: &v1alpha1.AppFetchLocalRef{Name: "s5"}}},
},
Template: []v1alpha1.AppTemplate{
v1alpha1.AppTemplate{Ytt: &v1alpha1.AppTemplateYtt{Inline: &v1alpha1.AppFetchInline{PathsFrom: []v1alpha1.AppFetchInlineSource{{SecretRef: &v1alpha1.AppFetchInlineSourceRef{"", "s6"}}}}}},
v1alpha1.AppTemplate{Ytt: &v1alpha1.AppTemplateYtt{Inline: &v1alpha1.AppFetchInline{PathsFrom: []v1alpha1.AppFetchInlineSource{{SecretRef: &v1alpha1.AppFetchInlineSourceRef{Name: "s6"}}}}}},
v1alpha1.AppTemplate{Ytt: &v1alpha1.AppTemplateYtt{ValuesFrom: []v1alpha1.AppTemplateValuesSource{{SecretRef: &v1alpha1.AppTemplateValuesSourceRef{Name: "s8"}}}}},
v1alpha1.AppTemplate{HelmTemplate: &v1alpha1.AppTemplateHelmTemplate{ValuesFrom: []v1alpha1.AppTemplateValuesSource{{SecretRef: &v1alpha1.AppTemplateValuesSourceRef{Name: "s7"}}}}},
},
Expand Down Expand Up @@ -112,12 +149,12 @@ func Test_ConfigMapRefs_RetrievesAllConfigMapRefs(t *testing.T) {
},
Spec: v1alpha1.AppSpec{
Fetch: []v1alpha1.AppFetch{
v1alpha1.AppFetch{Inline: &v1alpha1.AppFetchInline{PathsFrom: []v1alpha1.AppFetchInlineSource{{ConfigMapRef: &v1alpha1.AppFetchInlineSourceRef{"", "c"}}}}},
v1alpha1.AppFetch{Inline: &v1alpha1.AppFetchInline{PathsFrom: []v1alpha1.AppFetchInlineSource{{ConfigMapRef: &v1alpha1.AppFetchInlineSourceRef{Name: "c"}}}}},
},
Template: []v1alpha1.AppTemplate{
v1alpha1.AppTemplate{Ytt: &v1alpha1.AppTemplateYtt{Inline: &v1alpha1.AppFetchInline{PathsFrom: []v1alpha1.AppFetchInlineSource{{ConfigMapRef: &v1alpha1.AppFetchInlineSourceRef{"", "c1"}}}}}},
v1alpha1.AppTemplate{Ytt: &v1alpha1.AppTemplateYtt{Inline: &v1alpha1.AppFetchInline{PathsFrom: []v1alpha1.AppFetchInlineSource{{ConfigMapRef: &v1alpha1.AppFetchInlineSourceRef{Name: "c1"}}}}}},
v1alpha1.AppTemplate{Ytt: &v1alpha1.AppTemplateYtt{ValuesFrom: []v1alpha1.AppTemplateValuesSource{{ConfigMapRef: &v1alpha1.AppTemplateValuesSourceRef{Name: "c3"}}}}},
v1alpha1.AppTemplate{HelmTemplate: &v1alpha1.AppTemplateHelmTemplate{ValuesFrom: []v1alpha1.AppTemplateValuesSource{{ConfigMapRef: &v1alpha1.AppTemplateValuesSourceRef{"c2"}}}}},
v1alpha1.AppTemplate{HelmTemplate: &v1alpha1.AppTemplateHelmTemplate{ValuesFrom: []v1alpha1.AppTemplateValuesSource{{ConfigMapRef: &v1alpha1.AppTemplateValuesSourceRef{Name: "c2"}}}}},
},
},
}
Expand Down
73 changes: 37 additions & 36 deletions pkg/template/values.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright 2021 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0

package template

import (
Expand All @@ -18,43 +17,33 @@ import (
type Values struct {
ValuesFrom []v1alpha1.AppTemplateValuesSource
AdditionalValues AdditionalDownwardAPIValues

appContext AppContext
coreClient kubernetes.Interface
appContext AppContext
coreClient kubernetes.Interface
}

func (t Values) AsPaths(dirPath string) ([]string, func(), error) {
var valuesDirs []*memdir.TmpDir

cleanUpFunc := func() {
for _, valDir := range valuesDirs {
_ = valDir.Remove()
}
}

var allPaths []string

for _, source := range t.ValuesFrom {
var paths []string
var err error

valuesDir := memdir.NewTmpDir("template-values")

err = valuesDir.Create()
if err != nil {
cleanUpFunc()
return nil, nil, err
}

valuesDirs = append(valuesDirs, valuesDir)

switch {
case source.SecretRef != nil:
paths, err = t.writeFromSecret(valuesDir.Path(), *source.SecretRef)

case source.ConfigMapRef != nil:
paths, err = t.writeFromConfigMap(valuesDir.Path(), *source.ConfigMapRef)

case len(source.Path) > 0:
if source.Path == stdinPath {
paths = append(paths, stdinPath)
Expand All @@ -64,32 +53,26 @@ func (t Values) AsPaths(dirPath string) ([]string, func(), error) {
paths = append(paths, checkedPath)
}
}

case source.DownwardAPI != nil:
downwardAPIValues := DownwardAPIValues{
items: source.DownwardAPI.Items,
metadata: t.appContext.Metadata,
additionalDownwardAPIValues: t.AdditionalValues,
}
paths, err = t.writeFromDownwardAPI(valuesDir.Path(), downwardAPIValues)

default:
err = fmt.Errorf("Expected one of secretRef, configMapRef, downwardAPI, or path as a source")
err = fmt.Errorf("expected one of secretRef, configMapRef, downwardAPI, or path as a source")
}
if err != nil {
cleanUpFunc()
return nil, nil, fmt.Errorf("Preparing template values: %s", err)
return nil, nil, fmt.Errorf("preparing template values: %s", err)
}

allPaths = append(allPaths, paths...)
}

return allPaths, cleanUpFunc, nil
}

func (t Values) writeFromSecret(dstPath string,
secretRef v1alpha1.AppTemplateValuesSourceRef) ([]string, error) {

secret, err := t.coreClient.CoreV1().Secrets(t.appContext.Namespace).Get(
context.Background(), secretRef.Name, metav1.GetOptions{})
if err != nil {
Expand All @@ -98,55 +81,60 @@ func (t Values) writeFromSecret(dstPath string,

var result []string

for name, val := range secret.Data {
path, err := t.writeFile(dstPath, name, val)
if secretRef.Key != "" {
// If a key is provided, only write the value for that key
val, ok := secret.Data[secretRef.Key]
if !ok {
return nil, fmt.Errorf("key '%s' not found in secret '%s'", secretRef.Key, secretRef.Name)
}
path, err := t.writeFile(dstPath, secretRef.Key, val)
if err != nil {
return nil, err
}
result = append(result, path)
} else {
// If no key is provided, write all values in the secret
for name, val := range secret.Data {
path, err := t.writeFile(dstPath, name, val)
if err != nil {
return nil, err
}
result = append(result, path)
}
}

sort.Strings(result)

return result, nil
}

func (t Values) writeFromDownwardAPI(dstPath string, valuesExtractor DownwardAPIValues) ([]string, error) {
var result []string

dataValues, err := valuesExtractor.AsYAMLs()
if err != nil {
return nil, err
}

for idx, content := range dataValues {
path, err := t.writeFile(dstPath, fmt.Sprintf("downwardapi_%d.yaml", idx), content)
if err != nil {
return nil, err
}
result = append(result, path)
}

return result, nil
}

func (t Values) writeFile(dstPath, subPath string, content []byte) (string, error) {
newPath, err := memdir.ScopedPath(dstPath, subPath)
if err != nil {
return "", err
}

err = os.WriteFile(newPath, content, 0600)
if err != nil {
return "", fmt.Errorf("Writing file '%s': %s", newPath, err)
return "", fmt.Errorf("writing file '%s': %s", newPath, err)
}

return newPath, nil
}

func (t Values) writeFromConfigMap(dstPath string,
configMapRef v1alpha1.AppTemplateValuesSourceRef) ([]string, error) {

configMap, err := t.coreClient.CoreV1().ConfigMaps(t.appContext.Namespace).Get(
context.Background(), configMapRef.Name, metav1.GetOptions{})
if err != nil {
Expand All @@ -155,15 +143,28 @@ func (t Values) writeFromConfigMap(dstPath string,

var result []string

for name, val := range configMap.Data {
path, err := t.writeFile(dstPath, name, []byte(val))
if configMapRef.Key != "" {
// If a key is provided, only write the value for that key
val, ok := configMap.Data[configMapRef.Key]
if !ok {
return nil, fmt.Errorf("key '%s' not found in config map '%s'", configMapRef.Key, configMapRef.Name)
}
path, err := t.writeFile(dstPath, configMapRef.Key, []byte(val))
if err != nil {
return nil, err
}
result = append(result, path)
} else {
// If no key is provided, write all values in the config map
for name, val := range configMap.Data {
path, err := t.writeFile(dstPath, name, []byte(val))
if err != nil {
return nil, err
}
result = append(result, path)
}
}

sort.Strings(result)

return result, nil
}

0 comments on commit 8708d7b

Please sign in to comment.