Skip to content

Commit

Permalink
Add license header.
Browse files Browse the repository at this point in the history
Refactor to reduce duplication.

Signed-off-by: Kevin McDermott <[email protected]>
  • Loading branch information
bigkevmcd committed Oct 31, 2024
1 parent c8e95ca commit e5ab8e6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
16 changes: 16 additions & 0 deletions internal/server/cel.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2024 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package server

import (
Expand Down
28 changes: 9 additions & 19 deletions internal/server/receiver_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,12 @@ func (s *ReceiverServer) notifySingleResource(ctx context.Context, logger logr.L
return fmt.Errorf("unable to read %s '%s' error: %w", resource.Kind, objectKey, err)
}

if resourcePredicate != nil {
accept, err := resourcePredicate(resource)
return s.notifyResource(ctx, logger, resource, resourcePredicate)
}

func (s *ReceiverServer) notifyResource(ctx context.Context, logger logr.Logger, resource *metav1.PartialObjectMetadata, predicate resourcePredicate) error {
if predicate != nil {
accept, err := predicate(resource)
if err != nil {
return err
}
Expand Down Expand Up @@ -194,23 +198,9 @@ func (s *ReceiverServer) notifyDynamicResources(ctx context.Context, logger logr
return nil
}

for i, resource := range resources.Items {
if resourcePredicate != nil {
accept, err := resourcePredicate(&resource)
if err != nil {
return err
}
if !*accept {
logger.Info(fmt.Sprintf("resource '%s/%s.%s' NOT annotated because CEL expression returned false", resource.Kind, resource.Name, namespace))
continue
}
}

if err := s.annotate(ctx, &resources.Items[i]); err != nil {
return fmt.Errorf("failed to annotate resource: '%s/%s.%s': %w", resource.Kind, resource.Name, namespace, err)
} else {
logger.Info(fmt.Sprintf("resource '%s/%s.%s' annotated",
resource.Kind, resource.Name, namespace))
for i := range resources.Items {
if err := s.notifyResource(ctx, logger, &resources.Items[i], resourcePredicate); err != nil {
return err
}
}

Expand Down

0 comments on commit e5ab8e6

Please sign in to comment.