-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
728 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
// Copyright 2024 Coralogix Ltd. | ||
// | ||
// 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 v1alpha1 | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"google.golang.org/protobuf/types/known/wrapperspb" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
cxsdk "github.com/coralogix/coralogix-management-sdk/go" | ||
|
||
utils "github.com/coralogix/coralogix-operator/api" | ||
) | ||
|
||
// TCOLogsPoliciesSpec defines the desired state of TCOLogsPolicies. | ||
type TCOLogsPoliciesSpec struct { | ||
Policies []TCOLogsPolicy `json:"policies"` | ||
} | ||
|
||
type TCOLogsPolicy struct { | ||
Name string `json:"name"` | ||
|
||
// +optional | ||
Description *string `json:"description,omitempty"` | ||
|
||
// +optional | ||
//+kubebuilder:default=true | ||
Enabled bool `json:"enabled"` | ||
|
||
// +kubebuilder:validation:Enum=block;high;medium;low | ||
Priority string `json:"priority"` | ||
|
||
Severities []TCOLogsPolicySeverity `json:"severities"` | ||
|
||
// +optional | ||
ArchiveRetentionID *string `json:"archiveRetentionId,omitempty"` | ||
|
||
// +optional | ||
Applications *TCOLogsPolicyRule `json:"applications,omitempty"` | ||
|
||
// +optional | ||
Subsystems *TCOLogsPolicyRule `json:"subsystems,omitempty"` | ||
} | ||
|
||
// +kubebuilder:validation:Enum=info;warning;critical;error;debug;verbose | ||
type TCOLogsPolicySeverity string | ||
|
||
type TCOLogsPolicyRule struct { | ||
Names []string `json:"names"` | ||
|
||
// +kubebuilder:validation:Enum=is;is_not;starts_with;includes | ||
RuleType string `json:"ruleType"` | ||
} | ||
|
||
func (s *TCOLogsPoliciesSpec) ExtractOverwriteLogPoliciesRequest() (*cxsdk.AtomicOverwriteLogPoliciesRequest, error) { | ||
var policies []*cxsdk.CreateLogPolicyRequest | ||
var errs error | ||
|
||
for _, policy := range s.Policies { | ||
policyReq, err := policy.ExtractCreateLogPolicyRequest() | ||
if err != nil { | ||
errs = errors.Join(errs, err) | ||
} | ||
policies = append(policies, policyReq) | ||
} | ||
|
||
if errs != nil { | ||
return nil, errs | ||
} | ||
|
||
return &cxsdk.AtomicOverwriteLogPoliciesRequest{Policies: policies}, nil | ||
} | ||
|
||
func (p *TCOLogsPolicy) ExtractCreateLogPolicyRequest() (*cxsdk.CreateLogPolicyRequest, error) { | ||
var errs error | ||
priority, err := expandTCOLogsPolicyPriority(p.Priority) | ||
if err != nil { | ||
errs = errors.Join(errs, err) | ||
} | ||
|
||
applicationRule, err := expandTCOLogsPolicyRule(p.Applications) | ||
if err != nil { | ||
errs = errors.Join(errs, err) | ||
} | ||
|
||
subsystemRule, err := expandTCOLogsPolicyRule(p.Subsystems) | ||
if err != nil { | ||
errs = errors.Join(errs, err) | ||
} | ||
|
||
severities, err := expandTCOLogsPolicySeverities(p.Severities) | ||
if err != nil { | ||
errs = errors.Join(errs, err) | ||
} | ||
|
||
if errs != nil { | ||
return nil, errs | ||
} | ||
|
||
req := &cxsdk.CreateLogPolicyRequest{ | ||
Policy: &cxsdk.CreateGenericPolicyRequest{ | ||
Name: wrapperspb.String(p.Name), | ||
Description: utils.StringPointerToWrapperspbString(p.Description), | ||
Priority: priority, | ||
ApplicationRule: applicationRule, | ||
SubsystemRule: subsystemRule, | ||
ArchiveRetention: &cxsdk.ArchiveRetention{ | ||
Id: utils.StringPointerToWrapperspbString(p.ArchiveRetentionID), | ||
}, | ||
}, | ||
LogRules: &cxsdk.TCOLogRules{ | ||
Severities: severities, | ||
}, | ||
} | ||
|
||
return req, nil | ||
} | ||
|
||
func expandTCOLogsPolicyPriority(priority string) (cxsdk.TCOPolicyPriority, error) { | ||
priorityValue, ok := cxsdk.LogPolicyPriorityValueLookup["PRIORITY_TYPE_"+strings.ToUpper(priority)] | ||
if !ok { | ||
return 0, fmt.Errorf("invalid priority for TCOLogsPolicy: %s", priority) | ||
} | ||
return cxsdk.TCOPolicyPriority(priorityValue), nil | ||
} | ||
|
||
func expandTCOLogsPolicyRule(rule *TCOLogsPolicyRule) (*cxsdk.TCOPolicyRule, error) { | ||
if rule == nil { | ||
return nil, nil | ||
} | ||
|
||
ruleType, ok := cxsdk.LogPolicyRuleTypeValueLookup["RULE_TYPE_ID_"+strings.ToUpper(rule.RuleType)] | ||
if !ok { | ||
return nil, fmt.Errorf("invalid rule type for TCOLogsPolicyRule: %s", rule.RuleType) | ||
} | ||
|
||
return &cxsdk.TCOPolicyRule{ | ||
Name: wrapperspb.String(strings.Join(rule.Names, ",")), | ||
RuleTypeId: cxsdk.TCOPolicyRuleTypeID(ruleType), | ||
}, nil | ||
} | ||
|
||
func expandTCOLogsPolicySeverities(severities []TCOLogsPolicySeverity) ([]cxsdk.TCOPolicySeverity, error) { | ||
var result []cxsdk.TCOPolicySeverity | ||
for _, severity := range severities { | ||
severityValue, ok := cxsdk.LogPolicySeverityValueLookup["SEVERITY_"+strings.ToUpper(string(severity))] | ||
if !ok { | ||
return nil, fmt.Errorf("invalid severity for TCOLogsPolicySeverity: %s", severity) | ||
} | ||
result = append(result, cxsdk.TCOPolicySeverity(severityValue)) | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
// TCOLogsPoliciesStatus defines the observed state of TCOLogsPolicies. | ||
type TCOLogsPoliciesStatus struct{} | ||
|
||
// +kubebuilder:object:root=true | ||
// +kubebuilder:subresource:status | ||
|
||
// TCOLogsPolicies is the Schema for the tcologspolicies API. | ||
type TCOLogsPolicies struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ObjectMeta `json:"metadata,omitempty"` | ||
|
||
Spec TCOLogsPoliciesSpec `json:"spec,omitempty"` | ||
Status TCOLogsPoliciesStatus `json:"status,omitempty"` | ||
} | ||
|
||
// +kubebuilder:object:root=true | ||
|
||
// TCOLogsPoliciesList contains a list of TCOLogsPolicies. | ||
type TCOLogsPoliciesList struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ListMeta `json:"metadata,omitempty"` | ||
Items []TCOLogsPolicies `json:"items"` | ||
} | ||
|
||
func init() { | ||
SchemeBuilder.Register(&TCOLogsPolicies{}, &TCOLogsPoliciesList{}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.