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 14 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
8 changes: 5 additions & 3 deletions bundle/manifests/backstage-default-config_v1_configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ data:
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 will be replaced by the value of the `RELATED_IMAGE_postgresql` env var, if set
image: quay.io/fedora/postgresql-15:latest
imagePullPolicy: IfNotPresent
securityContext:
runAsNonRoot: true
Expand Down Expand Up @@ -188,7 +189,8 @@ data:
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 will be replaced by the value of the `RELATED_IMAGE_backstage` env var, if set
image: quay.io/janus-idp/backstage-showcase:latest
imagePullPolicy: IfNotPresent
name: install-dynamic-plugins
volumeMounts:
Expand All @@ -202,7 +204,7 @@ data:

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
imagePullPolicy: IfNotPresent
args:
- "--config"
Expand Down
gazarenkov marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ metadata:
}
]
capabilities: Seamless Upgrades
createdAt: "2024-01-29T20:18:14Z"
createdAt: "2024-02-12T14:45:30Z"
operatorframework.io/suggested-namespace: backstage-system
operators.operatorframework.io/builder: operator-sdk-v1.33.0
operators.operatorframework.io/project_layout: go.kubebuilder.io/v3
Expand Down
3 changes: 2 additions & 1 deletion config/manager/default-config/db-statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ 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 will be replaced by the value of the `RELATED_IMAGE_postgresql` env var, if set
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
6 changes: 4 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 will be replaced by the value of the `RELATED_IMAGE_backstage` env var, if set
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 @@ -54,7 +55,8 @@ spec:
ephemeral-storage: 5Gi
containers:
- name: backstage-backend
image: <RELATED_IMAGE_backstage> # will be replaced with the actual image quay.io/janus-idp/backstage-showcase:next
# image will be replaced by the value of the `RELATED_IMAGE_backstage` env var, if set
image: quay.io/janus-idp/backstage-showcase:latest
gazarenkov marked this conversation as resolved.
Show resolved Hide resolved
imagePullPolicy: IfNotPresent
args:
- "--config"
Expand Down
22 changes: 11 additions & 11 deletions controllers/backstage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ const (
BackstageAppLabel = "janus-idp.io/app"
)

var (
envPostgresImage string
envBackstageImage string
)

// BackstageReconciler reconciles a Backstage object
type BackstageReconciler struct {
client.Client
Expand All @@ -56,10 +61,6 @@ type BackstageReconciler struct {
Namespace string

IsOpenShift bool

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 +296,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)
var ok bool
if envPostgresImage, ok = os.LookupEnv("RELATED_IMAGE_postgresql"); !ok {
log.Info("RELATED_IMAGE_postgresql environment variable is not set, default will be used")
}
if envBackstageImage, ok = os.LookupEnv("RELATED_IMAGE_backstage"); !ok {
log.Info("RELATED_IMAGE_backstage environment variable is not set, default will be used")
}

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
108 changes: 6 additions & 102 deletions controllers/backstage_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,103 +33,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,11 +194,12 @@ 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 envBackstageImage != "" {
visitContainers(&deployment.Spec.Template, func(container *v1.Container) {
container.Image = envBackstageImage

})
}
}

func (r *BackstageReconciler) applyBackstageLabels(backstage bs.Backstage, deployment *appsv1.Deployment) {
Expand Down
115 changes: 5 additions & 110 deletions controllers/local_db_statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,111 +28,6 @@ import (
bs "janus-idp.io/backstage-operator/api/v1alpha1"
)

//var (
// DefaultLocalDbDeployment = `apiVersion: apps/v1
//kind: StatefulSet
//metadata:
// name: backstage-psql-cr1 # placeholder for 'backstage-psql-<cr-name>'
//spec:
// podManagementPolicy: OrderedReady
// replicas: 1
// selector:
// matchLabels:
// janus-idp.io/app: backstage-psql-cr1 # placeholder for 'backstage-psql-<cr-name>'
// serviceName: backstage-psql-cr1-hl # placeholder for 'backstage-psql-<cr-name>-hl'
// template:
// metadata:
// labels:
// janus-idp.io/app: backstage-psql-cr1 # placeholder for 'backstage-psql-<cr-name>'
// name: backstage-db-cr1 # placeholder for 'backstage-psql-<cr-name>'
// spec:
// containers:
// - env:
// - name: POSTGRESQL_PORT_NUMBER
// value: "5432"
// - name: POSTGRESQL_VOLUME_DIR
// value: /var/lib/pgsql/data
// - name: PGDATA
// value: /var/lib/pgsql/data/userdata
// envFrom:
// - secretRef:
// name: postgres-secrets
// image: quay.io/fedora/postgresql-15:latest
// imagePullPolicy: IfNotPresent
// securityContext:
// runAsNonRoot: true
// allowPrivilegeEscalation: false
// seccompProfile:
// type: RuntimeDefault
// capabilities:
// drop:
// - ALL
// livenessProbe:
// exec:
// command:
// - /bin/sh
// - -c
// - exec pg_isready -U ${POSTGRES_USER} -h 127.0.0.1 -p 5432
// failureThreshold: 6
// initialDelaySeconds: 30
// periodSeconds: 10
// successThreshold: 1
// timeoutSeconds: 5
// name: postgresql
// ports:
// - containerPort: 5432
// name: tcp-postgresql
// protocol: TCP
// readinessProbe:
// exec:
// command:
// - /bin/sh
// - -c
// - -e
// - |
// exec pg_isready -U ${POSTGRES_USER} -h 127.0.0.1 -p 5432
// failureThreshold: 6
// initialDelaySeconds: 5
// periodSeconds: 10
// successThreshold: 1
// timeoutSeconds: 5
// resources:
// requests:
// cpu: 250m
// memory: 256Mi
// limits:
// memory: 1024Mi
// volumeMounts:
// - mountPath: /dev/shm
// name: dshm
// - mountPath: /var/lib/pgsql/data
// name: data
// restartPolicy: Always
// securityContext: {}
// serviceAccount: default
// serviceAccountName: default
// volumes:
// - emptyDir:
// medium: Memory
// name: dshm
// updateStrategy:
// rollingUpdate:
// partition: 0
// type: RollingUpdate
// volumeClaimTemplates:
// - apiVersion: v1
// kind: PersistentVolumeClaim
// metadata:
// name: data
// spec:
// accessModes:
// - ReadWriteOnce
// resources:
// requests:
// storage: 1Gi
//`
//)

const (
ownerRefFmt = "failed to set owner reference: %s"
)
Expand Down Expand Up @@ -218,11 +113,11 @@ 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 envPostgresImage != "" {
visitContainers(&statefulSet.Spec.Template, func(container *v1.Container) {
container.Image = envPostgresImage
})
}
}

// cleanupLocalDbResources removes all local db related resources, including statefulset, services and generated secret.
Expand Down
Loading
Loading