Skip to content
This repository has been archived by the owner on Aug 19, 2024. It is now read-only.

Avoid hardcoded images #187

Merged
merged 15 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions api/v1alpha1/backstage_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ const (
DeployInProgress string = "DeployInProgress"
)

// Constants for image placeholders
const (
EnvPostGresImage string = "RELATED_IMAGE_postgresql"
EnvBackstageImage string = "RELATED_IMAGE_backstage"
)

// BackstageSpec defines the desired state of Backstage
type BackstageSpec struct {
// Configuration for Backstage. Optional.
Expand Down
2 changes: 1 addition & 1 deletion config/manager/default-config/db-statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ spec:
envFrom:
- secretRef:
name: <POSTGRESQL_SECRET> # will be replaced with 'backstage-psql-secrets-<cr-name>'
image: <RELATED_IMAGE_postgresql> # will be replaced with the actual image
image: quay.io/fedora/postgresql-15:latest
rm3l marked this conversation as resolved.
Show resolved Hide resolved
gazarenkov marked this conversation as resolved.
Show resolved Hide resolved
imagePullPolicy: IfNotPresent
securityContext:
runAsNonRoot: true
Expand Down
5 changes: 3 additions & 2 deletions config/manager/default-config/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ spec:
env:
- name: NPM_CONFIG_USERCONFIG
value: /opt/app-root/src/.npmrc.dynamic-plugins
image: <RELATED_IMAGE_backstage> # will be replaced with the actual image quay.io/janus-idp/backstage-showcase:next
#image: <RELATED_IMAGE_backstage> # will be replaced with the actual image quay.io/janus-idp/backstage-showcase:next
gazarenkov marked this conversation as resolved.
Show resolved Hide resolved
image: quay.io/janus-idp/backstage-showcase:latest
gazarenkov marked this conversation as resolved.
Show resolved Hide resolved
imagePullPolicy: IfNotPresent
name: install-dynamic-plugins
volumeMounts:
Expand All @@ -50,7 +51,7 @@ spec:

containers:
- name: backstage-backend
image: <RELATED_IMAGE_backstage> # will be replaced with the actual image quay.io/janus-idp/backstage-showcase:next
image: quay.io/janus-idp/backstage-showcase:latest
gazarenkov marked this conversation as resolved.
Show resolved Hide resolved
imagePullPolicy: IfNotPresent
args:
- "--config"
Expand Down
25 changes: 15 additions & 10 deletions controllers/backstage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ const (
BackstageAppLabel = "janus-idp.io/app"
)

// Constants for image placeholders
const (
EnvPostgresImage string = "RELATED_IMAGE_postgresql"
EnvBackstageImage string = "RELATED_IMAGE_backstage"
)

// BackstageReconciler reconciles a Backstage object
type BackstageReconciler struct {
client.Client
Expand All @@ -57,9 +63,9 @@ type BackstageReconciler struct {

IsOpenShift bool

PsqlImage string

BackstageImage string
//PsqlImage string
//
//BackstageImage string
}

//+kubebuilder:rbac:groups=janus-idp.io,resources=backstages,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -295,14 +301,13 @@ func (r *BackstageReconciler) labels(meta *v1.ObjectMeta, backstage bs.Backstage

// SetupWithManager sets up the controller with the Manager.
func (r *BackstageReconciler) SetupWithManager(mgr ctrl.Manager, log logr.Logger) error {
if len(r.PsqlImage) == 0 {
r.PsqlImage = "quay.io/fedora/postgresql-15:latest"
log.Info("Enviroment variable is not set, default is used", bs.EnvPostGresImage, r.PsqlImage)
}

if len(r.BackstageImage) == 0 {
r.BackstageImage = "quay.io/janus-idp/backstage-showcase:next"
log.Info("Enviroment variable is not set, default is used", bs.EnvBackstageImage, r.BackstageImage)
const imageNotSet = "environment variable is not set, default will be used:"
gazarenkov marked this conversation as resolved.
Show resolved Hide resolved
if _, ok := os.LookupEnv(EnvPostgresImage); !ok {
log.Info(imageNotSet, "", EnvPostgresImage)
gazarenkov marked this conversation as resolved.
Show resolved Hide resolved
}
if _, ok := os.LookupEnv(EnvBackstageImage); !ok {
log.Info(imageNotSet, "", EnvBackstageImage)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.Info(imageNotSet, "", EnvBackstageImage)
log.Info(imageNotSet, EnvBackstageImage, "")

Otherwise, the log message looks weird to me ;)

2024-02-09T10:58:24+01:00       INFO    setup   environment variable is not set, default will be used:  {"": "RELATED_IMAGE_backstage"}

}

builder := ctrl.NewControllerManagedBy(mgr).
Expand Down
12 changes: 6 additions & 6 deletions controllers/backstage_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ var _ = Describe("Backstage controller", func() {
Expect(err).To(Not(HaveOccurred()))

backstageReconciler = &BackstageReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
Namespace: ns,
OwnsRuntime: true,
PsqlImage: "test-postgresql-15:latest",
BackstageImage: "test-backstage-showcase:next",
Client: k8sClient,
Scheme: k8sClient.Scheme(),
Namespace: ns,
OwnsRuntime: true,
//PsqlImage: "test-postgresql-15:latest",
//BackstageImage: "test-backstage-showcase:next",
}
})

Expand Down
103 changes: 4 additions & 99 deletions controllers/backstage_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package controller
import (
"context"
"fmt"
"os"

appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
Expand All @@ -33,103 +34,6 @@ const (
_containersWorkingDir = "/opt/app-root/src"
)

//var (
// DefaultBackstageDeployment = fmt.Sprintf(`
//apiVersion: apps/v1
//kind: Deployment
//metadata:
// name: backstage
//spec:
// replicas: 1
// selector:
// matchLabels:
// janus-idp.io/app: # placeholder for 'backstage-<cr-name>'
// template:
// metadata:
// labels:
// janus-idp.io/app: # placeholder for 'backstage-<cr-name>'
// spec:
//# serviceAccountName: default
//
// volumes:
// - ephemeral:
// volumeClaimTemplate:
// spec:
// accessModes:
// - ReadWriteOnce
// resources:
// requests:
// storage: 1Gi
// name: dynamic-plugins-root
// - name: dynamic-plugins-npmrc
// secret:
// defaultMode: 420
// optional: true
// secretName: dynamic-plugins-npmrc
//
// initContainers:
// - command:
// - ./install-dynamic-plugins.sh
// - /dynamic-plugins-root
// env:
// - name: NPM_CONFIG_USERCONFIG
// value: %[3]s/.npmrc.dynamic-plugins
// image: 'quay.io/janus-idp/backstage-showcase:next'
// imagePullPolicy: IfNotPresent
// name: %[1]s
// volumeMounts:
// - mountPath: /dynamic-plugins-root
// name: dynamic-plugins-root
// - mountPath: %[3]s/.npmrc.dynamic-plugins
// name: dynamic-plugins-npmrc
// readOnly: true
// subPath: .npmrc
// workingDir: %[3]s
//
// containers:
// - name: %[2]s
// image: quay.io/janus-idp/backstage-showcase:next
// imagePullPolicy: IfNotPresent
// args:
// - "--config"
// - "dynamic-plugins-root/app-config.dynamic-plugins.yaml"
// readinessProbe:
// failureThreshold: 3
// httpGet:
// path: /healthcheck
// port: 7007
// scheme: HTTP
// initialDelaySeconds: 30
// periodSeconds: 10
// successThreshold: 2
// timeoutSeconds: 2
// livenessProbe:
// failureThreshold: 3
// httpGet:
// path: /healthcheck
// port: 7007
// scheme: HTTP
// initialDelaySeconds: 60
// periodSeconds: 10
// successThreshold: 1
// timeoutSeconds: 2
// ports:
// - name: http
// containerPort: 7007
// env:
// - name: APP_CONFIG_backend_listen_port
// value: "7007"
// envFrom:
// - secretRef:
// name: postgres-secrets
//# - secretRef:
//# name: backstage-secrets
// volumeMounts:
// - mountPath: %[3]s/dynamic-plugins-root
// name: dynamic-plugins-root
//`, _defaultBackstageInitContainerName, _defaultBackstageMainContainerName, _containersWorkingDir)
//)

// ContainerVisitor is called with each container
type ContainerVisitor func(container *v1.Container)

Expand Down Expand Up @@ -291,9 +195,10 @@ func (r *BackstageReconciler) validateAndUpdatePsqlSecretRef(backstage bs.Backst
}

func (r *BackstageReconciler) setDefaultDeploymentImage(deployment *appsv1.Deployment) {

visitContainers(&deployment.Spec.Template, func(container *v1.Container) {
if len(container.Image) == 0 || container.Image == fmt.Sprintf("<%s>", bs.EnvBackstageImage) {
container.Image = r.BackstageImage
if val, ok := os.LookupEnv(EnvBackstageImage); ok {
gazarenkov marked this conversation as resolved.
Show resolved Hide resolved
container.Image = val
}
})
}
Expand Down
8 changes: 6 additions & 2 deletions controllers/local_db_statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controller
import (
"context"
"fmt"
"os"

appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -219,9 +220,12 @@ func (r *BackstageReconciler) patchLocalDbStatefulSetObj(statefulSet *appsv1.Sta

func (r *BackstageReconciler) setDefaultStatefulSetImage(statefulSet *appsv1.StatefulSet) {
visitContainers(&statefulSet.Spec.Template, func(container *v1.Container) {
if len(container.Image) == 0 || container.Image == fmt.Sprintf("<%s>", bs.EnvPostGresImage) {
container.Image = r.PsqlImage
if val, ok := os.LookupEnv(EnvPostgresImage); ok {
gazarenkov marked this conversation as resolved.
Show resolved Hide resolved
container.Image = val
}
//if len(container.Image) == 0 || container.Image == fmt.Sprintf("<%s>", bs.EnvPostgresImage) {
gazarenkov marked this conversation as resolved.
Show resolved Hide resolved
// container.Image = r.PsqlImage
//}
})
}

Expand Down
3 changes: 2 additions & 1 deletion examples/janus-cr-with-app-configs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ data:
target: https://github.com/ododev/odo-backstage-software-template/blob/main/template.yaml
rules:
- allow: [Template]
# # catalog.providers.githubOrg.default.orgUrl

---
apiVersion: v1
Expand Down Expand Up @@ -132,7 +133,7 @@ data:
endpoints:
/explore-backend-completed:
target: 'http://localhost:7017'

rm3l marked this conversation as resolved.
Show resolved Hide resolved
---
apiVersion: v1
kind: ConfigMap
Expand Down
12 changes: 4 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,10 @@ func main() {
}

if err = (&controller.BackstageReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
OwnsRuntime: ownRuntime,
IsOpenShift: isOpenShift,
PsqlImage: os.Getenv(backstageiov1alpha1.EnvPostGresImage),
BackstageImage: os.Getenv(backstageiov1alpha1.EnvBackstageImage),
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
OwnsRuntime: ownRuntime,
IsOpenShift: isOpenShift,
}).SetupWithManager(mgr, setupLog); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Backstage")
os.Exit(1)
Expand All @@ -129,8 +127,6 @@ func main() {
"own-runtime", ownRuntime,
"env.LOCALBIN", os.Getenv("LOCALBIN"),
"isOpenShift", isOpenShift,
backstageiov1alpha1.EnvPostGresImage, os.Getenv(backstageiov1alpha1.EnvPostGresImage),
backstageiov1alpha1.EnvBackstageImage, os.Getenv(backstageiov1alpha1.EnvBackstageImage),
)
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
Expand Down