Skip to content

Commit

Permalink
Fix service raw configuration (janus-idp#203)
Browse files Browse the repository at this point in the history
* remove hardcoded images

* fix image

* Update examples/janus-cr-with-app-configs.yaml

Co-authored-by: Armel Soro <armel@rm3l.org>

* change lookup

* Update config/manager/default-config/db-statefulset.yaml

Co-authored-by: Armel Soro <armel@rm3l.org>

* Update config/manager/default-config/deployment.yaml

Co-authored-by: Armel Soro <armel@rm3l.org>

* change lookup

* change lookup

* Update config/manager/default-config/deployment.yaml

Co-authored-by: Armel Soro <armel@rm3l.org>

* add generated files

* fix image

* fix service raw config

---------

Co-authored-by: Armel Soro <armel@rm3l.org>
  • Loading branch information
gazarenkov and rm3l committed Mar 17, 2024
1 parent da211c0 commit 9b5bdd8
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 2 deletions.
4 changes: 2 additions & 2 deletions controllers/backstage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,13 @@ func (r *BackstageReconciler) readConfigMapOrDefault(ctx context.Context, name s
val, ok := cm.Data[key]
if !ok {
// key not found, default
lg.V(1).Info("custom configuration configMap and data exists, trying to apply it", "configMap", cm.Name, "key", key)
lg.V(1).Info("custom configuration configMap exists but no such key, applying default config", "configMap", cm.Name, "key", key)
err := readYamlFile(defFile(key), object)
if err != nil {
return fmt.Errorf("failed to read YAML file: %w", err)
}
} else {
lg.V(1).Info("custom configuration configMap exists but no such key, applying default config", "configMap", cm.Name, "key", key)
lg.V(1).Info("custom configuration configMap and data exists, trying to apply it", "configMap", cm.Name, "key", key)
err := readYaml([]byte(val), object)
if err != nil {
return fmt.Errorf("failed to read YAML: %w", err)
Expand Down
102 changes: 102 additions & 0 deletions controllers/backstage_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//
// Copyright (c) 2023 Red Hat, Inc.
// 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 controller

import (
"context"
"fmt"

"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

bs "janus-idp.io/backstage-operator/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (r *BackstageReconciler) reconcileBackstageService(ctx context.Context, backstage *bs.Backstage, ns string) error {
service := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: getDefaultObjName(*backstage),
Namespace: ns,
},
}

if _, err := controllerutil.CreateOrUpdate(ctx, r.Client, service, r.serviceObjectMutFun(ctx, service, *backstage,
backstage.Spec.RawRuntimeConfig.BackstageConfigName, "service.yaml", service.Name, service.Name)); err != nil {

if errors.IsConflict(err) {
return retryReconciliation(err)
}
msg := fmt.Sprintf("failed to deploy Backstage Service: %s", err)

setStatusCondition(backstage, bs.ConditionDeployed, metav1.ConditionFalse, bs.DeployFailed, msg)
return fmt.Errorf("%s %w", msg, err)
}

return nil
}

// selector for deploy.spec.template.spec.meta.label
// targetPort: http for deploy.spec.template.spec.containers.ports.name=http
func (r *BackstageReconciler) serviceObjectMutFun(ctx context.Context, targetService *corev1.Service, backstage bs.Backstage,
configName, configKey, serviceName, label string) controllerutil.MutateFn {
return func() error {

service := &corev1.Service{}
targetService.ObjectMeta.DeepCopyInto(&service.ObjectMeta)

err := r.readConfigMapOrDefault(ctx, configName, configKey, backstage.Namespace, service)
if err != nil {
return err
}

service.Name = serviceName
setLabel(&service.ObjectMeta.Labels, label)
setLabel(&service.Spec.Selector, label)
r.labels(&service.ObjectMeta, backstage)

if r.OwnsRuntime {
if err := controllerutil.SetControllerReference(&backstage, service, r.Scheme); err != nil {
return fmt.Errorf("failed to set owner reference: %s", err)
}
}

if err := validateServiceIPs(targetService, service); err != nil {
return err
}
service.Spec.ClusterIPs = targetService.Spec.ClusterIPs

service.ObjectMeta.DeepCopyInto(&targetService.ObjectMeta)
service.Spec.DeepCopyInto(&targetService.Spec)

return nil
}
}

func validateServiceIPs(targetService *corev1.Service, service *corev1.Service) error {
if len(targetService.Spec.ClusterIP) > 0 && service.Spec.ClusterIP != "" && service.Spec.ClusterIP != "None" && service.Spec.ClusterIP != targetService.Spec.ClusterIP {
return fmt.Errorf("backstage service IP can not be updated: %s, current: %s, new: %s", targetService.Name, targetService.Spec.ClusterIP, service.Spec.ClusterIP)
}
service.Spec.ClusterIP = targetService.Spec.ClusterIP
for _, ip1 := range targetService.Spec.ClusterIPs {
for _, ip2 := range service.Spec.ClusterIPs {
if len(ip1) > 0 && ip2 != "" && ip2 != "None" && ip1 != ip2 {
return fmt.Errorf("backstage service IPs can not be updated: %s, current: %v, new: %v", targetService.Name, targetService.Spec.ClusterIPs, service.Spec.ClusterIPs)
}
}
}
return nil
}
91 changes: 91 additions & 0 deletions controllers/local_db_services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Copyright 2023.
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 controller

import (
"context"
"fmt"

bs "janus-idp.io/backstage-operator/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

// var (`
//
// DefaultLocalDbService = `apiVersion: v1
//
// kind: Service
// metadata:
//
// name: backstage-psql-cr1 # placeholder for 'backstage-psql-<cr-name>'
//
// spec:
//
// selector:
// janus-idp.io/app: backstage-psql-cr1 # placeholder for 'backstage-psql-<cr-name>'
// ports:
// - port: 5432
//
// `
//
// DefaultLocalDbServiceHL = `apiVersion: v1
//
// kind: Service
// metadata:
//
// name: backstage-psql-cr1-hl # placeholder for 'backstage-psql-<cr-name>-hl'
//
// spec:
//
// selector:
// janus-idp.io/app: backstage-psql-cr1 # placeholder for 'backstage-psql-<cr-name>'
// clusterIP: None
// ports:
// - port: 5432
//
// `
// )
func (r *BackstageReconciler) reconcileLocalDbServices(ctx context.Context, backstage *bs.Backstage, ns string) error {
name := getDefaultDbObjName(*backstage)
err := r.reconcilePsqlService(ctx, backstage, name, name, "db-service.yaml", ns)
if err != nil {
return err
}
nameHL := fmt.Sprintf("backstage-psql-%s-hl", backstage.Name)
return r.reconcilePsqlService(ctx, backstage, nameHL, name, "db-service-hl.yaml", ns)

}

func (r *BackstageReconciler) reconcilePsqlService(ctx context.Context, backstage *bs.Backstage, serviceName, label, configKey, ns string) error {
service := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: serviceName,
Namespace: ns,
},
}
if _, err := controllerutil.CreateOrUpdate(ctx, r.Client, service, r.serviceObjectMutFun(ctx, service, *backstage, backstage.Spec.RawRuntimeConfig.LocalDbConfigName, configKey, serviceName, label)); err != nil {
if errors.IsConflict(err) {
return retryReconciliation(err)
}
msg := fmt.Sprintf("failed to deploy database service: %s", err)
setStatusCondition(backstage, bs.ConditionDeployed, metav1.ConditionFalse, bs.DeployFailed, msg)
return fmt.Errorf("%s %w", msg, err)
}
return nil
}

0 comments on commit 9b5bdd8

Please sign in to comment.