Skip to content

Commit

Permalink
Remove yaml template entirely
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVulaj committed Jun 4, 2024
1 parent 1d1542e commit 2549aeb
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 187 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,5 @@ Using the [operator-sdk](https://github.com/operator-framework/operator-sdk), ru
oc apply -f deploy/crds/managed.openshift.io_mustgathers_crd.yaml
oc new-project must-gather-operator
export DEFAULT_MUST_GATHER_IMAGE='quay.io/openshift/origin-must-gather:latest'
export JOB_TEMPLATE_FILE_NAME=./build/templates/job.template.yaml
OPERATOR_NAME=must-gather-operator operator-sdk run --verbose --local --namespace ''
```
6 changes: 2 additions & 4 deletions build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ FROM quay.io/app-sre/boilerplate:image-v5.0.0 AS builder
ENV OPERATOR=/usr/local/bin/must-gather-operator \
OPERATOR_BIN=must-gather-operator \
USER_UID=1001 \
USER_NAME=must-gather-operator \
JOB_TEMPLATE_FILE_NAME=/etc/templates/job.template.yaml
USER_NAME=must-gather-operator

RUN mkdir /src

Expand All @@ -21,8 +20,7 @@ FROM registry.access.redhat.com/ubi8/ubi-minimal:8.10-896.1716497715
ENV OPERATOR=/usr/local/bin/must-gather-operator \
OPERATOR_BIN=must-gather-operator \
USER_UID=1001 \
USER_NAME=must-gather-operator \
JOB_TEMPLATE_FILE_NAME=/etc/templates/job.template.yaml
USER_NAME=must-gather-operator

RUN microdnf install tar gzip openssh-clients wget shadow-utils procps && \
wget https://kojipkgs.fedoraproject.org/packages/sshpass/1.06/9.el8/x86_64/sshpass-1.06-9.el8.x86_64.rpm && \
Expand Down
74 changes: 0 additions & 74 deletions build/templates/job.template.yaml

This file was deleted.

113 changes: 8 additions & 105 deletions controllers/mustgather/mustgather_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,63 +31,23 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/yaml"
"os"
"reflect"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"strconv"
"strings"
)

const (
ControllerName = "mustgather-controller"

templateFileNameEnv = "JOB_TEMPLATE_FILE_NAME"
defaultMustGatherNamespace = "openshift-must-gather-operator"

gatherCommandBinaryAudit = "gather_audit_logs"
gatherCommandBinaryNoAudit = "gather"
gatherCommand = "\ntimeout %v bash -x -c -- '/usr/bin/%v'\n\nstatus=$?\nif [[ $status -eq 124 || $status -eq 137 ]]; then\n echo \"Gather timed out.\"\n exit 0\nfi"

containerEnvNameUsername = "username"
containerEnvNamePassword = "password"
containerEnvNameCaseID = "caseid"
containerEnvNameInternalUser = "internal_user"
containerEnvNameHttpProxy = "http_proxy"
containerEnvNameHttpsProxy = "https_proxy"
containerEnvNameNoProxy = "no_proxy"
)

var log = logf.Log.WithName(ControllerName)

func initializeTemplate(clusterVersion string) (string, error) {
templateFileName, ok := os.LookupEnv(templateFileNameEnv)
if !ok {
templateFileName = "/etc/templates/job.template.yaml"
}
text, err := os.ReadFile(templateFileName)
if err != nil {
log.Error(err, "Error reading job template file", "filename", templateFileName)
return "", err
}
// Inject the operator image URI from the pod's env variables
operatorImage, varPresent := os.LookupEnv("OPERATOR_IMAGE")
if !varPresent {
err := goerror.New("Operator image environment variable not found")
log.Error(err, "Error: no operator image found for job template")
return "", err
}

// TODO: make these normal template parameters instead. This is ugly but works
str := strings.Replace(string(text), "THIS_STRING_WILL_BE_REPLACED_BUT_DONT_CHANGE_IT", operatorImage, 1)
str = strings.Replace(str, "MUST_GATHER_IMAGE_DONT_CHANGE", clusterVersion, 1)
return str, err
}

// blank assignment to verify that MustGatherReconciler implements reconcile.Reconciler
var _ reconcile.Reconciler = &MustGatherReconciler{}

Expand Down Expand Up @@ -375,77 +335,20 @@ func (r *MustGatherReconciler) addFinalizer(reqLogger logr.Logger, m *mustgather
}

func (r *MustGatherReconciler) getJobFromInstance(instance *mustgatherv1alpha1.MustGather) (*batchv1.Job, error) {
version, err := r.getClusterVersionForJobTemplate("version")
if err != nil {
return nil, fmt.Errorf("failed to get cluster version for job template: %w", err)
}

jobTemplate, err := initializeTemplate(version)
if err != nil {
log.Error(err, "unable to initialize job template")
return nil, err
}

job, err := processJobTemplate(jobTemplate, instance)
if err != nil {
log.Error(err, "unable to process job template")
// Inject the operator image URI from the pod's env variables
operatorImage, varPresent := os.LookupEnv("OPERATOR_IMAGE")
if !varPresent {
err := goerror.New("Operator image environment variable not found")
log.Error(err, "Error: no operator image found for job template")
return nil, err
}

return job, nil
}

func processJobTemplate(jobTemplate string, instance *mustgatherv1alpha1.MustGather) (*batchv1.Job, error) {
job := &batchv1.Job{}
err := yaml.Unmarshal([]byte(jobTemplate), job)
version, err := r.getClusterVersionForJobTemplate("version")
if err != nil {
return nil, fmt.Errorf("failed to umarshal job template: %w", err)
}
job.ObjectMeta.Name = instance.ObjectMeta.Name
job.ObjectMeta.Namespace = instance.ObjectMeta.Namespace

var gatherCommandBinary string
if audit := instance.Spec.Audit; audit {
gatherCommandBinary = gatherCommandBinaryAudit
} else {
gatherCommandBinary = gatherCommandBinaryNoAudit
}

job.Spec.Template.Spec.Containers[0].Command = append(
job.Spec.Template.Spec.Containers[0].Command,
fmt.Sprintf(gatherCommand, instance.Spec.MustGatherTimeout.Duration, gatherCommandBinary),
)

for i, env := range job.Spec.Template.Spec.Containers[1].Env {
switch env.Name {
case containerEnvNameUsername, containerEnvNamePassword:
env.ValueFrom.SecretKeyRef.Name = instance.Spec.CaseManagementAccountSecretRef.Name
case containerEnvNameCaseID:
env.Value = instance.Spec.CaseID
case containerEnvNameInternalUser:
env.Value = strconv.FormatBool(instance.Spec.InternalUser)
}
job.Spec.Template.Spec.Containers[1].Env[i] = env
}

if httpProxy := instance.Spec.ProxyConfig.HTTPProxy; httpProxy != "" {
httpProxyEnvar := corev1.EnvVar{Name: containerEnvNameHttpProxy, Value: httpProxy}
job.Spec.Template.Spec.Containers[1].Env = append(job.Spec.Template.Spec.Containers[1].Env, httpProxyEnvar)
}

if httpsProxy := instance.Spec.ProxyConfig.HTTPSProxy; httpsProxy != "" {
httpProxyEnvar := corev1.EnvVar{Name: containerEnvNameHttpsProxy, Value: httpsProxy}
job.Spec.Template.Spec.Containers[1].Env = append(job.Spec.Template.Spec.Containers[1].Env, httpProxyEnvar)
}

if noProxy := instance.Spec.ProxyConfig.NoProxy; noProxy != "" {
noProxyEnvar := corev1.EnvVar{Name: containerEnvNameNoProxy, Value: noProxy}
job.Spec.Template.Spec.Containers[1].Env = append(job.Spec.Template.Spec.Containers[1].Env, noProxyEnvar)
return nil, fmt.Errorf("failed to get cluster version for job template: %w", err)
}

job.Spec.Template.Spec.ServiceAccountName = instance.Spec.ServiceAccountRef.Name

return job, nil
return getJobTemplate(operatorImage, version, *instance), nil
}

func (r *MustGatherReconciler) getClusterVersionForJobTemplate(clusterVersionName string) (string, error) {
Expand Down
3 changes: 0 additions & 3 deletions controllers/mustgather/mustgather_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mustgather
import (
"context"
configv1 "github.com/openshift/api/config/v1"
"os"
"sigs.k8s.io/controller-runtime/pkg/client"
"testing"

Expand All @@ -24,8 +23,6 @@ import (
)

func TestMustGatherController(t *testing.T) {
_ = os.Setenv("JOB_TEMPLATE_FILE_NAME", "../../../build/templates/job.template.yaml")

mgObj := createMustGatherObject()
secObj := createMustGatherSecretObject()

Expand Down
Loading

0 comments on commit 2549aeb

Please sign in to comment.