Skip to content

Commit

Permalink
added util func to check version and add version constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
akoserwal committed Mar 30, 2020
1 parent 49d29da commit c1f6333
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 31 deletions.
16 changes: 14 additions & 2 deletions pkg/controller/atlasmap/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package atlasmap

import (
"context"

"github.com/Masterminds/semver"
"github.com/atlasmap/atlasmap-operator/pkg/util"
"k8s.io/client-go/rest"

Expand Down Expand Up @@ -37,6 +37,18 @@ func newOperatorActions(log logr.Logger, mgr manager.Manager) []action {
log.Error(err, "Failed to determine cluster version. Defaulting to Kubernetes mode.")
}

var consoleLink action
if isOpenShift {
openShiftSemVer := util.GetClusterVersionSemVer(mgr.GetConfig())
constraint43, _ := semver.NewConstraint(">= 4.3")
isOpenShift43Plus := constraint43.Check(openShiftSemVer)

if isOpenShift43Plus {
consoleLink = newConsoleLinkAction(log.WithValues("type", "create-consolelink"), mgr)
}

}

var routeAction action
if isOpenShift {
routeAction = newRouteAction(log.WithValues("type", "create-route"), mgr)
Expand All @@ -47,7 +59,7 @@ func newOperatorActions(log logr.Logger, mgr manager.Manager) []action {
return []action{
newServiceAction(log.WithValues("type", "service"), mgr),
routeAction,
newConsoleLinkAction(log.WithValues("type", "create-consolelink"), mgr),
consoleLink,
newDeploymentAction(log.WithValues("type", "create-deployment"), mgr),
}
}
Expand Down
22 changes: 16 additions & 6 deletions pkg/controller/atlasmap/atlasmap_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package atlasmap
import (
"context"
consolev1 "github.com/openshift/api/console/v1"
"k8s.io/client-go/rest"
"reflect"

routev1 "github.com/openshift/api/route/v1"

configv1client "github.com/openshift/client-go/config/clientset/versioned"
"github.com/atlasmap/atlasmap-operator/pkg/apis/atlasmap/v1alpha1"
"github.com/atlasmap/atlasmap-operator/pkg/util"
appsv1 "k8s.io/api/apps/v1"
Expand All @@ -32,12 +33,19 @@ func Add(mgr manager.Manager) error {
if err != nil {
return err
}
return add(mgr, newReconciler(mgr))
}

// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
return &ReconcileAtlasMap{client: mgr.GetClient(), scheme: mgr.GetScheme()}
newReconciler := &ReconcileAtlasMap{
client: mgr.GetClient(),
config: mgr.GetConfig(),
scheme: mgr.GetScheme(),
}

configClient, err := configv1client.NewForConfig(mgr.GetConfig())
if err != nil {
return err
}
newReconciler.configClient = configClient
return add(mgr, newReconciler)
}

var actions []action
Expand Down Expand Up @@ -116,6 +124,8 @@ type ReconcileAtlasMap struct {
// that reads objects from the cache and writes to the apiserver
client client.Client
scheme *runtime.Scheme
config *rest.Config
configClient *configv1client.Clientset
}

// Reconcile reads that state of the cluster for a AtlasMap object and makes changes based on the state read
Expand Down
30 changes: 30 additions & 0 deletions pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package util

import (
"fmt"
"github.com/Masterminds/semver"
"github.com/atlasmap/atlasmap-operator/pkg/apis/atlasmap/v1alpha1"
configv1client "github.com/openshift/client-go/config/clientset/versioned"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"
"os"
Expand Down Expand Up @@ -48,3 +51,30 @@ func GetEnvVar(name string, defaultValue string) string {
}
return defaultValue
}

func GetClusterVersionSemVer(config *rest.Config) *semver.Version {
configClient, err := configv1client.NewForConfig(config)

var openShiftSemVer *semver.Version
clusterVersion, err := configClient.
ConfigV1().
ClusterVersions().
Get("version", metav1.GetOptions{})

if err != nil {
if errors.IsNotFound(err) {
// default to OpenShift 3 as ClusterVersion API was introduced in OpenShift 4
openShiftSemVer, _ = semver.NewVersion("3")
} else {
return nil
}
} else {
//latest version from the history
v := clusterVersion.Status.History[0].Version
openShiftSemVer, err = semver.NewVersion(v)
if err != nil {
return nil
}
}
return openShiftSemVer
}
53 changes: 30 additions & 23 deletions test/e2e/atlasmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"bytes"
goctx "context"
"fmt"
"github.com/Masterminds/semver"
"io"
"testing"
"time"
Expand Down Expand Up @@ -117,34 +118,40 @@ func atlasMapDeploymentTest(t *testing.T, f *framework.Framework, ctx *framework
host = atlasMapRoute.Spec.Host

//verify Console Link NamespaceDashboard
exampleConsoleLink := &consolev1.ConsoleLink{
ObjectMeta: metav1.ObjectMeta{
Name: crName,
Labels: map[string]string{"app": crName},
},
Spec: consolev1.ConsoleLinkSpec{
Location: consolev1.NamespaceDashboard,
NamespaceDashboard: &consolev1.NamespaceDashboardSpec{
Namespaces: []string{namespace},
openShiftSemVer := util.GetClusterVersionSemVer(f.KubeConfig)
constraint43, _ := semver.NewConstraint(">= 4.3")
isOpenShift43Plus := constraint43.Check(openShiftSemVer)

if isOpenShift43Plus {

exampleConsoleLink := &consolev1.ConsoleLink{
ObjectMeta: metav1.ObjectMeta{
Name: crName,
Labels: map[string]string{"app": crName},
},
},
}
exampleConsoleLink.Spec.Link.Text = "atlasmap"
exampleConsoleLink.Spec.Link.Href = "https://" + host
Spec: consolev1.ConsoleLinkSpec{
Location: consolev1.NamespaceDashboard,
NamespaceDashboard: &consolev1.NamespaceDashboardSpec{
Namespaces: []string{namespace},
},
},
}
exampleConsoleLink.Spec.Link.Text = "atlasmap"
exampleConsoleLink.Spec.Link.Href = "https://" + host

if err := f.Client.Create(goctx.TODO(), exampleConsoleLink, &framework.CleanupOptions{TestContext: ctx, Timeout: cleanupTimeout, RetryInterval: cleanupRetryInterval}); err != nil {
return err
}
if err := f.Client.Create(goctx.TODO(), exampleConsoleLink, &framework.CleanupOptions{TestContext: ctx, Timeout: cleanupTimeout, RetryInterval: cleanupRetryInterval}); err != nil {
return err
}

consoleLink := &consolev1.ConsoleLink{}
if err := f.Client.Get(goctx.TODO(), types.NamespacedName{Name: crName, Namespace: namespace}, consoleLink); err != nil {
return err
}
consoleLink := &consolev1.ConsoleLink{}
if err := f.Client.Get(goctx.TODO(), types.NamespacedName{Name: crName, Namespace: namespace}, consoleLink); err != nil {
return err
}

if consoleLink.Spec.Href != exampleConsoleLink.Spec.Href {
return fmt.Errorf("Expected ConsoleLink href to match %s but got %s", exampleConsoleLink.Spec.Href, consoleLink.Spec.Href)
if consoleLink.Spec.Href != exampleConsoleLink.Spec.Href {
return fmt.Errorf("Expected ConsoleLink href to match %s but got %s", exampleConsoleLink.Spec.Href, consoleLink.Spec.Href)
}
}

} else {
// Verify ingress was created
atlasMapIngress := &v1beta1.Ingress{}
Expand Down

0 comments on commit c1f6333

Please sign in to comment.