Skip to content

Commit 1012a31

Browse files
resources: create a service to go along with the deployment
After creating the deployment, create a service that exposes port 445 and matches the label specific to the share's server group. Signed-off-by: John Mulligan <jmulligan@redhat.com>
1 parent 0da5bd9 commit 1012a31

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

internal/resources/smbshare.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@ func (m *SmbShareManager) Update(ctx context.Context, instance *sambaoperatorv1a
148148
return Requeue
149149
}
150150

151+
_, created, err = m.getOrCreateService(
152+
ctx, planner, instance.Namespace)
153+
if err != nil {
154+
return Result{err: err}
155+
} else if resized {
156+
m.logger.Info("Created service")
157+
return Requeue
158+
}
159+
151160
m.logger.Info("Done updating SmbShare resources")
152161
return Done
153162
}
@@ -397,3 +406,41 @@ func (m *SmbShareManager) setServerGroup(
397406
s.Status.ServerGroup = s.ObjectMeta.Name
398407
return true, m.client.Status().Update(ctx, s)
399408
}
409+
410+
func (m *SmbShareManager) getOrCreateService(
411+
ctx context.Context, planner *sharePlanner, ns string) (
412+
*corev1.Service, bool, error) {
413+
// Check if the service already exists, if not create a new one
414+
found := &corev1.Service{}
415+
err := m.client.Get(
416+
ctx,
417+
types.NamespacedName{
418+
Name: planner.instanceName(),
419+
Namespace: ns,
420+
},
421+
found)
422+
if err == nil {
423+
return found, false, nil
424+
}
425+
426+
if errors.IsNotFound(err) {
427+
// not found - define a new deployment
428+
svc := newServiceForSmb(planner, ns)
429+
// set the smbshare instance as the owner and controller
430+
controllerutil.SetControllerReference(planner.SmbShare, svc, m.scheme)
431+
m.logger.Info("Creating a new Service",
432+
"Service.Namespace", svc.Namespace,
433+
"Service.Name", svc.Name)
434+
err = m.client.Create(ctx, svc)
435+
if err != nil {
436+
m.logger.Error(err, "Failed to create new Service",
437+
"Service.Namespace", svc.Namespace,
438+
"Service.Name", svc.Name)
439+
return svc, false, err
440+
}
441+
// Deployment created successfully
442+
return svc, true, nil
443+
}
444+
m.logger.Error(err, "Failed to get Service")
445+
return nil, false, err
446+
}

0 commit comments

Comments
 (0)