Skip to content

Commit

Permalink
Use v1 Ingresses when available
Browse files Browse the repository at this point in the history
Signed-off-by: Lucas Caparelli <[email protected]>
  • Loading branch information
LCaparelli committed Oct 28, 2020
1 parent 38b7f63 commit bf107b4
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 38 deletions.
9 changes: 5 additions & 4 deletions controllers/nexus/resource/deployment/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ import (

"github.com/m88i/nexus-operator/api/v1alpha1"
"github.com/m88i/nexus-operator/pkg/framework"
"github.com/m88i/nexus-operator/pkg/framework/kind"
"github.com/m88i/nexus-operator/pkg/logger"
)

var managedObjectsRef = map[string]resource.KubernetesResource{
framework.DeploymentKind: &appsv1.Deployment{},
framework.ServiceKind: &corev1.Service{},
kind.DeploymentKind: &appsv1.Deployment{},
kind.ServiceKind: &corev1.Service{},
}

// Manager is responsible for creating deployment-related resources, fetching deployed ones and comparing them
Expand All @@ -56,8 +57,8 @@ func NewManager(nexus *v1alpha1.Nexus, client client.Client) *Manager {

// GetRequiredResources returns the resources initialized by the manager
func (m *Manager) GetRequiredResources() ([]resource.KubernetesResource, error) {
m.log.Debug("Generating required resource", "kind", framework.DeploymentKind)
m.log.Debug("Generating required resource", "kind", framework.ServiceKind)
m.log.Debug("Generating required resource", "kind", kind.DeploymentKind)
m.log.Debug("Generating required resource", "kind", kind.ServiceKind)
return []resource.KubernetesResource{newDeployment(m.nexus), newService(m.nexus)}, nil
}

Expand Down
11 changes: 6 additions & 5 deletions controllers/nexus/resource/networking/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/m88i/nexus-operator/api/v1alpha1"
"github.com/m88i/nexus-operator/pkg/cluster/discovery"
"github.com/m88i/nexus-operator/pkg/framework"
"github.com/m88i/nexus-operator/pkg/framework/kind"
"github.com/m88i/nexus-operator/pkg/logger"
)

Expand Down Expand Up @@ -79,15 +80,15 @@ func NewManager(nexus *v1alpha1.Nexus, client client.Client) (*Manager, error) {

if ingressAvailable {
mgr.ingressAvailable = true
mgr.managedObjectsRef[framework.IngressKind] = &networkingv1.Ingress{}
mgr.managedObjectsRef[kind.IngressKind] = &networkingv1.Ingress{}
} else if legacyIngressAvailable {
mgr.legacyIngressAvailable = true
mgr.managedObjectsRef[framework.IngressKind] = &networkingv1beta1.Ingress{}
mgr.managedObjectsRef[kind.IngressKind] = &networkingv1beta1.Ingress{}
}

if routeAvailable {
mgr.routeAvailable = true
mgr.managedObjectsRef[framework.RouteKind] = &routev1.Route{}
mgr.managedObjectsRef[kind.RouteKind] = &routev1.Route{}
}

return mgr, nil
Expand All @@ -106,7 +107,7 @@ func (m *Manager) GetRequiredResources() ([]resource.KubernetesResource, error)
return nil, fmt.Errorf(resUnavailableFormat, "routes")
}

m.log.Debug("Generating required resource", "kind", framework.RouteKind)
m.log.Debug("Generating required resource", "kind", kind.RouteKind)
route := m.createRoute()
resources = append(resources, route)

Expand All @@ -115,7 +116,7 @@ func (m *Manager) GetRequiredResources() ([]resource.KubernetesResource, error)
return nil, fmt.Errorf(resUnavailableFormat, "ingresses")
}

m.log.Debug("Generating required resource", "kind", framework.IngressKind)
m.log.Debug("Generating required resource", "kind", kind.IngressKind)
ingress := m.createIngress()
resources = append(resources, ingress)
}
Expand Down
12 changes: 6 additions & 6 deletions controllers/nexus/resource/networking/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/m88i/nexus-operator/api/v1alpha1"
"github.com/m88i/nexus-operator/controllers/nexus/resource/deployment"
"github.com/m88i/nexus-operator/pkg/cluster/discovery"
"github.com/m88i/nexus-operator/pkg/framework"
"github.com/m88i/nexus-operator/pkg/framework/kind"
"github.com/m88i/nexus-operator/pkg/logger"
"github.com/m88i/nexus-operator/pkg/test"
)
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestNewManager(t *testing.T) {
ingressAvailable: true,
legacyIngressAvailable: false,
managedObjectsRef: map[string]resource.KubernetesResource{
framework.IngressKind: &networkingv1.Ingress{},
kind.IngressKind: &networkingv1.Ingress{},
},
},
k8sClientWithIngress,
Expand All @@ -78,7 +78,7 @@ func TestNewManager(t *testing.T) {
ingressAvailable: false,
legacyIngressAvailable: true,
managedObjectsRef: map[string]resource.KubernetesResource{
framework.IngressKind: &networkingv1beta1.Ingress{},
kind.IngressKind: &networkingv1beta1.Ingress{},
},
},
k8sClientWithLegacyIngress,
Expand All @@ -102,7 +102,7 @@ func TestNewManager(t *testing.T) {
ingressAvailable: false,
legacyIngressAvailable: false,
managedObjectsRef: map[string]resource.KubernetesResource{
framework.RouteKind: &routev1.Route{},
kind.RouteKind: &routev1.Route{},
},
},
ocpClient,
Expand Down Expand Up @@ -240,8 +240,8 @@ func TestManager_GetDeployedResources(t *testing.T) {
ingressAvailable: true,
routeAvailable: true,
managedObjectsRef: map[string]resource.KubernetesResource{
framework.RouteKind: &routev1.Route{},
framework.IngressKind: &networkingv1.Ingress{},
kind.RouteKind: &routev1.Route{},
kind.IngressKind: &networkingv1.Ingress{},
},
}
resources, err := mgr.GetDeployedResources()
Expand Down
5 changes: 3 additions & 2 deletions controllers/nexus/resource/persistence/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import (

"github.com/m88i/nexus-operator/api/v1alpha1"
"github.com/m88i/nexus-operator/pkg/framework"
"github.com/m88i/nexus-operator/pkg/framework/kind"
"github.com/m88i/nexus-operator/pkg/logger"
)

var managedObjectsRef = map[string]resource.KubernetesResource{
framework.PVCKind: &corev1.PersistentVolumeClaim{},
kind.PVCKind: &corev1.PersistentVolumeClaim{},
}

// Manager is responsible for creating persistence resources, fetching deployed ones and comparing them
Expand Down Expand Up @@ -57,7 +58,7 @@ func (m *Manager) GetRequiredResources() ([]resource.KubernetesResource, error)
return resources, nil
}

m.log.Debug("Generating required resource", "kind", framework.PVCKind)
m.log.Debug("Generating required resource", "kind", kind.PVCKind)
pvc := newPVC(m.nexus)
resources = append(resources, pvc)

Expand Down
9 changes: 5 additions & 4 deletions controllers/nexus/resource/security/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import (

"github.com/m88i/nexus-operator/api/v1alpha1"
"github.com/m88i/nexus-operator/pkg/framework"
"github.com/m88i/nexus-operator/pkg/framework/kind"
"github.com/m88i/nexus-operator/pkg/logger"
)

var managedObjectsRef = map[string]resource.KubernetesResource{
framework.SecretKind: &core.Secret{},
framework.SvcAccountKind: &core.ServiceAccount{},
kind.SecretKind: &core.Secret{},
kind.SvcAccountKind: &core.ServiceAccount{},
}

// Manager is responsible for creating security resources, fetching deployed ones and comparing them
Expand All @@ -52,8 +53,8 @@ func NewManager(nexus *v1alpha1.Nexus, client client.Client) *Manager {

// GetRequiredResources returns the resources initialized by the Manager
func (m *Manager) GetRequiredResources() ([]resource.KubernetesResource, error) {
m.log.Debug("Generating required resource", "kind", framework.SvcAccountKind)
m.log.Debug("Generating required resource", "kind", framework.SecretKind)
m.log.Debug("Generating required resource", "kind", kind.SvcAccountKind)
m.log.Debug("Generating required resource", "kind", kind.SecretKind)
return []resource.KubernetesResource{defaultServiceAccount(m.nexus), defaultSecret(m.nexus)}, nil
}

Expand Down
5 changes: 3 additions & 2 deletions controllers/nexus/resource/security/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"reflect"
"testing"

"github.com/m88i/nexus-operator/pkg/framework/kind"
"github.com/m88i/nexus-operator/pkg/logger"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -102,13 +103,13 @@ func TestManager_getDeployedSvcAccnt(t *testing.T) {
}

// first, test without creating the svcAccnt
err := framework.Fetch(mgr.client, framework.Key(mgr.nexus), managedObjectsRef[framework.SvcAccountKind], framework.SvcAccountKind)
err := framework.Fetch(mgr.client, framework.Key(mgr.nexus), managedObjectsRef[kind.SvcAccountKind], kind.SvcAccountKind)
assert.True(t, errors.IsNotFound(err))

// now test after creating the svcAccnt
svcAccnt := &corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Name: mgr.nexus.Name, Namespace: mgr.nexus.Namespace}}
assert.NoError(t, mgr.client.Create(ctx.TODO(), svcAccnt))
err = framework.Fetch(mgr.client, framework.Key(svcAccnt), svcAccnt, framework.SvcAccountKind)
err = framework.Fetch(mgr.client, framework.Key(svcAccnt), svcAccnt, kind.SvcAccountKind)
assert.NotNil(t, svcAccnt)
assert.NoError(t, err)
}
Expand Down
5 changes: 3 additions & 2 deletions controllers/nexus/server/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
corev1 "k8s.io/api/core/v1"

"github.com/m88i/nexus-operator/pkg/framework"
"github.com/m88i/nexus-operator/pkg/framework/kind"
)

const (
Expand Down Expand Up @@ -107,7 +108,7 @@ func (u *userOperation) createOperatorUserIfNotExists() (*nexus.User, error) {
func (u *userOperation) storeOperatorUserCredentials(user *nexus.User) error {
secret := &corev1.Secret{}
log.Debug("Attempt to store operator user credentials into Secret")
if err := framework.Fetch(u.k8sclient, framework.Key(u.nexus), secret, framework.SecretKind); err != nil {
if err := framework.Fetch(u.k8sclient, framework.Key(u.nexus), secret, kind.SecretKind); err != nil {
return err
}
if secret.StringData == nil {
Expand All @@ -124,7 +125,7 @@ func (u *userOperation) storeOperatorUserCredentials(user *nexus.User) error {

func (u *userOperation) getOperatorUserCredentials() (user, password string, err error) {
secret := &corev1.Secret{}
if err := framework.Fetch(u.k8sclient, framework.Key(u.nexus), secret, framework.SecretKind); err != nil {
if err := framework.Fetch(u.k8sclient, framework.Key(u.nexus), secret, kind.SecretKind); err != nil {
return "", "", err
}
return string(secret.Data[SecretKeyUsername]), string(secret.Data[SecretKeyPassword]), nil
Expand Down
19 changes: 19 additions & 0 deletions pkg/cluster/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package discovery

import (
"fmt"
"strings"

"k8s.io/client-go/discovery"
Expand Down Expand Up @@ -62,3 +63,21 @@ func hasGroupVersion(group, version string) (bool, error) {
}
return false, nil
}

// hasGroupVersionKind checks if the given group name, version and kind is available in the cluster
func hasGroupVersionKind(group, version, kind string) (bool, error) {
if hasGroupVersion, err := hasGroupVersion(group, version); err != nil || !hasGroupVersion {
return false, err
}

resources, err := cli.ServerResourcesForGroupVersion(fmt.Sprintf("%s/%s", group, version))
if err != nil {
return false, err
}
for _, resource := range resources.APIResources {
if resource.Kind == kind {
return true, nil
}
}
return false, nil
}
6 changes: 4 additions & 2 deletions pkg/cluster/discovery/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ package discovery
import (
networkingv1 "k8s.io/api/networking/v1"
networkingv1beta1 "k8s.io/api/networking/v1beta1"

"github.com/m88i/nexus-operator/pkg/framework/kind"
)

// IsIngressAvailable checks if the cluster supports Ingresses from k8s.io/api/networking/v1
func IsIngressAvailable() (bool, error) {
return hasGroupVersion(networkingv1.GroupName, networkingv1.SchemeGroupVersion.Version)
return hasGroupVersionKind(networkingv1.SchemeGroupVersion.Group, networkingv1.SchemeGroupVersion.Version, kind.IngressKind)
}

// IsLegacyIngressAvailable checks if the cluster supports Ingresses from k8s.io/api/networking/v1beta1
func IsLegacyIngressAvailable() (bool, error) {
return hasGroupVersion(networkingv1beta1.GroupName, networkingv1beta1.SchemeGroupVersion.Version)
return hasGroupVersionKind(networkingv1beta1.SchemeGroupVersion.Group, networkingv1beta1.SchemeGroupVersion.Version, kind.IngressKind)
}
4 changes: 3 additions & 1 deletion pkg/cluster/discovery/openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package discovery

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

"github.com/m88i/nexus-operator/pkg/framework/kind"
)

const (
Expand All @@ -29,5 +31,5 @@ func IsOpenShift() (bool, error) {

// IsRouteAvailable verifies if the current cluster has the Route API from OpenShift available
func IsRouteAvailable() (bool, error) {
return hasGroupVersion(routev1.GroupName, routev1.GroupVersion.Version)
return hasGroupVersionKind(routev1.GroupVersion.Group, routev1.GroupVersion.Version, kind.RouteKind)
}
13 changes: 7 additions & 6 deletions pkg/framework/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/m88i/nexus-operator/api/v1alpha1"
"github.com/m88i/nexus-operator/pkg/framework/kind"
"github.com/m88i/nexus-operator/pkg/test"
)

Expand All @@ -34,10 +35,10 @@ func TestFetchDeployedResources(t *testing.T) {
deployment := &appsv1.Deployment{ObjectMeta: nexus.ObjectMeta}
service := &corev1.Service{ObjectMeta: nexus.ObjectMeta}
managedObjectsRef := map[string]resource.KubernetesResource{
ServiceKind: &corev1.Service{},
DeploymentKind: &appsv1.Deployment{},
kind.ServiceKind: &corev1.Service{},
kind.DeploymentKind: &appsv1.Deployment{},
// we won't have a SA, but this is useful to test no error is triggered when a resource isn't found
SvcAccountKind: &corev1.ServiceAccount{},
kind.SvcAccountKind: &corev1.ServiceAccount{},
}
cli := test.NewFakeClientBuilder(deployment, service).Build()

Expand All @@ -50,7 +51,7 @@ func TestFetchDeployedResources(t *testing.T) {
func TestFetchDeployedResourcesFailure(t *testing.T) {
nexus := &v1alpha1.Nexus{ObjectMeta: metav1.ObjectMeta{Name: "nexus-test", Namespace: t.Name()}}
// managedObjectsRef cannot be empty in order to raise error, the content is irrelevant though
managedObjectsRef := map[string]resource.KubernetesResource{DeploymentKind: &appsv1.Deployment{}}
managedObjectsRef := map[string]resource.KubernetesResource{kind.DeploymentKind: &appsv1.Deployment{}}
cli := test.NewFakeClientBuilder().Build()
mockErrorMsg := "mock error"

Expand All @@ -63,14 +64,14 @@ func TestFetchDeployedResourcesFailure(t *testing.T) {
func TestFetch(t *testing.T) {
deployment := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "deployment", Namespace: t.Name()}}
cli := test.NewFakeClientBuilder(deployment).Build()
err := Fetch(cli, Key(deployment), deployment, DeploymentKind)
err := Fetch(cli, Key(deployment), deployment, kind.DeploymentKind)
assert.NoError(t, err)
}

func TestNotFoundFetch(t *testing.T) {
deployment := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "deployment", Namespace: t.Name()}}
cli := test.NewFakeClientBuilder().Build()
err := Fetch(cli, Key(deployment), deployment, DeploymentKind)
err := Fetch(cli, Key(deployment), deployment, kind.DeploymentKind)
assert.Error(t, err)
assert.True(t, errors.IsNotFound(err))
}
2 changes: 1 addition & 1 deletion pkg/framework/kinds.go → pkg/framework/kind/kinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package framework
package kind

const (
DeploymentKind = "Deployment"
Expand Down
7 changes: 4 additions & 3 deletions pkg/test/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/m88i/nexus-operator/api/v1alpha1"
"github.com/m88i/nexus-operator/pkg/framework/kind"
"github.com/m88i/nexus-operator/pkg/util"
)

Expand Down Expand Up @@ -67,21 +68,21 @@ func (b *FakeClientBuilder) OnOpenshift() *FakeClientBuilder {
util.Must(schemeBuilderOnOCP().AddToScheme(b.scheme))
b.resources = append(b.resources,
&metav1.APIResourceList{GroupVersion: openshiftGroupVersion},
&metav1.APIResourceList{GroupVersion: routev1.GroupVersion.String()})
&metav1.APIResourceList{GroupVersion: routev1.GroupVersion.String(), APIResources: []metav1.APIResource{{Kind: kind.RouteKind}}})
return b
}

// WithIngress makes the fake client aware of v1 Ingresses
func (b *FakeClientBuilder) WithIngress() *FakeClientBuilder {
util.Must(schemeBuilderWithIngress().AddToScheme(b.scheme))
b.resources = append(b.resources, &metav1.APIResourceList{GroupVersion: networkingv1.SchemeGroupVersion.String()})
b.resources = append(b.resources, &metav1.APIResourceList{GroupVersion: networkingv1.SchemeGroupVersion.String(), APIResources: []metav1.APIResource{{Kind: kind.IngressKind}}})
return b
}

// WithLegacyIngress makes the fake client aware of v1beta1 Ingresses
func (b *FakeClientBuilder) WithLegacyIngress() *FakeClientBuilder {
util.Must(schemeBuilderWithLegacyIngress().AddToScheme(b.scheme))
b.resources = append(b.resources, &metav1.APIResourceList{GroupVersion: networkingv1beta1.SchemeGroupVersion.String()})
b.resources = append(b.resources, &metav1.APIResourceList{GroupVersion: networkingv1beta1.SchemeGroupVersion.String(), APIResources: []metav1.APIResource{{Kind: kind.IngressKind}}})
return b
}

Expand Down

0 comments on commit bf107b4

Please sign in to comment.