Skip to content

Commit

Permalink
Remove explicit garbage collection (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
wallyqs authored and pires committed Mar 22, 2019
1 parent df2726b commit 6ea6932
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 214 deletions.
22 changes: 0 additions & 22 deletions cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import (
"github.com/nats-io/nats-operator/pkg/debug"
"github.com/nats-io/nats-operator/pkg/debug/local"
"github.com/nats-io/nats-operator/pkg/features"
"github.com/nats-io/nats-operator/pkg/garbagecollection"
kubernetesutil "github.com/nats-io/nats-operator/pkg/util/kubernetes"
"github.com/nats-io/nats-operator/pkg/util/probe"
"github.com/nats-io/nats-operator/version"
Expand Down Expand Up @@ -210,17 +209,6 @@ func run(ctx context.Context, featureMap features.FeatureMap, kubeCfg *rest.Conf
// Initialize the controller for NatsCluster resources.
c := controller.NewNatsClusterController(cfg)

// Start the garbage collector.
var (
gcNamespace string
)
if featureMap.IsEnabled(features.ClusterScoped) {
gcNamespace = v1.NamespaceAll
} else {
gcNamespace = namespace
}
go periodicFullGC(cfg.KubeCli.CoreV1(), gcNamespace, gcInterval)

// Start the chaos engine if the current instance is not cluster-scoped.
if !featureMap.IsEnabled(features.ClusterScoped) {
startChaos(context.Background(), cfg.KubeCli.CoreV1(), cfg.NatsOperatorNamespace, chaosLevel)
Expand All @@ -243,16 +231,6 @@ func newControllerConfig(featureMap features.FeatureMap, kubeConfig *rest.Config
}
}

func periodicFullGC(kubecli corev1client.CoreV1Interface, namespace string, d time.Duration) {
gc := garbagecollection.New(kubecli)
timer := time.NewTicker(d)
defer timer.Stop()
for {
<-timer.C
gc.FullyCollect(namespace)
}
}

func startChaos(ctx context.Context, kubecli corev1client.CoreV1Interface, ns string, chaosLevel int) {
m := chaos.NewMonkeys(kubecli)
ls := labels.SelectorFromSet(map[string]string{"app": "nats"})
Expand Down
5 changes: 1 addition & 4 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
natslisters "github.com/nats-io/nats-operator/pkg/client/listers/nats/v1alpha2"
"github.com/nats-io/nats-operator/pkg/cluster"
"github.com/nats-io/nats-operator/pkg/features"
"github.com/nats-io/nats-operator/pkg/garbagecollection"
kubernetesutil "github.com/nats-io/nats-operator/pkg/util/kubernetes"
)

Expand Down Expand Up @@ -196,11 +195,9 @@ func (c *Controller) processQueueItem(key string) error {
// Get the NatsCluster resource with this namespace/name.
natsCluster, err := c.natsClustersLister.NatsClusters(namespace).Get(name)
if err != nil {
// The NatsCluster resource may no longer exist, in which case we call the garbage collector.
// The NatsCluster resource may no longer exist.
if kubernetesutil.IsKubernetesResourceNotFoundError(err) {
// TODO Remove the garbage collection step and rely solely on the Kubernetes garbage collector.
c.logger.Warnf("natscluster %q was deleted", key)
garbagecollection.New(c.KubeCli.CoreV1()).CollectCluster(namespace, name, garbagecollection.NullUID)
return nil
}
return err
Expand Down
188 changes: 0 additions & 188 deletions pkg/garbagecollection/gc.go

This file was deleted.

88 changes: 88 additions & 0 deletions test/e2e/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
natsv1alpha2 "github.com/nats-io/nats-operator/pkg/apis/nats/v1alpha2"
"github.com/nats-io/nats-operator/pkg/conf"
"github.com/nats-io/nats-operator/pkg/constants"
"github.com/nats-io/nats-operator/pkg/util/retryutil"
"k8s.io/api/core/v1"
watchapi "k8s.io/apimachinery/pkg/watch"
)
Expand Down Expand Up @@ -342,3 +343,90 @@ func TestCreateServerWithCustomConfig(t *testing.T) {
t.Fatal(err)
}
}

func TestCreateAndDeleteClusterDependencies(t *testing.T) {
var (
size = 1
version = "1.4.0"
natsCluster *natsv1alpha2.NatsCluster
err error
)
if natsCluster, err = f.CreateCluster(f.Namespace, "test-nats-", size, version); err != nil {
t.Fatal(err)
}

ctx, done := context.WithTimeout(context.Background(), waitTimeout)
defer done()

err = f.WaitUntilSecretCondition(ctx, natsCluster, func(event watchapi.Event) (bool, error) {
// Just check that the secret is present.
secret := event.Object.(*v1.Secret)
_, ok := secret.Data[constants.ConfigFileName]
if !ok {
return false, nil
}

// Wait for a single pod to be created.
pods, err := f.PodsForNatsCluster(natsCluster)
if err != nil {
return false, err
}
if len(pods) < 1 {
return false, nil
}

// Confirm that there is a service for the NatsCluster.
svcs, err := f.ServicesForNatsCluster(natsCluster)
if err != nil {
return false, err
}
if len(svcs) < 1 {
return false, nil
}

return true, nil
})
if err != nil {
t.Fatal(err)
}

// Delete the cluster and there should be no more pods,
// secrets or services.
if err = f.DeleteCluster(natsCluster); err != nil {
t.Error(err)
}

// Wait for all the pods to report the expected routes and version.
err = retryutil.RetryWithContext(ctx, 5*time.Second, func() (bool, error) {
// Wait for the single pod to be deleted.
pods, err := f.PodsForNatsCluster(natsCluster)
if err != nil {
return false, err
}
if len(pods) > 0 {
return false, nil
}

// Confirm that there is a service for the NatsCluster
// and it is gone now.
svcs, err := f.ServicesForNatsCluster(natsCluster)
if err != nil {
return false, err
}
if len(svcs) > 0 {
return false, nil
}

secrets, err := f.SecretsForNatsCluster(natsCluster)
if err != nil {
return false, err
}
if len(secrets) > 0 {
return false, nil
}
return true, nil
})
if err != nil {
t.Fatal(err)
}
}
20 changes: 20 additions & 0 deletions test/e2e/framework/natscluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,26 @@ func (f *Framework) PodsForNatsCluster(natsCluster *natsv1alpha2.NatsCluster) ([
return pods.Items, nil
}

// ServicesForNatsCluster returns a slice containing all pods that belong
// to the specified NatsCluster resource.
func (f *Framework) ServicesForNatsCluster(natsCluster *natsv1alpha2.NatsCluster) ([]v1.Service, error) {
svc, err := f.KubeClient.CoreV1().Services(natsCluster.Namespace).List(kubernetesutil.ClusterListOpt(natsCluster.Name))
if err != nil {
return nil, err
}
return svc.Items, nil
}

// SecretsForNatsCluster returns a slice containing all pods that belong
// to the specified NatsCluster resource.
func (f *Framework) SecretsForNatsCluster(natsCluster *natsv1alpha2.NatsCluster) ([]v1.Secret, error) {
secrets, err := f.KubeClient.CoreV1().Secrets(natsCluster.Namespace).List(kubernetesutil.ClusterListOpt(natsCluster.Name))
if err != nil {
return nil, err
}
return secrets.Items, nil
}

// RouteCountForPod returns the number of routes reported by the
// specified pod.
func (f *Framework) RouteCountForPod(pod v1.Pod) (int, error) {
Expand Down

0 comments on commit 6ea6932

Please sign in to comment.