Skip to content

Commit

Permalink
Handle situations when operators were preinstalled
Browse files Browse the repository at this point in the history
The general plan is to continue adding `handle*` functions for the other
resources that an OperatorPolicy needs to examine. Each handle function
will update the policy's status (with conditions and relatedObjects),
and possibly emit compliance events. This may cause more compliance
events "than usual" compared to other controllers, but I think the
separation of concerns will help each function be more maintainable.

My hope is that some of the `*Cond` and `*Obj` functions in the status
section can be reused in the future handlers. There was already overlap
between the Subscription and OperatorGroup, so this seemed reasonable.

Refs:
 - https://issues.redhat.com/browse/ACM-9283

Signed-off-by: Justin Kulikauskas <jkulikau@redhat.com>
  • Loading branch information
JustinKuli committed Jan 25, 2024
1 parent f2aaa44 commit 2d800eb
Show file tree
Hide file tree
Showing 13 changed files with 1,282 additions and 290 deletions.
17 changes: 17 additions & 0 deletions api/v1/configurationpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtime "k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// A custom type is required since there is no way to have a kubebuilder marker
Expand Down Expand Up @@ -315,6 +316,22 @@ type ObjectResource struct {
Metadata ObjectMetadata `json:"metadata,omitempty"`
}

func ObjectResourceFromObj(obj client.Object) ObjectResource {
name := obj.GetName()
if name == "" {
name = "*"
}

return ObjectResource{
Kind: obj.GetObjectKind().GroupVersionKind().Kind,
APIVersion: obj.GetObjectKind().GroupVersionKind().GroupVersion().String(),
Metadata: ObjectMetadata{
Name: name,
Namespace: obj.GetNamespace(),
},
}
}

// ObjectMetadata contains the resource metadata for an object being processed by the policy
type ObjectMetadata struct {
// Name of the referent. More info:
Expand Down
25 changes: 25 additions & 0 deletions api/v1beta1/operatorpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,31 @@ type OperatorPolicyStatus struct {
RelatedObjects []policyv1.RelatedObject `json:"relatedObjects"`
}

func (status OperatorPolicyStatus) RelatedObjsOfKind(kind string) map[int]policyv1.RelatedObject {
objs := make(map[int]policyv1.RelatedObject)

for i, related := range status.RelatedObjects {
if related.Object.Kind == kind {
objs[i] = related
}
}

return objs
}

// Searches the conditions of the policy, and returns the index and condition matching the
// given condition Type. It will return -1 as the index if no condition of the specified
// Type is found.
func (status OperatorPolicyStatus) GetCondition(condType string) (int, metav1.Condition) {
for i, cond := range status.Conditions {
if cond.Type == condType {
return i, cond
}
}

return -1, metav1.Condition{}
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

Expand Down
Loading

0 comments on commit 2d800eb

Please sign in to comment.