BuildManagedServices creates a list of Kubernetes Services based on the additional managed services specified in the Cluster's ManagedServices configuration. Returns: - []corev1.Service: a slice of Service objects created from the managed services configuration. - error: an error if the creation of
(cluster apiv1.Cluster)
| 174 | // // use the created services |
| 175 | // } |
| 176 | func BuildManagedServices(cluster apiv1.Cluster) ([]corev1.Service, error) { |
| 177 | if cluster.Spec.Managed == nil || cluster.Spec.Managed.Services == nil { |
| 178 | return nil, nil |
| 179 | } |
| 180 | |
| 181 | managedServices := cluster.Spec.Managed.Services |
| 182 | if len(managedServices.Additional) == 0 { |
| 183 | return nil, nil |
| 184 | } |
| 185 | |
| 186 | services := make([]corev1.Service, len(managedServices.Additional)) |
| 187 | |
| 188 | for i := range managedServices.Additional { |
| 189 | serviceConfiguration := managedServices.Additional[i] |
| 190 | defaultService, err := buildDefaultService(cluster, serviceConfiguration) |
| 191 | if err != nil { |
| 192 | return nil, err |
| 193 | } |
| 194 | builder := servicespec.NewFrom(&serviceConfiguration.ServiceTemplate). |
| 195 | WithServiceType(defaultService.Spec.Type, false). |
| 196 | WithLabel(utils.IsManagedLabelName, "true"). |
| 197 | WithAnnotation(utils.UpdateStrategyAnnotation, string(serviceConfiguration.UpdateStrategy)). |
| 198 | SetSelectors(defaultService.Spec.Selector) |
| 199 | |
| 200 | for idx := range defaultService.Spec.Ports { |
| 201 | // we preserve the user settings over the default configuration, issue: #6389 |
| 202 | builder = builder.WithServicePortNoOverwrite(&defaultService.Spec.Ports[idx]) |
| 203 | } |
| 204 | |
| 205 | for key, value := range defaultService.Labels { |
| 206 | builder = builder.WithLabel(key, value) |
| 207 | } |
| 208 | |
| 209 | for key, value := range defaultService.Annotations { |
| 210 | builder = builder.WithAnnotation(key, value) |
| 211 | } |
| 212 | |
| 213 | serviceTemplate := builder.Build() |
| 214 | services[i] = corev1.Service{ |
| 215 | ObjectMeta: metav1.ObjectMeta{ |
| 216 | Name: serviceTemplate.ObjectMeta.Name, |
| 217 | Namespace: cluster.Namespace, |
| 218 | Labels: serviceTemplate.ObjectMeta.Labels, |
| 219 | Annotations: serviceTemplate.ObjectMeta.Annotations, |
| 220 | }, |
| 221 | Spec: serviceTemplate.Spec, |
| 222 | } |
| 223 | cluster.SetInheritedDataAndOwnership(&services[i].ObjectMeta) |
| 224 | } |
| 225 | |
| 226 | return services, nil |
| 227 | } |
| 228 | |
| 229 | func buildDefaultService(cluster apiv1.Cluster, serviceConf apiv1.ManagedService) (*corev1.Service, error) { |
| 230 | switch serviceConf.SelectorType { |
no test coverage detected