Skip to content

Commit f019b07

Browse files
authored
feat: only report allowed resources (#370)
* feat: only report allowed resources * fix
1 parent 7ae29b0 commit f019b07

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

event_reporter/reporter/application_event_reporter.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,10 @@ func (s *applicationEventReporter) processResource(
314314
reportedEntityParentApp *ReportedEntityParentApp,
315315
argoTrackingMetadata *ArgoTrackingMetadata,
316316
) error {
317+
if !isAllowedResource(rs) {
318+
return nil
319+
}
320+
317321
metricsEventType := metrics.MetricResourceEventType
318322
if utils.IsApp(rs) {
319323
metricsEventType = metrics.MetricChildAppEventType
@@ -335,6 +339,10 @@ func (s *applicationEventReporter) processResource(
335339
return nil
336340
}
337341

342+
if rs.Kind == "ConfigMap" && !isAllowedConfigMap(*actualState.Manifest) {
343+
return nil
344+
}
345+
338346
parentApplicationToReport, revisionMetadataToReport := s.getAppForResourceReporting(rs, ctx, logCtx, reportedEntityParentApp.app, reportedEntityParentApp.revisionsMetadata)
339347

340348
var originalAppRevisionMetadata *utils.AppSyncRevisionsMetadata = nil
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package reporter
2+
3+
import (
4+
"encoding/json"
5+
6+
appsv1 "k8s.io/api/apps/v1"
7+
corev1 "k8s.io/api/core/v1"
8+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
9+
10+
appv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
11+
)
12+
13+
type ResourceTypeKey struct {
14+
Group string
15+
Kind string
16+
}
17+
18+
var allowedResourceTypes = map[ResourceTypeKey]bool{
19+
// Kubernetes core resources
20+
{Group: appsv1.GroupName, Kind: "ReplicaSet"}: true,
21+
{Group: appsv1.GroupName, Kind: "Deployment"}: true,
22+
{Group: appsv1.GroupName, Kind: "StatefulSet"}: true,
23+
{Group: corev1.GroupName, Kind: "Service"}: true,
24+
{Group: corev1.GroupName, Kind: "ConfigMap"}: true,
25+
26+
// Argo CD resources
27+
{Group: "argoproj.io", Kind: "Application"}: true,
28+
{Group: "argoproj.io", Kind: "ApplicationSet"}: true,
29+
30+
// Argo Rollouts resources
31+
{Group: "argoproj.io", Kind: "Rollout"}: true,
32+
{Group: "argoproj.io", Kind: "AnalysisRun"}: true,
33+
34+
// Argo Workflows resources
35+
{Group: "argoproj.io", Kind: "Workflow"}: true,
36+
{Group: "argoproj.io", Kind: "WorkflowTemplate"}: true,
37+
{Group: "argoproj.io", Kind: "ClusterWorkflowTemplate"}: true,
38+
39+
// Argo Events resources
40+
{Group: "argoproj.io", Kind: "Sensor"}: true,
41+
{Group: "argoproj.io", Kind: "EventSource"}: true,
42+
43+
// Codefresh resources
44+
{Group: "codefresh.io", Kind: "Product"}: true,
45+
{Group: "codefresh.io", Kind: "PromotionFlow"}: true,
46+
{Group: "codefresh.io", Kind: "PromotionPolicy"}: true,
47+
{Group: "codefresh.io", Kind: "PromotionTemplate"}: true,
48+
{Group: "codefresh.io", Kind: "RestrictedGitSource"}: true,
49+
50+
// Bitnami resources
51+
{Group: "bitnami.com", Kind: "SealedSecret"}: true,
52+
}
53+
54+
const (
55+
CODEFRESH_IO_ENTITY = "codefresh_io_entity"
56+
CODEFRESH_CM_NAME = "codefresh-cm"
57+
)
58+
59+
func isAllowedResource(rs appv1.ResourceStatus) bool {
60+
gvk := rs.GroupVersionKind()
61+
resourceKey := ResourceTypeKey{
62+
Group: gvk.Group,
63+
Kind: gvk.Kind,
64+
}
65+
66+
return allowedResourceTypes[resourceKey]
67+
}
68+
69+
func isAllowedConfigMap(manifest string) bool {
70+
var u unstructured.Unstructured
71+
if err := json.Unmarshal([]byte(manifest), &u); err != nil {
72+
return false
73+
}
74+
75+
// Check if it's the codefresh-cm
76+
if u.GetName() == CODEFRESH_CM_NAME {
77+
return true
78+
}
79+
80+
// Check for the codefresh_io_entity label
81+
labels := u.GetLabels()
82+
_, hasCodefreshEntityLabel := labels[CODEFRESH_IO_ENTITY]
83+
return labels != nil && hasCodefreshEntityLabel
84+
}

0 commit comments

Comments
 (0)