-
Notifications
You must be signed in to change notification settings - Fork 50
/
service.go
88 lines (83 loc) · 2.65 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// Copyright (c) 2019-2020 Red Hat, Inc.
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
// Contributors:
// Red Hat, Inc. - initial API and implementation
//
package asyncstorage
import (
"github.com/devfile/devworkspace-operator/controllers/workspace/provision"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
func SyncWorkspaceSyncServiceToCluster(asyncDeploy *appsv1.Deployment, api provision.ClusterAPI) (*corev1.Service, error) {
specService := getWorkspaceSyncServiceSpec(asyncDeploy)
err := controllerutil.SetOwnerReference(asyncDeploy, specService, api.Scheme)
if err != nil {
return nil, err
}
clusterService, err := getWorkspaceSyncServiceCluster(asyncDeploy.Namespace, api)
if err != nil {
if !k8sErrors.IsNotFound(err) {
return nil, err
}
// Service does not exist; create it.
err := api.Client.Create(api.Ctx, specService)
if err != nil && !k8sErrors.IsAlreadyExists(err) {
return nil, err
}
return nil, NotReadyError
}
if !equality.Semantic.DeepDerivative(specService.Spec, clusterService.Spec) {
// Delete service so that it can be recreated.
err := api.Client.Delete(api.Ctx, clusterService)
if err != nil && !k8sErrors.IsGone(err) {
return nil, err
}
return nil, NotReadyError
}
return clusterService, nil
}
func getWorkspaceSyncServiceSpec(asyncDeploy *appsv1.Deployment) *corev1.Service {
return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: asyncServerServiceName,
Namespace: asyncDeploy.Namespace,
Labels: map[string]string{
"app.kubernetes.io/name": "async-storage", // TODO
"app.kubernetes.io/part-of": "devworkspace-operator",
},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "rsync-port",
Port: rsyncPort,
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromInt(rsyncPort),
},
},
Selector: asyncDeploy.Spec.Selector.MatchLabels,
},
}
}
func getWorkspaceSyncServiceCluster(namespace string, api provision.ClusterAPI) (*corev1.Service, error) {
service := &corev1.Service{}
namespacedName := types.NamespacedName{
Name: asyncServerServiceName,
Namespace: namespace,
}
err := api.Client.Get(api.Ctx, namespacedName, service)
return service, err
}