|
| 1 | +// Copyright 2024 Coralogix Ltd. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package v1alpha1 |
| 16 | + |
| 17 | +import ( |
| 18 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 19 | + |
| 20 | + cxsdk "github.com/coralogix/coralogix-management-sdk/go" |
| 21 | +) |
| 22 | + |
| 23 | +// Added in version v1.0.0 |
| 24 | +// ArchiveLogsTargetSpec defines the desired state of a Coralogix archive logs target. |
| 25 | +// +kubebuilder:validation:XValidation:rule="has(self.s3Target) != has(self.ibmCosTarget)",message="Exactly one of s3Target or ibmCosTarget must be specified" |
| 26 | +type ArchiveLogsTargetSpec struct { |
| 27 | + // The S3 target configuration. |
| 28 | + // +optional |
| 29 | + S3Target *S3Target `json:"s3Target,omitempty"` |
| 30 | + // The IBM COS target configuration. |
| 31 | + // +optional |
| 32 | + IbmCosTarget *IbmCosTarget `json:"ibmCosTarget,omitempty"` |
| 33 | +} |
| 34 | + |
| 35 | +type S3Target struct { |
| 36 | + // The region of the S3 bucket. |
| 37 | + Region string `json:"region,omitempty"` |
| 38 | + Bucket string `json:"bucketName,omitempty"` |
| 39 | +} |
| 40 | + |
| 41 | +type IbmCosTarget struct { |
| 42 | + // BucketCrn is the CRN of the IBM COS bucket. |
| 43 | + BucketCrn string `json:"bucketCrn,omitempty"` |
| 44 | + // Endpoint is the endpoint URL for the IBM COS service. |
| 45 | + Endpoint string `json:"endpoint,omitempty"` |
| 46 | + // ServiceCrn is the CRN of the service instance. |
| 47 | + // +optional |
| 48 | + ServiceCrn *string `json:"serviceCrn,omitempty"` |
| 49 | + // BucketType defines the type of the bucket. |
| 50 | + // +kubebuilder:validation:Enum=UNSPECIFIED;EXTERNAL;INTERNAL |
| 51 | + // +optional |
| 52 | + BucketType *string `json:"bucketType,omitempty"` |
| 53 | +} |
| 54 | + |
| 55 | +type ArchiveLogsTargetStatus struct { |
| 56 | + // ID is the identifier of the archive logs target. |
| 57 | + ID *string `json:"id,omitempty"` // The ID of the archive logs target, if applicable. |
| 58 | + // +optional |
| 59 | + Conditions []metav1.Condition `json:"conditions,omitempty"` |
| 60 | +} |
| 61 | + |
| 62 | +func (s *ArchiveLogsTargetSpec) ExtractSetTargetRequest(isTargetActive bool) (*cxsdk.SetTargetRequest, error) { |
| 63 | + if s.S3Target != nil { |
| 64 | + return &cxsdk.SetTargetRequest{ |
| 65 | + IsActive: isTargetActive, |
| 66 | + TargetSpec: &cxsdk.SetTargetRequestS3{ |
| 67 | + S3: &cxsdk.S3TargetSpec{ |
| 68 | + Region: &s.S3Target.Region, |
| 69 | + Bucket: s.S3Target.Bucket, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, nil |
| 73 | + } else { |
| 74 | + var bucketType cxsdk.IbmBucketType |
| 75 | + if s.IbmCosTarget.BucketType != nil { |
| 76 | + switch *s.IbmCosTarget.BucketType { |
| 77 | + case "UNSPECIFIED": |
| 78 | + bucketType = cxsdk.IbmBucketTypeUnspecified |
| 79 | + case "EXTERNAL": |
| 80 | + bucketType = cxsdk.IbmBucketTypeExternal |
| 81 | + case "INTERNAL": |
| 82 | + bucketType = cxsdk.IbmBucketTypeInternal |
| 83 | + |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return &cxsdk.SetTargetRequest{ |
| 88 | + IsActive: true, |
| 89 | + TargetSpec: &cxsdk.SetTargetRequestIbmCos{ |
| 90 | + IbmCos: &cxsdk.IBMCosTargetSpec{ |
| 91 | + BucketCrn: s.IbmCosTarget.BucketCrn, |
| 92 | + Endpoint: s.IbmCosTarget.Endpoint, |
| 93 | + ServiceCrn: s.IbmCosTarget.ServiceCrn, |
| 94 | + BucketType: &bucketType, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, nil |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func (i *ArchiveLogsTarget) GetConditions() []metav1.Condition { |
| 102 | + return i.Status.Conditions |
| 103 | +} |
| 104 | + |
| 105 | +func (i *ArchiveLogsTarget) SetConditions(conditions []metav1.Condition) { |
| 106 | + i.Status.Conditions = conditions |
| 107 | +} |
| 108 | + |
| 109 | +// ArchiveLogsTarget is the Schema for the archive logs targets API. |
| 110 | + |
| 111 | +// +kubebuilder:object:root=true |
| 112 | +// +kubebuilder:subresource:status |
| 113 | +type ArchiveLogsTarget struct { |
| 114 | + metav1.TypeMeta `json:",inline"` |
| 115 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 116 | + |
| 117 | + Spec ArchiveLogsTargetSpec `json:"spec,omitempty"` |
| 118 | + Status ArchiveLogsTargetStatus `json:"status,omitempty"` |
| 119 | +} |
| 120 | + |
| 121 | +// ArchiveLogsTargetList contains a list of ArchiveLogsTarget. |
| 122 | +// +kubebuilder:object:root=true |
| 123 | +type ArchiveLogsTargetList struct { |
| 124 | + metav1.TypeMeta `json:",inline"` |
| 125 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 126 | + Items []ArchiveLogsTarget `json:"items"` |
| 127 | +} |
| 128 | + |
| 129 | +func init() { |
| 130 | + SchemeBuilder.Register(&ArchiveLogsTarget{}, &ArchiveLogsTargetList{}) |
| 131 | +} |
0 commit comments