From d76295ab27e8d3fcbd9f099d2b46a047c6d0fbfa Mon Sep 17 00:00:00 2001 From: Ewout Prangsma Date: Fri, 23 Mar 2018 15:01:56 +0100 Subject: [PATCH] Moved PVC+Services to resources package --- pkg/deployment/deployment.go | 4 +-- pkg/deployment/deployment_inspector.go | 6 +++- pkg/deployment/{ => resources}/pvcs.go | 15 ++++---- pkg/deployment/{ => resources}/services.go | 40 +++++++++++++--------- 4 files changed, 40 insertions(+), 25 deletions(-) rename pkg/deployment/{ => resources}/pvcs.go (76%) rename pkg/deployment/{ => resources}/services.go (60%) diff --git a/pkg/deployment/deployment.go b/pkg/deployment/deployment.go index 8be572802..7a9c723f5 100644 --- a/pkg/deployment/deployment.go +++ b/pkg/deployment/deployment.go @@ -176,7 +176,7 @@ func (d *Deployment) run() { } // Create services - if err := d.createServices(d.apiObject); err != nil { + if err := d.resources.EnsureServices(); err != nil { d.failOnError(err, "Failed to create services") return } @@ -188,7 +188,7 @@ func (d *Deployment) run() { } // Create PVCs - if err := d.ensurePVCs(d.apiObject); err != nil { + if err := d.resources.EnsurePVCs(); err != nil { d.failOnError(err, "Failed to create persistent volume claims") return } diff --git a/pkg/deployment/deployment_inspector.go b/pkg/deployment/deployment_inspector.go index 0ce399324..8f629d4fd 100644 --- a/pkg/deployment/deployment_inspector.go +++ b/pkg/deployment/deployment_inspector.go @@ -74,7 +74,11 @@ func (d *Deployment) inspectDeployment(lastInterval time.Duration) time.Duration } // Ensure all resources are created - if err := d.ensurePVCs(d.apiObject); err != nil { + if err := d.resources.EnsureServices(); err != nil { + hasError = true + d.CreateEvent(k8sutil.NewErrorEvent("Service creation failed", err, d.apiObject)) + } + if err := d.resources.EnsurePVCs(); err != nil { hasError = true d.CreateEvent(k8sutil.NewErrorEvent("PVC creation failed", err, d.apiObject)) } diff --git a/pkg/deployment/pvcs.go b/pkg/deployment/resources/pvcs.go similarity index 76% rename from pkg/deployment/pvcs.go rename to pkg/deployment/resources/pvcs.go index b901a0aef..abb2b135b 100644 --- a/pkg/deployment/pvcs.go +++ b/pkg/deployment/resources/pvcs.go @@ -20,7 +20,7 @@ // Author Ewout Prangsma // -package deployment +package resources import ( api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1alpha" @@ -28,14 +28,17 @@ import ( "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" ) -// ensurePVCs creates all PVC's listed in member status -func (d *Deployment) ensurePVCs(apiObject *api.ArangoDeployment) error { - kubecli := d.deps.KubeCli +// EnsurePVCs creates all PVC's listed in member status +func (r *Resources) EnsurePVCs() error { + kubecli := r.context.GetKubeCli() + apiObject := r.context.GetAPIObject() deploymentName := apiObject.GetName() ns := apiObject.GetNamespace() owner := apiObject.AsOwner() + iterator := r.context.GetServerGroupIterator() + status := r.context.GetStatus() - if err := apiObject.ForeachServerGroup(func(group api.ServerGroup, spec api.ServerGroupSpec, status *api.MemberStatusList) error { + if err := iterator.ForeachServerGroup(func(group api.ServerGroup, spec api.ServerGroupSpec, status *api.MemberStatusList) error { for _, m := range *status { if m.PersistentVolumeClaimName != "" { storageClassName := spec.GetStorageClassName() @@ -47,7 +50,7 @@ func (d *Deployment) ensurePVCs(apiObject *api.ArangoDeployment) error { } } return nil - }, &d.status); err != nil { + }, &status); err != nil { return maskAny(err) } return nil diff --git a/pkg/deployment/services.go b/pkg/deployment/resources/services.go similarity index 60% rename from pkg/deployment/services.go rename to pkg/deployment/resources/services.go index 6063dcb6d..5831a5e0f 100644 --- a/pkg/deployment/services.go +++ b/pkg/deployment/resources/services.go @@ -20,18 +20,19 @@ // Author Ewout Prangsma // -package deployment +package resources import ( - api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1alpha" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" ) -// createServices creates all services needed to service the given deployment -func (d *Deployment) createServices(apiObject *api.ArangoDeployment) error { - log := d.deps.Log - kubecli := d.deps.KubeCli +// EnsureServices creates all services needed to service the deployment +func (r *Resources) EnsureServices() error { + log := r.log + kubecli := r.context.GetKubeCli() + apiObject := r.context.GetAPIObject() owner := apiObject.AsOwner() + spec := r.context.GetSpec() log.Debug().Msg("creating services...") @@ -39,23 +40,30 @@ func (d *Deployment) createServices(apiObject *api.ArangoDeployment) error { log.Debug().Err(err).Msg("Failed to create headless service") return maskAny(err) } - single := apiObject.Spec.GetMode().HasSingleServers() - if svcName, err := k8sutil.CreateDatabaseClientService(kubecli, apiObject, single, owner); err != nil { + single := spec.GetMode().HasSingleServers() + svcName, err := k8sutil.CreateDatabaseClientService(kubecli, apiObject, single, owner) + if err != nil { log.Debug().Err(err).Msg("Failed to create database client service") return maskAny(err) - } else { - d.status.ServiceName = svcName - if err := d.updateCRStatus(); err != nil { + } + status := r.context.GetStatus() + if status.ServiceName != svcName { + status.ServiceName = svcName + if err := r.context.UpdateStatus(status); err != nil { return maskAny(err) } } - if apiObject.Spec.Sync.IsEnabled() { - if svcName, err := k8sutil.CreateSyncMasterClientService(kubecli, apiObject, owner); err != nil { + + if spec.Sync.IsEnabled() { + svcName, err := k8sutil.CreateSyncMasterClientService(kubecli, apiObject, owner) + if err != nil { log.Debug().Err(err).Msg("Failed to create syncmaster client service") return maskAny(err) - } else { - d.status.ServiceName = svcName - if err := d.updateCRStatus(); err != nil { + } + status := r.context.GetStatus() + if status.SyncServiceName != svcName { + status.SyncServiceName = svcName + if err := r.context.UpdateStatus(status); err != nil { return maskAny(err) } }