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

[main] Render: fail if no oauth image is found+kind fix #1677

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
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type MultiClusterObservabilityReconciler struct {
CRDMap map[string]bool
APIReader client.Reader
RESTMapper meta.RESTMapper
ImageClient *imagev1client.ImageV1Client
ImageClient imagev1client.ImageV1Interface
}

// +kubebuilder:rbac:groups=observability.open-cluster-management.io,resources=multiclusterobservabilities,verbs=get;list;watch;create;update;patch;delete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
migrationv1alpha1 "sigs.k8s.io/kube-storage-version-migrator/pkg/apis/migration/v1alpha1"

imagev1 "github.com/openshift/api/image/v1"
fakeimageclient "github.com/openshift/client-go/image/clientset/versioned/fake"
fakeimagev1client "github.com/openshift/client-go/image/clientset/versioned/typed/image/v1/fake"
)

func init() {
Expand Down Expand Up @@ -350,8 +354,32 @@ func TestMultiClusterMonitoringCRUpdate(t *testing.T) {
).
Build()

// Create fake imagestream client
imageClient := &fakeimagev1client.FakeImageV1{Fake: &(fakeimageclient.NewSimpleClientset().Fake)}
_, err := imageClient.ImageStreams(config.OauthProxyImageStreamNamespace).Create(context.Background(),
&imagev1.ImageStream{
ObjectMeta: metav1.ObjectMeta{
Name: config.OauthProxyImageStreamName,
Namespace: config.OauthProxyImageStreamNamespace,
},
Spec: imagev1.ImageStreamSpec{
Tags: []imagev1.TagReference{
{
Name: "v4.4",
From: &corev1.ObjectReference{
Kind: "DockerImage",
Name: "quay.io/openshift-release-dev/ocp-v4.0-art-dev",
},
},
},
},
}, metav1.CreateOptions{})
if err != nil {
t.Fatal(err)
}

// Create a ReconcileMemcached object with the scheme and fake client.
r := &MultiClusterObservabilityReconciler{Client: cl, Scheme: s, CRDMap: map[string]bool{config.IngressControllerCRD: true}}
r := &MultiClusterObservabilityReconciler{Client: cl, Scheme: s, CRDMap: map[string]bool{config.IngressControllerCRD: true}, ImageClient: imageClient}
config.SetMonitoringCRName(name)
// Mock request to simulate Reconcile() being called on an event for a
// watched resource .
Expand All @@ -363,7 +391,7 @@ func TestMultiClusterMonitoringCRUpdate(t *testing.T) {

// Create empty client. The test secret specified in MCO is not yet created.
t.Log("Reconcile empty client")
_, err := r.Reconcile(context.TODO(), req)
_, err = r.Reconcile(context.TODO(), req)
if err != nil {
t.Fatalf("reconcile: (%v)", err)
}
Expand Down Expand Up @@ -759,8 +787,32 @@ func TestImageReplaceForMCO(t *testing.T) {
// Create a fake client to mock API calls.
cl := fake.NewClientBuilder().WithRuntimeObjects(objs...).Build()

// Create fake imagestream client
imageClient := &fakeimagev1client.FakeImageV1{Fake: &(fakeimageclient.NewSimpleClientset().Fake)}
_, err := imageClient.ImageStreams(config.OauthProxyImageStreamNamespace).Create(context.Background(),
&imagev1.ImageStream{
ObjectMeta: metav1.ObjectMeta{
Name: config.OauthProxyImageStreamName,
Namespace: config.OauthProxyImageStreamNamespace,
},
Spec: imagev1.ImageStreamSpec{
Tags: []imagev1.TagReference{
{
Name: "v4.4",
From: &corev1.ObjectReference{
Kind: "DockerImage",
Name: "quay.io/openshift-release-dev/ocp-v4.0-art-dev",
},
},
},
},
}, metav1.CreateOptions{})
if err != nil {
t.Fatal(err)
}

// Create a ReconcileMemcached object with the scheme and fake client.
r := &MultiClusterObservabilityReconciler{Client: cl, Scheme: s, CRDMap: map[string]bool{config.MCHCrdName: true, config.IngressControllerCRD: true}}
r := &MultiClusterObservabilityReconciler{Client: cl, Scheme: s, CRDMap: map[string]bool{config.MCHCrdName: true, config.IngressControllerCRD: true}, ImageClient: imageClient}
config.SetMonitoringCRName(name)

// Mock request to simulate Reconcile() being called on an event for a watched resource .
Expand All @@ -775,7 +827,7 @@ func TestImageReplaceForMCO(t *testing.T) {
config.SetImageManifests(testImagemanifestsMap)

// trigger another reconcile for MCH update event
_, err := r.Reconcile(context.TODO(), req)
_, err = r.Reconcile(context.TODO(), req)
if err != nil {
t.Fatalf("reconcile: (%v)", err)
}
Expand Down
24 changes: 22 additions & 2 deletions operators/multiclusterobservability/pkg/rendering/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/discovery"
logf "sigs.k8s.io/controller-runtime/pkg/log"

obv1beta2 "github.com/stolostron/multicluster-observability-operator/operators/multiclusterobservability/api/v1beta2"
Expand All @@ -29,7 +30,7 @@ type RendererOptions struct {

type MCORenderer struct {
kubeClient client.Client
imageClient *imagev1client.ImageV1Client
imageClient imagev1client.ImageV1Interface
renderer *rendererutil.Renderer
cr *obv1beta2.MultiClusterObservability
rendererOptions *RendererOptions
Expand All @@ -40,7 +41,7 @@ type MCORenderer struct {
renderMCOAFns map[string]rendererutil.RenderFn
}

func NewMCORenderer(multipleClusterMonitoring *obv1beta2.MultiClusterObservability, kubeClient client.Client, imageClient *imagev1client.ImageV1Client) *MCORenderer {
func NewMCORenderer(multipleClusterMonitoring *obv1beta2.MultiClusterObservability, kubeClient client.Client, imageClient imagev1client.ImageV1Interface) *MCORenderer {
mcoRenderer := &MCORenderer{
renderer: rendererutil.NewRenderer(),
cr: multipleClusterMonitoring,
Expand Down Expand Up @@ -193,3 +194,22 @@ func (r *MCORenderer) MCOAResources(namespace string, labels map[string]string)

return mcoaResources, nil
}

func (r *MCORenderer) HasImagestream() bool {
dcl := discovery.NewDiscoveryClient(r.imageClient.RESTClient())

apiList, err := dcl.ServerGroups()
if err != nil {
log.Error(err, "unable to get ServerGroups from imagestream detection")
return false
}

apiGroups := apiList.Groups
for i := 0; i < len(apiGroups); i++ {
if apiGroups[i].Name == "image.openshift.io" {
return true
}
}

return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,14 @@ func (r *MCORenderer) renderAlertManagerStatefulSet(res *resource.Resource, name
configReloaderContainer.Image = image
}

// If we're on OCP and has imagestreams, we always want the oauth image
// from the imagestream, and fail the reconcile if we don't find it.
// If we're on non-OCP (tests) we use the base template image
found, image = mcoconfig.GetOauthProxyImage(r.imageClient)
if found {
oauthProxyContainer.Image = image
} else if r.HasImagestream() {
return nil, fmt.Errorf("failed to get OAuth image for alertmanager")
}
oauthProxyContainer.ImagePullPolicy = imagePullPolicy

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
package rendering

import (
"context"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"testing"

imagev1 "github.com/openshift/api/image/v1"
fakeimageclient "github.com/openshift/client-go/image/clientset/versioned/fake"
fakeimagev1client "github.com/openshift/client-go/image/clientset/versioned/typed/image/v1/fake"
mcoshared "github.com/stolostron/multicluster-observability-operator/operators/multiclusterobservability/api/shared"
mcov1beta2 "github.com/stolostron/multicluster-observability-operator/operators/multiclusterobservability/api/v1beta2"
"github.com/stolostron/multicluster-observability-operator/operators/multiclusterobservability/pkg/config"
Expand Down Expand Up @@ -288,7 +292,31 @@ func renderTemplates(t *testing.T, kubeClient client.Client, mco *mcov1beta2.Mul
defer os.Unsetenv(templatesutil.TemplatesPathEnvVar)

config.ReadImageManifestConfigMap(kubeClient, "v1")
renderer := NewMCORenderer(mco, kubeClient, nil)

imageClient := &fakeimagev1client.FakeImageV1{Fake: &(fakeimageclient.NewSimpleClientset().Fake)}
_, err = imageClient.ImageStreams(config.OauthProxyImageStreamNamespace).Create(context.Background(),
&imagev1.ImageStream{
ObjectMeta: metav1.ObjectMeta{
Name: config.OauthProxyImageStreamName,
Namespace: config.OauthProxyImageStreamNamespace,
},
Spec: imagev1.ImageStreamSpec{
Tags: []imagev1.TagReference{
{
Name: "v4.4",
From: &corev1.ObjectReference{
Kind: "DockerImage",
Name: "quay.io/openshift-release-dev/ocp-v4.0-art-dev",
},
},
},
},
}, metav1.CreateOptions{})
if err != nil {
t.Fatal(err)
}

renderer := NewMCORenderer(mco, kubeClient, imageClient)

//load and render alertmanager templates
alertTemplates, err := templates.GetOrLoadAlertManagerTemplates(templatesutil.GetTemplateRenderer())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package rendering

import (
"fmt"

v1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -68,9 +70,14 @@ func (r *MCORenderer) renderGrafanaDeployments(res *resource.Resource,
}
spec.Containers[1].ImagePullPolicy = imagePullPolicy

// If we're on OCP and has imagestreams, we always want the oauth image
// from the imagestream, and fail the reconcile if we don't find it.
// If we're on non-OCP (tests) we use the base template image
found, image = config.GetOauthProxyImage(r.imageClient)
if found {
spec.Containers[2].Image = image
} else if r.HasImagestream() {
return nil, fmt.Errorf("failed to get OAuth image for alertmanager")
}
spec.Containers[2].ImagePullPolicy = imagePullPolicy

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package rendering

import (
"fmt"
"strings"

v1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -93,9 +94,14 @@ func (r *MCORenderer) renderProxyDeployment(res *resource.Resource,
spec.Containers[0].Image = image
}

// If we're on OCP and has imagestreams, we always want the oauth image
// from the imagestream, and fail the reconcile if we don't find it.
// If we're on non-OCP (tests) we use the base template image
found, image = mcoconfig.GetOauthProxyImage(r.imageClient)
if found {
spec.Containers[1].Image = image
} else if r.HasImagestream() {
return nil, fmt.Errorf("failed to get OAuth image for alertmanager")
}

for idx := range spec.Volumes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,30 @@ func TestRender(t *testing.T) {
}
kubeClient := fake.NewClientBuilder().WithObjects(clientCa).Build()

renderer := NewMCORenderer(mchcr, kubeClient, nil)
imageClient := &fakeimagev1client.FakeImageV1{Fake: &(fakeimageclient.NewSimpleClientset().Fake)}
_, err = imageClient.ImageStreams(config.OauthProxyImageStreamNamespace).Create(context.Background(),
&imagev1.ImageStream{
ObjectMeta: metav1.ObjectMeta{
Name: config.OauthProxyImageStreamName,
Namespace: config.OauthProxyImageStreamNamespace,
},
Spec: imagev1.ImageStreamSpec{
Tags: []imagev1.TagReference{
{
Name: "v4.4",
From: &corev1.ObjectReference{
Kind: "DockerImage",
Name: "quay.io/openshift-release-dev/ocp-v4.0-art-dev",
},
},
},
},
}, metav1.CreateOptions{})
if err != nil {
t.Fatal(err)
}

renderer := NewMCORenderer(mchcr, kubeClient, imageClient)
_, err = renderer.Render()
if err != nil {
t.Fatalf("failed to render MultiClusterObservability: %v", err)
Expand Down
Loading