Skip to content

Commit

Permalink
Merge pull request #36 from cynepco3hahue/get_image_repository_from_o…
Browse files Browse the repository at this point in the history
…perator

Get image repository from the operator if it does not exist
  • Loading branch information
Artyom Lukianov authored Aug 12, 2019
2 parents 5a50920 + c493750 commit 0089716
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (r *ReconcileMachineRemediationOperator) createOrUpdateComponents(mro *mrv1
}

for _, component := range components.Components {
glog.Infof("Creating objets for component %q", component)
glog.Infof("Creating objects for component %q", component)
if err := r.createOrUpdateServiceAccount(component, consts.NamespaceOpenshiftMachineAPI); err != nil {
return err
}
Expand All @@ -175,6 +175,7 @@ func (r *ReconcileMachineRemediationOperator) createOrUpdateComponents(mro *mrv1
OperatorVersion: r.operatorVersion,
Verbosity: "4",
}

if err := r.createOrUpdateDeployment(deployData); err != nil {
return err
}
Expand Down
42 changes: 42 additions & 0 deletions pkg/operator/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io/ioutil"
"strings"

"github.com/ghodss/yaml"

Expand Down Expand Up @@ -33,6 +34,19 @@ func (r *ReconcileMachineRemediationOperator) getDeployment(name string, namespa
}

func (r *ReconcileMachineRemediationOperator) createOrUpdateDeployment(data *components.DeploymentData) error {
if data.ImageRepository == "" {
imageRepository, err := r.getOperatorImageRepository()
if err != nil {
return err
}

data.ImageRepository = imageRepository
}

if data.PullPolicy == "" {
data.PullPolicy = corev1.PullIfNotPresent
}

newDeploy := components.NewDeployment(data)
newDeploy.Spec.Replicas = pointer.Int32Ptr(2)

Expand All @@ -58,6 +72,34 @@ func (r *ReconcileMachineRemediationOperator) createOrUpdateDeployment(data *com
return r.client.Update(context.TODO(), newDeploy)
}

func (r *ReconcileMachineRemediationOperator) getOperatorImageRepository() (string, error) {
ns, err := getOperatorNamespace()
if err != nil {
return "", err
}

operator, err := r.getDeployment(components.ComponentMachineRemediationOperator, ns)
if err != nil {
return "", err
}

image := strings.Split(operator.Spec.Template.Spec.Containers[0].Image, "/")
return strings.Join(image[:len(image)-1], "/"), nil
}

func getOperatorNamespace() (string, error) {
data, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err != nil {
return "", fmt.Errorf("failed to get operator namespace: %v", err)
}

if ns := strings.TrimSpace(string(data)); len(ns) > 0 {
return ns, nil
}

return "", fmt.Errorf("failed to get operator namespace: %v", err)
}

func (r *ReconcileMachineRemediationOperator) deleteDeployment(name string, namespace string) error {
deploy, err := r.getDeployment(name, namespace)
if errors.IsNotFound(err) {
Expand Down

0 comments on commit 0089716

Please sign in to comment.