Skip to content

Commit 63af9da

Browse files
committed
fix: Refactor createRestService to reuse setService
Signed-off-by: ntkathole <nikhilkathole2683@gmail.com>
1 parent e28a578 commit 63af9da

File tree

1 file changed

+41
-60
lines changed
  • infra/feast-operator/internal/controller/services

1 file changed

+41
-60
lines changed

infra/feast-operator/internal/controller/services/services.go

Lines changed: 41 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func (feast *FeastServices) deployFeastServiceByType(feastType FeastServiceType)
228228
if feastType == RegistryFeastType && feast.isRegistryServer() {
229229
registry := feast.Handler.FeatureStore.Status.Applied.Services.Registry
230230
if registry.Local.Server.RestAPI != nil && *registry.Local.Server.RestAPI {
231-
if err := feast.createRestService(); err != nil {
231+
if err := feast.createRestService(feastType); err != nil {
232232
return feast.setFeastServiceCondition(err, feastType)
233233
}
234234
} else {
@@ -537,11 +537,11 @@ func (feast *FeastServices) getContainerCommand(feastType FeastServiceType) []st
537537
if feastType == RegistryFeastType && feast.isRegistryServer() {
538538
registry := feast.Handler.FeatureStore.Status.Applied.Services.Registry
539539
if registry.Local.Server.GRPC != nil {
540-
if *registry.Local.Server.GRPC {
541-
deploySettings.Args = append(deploySettings.Args, "--grpc")
542-
} else {
543-
deploySettings.Args = append(deploySettings.Args, "--no-grpc")
544-
}
540+
if *registry.Local.Server.GRPC {
541+
deploySettings.Args = append(deploySettings.Args, "--grpc")
542+
} else {
543+
deploySettings.Args = append(deploySettings.Args, "--no-grpc")
544+
}
545545
}
546546
if registry.Local.Server.RestAPI != nil && *registry.Local.Server.RestAPI {
547547
deploySettings.Args = append(deploySettings.Args, "--rest-api")
@@ -632,7 +632,7 @@ func (feast *FeastServices) setInitContainer(podSpec *corev1.PodSpec, fsYamlB64
632632
}
633633
}
634634

635-
func (feast *FeastServices) setService(svc *corev1.Service, feastType FeastServiceType) error {
635+
func (feast *FeastServices) setService(svc *corev1.Service, feastType FeastServiceType, isRestService ...bool) error {
636636
svc.Labels = feast.getFeastTypeLabels(feastType)
637637
if feast.isOpenShiftTls(feastType) {
638638
if len(svc.Annotations) == 0 {
@@ -648,6 +648,14 @@ func (feast *FeastServices) setService(svc *corev1.Service, feastType FeastServi
648648
port = HttpsPort
649649
scheme = HttpsScheme
650650
}
651+
652+
var targetPort int32
653+
if len(isRestService) > 0 && isRestService[0] {
654+
targetPort = getTargetRestPort(feastType, tls)
655+
} else {
656+
targetPort = getTargetPort(feastType, tls)
657+
}
658+
651659
svc.Spec = corev1.ServiceSpec{
652660
Selector: feast.getLabels(),
653661
Type: corev1.ServiceTypeClusterIP,
@@ -656,7 +664,7 @@ func (feast *FeastServices) setService(svc *corev1.Service, feastType FeastServi
656664
Name: scheme,
657665
Port: port,
658666
Protocol: corev1.ProtocolTCP,
659-
TargetPort: intstr.FromInt(int(getTargetPort(feastType, tls))),
667+
TargetPort: intstr.FromInt(int(targetPort)),
660668
},
661669
},
662670
}
@@ -665,62 +673,23 @@ func (feast *FeastServices) setService(svc *corev1.Service, feastType FeastServi
665673
}
666674

667675
// createRestService creates a separate service for the Registry REST API
668-
func (feast *FeastServices) createRestService() error {
676+
func (feast *FeastServices) createRestService(feastType FeastServiceType) error {
669677
if feast.isRegistryServer() {
670678

671-
registry := feast.Handler.FeatureStore.Status.Applied.Services.Registry
672-
if registry.Local.Server.RestAPI == nil || !*registry.Local.Server.RestAPI {
673-
return nil
674-
}
675-
676-
svc := &corev1.Service{
677-
ObjectMeta: metav1.ObjectMeta{
678-
Name: feast.GetFeastRestServiceName(feastType),
679-
Namespace: feast.Handler.FeatureStore.Namespace,
680-
Labels: feast.getFeastTypeLabels(feastType),
681-
},
682-
}
683-
svc.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Service"))
684-
685-
if feast.isOpenShiftTls(feastType) {
686-
if len(svc.Annotations) == 0 {
687-
svc.Annotations = map[string]string{}
679+
registry := feast.Handler.FeatureStore.Status.Applied.Services.Registry
680+
if registry.Local.Server.RestAPI == nil || !*registry.Local.Server.RestAPI {
681+
return nil
688682
}
689-
svc.Annotations["service.beta.openshift.io/serving-cert-secret-name"] = svc.Name + tlsNameSuffix
690-
}
691683

692-
var port int32 = HttpPort
693-
scheme := HttpScheme
694-
tls := feast.getTlsConfigs(feastType)
695-
if tls.IsTLS() {
696-
port = HttpsPort
697-
scheme = HttpsScheme
698-
}
699-
700-
svc.Spec = corev1.ServiceSpec{
701-
Selector: feast.getLabels(),
702-
Type: corev1.ServiceTypeClusterIP,
703-
Ports: []corev1.ServicePort{
704-
{
705-
Name: scheme,
706-
Port: port,
707-
Protocol: corev1.ProtocolTCP,
708-
TargetPort: intstr.FromInt(int(getTargetRestPort(feastType, tls))),
709-
},
710-
},
711-
}
712-
713-
if err := controllerutil.SetControllerReference(feast.Handler.FeatureStore, svc, feast.Handler.Scheme); err != nil {
714-
return err
715-
}
716-
717-
logger := log.FromContext(feast.Handler.Context)
718-
if op, err := controllerutil.CreateOrUpdate(feast.Handler.Context, feast.Handler.Client, svc, controllerutil.MutateFn(func() error {
719-
return nil // No need to mutate as we set everything above
720-
})); err != nil {
721-
return err
722-
} else if op == controllerutil.OperationResultCreated || op == controllerutil.OperationResultUpdated {
723-
logger.Info("Successfully reconciled", "Service", svc.Name, "operation", op)
684+
logger := log.FromContext(feast.Handler.Context)
685+
svc := feast.initFeastRestSvc(feastType)
686+
if op, err := controllerutil.CreateOrUpdate(feast.Handler.Context, feast.Handler.Client, svc, controllerutil.MutateFn(func() error {
687+
return feast.setService(svc, feastType, true)
688+
})); err != nil {
689+
return err
690+
} else if op == controllerutil.OperationResultCreated || op == controllerutil.OperationResultUpdated {
691+
logger.Info("Successfully reconciled", "Service", svc.Name, "operation", op)
692+
}
724693
}
725694

726695
return nil
@@ -987,6 +956,18 @@ func (feast *FeastServices) initFeastSvc(feastType FeastServiceType) *corev1.Ser
987956
return svc
988957
}
989958

959+
func (feast *FeastServices) initFeastRestSvc(feastType FeastServiceType) *corev1.Service {
960+
svc := &corev1.Service{
961+
ObjectMeta: metav1.ObjectMeta{
962+
Name: feast.GetFeastRestServiceName(feastType),
963+
Namespace: feast.Handler.FeatureStore.Namespace,
964+
Labels: feast.getFeastTypeLabels(feastType),
965+
},
966+
}
967+
svc.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Service"))
968+
return svc
969+
}
970+
990971
func (feast *FeastServices) initFeastSA() *corev1.ServiceAccount {
991972
sa := &corev1.ServiceAccount{
992973
ObjectMeta: feast.GetObjectMeta(),

0 commit comments

Comments
 (0)