Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple creation events #1172

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package managedclusterinfo
import (
"context"
"fmt"
"reflect"

kessel "github.com/project-kessel/inventory-api/api/kessel/inventory/v1beta1/resources"
clusterinfov1beta1 "github.com/stolostron/cluster-lifecycle-api/clusterinfo/v1beta1"
Expand Down Expand Up @@ -48,6 +49,7 @@ func (r *ManagedClusterInfoInventorySyncer) Reconcile(ctx context.Context, req c
&kessel.CreateK8SClusterRequest{K8SCluster: k8sCluster}); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to create k8sCluster %v: %w", resp, err)
}
return ctrl.Result{}, nil
}
}

Expand Down Expand Up @@ -85,7 +87,10 @@ func (r *ManagedClusterInfoInventorySyncer) Reconcile(ctx context.Context, req c
func AddManagedClusterInfoInventorySyncer(mgr ctrl.Manager, inventoryRequester transport.Requester) error {
clusterInfoPredicate := predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
return e.ObjectOld.GetResourceVersion() < e.ObjectNew.GetResourceVersion()
return !reflect.DeepEqual(e.ObjectNew.(*clusterinfov1beta1.ManagedClusterInfo).Status,
e.ObjectOld.(*clusterinfov1beta1.ManagedClusterInfo).Status) ||
!reflect.DeepEqual(e.ObjectNew.GetLabels(), e.ObjectOld.GetLabels()) ||
e.ObjectNew.GetGeneration() != e.ObjectOld.GetGeneration()
},
CreateFunc: func(e event.CreateEvent) bool {
// add the annotation to identify the request is creating
Expand Down
72 changes: 25 additions & 47 deletions agent/pkg/controllers/inventory/policy/policy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package policy
import (
"context"
"fmt"
"reflect"

kesselv1betarelations "github.com/project-kessel/inventory-api/api/kessel/inventory/v1beta1/relationships"
kessel "github.com/project-kessel/inventory-api/api/kessel/inventory/v1beta1/resources"
Expand Down Expand Up @@ -38,7 +39,10 @@ func AddPolicyInventorySyncer(mgr ctrl.Manager, inventoryRequester transport.Req
if _, exist := e.ObjectOld.GetLabels()[constants.PolicyEventRootPolicyNameLabelKey]; exist {
return false
}
return e.ObjectOld.GetResourceVersion() < e.ObjectNew.GetResourceVersion()
return !reflect.DeepEqual(e.ObjectNew.(*policiesv1.Policy).Status,
e.ObjectOld.(*policiesv1.Policy).Status) ||
!reflect.DeepEqual(e.ObjectNew.GetLabels(), e.ObjectOld.GetLabels()) ||
e.ObjectNew.GetGeneration() != e.ObjectOld.GetGeneration()
},
CreateFunc: func(e event.CreateEvent) bool {
// do not trigger the create event for the replicated policies
Expand Down Expand Up @@ -83,13 +87,17 @@ func (p *PolicyInventorySyncer) Reconcile(ctx context.Context, req ctrl.Request)
}
return ctrl.Result{}, err
}

k8sPolicy := generateK8SPolicy(policy, p.reporterInstanceId)

annotations := policy.GetAnnotations()
if annotations != nil {
if _, ok := annotations[constants.InventoryResourceCreatingAnnotationlKey]; ok {
if resp, err := p.requester.GetHttpClient().PolicyServiceClient.CreateK8SPolicy(
ctx, createK8SClusterPolicy(*policy, p.reporterInstanceId)); err != nil {
ctx, &kessel.CreateK8SPolicyRequest{K8SPolicy: k8sPolicy}); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to create k8s-policy %v: %w", resp, err)
}
return ctrl.Result{}, nil
}
}

Expand Down Expand Up @@ -126,7 +134,7 @@ func (p *PolicyInventorySyncer) Reconcile(ctx context.Context, req ctrl.Request)
}

if resp, err := p.requester.GetHttpClient().PolicyServiceClient.UpdateK8SPolicy(
ctx, updateK8SClusterPolicy(*policy, p.reporterInstanceId)); err != nil {
ctx, &kessel.UpdateK8SPolicyRequest{K8SPolicy: k8sPolicy}); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to update k8s-policy %v: %w", resp, err)
}
for _, compliancePerClusterStatus := range policy.Status.Status {
Expand Down Expand Up @@ -185,58 +193,28 @@ func deleteK8SPolicyIsPropagatedToK8SCluster(subjectId, objectId, reporterInstan
}
}

func createK8SClusterPolicy(policy policiesv1.Policy, reporterInstanceId string) *kessel.CreateK8SPolicyRequest {
func generateK8SPolicy(policy *policiesv1.Policy, reporterInstanceId string) *kessel.K8SPolicy {
kesselLabels := []*kessel.ResourceLabel{}
for key, value := range policy.Labels {
kesselLabels = append(kesselLabels, &kessel.ResourceLabel{
Key: key,
Value: value,
})
}
return &kessel.CreateK8SPolicyRequest{
K8SPolicy: &kessel.K8SPolicy{
Metadata: &kessel.Metadata{
ResourceType: "k8s-policy",
Labels: kesselLabels,
},
ReporterData: &kessel.ReporterData{
ReporterType: kessel.ReporterData_ACM,
ReporterInstanceId: reporterInstanceId,
ReporterVersion: configs.GetMCHVersion(),
LocalResourceId: policy.Namespace + "/" + policy.Name,
},
ResourceData: &kessel.K8SPolicyDetail{
Disabled: policy.Spec.Disabled,
Severity: kessel.K8SPolicyDetail_MEDIUM, // need to update
},
return &kessel.K8SPolicy{
Metadata: &kessel.Metadata{
ResourceType: "k8s-policy",
Labels: kesselLabels,
},
}
}

func updateK8SClusterPolicy(policy policiesv1.Policy, reporterInstanceId string) *kessel.UpdateK8SPolicyRequest {
kesselLabels := []*kessel.ResourceLabel{}
for key, value := range policy.Labels {
kesselLabels = append(kesselLabels, &kessel.ResourceLabel{
Key: key,
Value: value,
})
}
return &kessel.UpdateK8SPolicyRequest{
K8SPolicy: &kessel.K8SPolicy{
Metadata: &kessel.Metadata{
ResourceType: "k8s-policy",
Labels: kesselLabels,
},
ReporterData: &kessel.ReporterData{
ReporterType: kessel.ReporterData_ACM,
ReporterInstanceId: reporterInstanceId,
ReporterVersion: configs.GetMCHVersion(),
LocalResourceId: policy.Namespace + "/" + policy.Name,
},
ResourceData: &kessel.K8SPolicyDetail{
Disabled: policy.Spec.Disabled,
Severity: kessel.K8SPolicyDetail_MEDIUM, // need to update
},
ReporterData: &kessel.ReporterData{
ReporterType: kessel.ReporterData_ACM,
ReporterInstanceId: reporterInstanceId,
ReporterVersion: configs.GetMCHVersion(),
LocalResourceId: policy.Namespace + "/" + policy.Name,
},
ResourceData: &kessel.K8SPolicyDetail{
Disabled: policy.Spec.Disabled,
Severity: kessel.K8SPolicyDetail_MEDIUM, // need to update
},
}
}
Expand Down
Loading