|
| 1 | +/* |
| 2 | +Copyright 2024 The etcd-operator Authors. |
| 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 factory |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/apimachinery/pkg/runtime" |
| 26 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 27 | + ctrl "sigs.k8s.io/controller-runtime" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 29 | + |
| 30 | + etcdaenixiov1alpha1 "github.com/aenix-io/etcd-operator/api/v1alpha1" |
| 31 | +) |
| 32 | + |
| 33 | +func GetClientServiceName(cluster *etcdaenixiov1alpha1.EtcdCluster) string { |
| 34 | + return fmt.Sprintf("%s-client", cluster.Name) |
| 35 | +} |
| 36 | + |
| 37 | +func CreateOrUpdateClusterService( |
| 38 | + ctx context.Context, |
| 39 | + cluster *etcdaenixiov1alpha1.EtcdCluster, |
| 40 | + rclient client.Client, |
| 41 | + rscheme *runtime.Scheme, |
| 42 | +) error { |
| 43 | + svc := &corev1.Service{ |
| 44 | + ObjectMeta: metav1.ObjectMeta{ |
| 45 | + Name: cluster.Name, |
| 46 | + Namespace: cluster.Namespace, |
| 47 | + Labels: map[string]string{ |
| 48 | + "app.kubernetes.io/name": "etcd", |
| 49 | + "app.kubernetes.io/instance": cluster.Name, |
| 50 | + "app.kubernetes.io/managed-by": "etcd-operator", |
| 51 | + }, |
| 52 | + }, |
| 53 | + Spec: corev1.ServiceSpec{ |
| 54 | + Ports: []corev1.ServicePort{ |
| 55 | + {Name: "peer", TargetPort: intstr.FromInt32(2380), Port: 2380, Protocol: corev1.ProtocolTCP}, |
| 56 | + {Name: "client", TargetPort: intstr.FromInt32(2379), Port: 2379, Protocol: corev1.ProtocolTCP}, |
| 57 | + }, |
| 58 | + Type: corev1.ServiceTypeClusterIP, |
| 59 | + ClusterIP: "None", |
| 60 | + Selector: map[string]string{ |
| 61 | + "app.kubernetes.io/name": "etcd", |
| 62 | + "app.kubernetes.io/instance": cluster.Name, |
| 63 | + "app.kubernetes.io/managed-by": "etcd-operator", |
| 64 | + }, |
| 65 | + PublishNotReadyAddresses: true, |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + if err := ctrl.SetControllerReference(cluster, svc, rscheme); err != nil { |
| 70 | + return fmt.Errorf("cannot set controller reference: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + return reconcileService(ctx, rclient, cluster.Name, svc) |
| 74 | +} |
| 75 | + |
| 76 | +func CreateOrUpdateClientService( |
| 77 | + ctx context.Context, |
| 78 | + cluster *etcdaenixiov1alpha1.EtcdCluster, |
| 79 | + rclient client.Client, |
| 80 | + rscheme *runtime.Scheme, |
| 81 | +) error { |
| 82 | + svc := &corev1.Service{ |
| 83 | + ObjectMeta: metav1.ObjectMeta{ |
| 84 | + Name: GetClientServiceName(cluster), |
| 85 | + Namespace: cluster.Namespace, |
| 86 | + Labels: map[string]string{ |
| 87 | + "app.kubernetes.io/name": "etcd", |
| 88 | + "app.kubernetes.io/instance": cluster.Name, |
| 89 | + "app.kubernetes.io/managed-by": "etcd-operator", |
| 90 | + }, |
| 91 | + }, |
| 92 | + Spec: corev1.ServiceSpec{ |
| 93 | + Ports: []corev1.ServicePort{ |
| 94 | + {Name: "client", TargetPort: intstr.FromInt32(2379), Port: 2379, Protocol: corev1.ProtocolTCP}, |
| 95 | + }, |
| 96 | + Type: corev1.ServiceTypeClusterIP, |
| 97 | + Selector: map[string]string{ |
| 98 | + "app.kubernetes.io/name": "etcd", |
| 99 | + "app.kubernetes.io/instance": cluster.Name, |
| 100 | + "app.kubernetes.io/managed-by": "etcd-operator", |
| 101 | + }, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + if err := ctrl.SetControllerReference(cluster, svc, rscheme); err != nil { |
| 106 | + return fmt.Errorf("cannot set controller reference: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + return reconcileService(ctx, rclient, cluster.Name, svc) |
| 110 | +} |
0 commit comments