Skip to content

Commit

Permalink
chore: speed up resolveResourceReferences (#625)
Browse files Browse the repository at this point in the history
* chore: speed up resolveResourceReferences

Signed-off-by: Michael Crenshaw <[email protected]>

* revert unnecessary changes

Signed-off-by: Michael Crenshaw <[email protected]>

---------

Signed-off-by: Michael Crenshaw <[email protected]>
  • Loading branch information
crenshaw-dev authored Sep 16, 2024
1 parent bd7681a commit 3d9aab3
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions pkg/cache/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package cache
import (
"encoding/json"
"fmt"
"regexp"
v1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"regexp"

"github.com/argoproj/gitops-engine/pkg/utils/kube"
)
Expand All @@ -25,15 +25,15 @@ func (c *clusterCache) resolveResourceReferences(un *unstructured.Unstructured)
switch {

// Special case for endpoint. Remove after https://github.com/kubernetes/kubernetes/issues/28483 is fixed
case gvk.Group == "" && gvk.Kind == kube.EndpointsKind && len(un.GetOwnerReferences()) == 0:
case gvk.Group == "" && gvk.Kind == kube.EndpointsKind && len(ownerRefs) == 0:
ownerRefs = append(ownerRefs, metav1.OwnerReference{
Name: un.GetName(),
Kind: kube.ServiceKind,
APIVersion: "v1",
})

// Special case for Operator Lifecycle Manager ClusterServiceVersion:
case un.GroupVersionKind().Group == "operators.coreos.com" && un.GetKind() == "ClusterServiceVersion":
case gvk.Group == "operators.coreos.com" && gvk.Kind == "ClusterServiceVersion":
if un.GetAnnotations()["olm.operatorGroup"] != "" {
ownerRefs = append(ownerRefs, metav1.OwnerReference{
Name: un.GetAnnotations()["olm.operatorGroup"],
Expand All @@ -43,12 +43,12 @@ func (c *clusterCache) resolveResourceReferences(un *unstructured.Unstructured)
}

// Edge case: consider auto-created service account tokens as a child of service account objects
case un.GetKind() == kube.SecretKind && un.GroupVersionKind().Group == "":
case gvk.Kind == kube.SecretKind && gvk.Group == "":
if yes, ref := isServiceAccountTokenSecret(un); yes {
ownerRefs = append(ownerRefs, ref)
}

case (un.GroupVersionKind().Group == "apps" || un.GroupVersionKind().Group == "extensions") && un.GetKind() == kube.StatefulSetKind:
case (gvk.Group == "apps" || gvk.Group == "extensions") && gvk.Kind == kube.StatefulSetKind:
if refs, err := isStatefulSetChild(un); err != nil {
c.log.Error(err, fmt.Sprintf("Failed to extract StatefulSet %s/%s PVC references", un.GetNamespace(), un.GetName()))
} else {
Expand All @@ -74,7 +74,7 @@ func isStatefulSetChild(un *unstructured.Unstructured) (func(kube.ResourceKey) b
return func(key kube.ResourceKey) bool {
if key.Kind == kube.PersistentVolumeClaimKind && key.GroupKind().Group == "" {
for _, templ := range templates {
if match, _ := regexp.MatchString(fmt.Sprintf(`%s-%s-\d+$`, templ.Name, un.GetName()), key.Name); match {
if match, _ := regexp.MatchString(fmt.Sprintf(`%s-%s-\d+$`, templ.Name, un.GetName()), key.Name); match {
return true
}
}
Expand Down

0 comments on commit 3d9aab3

Please sign in to comment.