Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #34 from integr8ly/upgrades
Browse files Browse the repository at this point in the history
update gitea image automatically
  • Loading branch information
pb82 authored Jan 23, 2020
2 parents 4405eb9 + f2cd279 commit 1a39d43
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion pkg/controller/gitea/gitea_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package gitea
import (
"context"
"fmt"
v12 "k8s.io/api/apps/v1"
"log"
"time"

"k8s.io/api/apps/v1beta1"

Expand All @@ -28,6 +30,10 @@ const (
PhaseDone
)

const (
RequeueDelay = time.Second * 30
)

// Add creates a new Gitea Controller and adds it to the Manager. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(mgr manager.Manager) error {
Expand Down Expand Up @@ -99,7 +105,7 @@ func (r *ReconcileGitea) Reconcile(request reconcile.Request) (reconcile.Result,
case PhaseInstallGitea:
return r.InstallGitea(instanceCopy)
case PhaseDone:
log.Printf("Gitea installation complete")
return r.UpdateGitea(instanceCopy)
}

return reconcile.Result{}, nil
Expand Down Expand Up @@ -212,3 +218,35 @@ func (r *ReconcileGitea) CreateResource(cr *integreatlyv1alpha1.Gitea, resourceN
}
return nil
}

// Creates a generic kubernetes resource from a templates
func (r *ReconcileGitea) UpdateGitea(cr *integreatlyv1alpha1.Gitea) (reconcile.Result, error) {
selector := types.NamespacedName{
Namespace: cr.Namespace,
Name: GiteaDeploymentName,
}

deployment := v12.Deployment{}
err := r.client.Get(context.TODO(), selector, &deployment)
if err != nil {
return reconcile.Result{RequeueAfter: RequeueDelay}, nil
}

if len(deployment.Spec.Template.Spec.Containers) == 0 {
log.Print("no containers in deployment")
return reconcile.Result{RequeueAfter: RequeueDelay}, nil
}

expectedImage := fmt.Sprintf("%s:%s", GiteaImage, GiteaVersion)
currentImage := deployment.Spec.Template.Spec.Containers[0].Image

if currentImage != expectedImage {
log.Print("Updating gitea to ", expectedImage)
deployment.Spec.Template.Spec.Containers[0].Image = expectedImage
err = r.client.Update(context.TODO(), &deployment)
return reconcile.Result{RequeueAfter: RequeueDelay}, err
}

log.Print("Gitea image is up to date: ", currentImage)
return reconcile.Result{RequeueAfter: RequeueDelay}, nil
}

0 comments on commit 1a39d43

Please sign in to comment.