-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to accept function spec from catalogs
This commit modifies the logic to read function specs from catalog in addition to the annotations. This feature is currently enabled only with transformers. Signed-off-by: Varsha Prasad Narsing <varshaprasad96@gmail.com>
- Loading branch information
1 parent
ef7f06a
commit 4ccb1bf
Showing
9 changed files
with
328 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
// Copyright 2019 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package fnplugin | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"sigs.k8s.io/kustomize/api/provider" | ||
"sigs.k8s.io/kustomize/kyaml/fn/framework" | ||
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil" | ||
"sigs.k8s.io/kustomize/kyaml/yaml" | ||
) | ||
|
||
var factory = provider.NewDefaultDepProvider().GetResourceFactory() | ||
|
||
func Test_GetFunctionSpec(t *testing.T) { | ||
var tests = []struct { | ||
name string | ||
resource string | ||
catalogs []framework.Catalog | ||
expectedFn string | ||
missingFn bool | ||
}{ | ||
// fn annotation, no catalogs | ||
{ | ||
name: "fn from annotation", | ||
resource: ` | ||
apiVersion: example.com/v1 | ||
kind: Example | ||
metadata: | ||
name: test | ||
annotations: | ||
config.kubernetes.io/function: |- | ||
container: | ||
image: foo:v1.0.0 | ||
`, | ||
expectedFn: ` | ||
container: | ||
image: foo:v1.0.0 | ||
`, | ||
catalogs: []framework.Catalog{}, | ||
}, | ||
// from catalog, no annotation | ||
{ | ||
name: "fn from catalog", | ||
resource: ` | ||
apiVersion: example.com/v1 | ||
kind: Example | ||
metadata: | ||
name: test | ||
`, | ||
catalogs: []framework.Catalog{ | ||
{ | ||
Spec: framework.CatalogSpec{ | ||
KrmFunctions: []framework.KrmFunctionDefinitionSpec{ | ||
{ | ||
Group: "example.com", | ||
Names: framework.KRMFunctionNames{ | ||
Kind: "Example", | ||
}, | ||
Versions: []framework.KRMFunctionVersion{ | ||
{ | ||
Name: "v1", | ||
Runtime: runtimeutil.FunctionSpec{ | ||
Container: runtimeutil.ContainerSpec{ | ||
Image: "foo:v1.0.0", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedFn: ` | ||
container: | ||
image: foo:v1.0.0 | ||
`, | ||
}, | ||
{ | ||
// nil fn spec | ||
resource: ` | ||
apiVersion: example.com/v1 | ||
kind: Example | ||
metadata: | ||
name: test | ||
`, | ||
catalogs: []framework.Catalog{}, | ||
missingFn: true, | ||
}, | ||
// when both are provided, fn spec is taken from catalog. | ||
{ | ||
name: "accept catalog fn spec when both are provided", | ||
resource: ` | ||
apiVersion: example.com/v1 | ||
kind: Example | ||
metadata: | ||
name: test | ||
annotations: | ||
config.kubernetes.io/function: |- | ||
container: | ||
image: foo:v1.0.0 | ||
`, | ||
catalogs: []framework.Catalog{ | ||
{ | ||
Spec: framework.CatalogSpec{ | ||
KrmFunctions: []framework.KrmFunctionDefinitionSpec{ | ||
{ | ||
Group: "example.com", | ||
Names: framework.KRMFunctionNames{ | ||
Kind: "Example", | ||
}, | ||
Versions: []framework.KRMFunctionVersion{ | ||
{ | ||
Name: "v1", | ||
Runtime: runtimeutil.FunctionSpec{ | ||
Container: runtimeutil.ContainerSpec{ | ||
Image: "foocatalog:v1.0.0", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedFn: ` | ||
container: | ||
image: foocatalog:v1.0.0 | ||
`, | ||
}, | ||
} | ||
|
||
for i := range tests { | ||
tt := tests[i] | ||
t.Run(tt.name, func(t *testing.T) { | ||
resource, err := factory.FromBytes([]byte(tt.resource)) | ||
assert.NoError(t, err) | ||
fn, err := GetFunctionSpec(resource, tt.catalogs) | ||
if tt.missingFn { | ||
if err == nil && !assert.Nil(t, fn) { | ||
t.FailNow() | ||
} | ||
return | ||
} | ||
|
||
b, err := yaml.Marshal(fn) | ||
if !assert.NoError(t, err) { | ||
t.FailNow() | ||
} | ||
if !assert.Equal(t, | ||
strings.TrimSpace(tt.expectedFn), | ||
strings.TrimSpace(string(b))) { | ||
t.FailNow() | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.