Skip to content

Commit

Permalink
Add Scope CRD (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
assafad1 authored Jan 2, 2025
1 parent 34486fb commit b9c3e24
Show file tree
Hide file tree
Showing 15 changed files with 1,025 additions and 0 deletions.
9 changes: 9 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,13 @@ resources:
kind: CustomRole
path: github.com/coralogix/coralogix-operator/api/coralogix/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: coralogix.com
group: coralogix
kind: Scope
path: github.com/coralogix/coralogix-operator/api/coralogix/v1alpha1
version: v1alpha1
version: "3"
120 changes: 120 additions & 0 deletions api/coralogix/v1alpha1/scope_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// 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 (
"fmt"
"strings"

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

cxsdk "github.com/coralogix/coralogix-management-sdk/go"
)

// ScopeSpec defines the desired state of Scope.
type ScopeSpec struct {
Name string `json:"name"`

// +optional
Description *string `json:"description,omitempty"`

Filters []ScopeFilter `json:"filters"`

// +kubebuilder:validation:Enum=<v1>true;<v1>false
DefaultExpression string `json:"defaultExpression"`
}

// ScopeFilter defines a filter for a scope
type ScopeFilter struct {
// +kubebuilder:validation:Enum=logs;spans;unspecified
EntityType string `json:"entityType"`

Expression string `json:"expression"`
}

func (s *ScopeSpec) ExtractCreateScopeRequest() (*cxsdk.CreateScopeRequest, error) {
filters, err := s.ExtractScopeFilters()
if err != nil {
return nil, err
}

return &cxsdk.CreateScopeRequest{
DisplayName: s.Name,
Description: s.Description,
Filters: filters,
DefaultExpression: s.DefaultExpression,
}, nil
}

func (s *ScopeSpec) ExtractUpdateScopeRequest(id string) (*cxsdk.UpdateScopeRequest, error) {
filters, err := s.ExtractScopeFilters()
if err != nil {
return nil, err
}

return &cxsdk.UpdateScopeRequest{
Id: id,
DisplayName: s.Name,
Description: s.Description,
Filters: filters,
DefaultExpression: s.DefaultExpression,
}, nil
}

func (s *ScopeSpec) ExtractScopeFilters() ([]*cxsdk.ScopeFilter, error) {
var filters []*cxsdk.ScopeFilter
for _, f := range s.Filters {
entityType, ok := cxsdk.EntityTypeValueLookup["ENTITY_TYPE_"+strings.ToUpper(f.EntityType)]
if !ok {
return nil, fmt.Errorf("invalid entity type: %s", f.EntityType)
}
filters = append(filters, &cxsdk.ScopeFilter{
EntityType: cxsdk.EntityType(entityType),
Expression: f.Expression,
})
}

return filters, nil
}

// ScopeStatus defines the observed state of Scope.
type ScopeStatus struct {
ID *string `json:"id"`
}

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

// Scope is the Schema for the scopes API.
type Scope struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ScopeSpec `json:"spec,omitempty"`
Status ScopeStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// ScopeList contains a list of Scope.
type ScopeList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Scope `json:"items"`
}

func init() {
SchemeBuilder.Register(&Scope{}, &ScopeList{})
}
119 changes: 119 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.

26 changes: 26 additions & 0 deletions charts/coralogix-operator/templates/cluster_role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,32 @@ rules:
- get
- patch
- update
- apiGroups:
- coralogix.com
resources:
- scopes
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- coralogix.com
resources:
- scopes/finalizers
verbs:
- update
- apiGroups:
- coralogix.com
resources:
- scopes/status
verbs:
- get
- patch
- update
- apiGroups:
- monitoring.coreos.com
resources:
Expand Down
85 changes: 85 additions & 0 deletions charts/coralogix-operator/templates/crds/coralogix.com_scopes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.15.0
name: scopes.coralogix.com
spec:
group: coralogix.com
names:
kind: Scope
listKind: ScopeList
plural: scopes
singular: scope
scope: Namespaced
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
description: Scope is the Schema for the scopes API.
properties:
apiVersion:
description: |-
APIVersion defines the versioned schema of this representation of an object.
Servers should convert recognized schemas to the latest internal value, and
may reject unrecognized values.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
type: string
kind:
description: |-
Kind is a string value representing the REST resource this object represents.
Servers may infer this from the endpoint the client submits requests to.
Cannot be updated.
In CamelCase.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
type: string
metadata:
type: object
spec:
description: ScopeSpec defines the desired state of Scope.
properties:
defaultExpression:
enum:
- <v1>true
- <v1>false
type: string
description:
type: string
filters:
items:
description: ScopeFilter defines a filter for a scope
properties:
entityType:
enum:
- logs
- spans
- unspecified
type: string
expression:
type: string
required:
- entityType
- expression
type: object
type: array
name:
type: string
required:
- defaultExpression
- filters
- name
type: object
status:
description: ScopeStatus defines the observed state of Scope.
properties:
id:
type: string
required:
- id
type: object
type: object
served: true
storage: true
subresources:
status: {}
8 changes: 8 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "CustomRole")
os.Exit(1)
}
if err = (&coralogixcontrollers.ScopeReconciler{
ScopesClient: sdkClientSet.Scopes(),
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Scope")
os.Exit(1)
}

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

0 comments on commit b9c3e24

Please sign in to comment.