Skip to content

Commit

Permalink
Add TCOLogsPolicies CRD
Browse files Browse the repository at this point in the history
  • Loading branch information
assafad1 committed Jan 8, 2025
1 parent e69eb3a commit 275a4a9
Show file tree
Hide file tree
Showing 13 changed files with 728 additions and 3 deletions.
9 changes: 9 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,13 @@ resources:
kind: Group
path: github.com/coralogix/coralogix-operator/api/coralogix/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: coralogix.com
group: coralogix
kind: TCOLogsPolicies
path: github.com/coralogix/coralogix-operator/api/coralogix/v1alpha1
version: v1alpha1
version: "3"
197 changes: 197 additions & 0 deletions api/coralogix/v1alpha1/tcologspolicies_types.go
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{})
}
156 changes: 156 additions & 0 deletions api/coralogix/v1alpha1/zz_generated.deepcopy.go

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

8 changes: 8 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "Group")
os.Exit(1)
}
if err = (&coralogixcontrollers.TCOLogsPoliciesReconciler{
TCOClient: sdkClientSet.TCOPolicies(),
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "TCOLogsPolicies")
os.Exit(1)
}

if prometheusRuleController {
if err = (&controllers.AlertmanagerConfigReconciler{
Expand Down
Loading

0 comments on commit 275a4a9

Please sign in to comment.