-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: peefy <[email protected]>
- Loading branch information
Showing
9 changed files
with
168 additions
and
82 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
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
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,50 @@ | ||
package process | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn" | ||
"kcl-lang.io/krm-kcl/pkg/config" | ||
"kcl-lang.io/krm-kcl/pkg/kube" | ||
) | ||
|
||
func init() { | ||
// Set the fast eval mode for KCL | ||
os.Setenv("KCL_FAST_EVAL", "1") | ||
} | ||
|
||
// Process is a function that takes a pointer to a ResourceList and processes | ||
// it using the KCL function. It returns a boolean indicating whether the | ||
// processing was successful, and an error (if any). | ||
func Process(resourceList *fn.ResourceList) (bool, error) { | ||
resourceListYAML, err := resourceList.ToYAML() | ||
if err != nil { | ||
return false, err | ||
} | ||
res, err := kube.ParseResourceList(resourceListYAML) | ||
if err != nil { | ||
return false, err | ||
} | ||
err = func() error { | ||
r := &config.KCLRun{} | ||
if err := r.Config(res.FunctionConfig); err != nil { | ||
return err | ||
} | ||
return r.TransformResourceList(res) | ||
}() | ||
if err != nil { | ||
resourceList.Results = []*fn.Result{ | ||
{ | ||
Message: err.Error(), | ||
Severity: fn.Error, | ||
}, | ||
} | ||
return false, nil | ||
} | ||
processedItems, err := fn.ParseKubeObjects([]byte(res.Items.MustString())) | ||
if err != nil { | ||
return false, err | ||
} | ||
resourceList.Items = processedItems | ||
return true, nil | ||
} |
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,76 @@ | ||
package process | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn" | ||
"sigs.k8s.io/kustomize/kyaml/yaml" | ||
) | ||
|
||
func TestProcess(t *testing.T) { | ||
p := fn.ResourceListProcessorFunc(Process) | ||
input := `kind: ResourceList | ||
items: | ||
- apiVersion: apps/v1 | ||
kind: Deployment | ||
spec: | ||
replicas: '2' | ||
- kind: Service | ||
functionConfig: | ||
apiVersion: krm.kcl.dev/v1alpha1 | ||
kind: KCLRun | ||
metadata: | ||
name: conditionally-add-annotations | ||
spec: | ||
params: | ||
replicas: "5" | ||
source: | | ||
params = option("params") | ||
replicas = params.replicas | ||
setReplicas = lambda items, replicas { | ||
[item | {if item.kind == "Deployment": spec.replicas = replicas} for item in items] | ||
} | ||
items = setReplicas(option("items"), replicas) | ||
` | ||
expected := `apiVersion: config.kubernetes.io/v1 | ||
kind: ResourceList | ||
items: | ||
- kind: Service | ||
- apiVersion: apps/v1 | ||
kind: Deployment | ||
spec: | ||
replicas: '5' | ||
functionConfig: | ||
apiVersion: krm.kcl.dev/v1alpha1 | ||
kind: KCLRun | ||
metadata: | ||
name: conditionally-add-annotations | ||
spec: | ||
params: | ||
replicas: "5" | ||
source: | | ||
params = option("params") | ||
replicas = params.replicas | ||
setReplicas = lambda items, replicas { | ||
[item | {if item.kind == "Deployment": spec.replicas = replicas} for item in items] | ||
} | ||
items = setReplicas(option("items"), replicas) | ||
` | ||
output, err := fn.Run(p, []byte(input)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
expectedYaml, err := yaml.Parse(expected) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
outputYaml, err := yaml.Parse(string(output)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
expectedYamlString := expectedYaml.MustString() | ||
outputYamlString := outputYaml.MustString() | ||
if expectedYamlString != outputYamlString { | ||
t.Errorf("test failed, expected %s got %s", expectedYamlString, outputYamlString) | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -17,7 +17,7 @@ metadata: | |
labels: | ||
app: nginx | ||
spec: | ||
replicas: 6 | ||
replicas: 2 | ||
selector: | ||
matchLabels: | ||
app: nginx | ||
|