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

MustNotHave mode for OperatorPolicy #222

Merged
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
86 changes: 71 additions & 15 deletions api/v1beta1/operatorpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package v1beta1

import (
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

Expand All @@ -15,7 +17,6 @@ import (
type StatusConfigAction string

// RemovalAction : Keep, Delete, or DeleteIfUnused
// +kubebuilder:validation:Enum=Keep;Delete;DeleteIfUnused
type RemovalAction string

const (
Expand All @@ -36,20 +37,75 @@ const (
DeleteIfUnused RemovalAction = "DeleteIfUnused"
)

// RemovalBehavior defines resource behavior when policy is removed
func (ra RemovalAction) IsKeep() bool {
return strings.EqualFold(string(ra), string(Keep))
}

func (ra RemovalAction) IsDelete() bool {
return strings.EqualFold(string(ra), string(Delete))
}

func (ra RemovalAction) IsDeleteIfUnused() bool {
return strings.EqualFold(string(ra), string(DeleteIfUnused))
}

type RemovalBehavior struct {
// Kind OperatorGroup
//+kubebuilder:default=DeleteIfUnused
//+kubebuilder:validation:Enum=Keep;Delete;DeleteIfUnused
// Specifies whether to delete the OperatorGroup; defaults to 'DeleteIfUnused' which
// will only delete the OperatorGroup if there is not another Subscription using it.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So "Delete" means delete even though another sub uses this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it means "Delete, regardless of any consequences". Kind of like a mustnothave configuration policy.

OperatorGroups RemovalAction `json:"operatorGroups,omitempty"`
// Kind Subscription

//+kubebuilder:default=Delete
//+kubebuilder:validation:Enum=Keep;Delete
// Specifies whether to delete the Subscription; defaults to 'Delete'
Subscriptions RemovalAction `json:"subscriptions,omitempty"`
// Kind ClusterServiceVersion

//+kubebuilder:default=Delete
//+kubebuilder:validation:Enum=Keep;Delete
// Specifies whether to delete the ClusterServiceVersion; defaults to 'Delete'
Copy link
Contributor

@yiraeChristineKim yiraeChristineKim Apr 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add when this happens? maybe top of RemovalBehavior

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the CRD description for removalBehavior, it says "RemovalBehavior defines what resources will be removed by enforced mustnothave policies ..."

Is that what you meant? Or what's another place to put it?

CSVs RemovalAction `json:"clusterServiceVersions,omitempty"`
// Kind InstallPlan

//+kubebuilder:default=Keep
//+kubebuilder:validation:Enum=Keep;Delete
// Specifies whether to delete any InstallPlans associated with the operator; defaults
// to 'Keep' because those objects are only for history
InstallPlan RemovalAction `json:"installPlans,omitempty"`
// Kind CustomResourceDefinitions

//+kubebuilder:default=Keep
//+kubebuilder:validation:Enum=Keep;Delete
// Specifies whether to delete any CustomResourceDefinitions associated with the operator;
// defaults to 'Keep' because deleting them should be done deliberately
CRDs RemovalAction `json:"customResourceDefinitions,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these CRDs refer to CRDs created by OLM as a prerequisite for installing the desired operator?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are the CRDs created by OLM, as defined in the PackageManifest/ClusterServiceVersion for the operator.

// Kind APIServiceDefinitions
APIServiceDefinitions RemovalAction `json:"apiServiceDefinitions,omitempty"`
}

// ApplyDefaults ensures that unset fields in a RemovalBehavior behave as if they were
// set to the default values. In a cluster, kubernetes API validation should ensure that
// there are no unset values, and should apply the default values itself.
func (rb RemovalBehavior) ApplyDefaults() RemovalBehavior {
withDefaults := *rb.DeepCopy()

if withDefaults.OperatorGroups == "" {
withDefaults.OperatorGroups = DeleteIfUnused
}

if withDefaults.Subscriptions == "" {
withDefaults.Subscriptions = Delete
}

if withDefaults.CSVs == "" {
withDefaults.CSVs = Delete
}

if withDefaults.InstallPlan == "" {
withDefaults.InstallPlan = Keep
}

if withDefaults.CRDs == "" {
withDefaults.CRDs = Keep
}

return withDefaults
}

// StatusConfig defines how resource statuses affect the OperatorPolicy status and compliance
Expand All @@ -64,7 +120,7 @@ type StatusConfig struct {
type OperatorPolicySpec struct {
Severity policyv1.Severity `json:"severity,omitempty"` // low, medium, high
RemediationAction policyv1.RemediationAction `json:"remediationAction,omitempty"` // inform, enforce
ComplianceType policyv1.ComplianceType `json:"complianceType"` // musthave
ComplianceType policyv1.ComplianceType `json:"complianceType"` // musthave, mustnothave

// Include the name, namespace, and any `spec` fields for the OperatorGroup.
// For more info, see `kubectl explain operatorgroup.spec` or
Expand All @@ -84,11 +140,11 @@ type OperatorPolicySpec struct {
// in 'inform' mode, and which installPlans are approved when in 'enforce' mode
Versions []policyv1.NonEmptyString `json:"versions,omitempty"`

// FUTURE
//nolint:dupword
// RemovalBehavior RemovalBehavior `json:"removalBehavior,omitempty"`
//nolint:dupword
// StatusConfig StatusConfig `json:"statusConfig,omitempty"`
//+kubebuilder:default={}
// RemovalBehavior defines what resources will be removed by enforced mustnothave policies.
// When in inform mode, any resources that would be deleted if the policy was enforced will
// be causes for NonCompliance, but resources that would be kept will be considered Compliant.
RemovalBehavior RemovalBehavior `json:"removalBehavior,omitempty"`
}

// OperatorPolicyStatus defines the observed state of OperatorPolicy
Expand Down
1 change: 1 addition & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading