forked from samba-in-kubernetes/samba-operator
/
smbservice.go
184 lines (166 loc) · 5.73 KB
/
smbservice.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package resources
import (
"context"
"strings"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
sambaoperatorv1alpha1 "github.com/obnoxxx/samba-operator/api/v1alpha1"
"github.com/obnoxxx/samba-operator/internal/conf"
)
// SmbServiceManager is used to manage SmbService resources.
type SmbServiceManager struct {
client client.Client
scheme *runtime.Scheme
logger Logger
}
// NewSmbServiceManager creates a SmbServiceManager.
func NewSmbServiceManager(client client.Client, scheme *runtime.Scheme, logger Logger) *SmbServiceManager {
return &SmbServiceManager{
client: client,
scheme: scheme,
logger: logger,
}
}
// Update should be called when a SmbService resource changes.
func (m *SmbServiceManager) Update(ctx context.Context, nsname types.NamespacedName) Result {
instance := &sambaoperatorv1alpha1.SmbService{}
err := m.client.Get(ctx, nsname, instance)
if err != nil {
if errors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
return Done
}
// Error reading the object - requeue the request.
return Done
}
// Check if the deployment already exists, if not create a new one
found := &appsv1.Deployment{}
err = m.client.Get(ctx, types.NamespacedName{Name: instance.Name, Namespace: instance.Namespace}, found)
if err != nil && errors.IsNotFound(err) {
// not found - define a new deployment
dep := m.deploymentForSmbService(instance, instance.Namespace)
m.logger.Info("Creating a new Deployment", "Deployment.Namespace", dep.Namespace, "Deployment.Name", dep.Name)
err = m.client.Create(ctx, dep)
if err != nil {
m.logger.Error(err, "Failed to create new Deployment", "Deployment.Namespace", dep.Namespace, "Deployment.Name", dep.Name)
return Result{err: err}
}
// Deployment created successfully - return and requeue
return Requeue
} else if err != nil {
m.logger.Error(err, "Failed to get Deployment")
return Result{err: err}
}
// Ensure the deployment size is the same as the spec
var size int32 = 1
if *found.Spec.Replicas != size {
found.Spec.Replicas = &size
err = m.client.Update(ctx, found)
if err != nil {
m.logger.Error(err, "Failed to update Deployment", "Deployment.Namespace", found.Namespace, "Deployment.Name", found.Name)
return Result{err: err}
}
// Spec updated - return and requeue
return Requeue
}
return Done
}
// deploymentForSmbService returns a smbservice deployment object
func (m *SmbServiceManager) deploymentForSmbService(s *sambaoperatorv1alpha1.SmbService, ns string) *appsv1.Deployment {
// TODO: it is not the best to be grabbing the global conf this "deep" in
// the operator, but rather than refactor everything at once, we at least
// stop using hard coded parameters.
cfg := conf.Get()
// labels - do I need them?
labels := labelsForSmbService(s.Name)
smb_volume := s.Spec.PvcName + "-smb"
var size int32 = 1
dep := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: s.Name,
Namespace: ns,
Labels: labels,
},
Spec: appsv1.DeploymentSpec{
Replicas: &size,
Selector: &metav1.LabelSelector{
MatchLabels: labels,
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{{
Name: smb_volume,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: s.Spec.PvcName,
},
},
}},
Containers: []corev1.Container{{
Image: cfg.SmbdContainerImage,
Name: cfg.SmbdContainerName,
//NEEDED? - Command: []string{"cmd", "arg", "arg", "..."},
Ports: []corev1.ContainerPort{{
ContainerPort: 445,
Name: "smb",
}},
VolumeMounts: []corev1.VolumeMount{{
MountPath: "/share",
Name: smb_volume,
}},
}},
},
},
},
}
// set the smbservice instance as the owner and controller
controllerutil.SetControllerReference(s, dep, m.scheme)
return dep
}
// labelsForSmbService returns the labels for selecting the resources
// belonging to the given smbservice CR name.
func labelsForSmbService(name string) map[string]string {
return map[string]string{
// top level labes
"app": "smbservice",
// k8s namespaced labels
// See: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/
"app.kubernetes.io/name": "samba",
"app.kubernetes.io/instance": labelValue("samba", name),
"app.kubernetes.io/component": "smbd",
"app.kubernetes.io/part-of": "samba",
"app.kubernetes.io/managed-by": "samba-operator",
// our namespaced labels
"samba-operator.samba.org/smbservice": labelValue(name),
"samba-operator.samba.org/share": "share",
}
}
func labelValue(s ...string) string {
out := strings.Join(s, "-")
if len(out) > 63 {
out = out[:63]
}
return out
}