1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 07:57:07 +00:00
kyverno/cmd/cli/kubectl-kyverno/processor/generate.go

141 lines
4.6 KiB
Go
Raw Normal View History

package processor
import (
"fmt"
"io"
"strings"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
kyvernov1beta1 "github.com/kyverno/kyverno/api/kyverno/v1beta1"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/log"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/store"
fix: Provide kind list hints to the fake dynamic client. (#9036) * fix: Provide kind list hints to the fake dynamic client. If one uses the `cloneList` option of `generate` without this, a panic occurs. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Added test for `cloneList`. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * fix: ttl cleanup not working with cluster wide resources (#9060) Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Fix Helm chart to not error when replicas defined (#9066) Fixes #8941 Signed-off-by: Trey Dockendorf <tdockendorf@osc.edu> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * fix: add nodeSelector to the reports cleanup helm hook (#9065) Signed-off-by: Mariam Fahmy <mariam.fahmy@nirmata.com> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * optimize JSON context processing using in-memory maps (#8322) * optimize JSON context processing using in memory maps Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix excessive logs Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix mutate resource diff Signed-off-by: Jim Bugwadia <jim@nirmata.com> * uncomment tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * copy resource, as it can be modified Signed-off-by: Jim Bugwadia <jim@nirmata.com> * clear prior resource to prevent mutating original Signed-off-by: Jim Bugwadia <jim@nirmata.com> * linter fix Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix ImageInfo to unstructured conversion Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix custom image extractors Signed-off-by: Jim Bugwadia <jim@nirmata.com> * do not update mutated resource in JSON context Signed-off-by: Jim Bugwadia <jim@nirmata.com> * address review comments Signed-off-by: Jim Bugwadia <jim@nirmata.com> --------- Signed-off-by: Jim Bugwadia <jim@nirmata.com> Signed-off-by: shuting <shuting@nirmata.com> Co-authored-by: Vishal Choudhary <sendtovishalchoudhary@gmail.com> Co-authored-by: shuting <shuting@nirmata.com> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Ran `gci` to silence a lint warning. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Added a log message when an invalid or incomplete `cloneList` kind is supplied. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> --------- Signed-off-by: Anton Chernev <anton.chernev@gmail.com> Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Signed-off-by: Trey Dockendorf <tdockendorf@osc.edu> Signed-off-by: Mariam Fahmy <mariam.fahmy@nirmata.com> Signed-off-by: Jim Bugwadia <jim@nirmata.com> Signed-off-by: shuting <shuting@nirmata.com> Co-authored-by: Anton Chernev <a-anchernov@expediagroup.com> Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Co-authored-by: treydock <tdockendorf@osc.edu> Co-authored-by: Mariam Fahmy <mariam.fahmy@nirmata.com> Co-authored-by: Jim Bugwadia <jim@nirmata.com> Co-authored-by: Vishal Choudhary <sendtovishalchoudhary@gmail.com> Co-authored-by: shuting <shuting@nirmata.com>
2023-12-07 06:03:27 +00:00
"github.com/kyverno/kyverno/pkg/autogen"
"github.com/kyverno/kyverno/pkg/background/generate"
"github.com/kyverno/kyverno/pkg/clients/dclient"
"github.com/kyverno/kyverno/pkg/config"
"github.com/kyverno/kyverno/pkg/engine"
"github.com/kyverno/kyverno/pkg/engine/adapters"
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
"github.com/kyverno/kyverno/pkg/engine/jmespath"
"github.com/kyverno/kyverno/pkg/imageverifycache"
fix: Provide kind list hints to the fake dynamic client. (#9036) * fix: Provide kind list hints to the fake dynamic client. If one uses the `cloneList` option of `generate` without this, a panic occurs. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Added test for `cloneList`. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * fix: ttl cleanup not working with cluster wide resources (#9060) Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Fix Helm chart to not error when replicas defined (#9066) Fixes #8941 Signed-off-by: Trey Dockendorf <tdockendorf@osc.edu> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * fix: add nodeSelector to the reports cleanup helm hook (#9065) Signed-off-by: Mariam Fahmy <mariam.fahmy@nirmata.com> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * optimize JSON context processing using in-memory maps (#8322) * optimize JSON context processing using in memory maps Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix excessive logs Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix mutate resource diff Signed-off-by: Jim Bugwadia <jim@nirmata.com> * uncomment tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * copy resource, as it can be modified Signed-off-by: Jim Bugwadia <jim@nirmata.com> * clear prior resource to prevent mutating original Signed-off-by: Jim Bugwadia <jim@nirmata.com> * linter fix Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix ImageInfo to unstructured conversion Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix custom image extractors Signed-off-by: Jim Bugwadia <jim@nirmata.com> * do not update mutated resource in JSON context Signed-off-by: Jim Bugwadia <jim@nirmata.com> * address review comments Signed-off-by: Jim Bugwadia <jim@nirmata.com> --------- Signed-off-by: Jim Bugwadia <jim@nirmata.com> Signed-off-by: shuting <shuting@nirmata.com> Co-authored-by: Vishal Choudhary <sendtovishalchoudhary@gmail.com> Co-authored-by: shuting <shuting@nirmata.com> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Ran `gci` to silence a lint warning. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Added a log message when an invalid or incomplete `cloneList` kind is supplied. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> --------- Signed-off-by: Anton Chernev <anton.chernev@gmail.com> Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Signed-off-by: Trey Dockendorf <tdockendorf@osc.edu> Signed-off-by: Mariam Fahmy <mariam.fahmy@nirmata.com> Signed-off-by: Jim Bugwadia <jim@nirmata.com> Signed-off-by: shuting <shuting@nirmata.com> Co-authored-by: Anton Chernev <a-anchernov@expediagroup.com> Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Co-authored-by: treydock <tdockendorf@osc.edu> Co-authored-by: Mariam Fahmy <mariam.fahmy@nirmata.com> Co-authored-by: Jim Bugwadia <jim@nirmata.com> Co-authored-by: Vishal Choudhary <sendtovishalchoudhary@gmail.com> Co-authored-by: shuting <shuting@nirmata.com>
2023-12-07 06:03:27 +00:00
kubeutils "github.com/kyverno/kyverno/pkg/utils/kube"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
)
func handleGeneratePolicy(out io.Writer, store *store.Store, generateResponse *engineapi.EngineResponse, policyContext engine.PolicyContext, ruleToCloneSourceResource map[string]string) ([]engineapi.RuleResponse, error) {
newResource := policyContext.NewResource()
objects := []runtime.Object{&newResource}
for _, rule := range generateResponse.PolicyResponse.Rules {
if path, ok := ruleToCloneSourceResource[rule.Name()]; ok {
resourceBytes, err := resource.GetFileBytes(path)
if err != nil {
fmt.Fprintf(out, "failed to get resource bytes\n")
} else {
r, err := resource.GetUnstructuredResources(resourceBytes)
if err != nil {
fmt.Fprintf(out, "failed to convert resource bytes to unstructured format\n")
}
for _, res := range r {
objects = append(objects, res)
}
}
}
}
fix: Provide kind list hints to the fake dynamic client. (#9036) * fix: Provide kind list hints to the fake dynamic client. If one uses the `cloneList` option of `generate` without this, a panic occurs. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Added test for `cloneList`. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * fix: ttl cleanup not working with cluster wide resources (#9060) Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Fix Helm chart to not error when replicas defined (#9066) Fixes #8941 Signed-off-by: Trey Dockendorf <tdockendorf@osc.edu> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * fix: add nodeSelector to the reports cleanup helm hook (#9065) Signed-off-by: Mariam Fahmy <mariam.fahmy@nirmata.com> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * optimize JSON context processing using in-memory maps (#8322) * optimize JSON context processing using in memory maps Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix excessive logs Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix mutate resource diff Signed-off-by: Jim Bugwadia <jim@nirmata.com> * uncomment tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * copy resource, as it can be modified Signed-off-by: Jim Bugwadia <jim@nirmata.com> * clear prior resource to prevent mutating original Signed-off-by: Jim Bugwadia <jim@nirmata.com> * linter fix Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix ImageInfo to unstructured conversion Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix custom image extractors Signed-off-by: Jim Bugwadia <jim@nirmata.com> * do not update mutated resource in JSON context Signed-off-by: Jim Bugwadia <jim@nirmata.com> * address review comments Signed-off-by: Jim Bugwadia <jim@nirmata.com> --------- Signed-off-by: Jim Bugwadia <jim@nirmata.com> Signed-off-by: shuting <shuting@nirmata.com> Co-authored-by: Vishal Choudhary <sendtovishalchoudhary@gmail.com> Co-authored-by: shuting <shuting@nirmata.com> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Ran `gci` to silence a lint warning. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Added a log message when an invalid or incomplete `cloneList` kind is supplied. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> --------- Signed-off-by: Anton Chernev <anton.chernev@gmail.com> Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Signed-off-by: Trey Dockendorf <tdockendorf@osc.edu> Signed-off-by: Mariam Fahmy <mariam.fahmy@nirmata.com> Signed-off-by: Jim Bugwadia <jim@nirmata.com> Signed-off-by: shuting <shuting@nirmata.com> Co-authored-by: Anton Chernev <a-anchernov@expediagroup.com> Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Co-authored-by: treydock <tdockendorf@osc.edu> Co-authored-by: Mariam Fahmy <mariam.fahmy@nirmata.com> Co-authored-by: Jim Bugwadia <jim@nirmata.com> Co-authored-by: Vishal Choudhary <sendtovishalchoudhary@gmail.com> Co-authored-by: shuting <shuting@nirmata.com>
2023-12-07 06:03:27 +00:00
listKinds := map[schema.GroupVersionResource]string{}
// Collect items in a potential cloneList to provide list kinds to the fake dynamic client.
for _, rule := range autogen.ComputeRules(policyContext.Policy(), "") {
fix: Provide kind list hints to the fake dynamic client. (#9036) * fix: Provide kind list hints to the fake dynamic client. If one uses the `cloneList` option of `generate` without this, a panic occurs. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Added test for `cloneList`. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * fix: ttl cleanup not working with cluster wide resources (#9060) Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Fix Helm chart to not error when replicas defined (#9066) Fixes #8941 Signed-off-by: Trey Dockendorf <tdockendorf@osc.edu> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * fix: add nodeSelector to the reports cleanup helm hook (#9065) Signed-off-by: Mariam Fahmy <mariam.fahmy@nirmata.com> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * optimize JSON context processing using in-memory maps (#8322) * optimize JSON context processing using in memory maps Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix excessive logs Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix mutate resource diff Signed-off-by: Jim Bugwadia <jim@nirmata.com> * uncomment tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * copy resource, as it can be modified Signed-off-by: Jim Bugwadia <jim@nirmata.com> * clear prior resource to prevent mutating original Signed-off-by: Jim Bugwadia <jim@nirmata.com> * linter fix Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix ImageInfo to unstructured conversion Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix custom image extractors Signed-off-by: Jim Bugwadia <jim@nirmata.com> * do not update mutated resource in JSON context Signed-off-by: Jim Bugwadia <jim@nirmata.com> * address review comments Signed-off-by: Jim Bugwadia <jim@nirmata.com> --------- Signed-off-by: Jim Bugwadia <jim@nirmata.com> Signed-off-by: shuting <shuting@nirmata.com> Co-authored-by: Vishal Choudhary <sendtovishalchoudhary@gmail.com> Co-authored-by: shuting <shuting@nirmata.com> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Ran `gci` to silence a lint warning. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Added a log message when an invalid or incomplete `cloneList` kind is supplied. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> --------- Signed-off-by: Anton Chernev <anton.chernev@gmail.com> Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Signed-off-by: Trey Dockendorf <tdockendorf@osc.edu> Signed-off-by: Mariam Fahmy <mariam.fahmy@nirmata.com> Signed-off-by: Jim Bugwadia <jim@nirmata.com> Signed-off-by: shuting <shuting@nirmata.com> Co-authored-by: Anton Chernev <a-anchernov@expediagroup.com> Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Co-authored-by: treydock <tdockendorf@osc.edu> Co-authored-by: Mariam Fahmy <mariam.fahmy@nirmata.com> Co-authored-by: Jim Bugwadia <jim@nirmata.com> Co-authored-by: Vishal Choudhary <sendtovishalchoudhary@gmail.com> Co-authored-by: shuting <shuting@nirmata.com>
2023-12-07 06:03:27 +00:00
if !rule.HasGenerate() || len(rule.Generation.CloneList.Kinds) == 0 {
continue
}
for _, kind := range rule.Generation.CloneList.Kinds {
apiVersion, kind := kubeutils.GetKindFromGVK(kind)
if apiVersion == "" || kind == "" {
continue
}
gv, err := schema.ParseGroupVersion(apiVersion)
if err != nil {
fmt.Fprintf(out, "failed to parse group and version from clone list kind %s: %v\n", apiVersion, err)
continue
}
listKinds[schema.GroupVersionResource{
Group: gv.Group,
Version: gv.Version,
Resource: strings.ToLower(kind) + "s",
}] = kind + "List"
}
}
c, err := initializeMockController(out, store, listKinds, objects)
if err != nil {
fmt.Fprintln(out, "error at controller")
return nil, err
}
gr := kyvernov1beta1.UpdateRequest{
Spec: kyvernov1beta1.UpdateRequestSpec{
Type: kyvernov1beta1.Generate,
Policy: generateResponse.Policy().GetName(),
Resource: kyvernov1.ResourceSpec{
Kind: generateResponse.Resource.GetKind(),
Namespace: generateResponse.Resource.GetNamespace(),
Name: generateResponse.Resource.GetName(),
APIVersion: generateResponse.Resource.GetAPIVersion(),
},
},
}
var newRuleResponse []engineapi.RuleResponse
for _, rule := range generateResponse.PolicyResponse.Rules {
genResource, err := c.ApplyGeneratePolicy(log.Log.V(2), &policyContext, gr, []string{rule.Name()})
if err != nil {
return nil, err
}
if genResource != nil {
unstrGenResource, err := c.GetUnstrResource(genResource[0])
if err != nil {
return nil, err
}
newRuleResponse = append(newRuleResponse, *rule.WithGeneratedResource(*unstrGenResource))
}
}
return newRuleResponse, nil
}
func initializeMockController(out io.Writer, s *store.Store, gvrToListKind map[schema.GroupVersionResource]string, objects []runtime.Object) (*generate.GenerateController, error) {
fix: Provide kind list hints to the fake dynamic client. (#9036) * fix: Provide kind list hints to the fake dynamic client. If one uses the `cloneList` option of `generate` without this, a panic occurs. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Added test for `cloneList`. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * fix: ttl cleanup not working with cluster wide resources (#9060) Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Fix Helm chart to not error when replicas defined (#9066) Fixes #8941 Signed-off-by: Trey Dockendorf <tdockendorf@osc.edu> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * fix: add nodeSelector to the reports cleanup helm hook (#9065) Signed-off-by: Mariam Fahmy <mariam.fahmy@nirmata.com> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * optimize JSON context processing using in-memory maps (#8322) * optimize JSON context processing using in memory maps Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix excessive logs Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix mutate resource diff Signed-off-by: Jim Bugwadia <jim@nirmata.com> * uncomment tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * copy resource, as it can be modified Signed-off-by: Jim Bugwadia <jim@nirmata.com> * clear prior resource to prevent mutating original Signed-off-by: Jim Bugwadia <jim@nirmata.com> * linter fix Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix ImageInfo to unstructured conversion Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix custom image extractors Signed-off-by: Jim Bugwadia <jim@nirmata.com> * do not update mutated resource in JSON context Signed-off-by: Jim Bugwadia <jim@nirmata.com> * address review comments Signed-off-by: Jim Bugwadia <jim@nirmata.com> --------- Signed-off-by: Jim Bugwadia <jim@nirmata.com> Signed-off-by: shuting <shuting@nirmata.com> Co-authored-by: Vishal Choudhary <sendtovishalchoudhary@gmail.com> Co-authored-by: shuting <shuting@nirmata.com> Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Ran `gci` to silence a lint warning. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> * Added a log message when an invalid or incomplete `cloneList` kind is supplied. Signed-off-by: Anton Chernev <anton.chernev@gmail.com> --------- Signed-off-by: Anton Chernev <anton.chernev@gmail.com> Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Signed-off-by: Trey Dockendorf <tdockendorf@osc.edu> Signed-off-by: Mariam Fahmy <mariam.fahmy@nirmata.com> Signed-off-by: Jim Bugwadia <jim@nirmata.com> Signed-off-by: shuting <shuting@nirmata.com> Co-authored-by: Anton Chernev <a-anchernov@expediagroup.com> Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Co-authored-by: treydock <tdockendorf@osc.edu> Co-authored-by: Mariam Fahmy <mariam.fahmy@nirmata.com> Co-authored-by: Jim Bugwadia <jim@nirmata.com> Co-authored-by: Vishal Choudhary <sendtovishalchoudhary@gmail.com> Co-authored-by: shuting <shuting@nirmata.com>
2023-12-07 06:03:27 +00:00
client, err := dclient.NewFakeClient(runtime.NewScheme(), gvrToListKind, objects...)
if err != nil {
fmt.Fprintf(out, "Failed to mock dynamic client")
return nil, err
}
gvrs := sets.New[schema.GroupVersionResource]()
for _, object := range objects {
gvk := object.GetObjectKind().GroupVersionKind()
gvrs.Insert(gvk.GroupVersion().WithResource(strings.ToLower(gvk.Kind) + "s"))
}
client.SetDiscovery(dclient.NewFakeDiscoveryClient(gvrs.UnsortedList()))
cfg := config.NewDefaultConfiguration(false)
c := generate.NewGenerateControllerWithOnlyClient(client, engine.NewEngine(
cfg,
config.NewDefaultMetricsConfiguration(),
jmespath.New(cfg),
adapters.Client(client),
nil,
imageverifycache.DisabledImageVerifyCache(),
store.ContextLoaderFactory(s, nil),
nil,
))
return c, nil
}