Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow for images in spec.version #1764

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 15 additions & 1 deletion controllers/grafana_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"reflect"
"strings"
"time"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -99,8 +101,16 @@ func (r *GrafanaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return r.updateStatus(grafana, nextStatus)
}

// set spec to the current default version to avoid accidental updates when we
// change the default. For clusters where RELATED_IMAGE_GRAFANA is set to an
// image hash, we want to set this to the value of the variable to support air
// gapped clusters as well
if grafana.Spec.Version == "" {
grafana.Spec.Version = config.GrafanaVersion
targetVersion := config.GrafanaVersion
if envVersion := os.Getenv("RELATED_IMAGE_GRAFANA"); isImageSHA256(envVersion) {
targetVersion = envVersion
}
grafana.Spec.Version = targetVersion
if err := r.Client.Update(ctx, grafana); err != nil {
return ctrl.Result{}, fmt.Errorf("updating grafana version in spec: %w", err)
}
Expand Down Expand Up @@ -261,3 +271,7 @@ func (r *GrafanaReconciler) getReconcilerForStage(stage grafanav1beta1.OperatorS
return nil
}
}

func isImageSHA256(image string) bool {
return strings.Contains(image, "@sha256:")
}
22 changes: 5 additions & 17 deletions controllers/reconcilers/grafana/deployment_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package grafana
import (
"context"
"fmt"
"os"
"strings"

"github.com/grafana/grafana-operator/v5/api/v1beta1"
Expand Down Expand Up @@ -31,7 +30,6 @@ const (
ReadinessProbePeriodSeconds int32 = 10
ReadinessProbeSuccessThreshold int32 = 1
ReadinessProbeTimeoutSeconds int32 = 3
RelatedImageGrafanaEnvVar = "RELATED_IMAGE_GRAFANA"
)

type DeploymentReconciler struct {
Expand All @@ -46,10 +44,6 @@ func NewDeploymentReconciler(client client.Client, isOpenShift bool) reconcilers
}
}

func IsImageSHA256(image string) bool {
return strings.Contains(image, "@sha256:")
}

func (r *DeploymentReconciler) Reconcile(ctx context.Context, cr *v1beta1.Grafana, status *v1beta1.GrafanaStatus, vars *v1beta1.OperatorReconcileVars, scheme *runtime.Scheme) (v1beta1.OperatorStageStatus, error) {
logger := log.FromContext(ctx).WithName("DeploymentReconciler")

Expand Down Expand Up @@ -141,19 +135,13 @@ func getVolumeMounts(cr *v1beta1.Grafana, scheme *runtime.Scheme) []v1.VolumeMou
}

func getGrafanaImage(cr *v1beta1.Grafana) string {
grafanaImg := os.Getenv(RelatedImageGrafanaEnvVar)
if IsImageSHA256(grafanaImg) {
return grafanaImg
if cr.Spec.Version == "" {
return fmt.Sprintf("%s:%s", config2.GrafanaImage, config2.GrafanaVersion)
}

if cr.Spec.Version != "" {
return fmt.Sprintf("%s:%s", config2.GrafanaImage, cr.Spec.Version)
}

if grafanaImg == "" {
grafanaImg = fmt.Sprintf("%s:%s", config2.GrafanaImage, config2.GrafanaVersion)
if strings.ContainsAny(cr.Spec.Version, ":/@") {
return cr.Spec.Version
}
return grafanaImg
return fmt.Sprintf("%s:%s", config2.GrafanaImage, cr.Spec.Version)
}

func getContainers(cr *v1beta1.Grafana, scheme *runtime.Scheme, vars *v1beta1.OperatorReconcileVars, openshiftPlatform bool) []v1.Container {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ func Test_getGrafanaImage_specificVersion(t *testing.T) {
assert.Equal(t, expectedDeploymentImage, getGrafanaImage(cr))
}

func Test_getGrafanaImage_withEnvironmentOverride(t *testing.T) {
func Test_getGrafanaImage_withImageInVersion(t *testing.T) {
expectedDeploymentImage := "docker.io/grafana/grafana@sha256:b7fcb534f7b3512801bb3f4e658238846435804deb479d105b5cdc680847c272"
cr := &v1beta1.Grafana{
Spec: v1beta1.GrafanaSpec{
Version: "",
Version: expectedDeploymentImage,
},
}

expectedDeploymentImage := "I want this grafana image"
t.Setenv("RELATED_IMAGE_GRAFANA", expectedDeploymentImage)

assert.Equal(t, expectedDeploymentImage, getGrafanaImage(cr))
}
Loading