Skip to content

Commit

Permalink
Fix to get the resource key from the annoation
Browse files Browse the repository at this point in the history
Signed-off-by: Yoshiki Fujikane <[email protected]>
  • Loading branch information
ffjlabo committed Apr 4, 2024
1 parent 7b1103d commit 820a255
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
10 changes: 7 additions & 3 deletions pkg/app/piped/livestatestore/kubernetes/reflector.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,11 @@ func (r *reflector) start(_ context.Context) error {

func (r *reflector) onObjectAdd(obj interface{}) {
u := obj.(*unstructured.Unstructured)
key := provider.MakeResourceKey(u)
key := provider.GetResourceKeyFromActualResource(u)
if anoKey := u.GetAnnotations()[provider.LabelResourceKey]; anoKey != "" {
key, _ = provider.DecodeResourceKey(anoKey)

}

Check warning on line 256 in pkg/app/piped/livestatestore/kubernetes/reflector.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/livestatestore/kubernetes/reflector.go#L252-L256

Added lines #L252 - L256 were not covered by tests

// Ignore all predefined ones.
if _, ok := ignoreResourceKeys[key.String()]; ok {
Expand Down Expand Up @@ -283,7 +287,7 @@ func (r *reflector) onObjectUpdate(oldObj, obj interface{}) {
oldU := oldObj.(*unstructured.Unstructured)

// Ignore all predefined ones.
key := provider.MakeResourceKey(u)
key := provider.GetResourceKeyFromActualResource(u)

Check warning on line 290 in pkg/app/piped/livestatestore/kubernetes/reflector.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/livestatestore/kubernetes/reflector.go#L290

Added line #L290 was not covered by tests
if _, ok := ignoreResourceKeys[key.String()]; ok {
kubernetesmetrics.IncResourceEventsCounter(
kubernetesmetrics.LabelEventUpdate,
Expand Down Expand Up @@ -312,7 +316,7 @@ func (r *reflector) onObjectUpdate(oldObj, obj interface{}) {

func (r *reflector) onObjectDelete(obj interface{}) {
u := obj.(*unstructured.Unstructured)
key := provider.MakeResourceKey(u)
key := provider.GetResourceKeyFromActualResource(u)

Check warning on line 319 in pkg/app/piped/livestatestore/kubernetes/reflector.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/livestatestore/kubernetes/reflector.go#L319

Added line #L319 was not covered by tests

// Ignore all predefined ones.
if _, ok := ignoreResourceKeys[key.String()]; ok {
Expand Down
6 changes: 3 additions & 3 deletions pkg/app/piped/livestatestore/kubernetes/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (s *store) initialize() {
}

// Add the missing resource into the dependedResources of the app.
key := provider.MakeResourceKey(an.resource)
key := provider.GetResourceKeyFromActualResource(an.resource)

Check warning on line 76 in pkg/app/piped/livestatestore/kubernetes/store.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/livestatestore/kubernetes/store.go#L76

Added line #L76 was not covered by tests

// Ignore in case appNodes with appID not existed in store.
if s.apps[appID] == nil {
Expand Down Expand Up @@ -103,7 +103,7 @@ func (s *store) initialize() {
func (s *store) addResource(obj *unstructured.Unstructured, appID string) {
var (
uid = string(obj.GetUID())
key = provider.MakeResourceKey(obj)
key = provider.GetResourceKeyFromActualResource(obj)

Check warning on line 106 in pkg/app/piped/livestatestore/kubernetes/store.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/livestatestore/kubernetes/store.go#L106

Added line #L106 was not covered by tests
owners = obj.GetOwnerReferences()
now = time.Now()
)
Expand Down Expand Up @@ -189,7 +189,7 @@ func (s *store) onDeleteResource(obj *unstructured.Unstructured) {
var (
uid = string(obj.GetUID())
appID = obj.GetAnnotations()[provider.LabelApplication]
key = provider.MakeResourceKey(obj)
key = provider.GetResourceKeyFromActualResource(obj)

Check warning on line 192 in pkg/app/piped/livestatestore/kubernetes/store.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/livestatestore/kubernetes/store.go#L192

Added line #L192 was not covered by tests
owners = obj.GetOwnerReferences()
now = time.Now()
)
Expand Down
15 changes: 15 additions & 0 deletions pkg/app/piped/platformprovider/kubernetes/resourcekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,21 @@ func (k ResourceKey) IsEqualWithIgnoringNamespace(a ResourceKey) bool {
return true
}

// GetResourceKeyFromActualResource extracts the ResourceKey from the given resource object.
// If the ResourceKey is not found in the annotations, it will create a new one.
func GetResourceKeyFromActualResource(obj *unstructured.Unstructured) ResourceKey {
annotation, ok := obj.GetAnnotations()[LabelResourceKey]
if !ok {
return MakeResourceKey(obj)
}

Check warning on line 255 in pkg/app/piped/platformprovider/kubernetes/resourcekey.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/platformprovider/kubernetes/resourcekey.go#L251-L255

Added lines #L251 - L255 were not covered by tests

key, err := DecodeResourceKey(annotation)
if err != nil {
return MakeResourceKey(obj)
}
return key

Check warning on line 261 in pkg/app/piped/platformprovider/kubernetes/resourcekey.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/platformprovider/kubernetes/resourcekey.go#L257-L261

Added lines #L257 - L261 were not covered by tests
}

func MakeResourceKey(obj *unstructured.Unstructured) ResourceKey {
k := ResourceKey{
APIVersion: obj.GetAPIVersion(),
Expand Down

0 comments on commit 820a255

Please sign in to comment.