forked from janus-idp/operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix service raw configuration (janus-idp#203)
* 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
1 parent
da211c0
commit 9b5bdd8
Showing
3 changed files
with
195 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |