Skip to content

Commit

Permalink
Updates Hub DB deployment in case of openshift
Browse files Browse the repository at this point in the history
This patch adds a transformer which updates the db deployment,
in case of openshift to use productized postgres images and also
replace a few env and commands for readiness and liveness probes

Signed-off-by: Puneet Punamiya <ppunamiy@redhat.com>
  • Loading branch information
PuneetPunamiya committed Mar 2, 2022
1 parent 298e1ad commit e1ee74c
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 3 deletions.
2 changes: 2 additions & 0 deletions config/openshift/base/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ spec:
key: DEFAULT_TARGET_NAMESPACE
- name: CONFIG_OBSERVABILITY_NAME
value: tekton-config-observability
- name: IMAGE_HUB_DB
value: registry.redhat.io/rhel8/postgresql-13:1-18
# - name: IMAGE_ADDONS_TKN_CLI_SERVE
# value: docker.io/rupali/serve-tkn:v2
# - name: IMAGE_ADDONS_PARAM_TKN_IMAGE
Expand Down
1 change: 1 addition & 0 deletions pkg/reconciler/common/transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (
PipelinesImagePrefix = "IMAGE_PIPELINES_"
TriggersImagePrefix = "IMAGE_TRIGGERS_"
AddonsImagePrefix = "IMAGE_ADDONS_"
HubDbImagePrefix = "IMAGE_HUB_"

ArgPrefix = "arg_"
ParamPrefix = "param_"
Expand Down
11 changes: 9 additions & 2 deletions pkg/reconciler/kubernetes/tektonhub/tektonhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,17 @@ func (r *Reconciler) setupAndCreateInstallerSet(ctx context.Context, manifestLoc

manifest = manifest.Filter(mf.Not(mf.Any(mf.ByKind("Secret"), mf.ByKind("Namespace"), mf.ByKind("ConfigMap"))))

manifest, err := manifest.Transform(
images := common.ToLowerCaseKeys(common.ImagesFromEnv(common.HubDbImagePrefix))
trans := r.extension.Transformers(th)
extra := []mf.Transformer{
mf.InjectOwner(th),
mf.InjectNamespace(namespace),
)
common.DeploymentImages(images),
}
trans = append(trans, extra...)

manifest, err := manifest.Transform(trans...)

if err != nil {
logger.Error("failed to transform manifest")
return err
Expand Down
69 changes: 68 additions & 1 deletion pkg/reconciler/openshift/tektonhub/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"path/filepath"
"strings"

"github.com/go-logr/zapr"
mfc "github.com/manifestival/client-go-client"
Expand All @@ -31,7 +32,11 @@ import (
operatorclient "github.com/tektoncd/operator/pkg/client/injection/client"
"github.com/tektoncd/operator/pkg/reconciler/common"
"go.uber.org/zap"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
kubeclient "knative.dev/pkg/client/injection/kube/client"
"knative.dev/pkg/injection"
Expand All @@ -44,6 +49,12 @@ const (
tektonHubUiResourceKey string = "ui"
)

var replaceVal = map[string]string{
"POSTGRES_DB": "POSTGRESQL_DATABASE",
"POSTGRES_USER": "POSTGRESQL_USER",
"POSTGRES_PASSWORD": "POSTGRESQL_PASSWORD",
}

func OpenShiftExtension(ctx context.Context) common.Extension {
logger := logging.FromContext(ctx)
mfclient, err := mfc.NewClient(injection.GetConfig(ctx))
Expand Down Expand Up @@ -71,7 +82,7 @@ type openshiftExtension struct {
}

func (oe openshiftExtension) Transformers(comp v1alpha1.TektonComponent) []mf.Transformer {
return nil
return []mf.Transformer{UpdateDbDeployment()}
}

func (oe openshiftExtension) PreReconcile(ctx context.Context, tc v1alpha1.TektonComponent) error {
Expand Down Expand Up @@ -196,3 +207,59 @@ func getRouteHost(manifest *mf.Manifest, routeName string) (string, error) {
}
return hostUrl, nil
}

func UpdateDbDeployment() mf.Transformer {
return func(u *unstructured.Unstructured) error {
if u.GetKind() != "Deployment" {
return nil
}

d := &appsv1.Deployment{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, d)
if err != nil {
return err
}

if d.Name == "db" {
env := d.Spec.Template.Spec.Containers[0].Env

replaceEnv(env)

d.Spec.Template.Spec.Containers[0].Env = env

mountPath := "/var/lib/pgsql/data"
d.Spec.Template.Spec.Containers[0].VolumeMounts[0].MountPath = mountPath

replaceProbeCommand(d.Spec.Template.Spec.Containers[0].ReadinessProbe.Exec.Command)
replaceProbeCommand(d.Spec.Template.Spec.Containers[0].LivenessProbe.Exec.Command)

unstrObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(d)
if err != nil {
return err
}
u.SetUnstructuredContent(unstrObj)

return nil
}

return nil
}
}

func replaceProbeCommand(data []string) {
if strings.Contains(data[2], "POSTGRES_USER") {
data[2] = strings.ReplaceAll(data[2], "POSTGRES_USER", "POSTGRESQL_USER")
}
if strings.Contains(data[2], "POSTGRES_DB") {
data[2] = strings.ReplaceAll(data[2], "POSTGRES_DB", "POSTGRESQL_DATABASE")
}
}

func replaceEnv(envs []corev1.EnvVar) {
for i, e := range envs {
_, ok := replaceVal[e.Name]
if ok {
envs[i].Name = replaceVal[e.Name]
}
}
}
51 changes: 51 additions & 0 deletions pkg/reconciler/openshift/tektonhub/extension_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2022 The Tekton Authors
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 tektonhub

import (
"path"
"strings"
"testing"

mf "github.com/manifestival/manifestival"
"gotest.tools/v3/assert"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/runtime"
)

func TestUpdateDbDeployment(t *testing.T) {
testData := path.Join("testdata", "update-db-deployment.yaml")

manifest, err := mf.ManifestFrom(mf.Recursive(testData))
assert.NilError(t, err)

newManifest, err := manifest.Transform(UpdateDbDeployment())
assert.NilError(t, err)

d := &appsv1.Deployment{}
err = runtime.DefaultUnstructuredConverter.FromUnstructured(newManifest.Resources()[0].Object, d)
assert.NilError(t, err)

env := d.Spec.Template.Spec.Containers[0].Env
assert.Equal(t, env[0].Name, "POSTGRESQL_DATABASE")

mountPath := d.Spec.Template.Spec.Containers[0].VolumeMounts[0].MountPath
assert.Equal(t, mountPath, "/var/lib/pgsql/data")

cmd := d.Spec.Template.Spec.Containers[0].ReadinessProbe.Exec.Command
assert.Equal(t, strings.Contains(cmd[2], "POSTGRESQL_USER"), true)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Copyright © 2022 The Tekton Authors.
#
# 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.

apiVersion: apps/v1
kind: Deployment
metadata:
name: db
labels:
app: db
spec:
replicas: 1
selector:
matchLabels:
app: db
template:
metadata:
labels:
app: db
spec:
containers:
- name: db
image: postgres:13@sha256:260a98d976574b439712c35914fdcb840755233f79f3e27ea632543f78b7a21e
imagePullPolicy: IfNotPresent
ports:
- containerPort: 5432
protocol: TCP
env:
- name: POSTGRES_DB
valueFrom:
secretKeyRef:
name: db
key: POSTGRES_DB
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
name: db
key: POSTGRES_USER
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: db
key: POSTGRES_PASSWORD
- name: PGDATA
value: /var/lib/postgresql/data/pgdata
volumeMounts:
- name: db
mountPath: /var/lib/postgresql/data
readinessProbe:
exec:
command: [bash, -c, "psql -w -U ${POSTGRES_USER} -d ${POSTGRES_DB} -c 'SELECT 1'"]
initialDelaySeconds: 15
timeoutSeconds: 2
periodSeconds: 15
livenessProbe:
exec:
command: [bash, -c, "psql -w -U ${POSTGRES_USER} -d ${POSTGRES_DB} -c 'SELECT 1'"]
initialDelaySeconds: 45
timeoutSeconds: 2
periodSeconds: 15
volumes:
- name: db
persistentVolumeClaim:
claimName: db
restartPolicy: Always

0 comments on commit e1ee74c

Please sign in to comment.