|
| 1 | +// |
| 2 | +// Copyright (c) 2023 Red Hat, Inc. |
| 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 v1alpha2 |
| 16 | + |
| 17 | +import ( |
| 18 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 19 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 20 | + "k8s.io/utils/ptr" |
| 21 | +) |
| 22 | + |
| 23 | +type BackstageConditionReason string |
| 24 | + |
| 25 | +type BackstageConditionType string |
| 26 | + |
| 27 | +const ( |
| 28 | + BackstageConditionTypeDeployed BackstageConditionType = "Deployed" |
| 29 | + |
| 30 | + BackstageConditionReasonDeployed BackstageConditionReason = "Deployed" |
| 31 | + BackstageConditionReasonFailed BackstageConditionReason = "DeployFailed" |
| 32 | + BackstageConditionReasonInProgress BackstageConditionReason = "DeployInProgress" |
| 33 | +) |
| 34 | + |
| 35 | +// BackstageSpec defines the desired state of Backstage |
| 36 | +type BackstageSpec struct { |
| 37 | + // Configuration for Backstage. Optional. |
| 38 | + Application *Application `json:"application,omitempty"` |
| 39 | + |
| 40 | + // Raw Runtime RuntimeObjects configuration. For Advanced scenarios. |
| 41 | + RawRuntimeConfig *RuntimeConfig `json:"rawRuntimeConfig,omitempty"` |
| 42 | + |
| 43 | + // Configuration for database access. Optional. |
| 44 | + Database *Database `json:"database,omitempty"` |
| 45 | + |
| 46 | + // Valid fragment of Deployment to be merged with default/raw configuration. |
| 47 | + // Set the Deployment's metadata and|or spec fields you want to override or add. |
| 48 | + // Optional. |
| 49 | + // +kubebuilder:pruning:PreserveUnknownFields |
| 50 | + Deployment *BackstageDeployment `json:"deployment,omitempty"` |
| 51 | +} |
| 52 | + |
| 53 | +type BackstageDeployment struct { |
| 54 | + // Valid fragment of Deployment to be merged with default/raw configuration. |
| 55 | + // Set the Deployment's metadata and|or spec fields you want to override or add. |
| 56 | + // Optional. |
| 57 | + // +kubebuilder:pruning:PreserveUnknownFields |
| 58 | + Patch *apiextensionsv1.JSON `json:"patch,omitempty"` |
| 59 | +} |
| 60 | + |
| 61 | +type RuntimeConfig struct { |
| 62 | + // Name of ConfigMap containing Backstage runtime objects configuration |
| 63 | + BackstageConfigName string `json:"backstageConfig,omitempty"` |
| 64 | + // Name of ConfigMap containing LocalDb (PostgreSQL) runtime objects configuration |
| 65 | + LocalDbConfigName string `json:"localDbConfig,omitempty"` |
| 66 | +} |
| 67 | + |
| 68 | +type Database struct { |
| 69 | + // Control the creation of a local PostgreSQL DB. Set to false if using for example an external Database for Backstage. |
| 70 | + // +optional |
| 71 | + //+kubebuilder:default=true |
| 72 | + EnableLocalDb *bool `json:"enableLocalDb,omitempty"` |
| 73 | + |
| 74 | + // Name of the secret for database authentication. Optional. |
| 75 | + // For a local database deployment (EnableLocalDb=true), a secret will be auto generated if it does not exist. |
| 76 | + // The secret shall include information used for the database access. |
| 77 | + // An example for PostgreSQL DB access: |
| 78 | + // "POSTGRES_PASSWORD": "rl4s3Fh4ng3M4" |
| 79 | + // "POSTGRES_PORT": "5432" |
| 80 | + // "POSTGRES_USER": "postgres" |
| 81 | + // "POSTGRESQL_ADMIN_PASSWORD": "rl4s3Fh4ng3M4" |
| 82 | + // "POSTGRES_HOST": "backstage-psql-bs1" # For local database, set to "backstage-psql-<CR name>". |
| 83 | + AuthSecretName string `json:"authSecretName,omitempty"` |
| 84 | +} |
| 85 | + |
| 86 | +type Application struct { |
| 87 | + // References to existing app-configs ConfigMap objects, that will be mounted as files in the specified mount path. |
| 88 | + // Each element can be a reference to any ConfigMap or Secret, |
| 89 | + // and will be mounted inside the main application container under a specified mount directory. |
| 90 | + // Additionally, each file will be passed as a `--config /mount/path/to/configmap/key` to the |
| 91 | + // main container args in the order of the entries defined in the AppConfigs list. |
| 92 | + // But bear in mind that for a single ConfigMap element containing several filenames, |
| 93 | + // the order in which those files will be appended to the main container args cannot be guaranteed. |
| 94 | + // So if you want to pass multiple app-config files, it is recommended to pass one ConfigMap per app-config file. |
| 95 | + // +optional |
| 96 | + AppConfig *AppConfig `json:"appConfig,omitempty"` |
| 97 | + |
| 98 | + // Reference to an existing ConfigMap for Dynamic Plugins. |
| 99 | + // A new one will be generated with the default config if not set. |
| 100 | + // The ConfigMap object must have an existing key named: 'dynamic-plugins.yaml'. |
| 101 | + // +optional |
| 102 | + DynamicPluginsConfigMapName string `json:"dynamicPluginsConfigMapName,omitempty"` |
| 103 | + |
| 104 | + // References to existing Config objects to use as extra config files. |
| 105 | + // They will be mounted as files in the specified mount path. |
| 106 | + // Each element can be a reference to any ConfigMap or Secret. |
| 107 | + // +optional |
| 108 | + ExtraFiles *ExtraFiles `json:"extraFiles,omitempty"` |
| 109 | + |
| 110 | + // Extra environment variables |
| 111 | + // +optional |
| 112 | + ExtraEnvs *ExtraEnvs `json:"extraEnvs,omitempty"` |
| 113 | + |
| 114 | + // Number of desired replicas to set in the Backstage Deployment. |
| 115 | + // Defaults to 1. |
| 116 | + // +optional |
| 117 | + //+kubebuilder:default=1 |
| 118 | + Replicas *int32 `json:"replicas,omitempty"` |
| 119 | + |
| 120 | + // Custom image to use in all containers (including Init Containers). |
| 121 | + // It is your responsibility to make sure the image is from trusted sources and has been validated for security compliance |
| 122 | + // +optional |
| 123 | + Image *string `json:"image,omitempty"` |
| 124 | + |
| 125 | + // Image Pull Secrets to use in all containers (including Init Containers) |
| 126 | + // +optional |
| 127 | + ImagePullSecrets []string `json:"imagePullSecrets,omitempty"` |
| 128 | + |
| 129 | + // Route configuration. Used for OpenShift only. |
| 130 | + Route *Route `json:"route,omitempty"` |
| 131 | +} |
| 132 | + |
| 133 | +type AppConfig struct { |
| 134 | + // Mount path for all app-config files listed in the ConfigMapRefs field |
| 135 | + // +optional |
| 136 | + // +kubebuilder:default=/opt/app-root/src |
| 137 | + MountPath string `json:"mountPath,omitempty"` |
| 138 | + |
| 139 | + // List of ConfigMaps storing the app-config files. Will be mounted as files under the MountPath specified. |
| 140 | + // For each item in this array, if a key is not specified, it means that all keys in the ConfigMap will be mounted as files. |
| 141 | + // Otherwise, only the specified key will be mounted as a file. |
| 142 | + // Bear in mind not to put sensitive data in those ConfigMaps. Instead, your app-config content can reference |
| 143 | + // environment variables (which you can set with the ExtraEnvs field) and/or include extra files (see the ExtraFiles field). |
| 144 | + // More details on https://backstage.io/docs/conf/writing/. |
| 145 | + // +optional |
| 146 | + ConfigMaps []ObjectKeyRef `json:"configMaps,omitempty"` |
| 147 | +} |
| 148 | + |
| 149 | +type ExtraFiles struct { |
| 150 | + // Mount path for all extra configuration files listed in the Items field |
| 151 | + // +optional |
| 152 | + // +kubebuilder:default=/opt/app-root/src |
| 153 | + MountPath string `json:"mountPath,omitempty"` |
| 154 | + |
| 155 | + // List of references to ConfigMaps objects mounted as extra files under the MountPath specified. |
| 156 | + // For each item in this array, if a key is not specified, it means that all keys in the ConfigMap will be mounted as files. |
| 157 | + // Otherwise, only the specified key will be mounted as a file. |
| 158 | + // +optional |
| 159 | + ConfigMaps []ObjectKeyRef `json:"configMaps,omitempty"` |
| 160 | + |
| 161 | + // List of references to Secrets objects mounted as extra files under the MountPath specified. |
| 162 | + // For each item in this array, a key must be specified that will be mounted as a file. |
| 163 | + // +optional |
| 164 | + Secrets []ObjectKeyRef `json:"secrets,omitempty"` |
| 165 | +} |
| 166 | + |
| 167 | +type ExtraEnvs struct { |
| 168 | + // List of references to ConfigMaps objects to inject as additional environment variables. |
| 169 | + // For each item in this array, if a key is not specified, it means that all keys in the ConfigMap will be injected as additional environment variables. |
| 170 | + // Otherwise, only the specified key will be injected as an additional environment variable. |
| 171 | + // +optional |
| 172 | + ConfigMaps []ObjectKeyRef `json:"configMaps,omitempty"` |
| 173 | + |
| 174 | + // List of references to Secrets objects to inject as additional environment variables. |
| 175 | + // For each item in this array, if a key is not specified, it means that all keys in the Secret will be injected as additional environment variables. |
| 176 | + // Otherwise, only the specified key will be injected as environment variable. |
| 177 | + // +optional |
| 178 | + Secrets []ObjectKeyRef `json:"secrets,omitempty"` |
| 179 | + |
| 180 | + // List of name and value pairs to add as environment variables. |
| 181 | + // +optional |
| 182 | + Envs []Env `json:"envs,omitempty"` |
| 183 | +} |
| 184 | + |
| 185 | +type ObjectKeyRef struct { |
| 186 | + // Name of the object |
| 187 | + // We support only ConfigMaps and Secrets. |
| 188 | + //+kubebuilder:validation:Required |
| 189 | + Name string `json:"name"` |
| 190 | + |
| 191 | + // Key in the object |
| 192 | + // +optional |
| 193 | + Key string `json:"key,omitempty"` |
| 194 | +} |
| 195 | + |
| 196 | +type Env struct { |
| 197 | + // Name of the environment variable |
| 198 | + //+kubebuilder:validation:Required |
| 199 | + Name string `json:"name"` |
| 200 | + |
| 201 | + // Value of the environment variable |
| 202 | + //+kubebuilder:validation:Required |
| 203 | + Value string `json:"value"` |
| 204 | +} |
| 205 | + |
| 206 | +// BackstageStatus defines the observed state of Backstage |
| 207 | +type BackstageStatus struct { |
| 208 | + // Conditions is the list of conditions describing the state of the runtime |
| 209 | + // +optional |
| 210 | + Conditions []metav1.Condition `json:"conditions,omitempty"` |
| 211 | +} |
| 212 | + |
| 213 | +//+kubebuilder:object:root=true |
| 214 | +//+kubebuilder:subresource:status |
| 215 | +//+kubebuilder:storageversion |
| 216 | + |
| 217 | +// Backstage is the Schema for the backstages API |
| 218 | +type Backstage struct { |
| 219 | + metav1.TypeMeta `json:",inline"` |
| 220 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 221 | + |
| 222 | + Spec BackstageSpec `json:"spec,omitempty"` |
| 223 | + Status BackstageStatus `json:"status,omitempty"` |
| 224 | +} |
| 225 | + |
| 226 | +//+kubebuilder:object:root=true |
| 227 | + |
| 228 | +// BackstageList contains a list of Backstage |
| 229 | +type BackstageList struct { |
| 230 | + metav1.TypeMeta `json:",inline"` |
| 231 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 232 | + Items []Backstage `json:"items"` |
| 233 | +} |
| 234 | + |
| 235 | +// Route specifies configuration parameters for OpenShift Route for Backstage. |
| 236 | +// Only a secured edge route is supported for Backstage. |
| 237 | +type Route struct { |
| 238 | + // Control the creation of a Route on OpenShift. |
| 239 | + // +optional |
| 240 | + //+kubebuilder:default=true |
| 241 | + Enabled *bool `json:"enabled,omitempty"` |
| 242 | + |
| 243 | + // Host is an alias/DNS that points to the service. Optional. |
| 244 | + // Ignored if Enabled is false. |
| 245 | + // If not specified a route name will typically be automatically |
| 246 | + // chosen. Must follow DNS952 subdomain conventions. |
| 247 | + // +optional |
| 248 | + // +kubebuilder:validation:MaxLength=253 |
| 249 | + // +kubebuilder:validation:Pattern=`^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$` |
| 250 | + Host string `json:"host,omitempty" protobuf:"bytes,1,opt,name=host"` |
| 251 | + |
| 252 | + // Subdomain is a DNS subdomain that is requested within the ingress controller's |
| 253 | + // domain (as a subdomain). |
| 254 | + // Ignored if Enabled is false. |
| 255 | + // Example: subdomain `frontend` automatically receives the router subdomain |
| 256 | + // `apps.mycluster.com` to have a full hostname `frontend.apps.mycluster.com`. |
| 257 | + // +optional |
| 258 | + // +kubebuilder:validation:MaxLength=253 |
| 259 | + // +kubebuilder:validation:Pattern=`^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$` |
| 260 | + Subdomain string `json:"subdomain,omitempty"` |
| 261 | + |
| 262 | + // The tls field provides the ability to configure certificates for the route. |
| 263 | + // Ignored if Enabled is false. |
| 264 | + // +optional |
| 265 | + TLS *TLS `json:"tls,omitempty"` |
| 266 | +} |
| 267 | + |
| 268 | +type TLS struct { |
| 269 | + // certificate provides certificate contents. This should be a single serving certificate, not a certificate |
| 270 | + // chain. Do not include a CA certificate. |
| 271 | + Certificate string `json:"certificate,omitempty"` |
| 272 | + |
| 273 | + // ExternalCertificateSecretName provides certificate contents as a secret reference. |
| 274 | + // This should be a single serving certificate, not a certificate |
| 275 | + // chain. Do not include a CA certificate. The secret referenced should |
| 276 | + // be present in the same namespace as that of the Route. |
| 277 | + // Forbidden when `certificate` is set. |
| 278 | + // +optional |
| 279 | + ExternalCertificateSecretName string `json:"externalCertificateSecretName,omitempty"` |
| 280 | + |
| 281 | + // key provides key file contents |
| 282 | + Key string `json:"key,omitempty"` |
| 283 | + |
| 284 | + // caCertificate provides the cert authority certificate contents |
| 285 | + CACertificate string `json:"caCertificate,omitempty"` |
| 286 | +} |
| 287 | + |
| 288 | +func init() { |
| 289 | + SchemeBuilder.Register(&Backstage{}, &BackstageList{}) |
| 290 | +} |
| 291 | + |
| 292 | +// IsLocalDbEnabled returns true if Local database is configured and enabled |
| 293 | +func (s *BackstageSpec) IsLocalDbEnabled() bool { |
| 294 | + if s.Database == nil { |
| 295 | + return true |
| 296 | + } |
| 297 | + return ptr.Deref(s.Database.EnableLocalDb, true) |
| 298 | +} |
| 299 | + |
| 300 | +// IsRouteEnabled returns value of Application.Route.Enabled if defined or true by default |
| 301 | +func (s *BackstageSpec) IsRouteEnabled() bool { |
| 302 | + if s.Application != nil && s.Application.Route != nil { |
| 303 | + return ptr.Deref(s.Application.Route.Enabled, true) |
| 304 | + } |
| 305 | + return true |
| 306 | +} |
| 307 | + |
| 308 | +func (s *BackstageSpec) IsAuthSecretSpecified() bool { |
| 309 | + return s.Database != nil && s.Database.AuthSecretName != "" |
| 310 | +} |
0 commit comments