|
| 1 | +/* |
| 2 | +Copyright 2025. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1beta1 |
| 18 | + |
| 19 | +import ( |
| 20 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 21 | +) |
| 22 | + |
| 23 | +// GrafanaServiceAccountTokenSpec defines a token for a service account |
| 24 | +type GrafanaServiceAccountTokenSpec struct { |
| 25 | + // Name of the token |
| 26 | + // +kubebuilder:validation:Required |
| 27 | + // +kubebuilder:validation:MinLength=1 |
| 28 | + Name string `json:"name"` |
| 29 | + |
| 30 | + // Expiration date of the token. If not set, the token never expires |
| 31 | + // +optional |
| 32 | + // +kubebuilder:validation:Type=string |
| 33 | + // +kubebuilder:validation:Format=date-time |
| 34 | + Expires *metav1.Time `json:"expires,omitempty"` |
| 35 | + |
| 36 | + // Name of the secret to store the token. If not set, a name will be generated |
| 37 | + // +optional |
| 38 | + // +kubebuilder:validation:MinLength=1 |
| 39 | + SecretName string `json:"secretName,omitempty"` |
| 40 | +} |
| 41 | + |
| 42 | +// GrafanaServiceAccountSpec defines the desired state of a GrafanaServiceAccount. |
| 43 | +type GrafanaServiceAccountSpec struct { |
| 44 | + // How often the resource is synced, defaults to 10m0s if not set |
| 45 | + // +optional |
| 46 | + // +kubebuilder:validation:Type=string |
| 47 | + // +kubebuilder:validation:Pattern="^([0-9]+(\\.[0-9]+)?(ns|us|µs|ms|s|m|h))+$" |
| 48 | + // +kubebuilder:default="10m0s" |
| 49 | + // +kubebuilder:validation:XValidation:rule="duration(self) > duration('0s')",message="spec.resyncPeriod must be greater than 0" |
| 50 | + ResyncPeriod metav1.Duration `json:"resyncPeriod,omitempty"` |
| 51 | + |
| 52 | + // Suspend pauses reconciliation of the service account |
| 53 | + // +optional |
| 54 | + // +kubebuilder:default=false |
| 55 | + Suspend bool `json:"suspend,omitempty"` |
| 56 | + |
| 57 | + // Name of the Grafana instance to create the service account for |
| 58 | + // +kubebuilder:validation:Required |
| 59 | + // +kubebuilder:validation:MinLength=1 |
| 60 | + // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="spec.instanceName is immutable" |
| 61 | + InstanceName string `json:"instanceName"` |
| 62 | + |
| 63 | + // Name of the service account in Grafana |
| 64 | + // +kubebuilder:validation:Required |
| 65 | + // +kubebuilder:validation:MinLength=1 |
| 66 | + // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="spec.name is immutable" |
| 67 | + Name string `json:"name"` |
| 68 | + |
| 69 | + // Role of the service account (Viewer, Editor, Admin) |
| 70 | + // +kubebuilder:validation:Required |
| 71 | + // +kubebuilder:validation:Enum=Viewer;Editor;Admin |
| 72 | + Role string `json:"role"` |
| 73 | + |
| 74 | + // Whether the service account is disabled |
| 75 | + // +optional |
| 76 | + // +kubebuilder:default=false |
| 77 | + IsDisabled bool `json:"isDisabled,omitempty"` |
| 78 | + |
| 79 | + // Tokens to create for the service account |
| 80 | + // +optional |
| 81 | + // +listType=map |
| 82 | + // +listMapKey=name |
| 83 | + Tokens []GrafanaServiceAccountTokenSpec `json:"tokens,omitempty"` |
| 84 | +} |
| 85 | + |
| 86 | +// GrafanaServiceAccountSecretStatus describes a Secret created in Kubernetes to store the service account token. |
| 87 | +type GrafanaServiceAccountSecretStatus struct { |
| 88 | + Namespace string `json:"namespace,omitempty"` |
| 89 | + Name string `json:"name,omitempty"` |
| 90 | +} |
| 91 | + |
| 92 | +// GrafanaServiceAccountTokenStatus describes a token created in Grafana. |
| 93 | +type GrafanaServiceAccountTokenStatus struct { |
| 94 | + Name string `json:"name"` |
| 95 | + |
| 96 | + // Expiration time of the token |
| 97 | + // N.B. There's possible discrepancy with the expiration time in spec |
| 98 | + // It happens because Grafana API accepts TTL in seconds then calculates the expiration time against the current time |
| 99 | + Expires *metav1.Time `json:"expires,omitempty"` |
| 100 | + |
| 101 | + // ID of the token in Grafana |
| 102 | + ID int64 `json:"id"` |
| 103 | + |
| 104 | + // Name of the secret containing the token |
| 105 | + Secret *GrafanaServiceAccountSecretStatus `json:"secret,omitempty"` |
| 106 | +} |
| 107 | + |
| 108 | +// GrafanaServiceAccountInfo describes the Grafana service account information. |
| 109 | +type GrafanaServiceAccountInfo struct { |
| 110 | + Name string `json:"name"` |
| 111 | + Login string `json:"login"` |
| 112 | + |
| 113 | + // ID of the service account in Grafana |
| 114 | + ID int64 `json:"id"` |
| 115 | + |
| 116 | + // Role is the Grafana role for the service account (Viewer, Editor, Admin) |
| 117 | + Role string `json:"role"` |
| 118 | + |
| 119 | + // IsDisabled indicates if the service account is disabled |
| 120 | + IsDisabled bool `json:"isDisabled"` |
| 121 | + |
| 122 | + // Information about tokens |
| 123 | + // +optional |
| 124 | + Tokens []GrafanaServiceAccountTokenStatus `json:"tokens,omitempty"` |
| 125 | +} |
| 126 | + |
| 127 | +// GrafanaServiceAccountStatus defines the observed state of a GrafanaServiceAccount |
| 128 | +type GrafanaServiceAccountStatus struct { |
| 129 | + GrafanaCommonStatus `json:",inline"` |
| 130 | + |
| 131 | + // Info contains the Grafana service account information |
| 132 | + Account *GrafanaServiceAccountInfo `json:"account,omitempty"` |
| 133 | +} |
| 134 | + |
| 135 | +//+kubebuilder:object:root=true |
| 136 | +//+kubebuilder:subresource:status |
| 137 | + |
| 138 | +// GrafanaServiceAccount is the Schema for the grafanaserviceaccounts API |
| 139 | +// +kubebuilder:resource:categories={grafana-operator} |
| 140 | +type GrafanaServiceAccount struct { |
| 141 | + metav1.TypeMeta `json:",inline"` |
| 142 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 143 | + |
| 144 | + Spec GrafanaServiceAccountSpec `json:"spec,omitempty"` |
| 145 | + Status GrafanaServiceAccountStatus `json:"status,omitempty"` |
| 146 | +} |
| 147 | + |
| 148 | +//+kubebuilder:object:root=true |
| 149 | + |
| 150 | +// GrafanaServiceAccountList contains a list of GrafanaServiceAccount |
| 151 | +type GrafanaServiceAccountList struct { |
| 152 | + metav1.TypeMeta `json:",inline"` |
| 153 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 154 | + Items []GrafanaServiceAccount `json:"items"` |
| 155 | +} |
| 156 | + |
| 157 | +// Find searches for a GrafanaServiceAccount by namespace/name in the list. |
| 158 | +func (in *GrafanaServiceAccountList) Find(namespace, name string) *GrafanaServiceAccount { |
| 159 | + for i := range in.Items { |
| 160 | + if in.Items[i].Namespace == namespace && in.Items[i].Name == name { |
| 161 | + return &in.Items[i] |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return nil |
| 166 | +} |
| 167 | + |
| 168 | +// MatchNamespace returns the namespace where this service account is defined. |
| 169 | +func (in *GrafanaServiceAccount) MatchNamespace() string { |
| 170 | + return in.Namespace |
| 171 | +} |
| 172 | + |
| 173 | +// AllowCrossNamespace indicates whether cross-namespace import is allowed for this resource. |
| 174 | +func (in *GrafanaServiceAccount) AllowCrossNamespace() bool { |
| 175 | + // return in.Spec.AllowCrossNamespaceImport |
| 176 | + return false |
| 177 | +} |
| 178 | + |
| 179 | +func (in *GrafanaServiceAccount) CommonStatus() *GrafanaCommonStatus { |
| 180 | + return &in.Status.GrafanaCommonStatus |
| 181 | +} |
| 182 | + |
| 183 | +func init() { |
| 184 | + SchemeBuilder.Register(&GrafanaServiceAccount{}, &GrafanaServiceAccountList{}) |
| 185 | +} |
0 commit comments