|
| 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