|
| 1 | +/* |
| 2 | +Copyright 2024. |
| 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 objectstore |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + barmanapi "github.com/cloudnative-pg/barman-cloud/pkg/api" |
| 23 | + "github.com/cloudnative-pg/machinery/pkg/api" |
| 24 | + appsv1 "k8s.io/api/apps/v1" |
| 25 | + corev1 "k8s.io/api/core/v1" |
| 26 | + "k8s.io/apimachinery/pkg/api/resource" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 29 | + "k8s.io/utils/ptr" |
| 30 | + |
| 31 | + pluginBarmanCloudV1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1" |
| 32 | +) |
| 33 | + |
| 34 | +func NewAzuriteObjectStoreResources(namespace, name string) Resources { |
| 35 | + return Resources{ |
| 36 | + Deployment: newAzuriteDeployment(namespace, name), |
| 37 | + Service: newAzuriteService(namespace, name), |
| 38 | + PVC: newAzuritePVC(namespace, name), |
| 39 | + Secret: newAzuriteSecret(namespace, name), |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func newAzuriteDeployment(namespace, name string) *appsv1.Deployment { |
| 44 | + return &appsv1.Deployment{ |
| 45 | + TypeMeta: metav1.TypeMeta{ |
| 46 | + Kind: "Deployment", |
| 47 | + APIVersion: "apps/v1", |
| 48 | + }, |
| 49 | + ObjectMeta: metav1.ObjectMeta{ |
| 50 | + Name: name, |
| 51 | + Namespace: namespace, |
| 52 | + }, |
| 53 | + Spec: appsv1.DeploymentSpec{ |
| 54 | + Replicas: ptr.To(int32(1)), |
| 55 | + Selector: &metav1.LabelSelector{ |
| 56 | + MatchLabels: map[string]string{ |
| 57 | + "app": name, |
| 58 | + }, |
| 59 | + }, |
| 60 | + Template: corev1.PodTemplateSpec{ |
| 61 | + ObjectMeta: metav1.ObjectMeta{ |
| 62 | + Labels: map[string]string{ |
| 63 | + "app": name, |
| 64 | + }, |
| 65 | + }, |
| 66 | + Spec: corev1.PodSpec{ |
| 67 | + Containers: []corev1.Container{ |
| 68 | + { |
| 69 | + Name: name, |
| 70 | + // TODO: renovate the image |
| 71 | + Image: "mcr.microsoft.com/azure-storage/azurite", |
| 72 | + Ports: []corev1.ContainerPort{ |
| 73 | + { |
| 74 | + ContainerPort: 10000, |
| 75 | + }, |
| 76 | + }, |
| 77 | + Env: []corev1.EnvVar{ |
| 78 | + { |
| 79 | + Name: "AZURITE_ACCOUNTS", |
| 80 | + ValueFrom: &corev1.EnvVarSource{ |
| 81 | + SecretKeyRef: &corev1.SecretKeySelector{ |
| 82 | + LocalObjectReference: corev1.LocalObjectReference{ |
| 83 | + Name: name, |
| 84 | + }, |
| 85 | + Key: "AZURITE_ACCOUNTS", |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + VolumeMounts: []corev1.VolumeMount{ |
| 91 | + { |
| 92 | + Name: "data", |
| 93 | + MountPath: "/data", |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + Volumes: []corev1.Volume{ |
| 99 | + { |
| 100 | + Name: "data", |
| 101 | + VolumeSource: corev1.VolumeSource{ |
| 102 | + PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ |
| 103 | + ClaimName: name, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func newAzuriteService(namespace, name string) *corev1.Service { |
| 115 | + return &corev1.Service{ |
| 116 | + TypeMeta: metav1.TypeMeta{ |
| 117 | + Kind: "Service", |
| 118 | + APIVersion: "v1", |
| 119 | + }, |
| 120 | + ObjectMeta: metav1.ObjectMeta{ |
| 121 | + Name: name, |
| 122 | + Namespace: namespace, |
| 123 | + }, |
| 124 | + Spec: corev1.ServiceSpec{ |
| 125 | + Selector: map[string]string{ |
| 126 | + "app": name, |
| 127 | + }, |
| 128 | + Ports: []corev1.ServicePort{ |
| 129 | + { |
| 130 | + Port: 10000, |
| 131 | + TargetPort: intstr.FromInt32(10000), |
| 132 | + Protocol: corev1.ProtocolTCP, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func newAzuriteSecret(namespace, name string) *corev1.Secret { |
| 140 | + return &corev1.Secret{ |
| 141 | + ObjectMeta: metav1.ObjectMeta{ |
| 142 | + Namespace: namespace, |
| 143 | + Name: name, |
| 144 | + }, |
| 145 | + StringData: map[string]string{ |
| 146 | + "AZURITE_ACCOUNTS": "storageaccountname:c3RvcmFnZWFjY291bnRrZXk=", |
| 147 | + "AZURE_CONNECTION_STRING": "DefaultEndpointsProtocol=http;AccountName=storageaccountname;" + |
| 148 | + "AccountKey=c3RvcmFnZWFjY291bnRrZXk=;" + |
| 149 | + fmt.Sprintf("BlobEndpoint=http://%v:10000/storageaccountname;", name), |
| 150 | + }, |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func newAzuritePVC(namespace, name string) *corev1.PersistentVolumeClaim { |
| 155 | + return &corev1.PersistentVolumeClaim{ |
| 156 | + TypeMeta: metav1.TypeMeta{ |
| 157 | + Kind: "PersistentVolumeClaim", |
| 158 | + APIVersion: "v1", |
| 159 | + }, |
| 160 | + ObjectMeta: metav1.ObjectMeta{ |
| 161 | + Name: name, |
| 162 | + Namespace: namespace, |
| 163 | + }, |
| 164 | + Spec: corev1.PersistentVolumeClaimSpec{ |
| 165 | + AccessModes: []corev1.PersistentVolumeAccessMode{ |
| 166 | + corev1.ReadWriteOnce, |
| 167 | + }, |
| 168 | + Resources: corev1.VolumeResourceRequirements{ |
| 169 | + Requests: corev1.ResourceList{ |
| 170 | + corev1.ResourceStorage: resource.MustParse(DefaultSize), |
| 171 | + }, |
| 172 | + }, |
| 173 | + }, |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +func NewAzuriteObjectStore(namespace, name, azuriteOSName string) *pluginBarmanCloudV1.ObjectStore { |
| 178 | + return &pluginBarmanCloudV1.ObjectStore{ |
| 179 | + TypeMeta: metav1.TypeMeta{ |
| 180 | + Kind: "ObjectStore", |
| 181 | + APIVersion: "barmancloud.cnpg.io/v1", |
| 182 | + }, |
| 183 | + ObjectMeta: metav1.ObjectMeta{ |
| 184 | + Name: name, |
| 185 | + Namespace: namespace, |
| 186 | + }, |
| 187 | + Spec: pluginBarmanCloudV1.ObjectStoreSpec{ |
| 188 | + Configuration: barmanapi.BarmanObjectStoreConfiguration{ |
| 189 | + BarmanCredentials: barmanapi.BarmanCredentials{ |
| 190 | + Azure: &barmanapi.AzureCredentials{ |
| 191 | + ConnectionString: &api.SecretKeySelector{ |
| 192 | + LocalObjectReference: api.LocalObjectReference{ |
| 193 | + Name: azuriteOSName, |
| 194 | + }, |
| 195 | + Key: "AZURE_CONNECTION_STRING", |
| 196 | + }, |
| 197 | + }, |
| 198 | + }, |
| 199 | + DestinationPath: fmt.Sprintf("http://%v:10000/storageaccountname/backups/", azuriteOSName), |
| 200 | + }, |
| 201 | + }, |
| 202 | + } |
| 203 | +} |
0 commit comments