Skip to content

Commit

Permalink
small improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
evalsocket committed Sep 16, 2020
1 parent 4d3b310 commit 6089f56
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion pkg/kyverno/report/command.go
Expand Up @@ -25,7 +25,7 @@ func Command() *cobra.Command {
return err
},
}
cmd.AddCommand(HelmCommand())
cmd.AddCommand(AppCommand())
cmd.AddCommand(NamespaceCommand())
cmd.AddCommand(ClusterCommand())
cmd.AddCommand(AllReportsCommand())
Expand Down
29 changes: 14 additions & 15 deletions pkg/kyverno/report/common.go
Expand Up @@ -38,7 +38,7 @@ import (
)

const (
Helm string = "App"
App string = "App"
Namespace string = "Namespace"
Cluster string = "Cluster"
All string = "All"
Expand Down Expand Up @@ -150,7 +150,7 @@ func backgroundScan(n, scope, policychange string, wg *sync.WaitGroup, restConfi
// key uid
resourceMap := map[string]map[string]unstructured.Unstructured{}
resourceMap[Cluster] = make(map[string]unstructured.Unstructured)
resourceMap[Helm] = make(map[string]unstructured.Unstructured)
resourceMap[App] = make(map[string]unstructured.Unstructured)
resourceMap[Namespace] = make(map[string]unstructured.Unstructured)
for _, p := range cpolicies {
for _, rule := range p.Spec.Rules {
Expand All @@ -173,7 +173,7 @@ func backgroundScan(n, scope, policychange string, wg *sync.WaitGroup, restConfi
_, okChart := labels["app"]
_, okRelease := labels["release"]
if okChart && okRelease {
policy.MergeResources(resourceMap[Helm], rMap)
policy.MergeResources(resourceMap[App], rMap)
} else if r.GetNamespace() != "" {
policy.MergeResources(resourceMap[Namespace], rMap)
}
Expand All @@ -190,22 +190,22 @@ func backgroundScan(n, scope, policychange string, wg *sync.WaitGroup, restConfi
case Cluster:
resourceMap[Cluster] = policy.ExcludePod(resourceMap[Cluster], log.Log)
delete(resourceMap,Namespace)
delete(resourceMap,Helm)
delete(resourceMap,App)
break
case Namespace:
resourceMap[Namespace] = policy.ExcludePod(resourceMap[Namespace], log.Log)
delete(resourceMap,Cluster)
delete(resourceMap,Helm)
delete(resourceMap,App)
break
case Helm:
resourceMap[Helm] = policy.ExcludePod(resourceMap[Helm], log.Log)
case App:
resourceMap[App] = policy.ExcludePod(resourceMap[App], log.Log)
delete(resourceMap,Namespace)
delete(resourceMap,Cluster)
break
case All:
resourceMap[Cluster] = policy.ExcludePod(resourceMap[Cluster], log.Log)
resourceMap[Namespace] = policy.ExcludePod(resourceMap[Namespace], log.Log)
resourceMap[Helm] = policy.ExcludePod(resourceMap[Helm], log.Log)
resourceMap[App] = policy.ExcludePod(resourceMap[App], log.Log)
}
}
results := make(map[string][]policyreportv1alpha1.PolicyReportResult)
Expand Down Expand Up @@ -238,7 +238,7 @@ func createReport(kclient *kyvernoclient.Clientset, name, namespace string, resu
if len(str) == 1 {
scope = Cluster
} else if strings.Contains(name, "policyreport-helm-") {
scope = Helm
scope = App
} else {
scope = Cluster
}
Expand Down Expand Up @@ -314,7 +314,7 @@ func createResults(policyContext engine.PolicyContext, key string, results map[s

for _, v := range pv {
var appname string
if key == Helm {
if key == App {
labels := policyContext.NewResource.GetLabels()
_, okChart := labels["app"]
_, okRelease := labels["release"]
Expand Down Expand Up @@ -381,8 +381,8 @@ func configmapScan(scope string, wg *sync.WaitGroup, restConfig *rest.Config, lo
if err := json.Unmarshal([]byte(job.Data[Cluster]), &response); err != nil {
lgr.Error(err, "Error in json marshal of namespace data")
}
} else if scope == Helm {
if err := json.Unmarshal([]byte(job.Data[Helm]), &response); err != nil {
} else if scope == App {
if err := json.Unmarshal([]byte(job.Data[App]), &response); err != nil {
lgr.Error(err, "Error in json marshal of namespace data")
}
} else if scope == Namespace {
Expand All @@ -393,7 +393,7 @@ func configmapScan(scope string, wg *sync.WaitGroup, restConfig *rest.Config, lo
if err := json.Unmarshal([]byte(job.Data[Cluster]), &response); err != nil {
lgr.Error(err, "Error in json marshal of namespace data")
}
if err := json.Unmarshal([]byte(job.Data[Helm]), &response); err != nil {
if err := json.Unmarshal([]byte(job.Data[App]), &response); err != nil {
lgr.Error(err, "Error in json marshal of namespace data")
}
if err := json.Unmarshal([]byte(job.Data[Namespace]), &response); err != nil {
Expand Down Expand Up @@ -426,7 +426,7 @@ func configmapScan(scope string, wg *sync.WaitGroup, restConfig *rest.Config, lo
// Increase Count
if k == Cluster {
appname = fmt.Sprintf("clusterpolicyreport")
} else if k == Helm {
} else if k == App {
resource, err := dClient.GetResource(v.Resource.GetAPIVersion(), v.Resource.GetKind(), v.Resource.GetNamespace(), v.Resource.GetName())
if err != nil {
lgr.Error(err, "failed to get resource")
Expand All @@ -447,7 +447,6 @@ func configmapScan(scope string, wg *sync.WaitGroup, restConfig *rest.Config, lo

}
}
fmt.Println(results)
for k := range results {
if k != "" {
continue
Expand Down
8 changes: 4 additions & 4 deletions pkg/kyverno/report/helm.go
Expand Up @@ -14,7 +14,7 @@ import (
log "sigs.k8s.io/controller-runtime/pkg/log"
)

func HelmCommand() *cobra.Command {
func AppCommand() *cobra.Command {
kubernetesConfig := genericclioptions.NewConfigFlags(true)
var mode, policy, namespace string
cmd := &cobra.Command{
Expand All @@ -40,7 +40,7 @@ func HelmCommand() *cobra.Command {
if mode == "cli" {
if namespace != "" {
wg.Add(1)
go backgroundScan(namespace, Helm, policy, &wg, restConfig, logger)
go backgroundScan(namespace, App, policy, &wg, restConfig, logger)
} else {
ns, err := kubeClient.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
Expand All @@ -49,12 +49,12 @@ func HelmCommand() *cobra.Command {
}
wg.Add(len(ns.Items))
for _, n := range ns.Items {
go backgroundScan(n.GetName(), Helm, policy, &wg, restConfig, logger)
go backgroundScan(n.GetName(), App, policy, &wg, restConfig, logger)
}
}
} else {
wg.Add(1)
go configmapScan(Helm, &wg, restConfig, logger)
go configmapScan(App, &wg, restConfig, logger)
}
wg.Wait()
os.Exit(0)
Expand Down
25 changes: 13 additions & 12 deletions pkg/policyreport/generator.go
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/go-logr/logr"
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
report "github.com/nirmata/kyverno/pkg/kyverno/report"
policyreportclient "github.com/nirmata/kyverno/pkg/client/clientset/versioned"
policyreportv1alpha1 "github.com/nirmata/kyverno/pkg/client/clientset/versioned/typed/policyreport/v1alpha1"
policyreportinformer "github.com/nirmata/kyverno/pkg/client/informers/externalversions/policyreport/v1alpha1"
Expand Down Expand Up @@ -112,7 +113,7 @@ func (i Info) toKey() string {
// make the struct hashable

type PVEvent struct {
Helm map[string][]Info
App map[string][]Info
Namespace map[string][]Info
Cluster map[string][]Info
}
Expand Down Expand Up @@ -143,7 +144,7 @@ func NewPRGenerator(client *policyreportclient.Clientset,
policyStatusListener: policyStatus,
configmap: nil,
inMemoryConfigMap: &PVEvent{
Helm: make(map[string][]Info),
App: make(map[string][]Info),
Namespace: make(map[string][]Info),
Cluster: make(map[string][]Info),
},
Expand Down Expand Up @@ -187,13 +188,13 @@ func (gen *Generator) Run(workers int, stopCh <-chan struct{}) {
err := gen.createConfigmap()
scops := []string{}
if len(gen.inMemoryConfigMap.Namespace) > 0 {
scops = append(scops, "Namespace")
scops = append(scops, report.Namespace)
}
if len(gen.inMemoryConfigMap.Helm) > 0 {
scops = append(scops, "Helm")
if len(gen.inMemoryConfigMap.App) > 0 {
scops = append(scops, report.App)
}
if len(gen.inMemoryConfigMap.Cluster["cluster"]) > 0 {
scops = append(scops, "Cluster")
scops = append(scops, report.Cluster)
}
gen.job.Add(jobs.JobInfo{
JobType: "CONFIGMAP",
Expand All @@ -203,7 +204,7 @@ func (gen *Generator) Run(workers int, stopCh <-chan struct{}) {
gen.log.Error(err, "configmap error")
}
gen.inMemoryConfigMap = &PVEvent{
Helm: make(map[string][]Info),
App: make(map[string][]Info),
Namespace: make(map[string][]Info),
Cluster: make(map[string][]Info),
}
Expand Down Expand Up @@ -297,12 +298,12 @@ func (gen *Generator) createConfigmap() error {
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(configmap.UnstructuredContent(), &cm); err != nil {
return err
}
rawData, _ := json.Marshal(gen.inMemoryConfigMap.Helm)
cm.Data["Helm"] = string(rawData)
rawData, _ := json.Marshal(gen.inMemoryConfigMap.App)
cm.Data[report.App] = string(rawData)
rawData, _ = json.Marshal(gen.inMemoryConfigMap.Cluster)
cm.Data["Cluster"] = string(rawData)
cm.Data[report.Cluster] = string(rawData)
rawData, _ = json.Marshal(gen.inMemoryConfigMap.Namespace)
cm.Data["Namespace"] = string(rawData)
cm.Data[report.Namespace] = string(rawData)

_, err = gen.dclient.UpdateResource("", "ConfigMap", config.KubePolicyNamespace, cm, false)
if err != nil {
Expand All @@ -326,7 +327,7 @@ func (gen *Generator) syncHandler(info Info) error {
_, okChart := labels["app"]
_, okRelease := labels["release"]
if okChart && okRelease {
gen.inMemoryConfigMap.Helm[info.Resource.GetNamespace()] = append(gen.inMemoryConfigMap.Helm[info.Resource.GetNamespace()], info)
gen.inMemoryConfigMap.App[info.Resource.GetNamespace()] = append(gen.inMemoryConfigMap.App[info.Resource.GetNamespace()], info)
return nil
} else if info.Resource.GetNamespace() == "" {
// cluster scope resource generate a clusterpolicy violation
Expand Down

0 comments on commit 6089f56

Please sign in to comment.