From bf107b48282ade627a8e7a4900f9c6a4a79996fb Mon Sep 17 00:00:00 2001 From: Lucas Caparelli Date: Wed, 28 Oct 2020 10:12:18 -0300 Subject: [PATCH] Use v1 Ingresses when available Signed-off-by: Lucas Caparelli --- .../nexus/resource/deployment/manager.go | 9 +++++---- .../nexus/resource/networking/manager.go | 11 ++++++----- .../nexus/resource/networking/manager_test.go | 12 ++++++------ .../nexus/resource/persistence/manager.go | 5 +++-- .../nexus/resource/security/manager.go | 9 +++++---- .../nexus/resource/security/manager_test.go | 5 +++-- controllers/nexus/server/users.go | 5 +++-- pkg/cluster/discovery/discovery.go | 19 +++++++++++++++++++ pkg/cluster/discovery/kubernetes.go | 6 ++++-- pkg/cluster/discovery/openshift.go | 4 +++- pkg/framework/fetcher_test.go | 13 +++++++------ pkg/framework/{ => kind}/kinds.go | 2 +- pkg/test/client.go | 7 ++++--- 13 files changed, 69 insertions(+), 38 deletions(-) rename pkg/framework/{ => kind}/kinds.go (97%) diff --git a/controllers/nexus/resource/deployment/manager.go b/controllers/nexus/resource/deployment/manager.go index 5051501f..d96fe06d 100644 --- a/controllers/nexus/resource/deployment/manager.go +++ b/controllers/nexus/resource/deployment/manager.go @@ -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 @@ -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 } diff --git a/controllers/nexus/resource/networking/manager.go b/controllers/nexus/resource/networking/manager.go index ccfbac2b..718efe6e 100644 --- a/controllers/nexus/resource/networking/manager.go +++ b/controllers/nexus/resource/networking/manager.go @@ -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" ) @@ -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 @@ -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) @@ -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) } diff --git a/controllers/nexus/resource/networking/manager_test.go b/controllers/nexus/resource/networking/manager_test.go index 5875eeb6..fc4638f9 100644 --- a/controllers/nexus/resource/networking/manager_test.go +++ b/controllers/nexus/resource/networking/manager_test.go @@ -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" ) @@ -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, @@ -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, @@ -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, @@ -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() diff --git a/controllers/nexus/resource/persistence/manager.go b/controllers/nexus/resource/persistence/manager.go index 64d7d5a3..1b879627 100644 --- a/controllers/nexus/resource/persistence/manager.go +++ b/controllers/nexus/resource/persistence/manager.go @@ -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 @@ -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) diff --git a/controllers/nexus/resource/security/manager.go b/controllers/nexus/resource/security/manager.go index bb71f931..dd0217ff 100644 --- a/controllers/nexus/resource/security/manager.go +++ b/controllers/nexus/resource/security/manager.go @@ -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 @@ -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 } diff --git a/controllers/nexus/resource/security/manager_test.go b/controllers/nexus/resource/security/manager_test.go index 08087dd5..1a70b321 100644 --- a/controllers/nexus/resource/security/manager_test.go +++ b/controllers/nexus/resource/security/manager_test.go @@ -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" @@ -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) } diff --git a/controllers/nexus/server/users.go b/controllers/nexus/server/users.go index db4a5d36..0b1e4ad0 100644 --- a/controllers/nexus/server/users.go +++ b/controllers/nexus/server/users.go @@ -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 ( @@ -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 { @@ -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 diff --git a/pkg/cluster/discovery/discovery.go b/pkg/cluster/discovery/discovery.go index be9a5feb..3d20ebe9 100644 --- a/pkg/cluster/discovery/discovery.go +++ b/pkg/cluster/discovery/discovery.go @@ -15,6 +15,7 @@ package discovery import ( + "fmt" "strings" "k8s.io/client-go/discovery" @@ -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 +} diff --git a/pkg/cluster/discovery/kubernetes.go b/pkg/cluster/discovery/kubernetes.go index 7e499c84..a36d571f 100644 --- a/pkg/cluster/discovery/kubernetes.go +++ b/pkg/cluster/discovery/kubernetes.go @@ -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) } diff --git a/pkg/cluster/discovery/openshift.go b/pkg/cluster/discovery/openshift.go index 96cbd7c8..4168562b 100644 --- a/pkg/cluster/discovery/openshift.go +++ b/pkg/cluster/discovery/openshift.go @@ -16,6 +16,8 @@ package discovery import ( routev1 "github.com/openshift/api/route/v1" + + "github.com/m88i/nexus-operator/pkg/framework/kind" ) const ( @@ -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) } diff --git a/pkg/framework/fetcher_test.go b/pkg/framework/fetcher_test.go index afb38a99..f5fe3e90 100644 --- a/pkg/framework/fetcher_test.go +++ b/pkg/framework/fetcher_test.go @@ -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" ) @@ -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() @@ -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" @@ -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)) } diff --git a/pkg/framework/kinds.go b/pkg/framework/kind/kinds.go similarity index 97% rename from pkg/framework/kinds.go rename to pkg/framework/kind/kinds.go index d3b4bc10..63d95c16 100644 --- a/pkg/framework/kinds.go +++ b/pkg/framework/kind/kinds.go @@ -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" diff --git a/pkg/test/client.go b/pkg/test/client.go index 6739fb14..e1632c66 100644 --- a/pkg/test/client.go +++ b/pkg/test/client.go @@ -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" ) @@ -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 }