-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delete a subcluster pods after it is shut down (#968)
After a subcluster is "shut down"(shutdown set to true), its pods will stay down and cannot get restarted by the operator. We go a step further and delete the pods by scaling the statefulset to zero. This is useful for realdb as we do not want to keep down pods around and the EKS instances will also be shut down. When shutdown is set back to false, the statefulset will be scale up to its original size(subcluster size in spec).
- Loading branch information
Showing
10 changed files
with
255 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
(c) Copyright [2021-2024] Open Text. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
You may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package sandbox | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/go-logr/logr" | ||
v1 "github.com/vertica/vertica-kubernetes/api/v1" | ||
"github.com/vertica/vertica-kubernetes/pkg/controllers" | ||
"github.com/vertica/vertica-kubernetes/pkg/iter" | ||
vmeta "github.com/vertica/vertica-kubernetes/pkg/meta" | ||
"github.com/vertica/vertica-kubernetes/pkg/names" | ||
"github.com/vertica/vertica-kubernetes/pkg/podfacts" | ||
"k8s.io/client-go/util/retry" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
) | ||
|
||
// ScaleStafulsetReconciler will make sure that the sandbox's subclusters that are | ||
// shut down have their pods removed. | ||
type ScaleStafulsetReconciler struct { | ||
VRec *SandboxConfigMapReconciler | ||
Vdb *v1.VerticaDB | ||
Log logr.Logger | ||
PFacts *podfacts.PodFacts | ||
} | ||
|
||
func MakeScaleStafulsetReconciler(r *SandboxConfigMapReconciler, | ||
vdb *v1.VerticaDB, pfacts *podfacts.PodFacts) controllers.ReconcileActor { | ||
return &ScaleStafulsetReconciler{ | ||
VRec: r, | ||
Vdb: vdb, | ||
PFacts: pfacts, | ||
} | ||
} | ||
|
||
func (s *ScaleStafulsetReconciler) Reconcile(ctx context.Context, _ *ctrl.Request) (ctrl.Result, error) { | ||
scMap := s.Vdb.GenSubclusterMap() | ||
finder := iter.MakeSubclusterFinder(s.VRec.GetClient(), s.Vdb) | ||
stss, err := finder.FindStatefulSets(ctx, iter.FindInVdb, s.PFacts.SandboxName) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
for inx := range stss.Items { | ||
sts := &stss.Items[inx] | ||
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error { | ||
nm := names.GenNamespacedName(s.Vdb, sts.Name) | ||
err := s.VRec.GetClient().Get(ctx, nm, sts) | ||
if err != nil { | ||
return err | ||
} | ||
oldSize := sts.Spec.Replicas | ||
sc := scMap[sts.Labels[vmeta.SubclusterNameLabel]] | ||
if sc == nil { | ||
return fmt.Errorf("subcluster %s not found in vdb", sts.Labels[vmeta.SubclusterNameLabel]) | ||
} | ||
newSize := sc.GetStsSize(s.Vdb) | ||
if *oldSize == newSize { | ||
return nil | ||
} | ||
sts.Spec.Replicas = &newSize | ||
return s.VRec.GetClient().Update(ctx, sts) | ||
}) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
return ctrl.Result{}, nil | ||
} |
70 changes: 70 additions & 0 deletions
70
pkg/controllers/sandbox/scalestatefulset_reconciler_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
(c) Copyright [2021-2024] Open Text. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
You may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package sandbox | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
v1 "github.com/vertica/vertica-kubernetes/api/v1" | ||
"github.com/vertica/vertica-kubernetes/pkg/cmds" | ||
"github.com/vertica/vertica-kubernetes/pkg/names" | ||
"github.com/vertica/vertica-kubernetes/pkg/podfacts" | ||
"github.com/vertica/vertica-kubernetes/pkg/test" | ||
appsv1 "k8s.io/api/apps/v1" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
) | ||
|
||
var _ = Describe("scalestatefulset_reconciler", func() { | ||
ctx := context.Background() | ||
|
||
It("should scale the sts to zero if subcluster is shut down", func() { | ||
vdb := v1.MakeVDB() | ||
const sc1 = "sc1" | ||
vdb.Spec.Subclusters = []v1.Subcluster{ | ||
{Name: sc1, Size: 3, Shutdown: false}, | ||
} | ||
vdb.Spec.Sandboxes = []v1.Sandbox{ | ||
{Name: sc1, Subclusters: []v1.SubclusterName{{Name: sc1}}}, | ||
} | ||
vdb.Status.Subclusters = []v1.SubclusterStatus{ | ||
{Name: sc1, Shutdown: true}, | ||
} | ||
vdb.Status.Sandboxes = []v1.SandboxStatus{ | ||
{Name: sc1, Subclusters: []string{sc1}}, | ||
} | ||
test.CreatePods(ctx, k8sClient, vdb, test.AllPodsRunning) | ||
defer test.DeletePods(ctx, k8sClient, vdb) | ||
|
||
sts := &appsv1.StatefulSet{} | ||
Expect(k8sClient.Get(ctx, names.GenStsName(vdb, &vdb.Spec.Subclusters[0]), sts)).Should(Succeed()) | ||
Expect(*sts.Spec.Replicas).Should(Equal(vdb.Spec.Subclusters[0].Size)) | ||
|
||
vdb.Spec.Subclusters[0].Shutdown = true | ||
fpr := &cmds.FakePodRunner{} | ||
pfacts := podfacts.MakePodFacts(sbRec, fpr, logger, TestPassword) | ||
pfacts.SandboxName = sc1 | ||
r := MakeScaleStafulsetReconciler(sbRec, vdb, &pfacts) | ||
res, err := r.Reconcile(ctx, &ctrl.Request{}) | ||
Expect(err).Should(Succeed()) | ||
Expect(res).Should(Equal(ctrl.Result{})) | ||
|
||
newSts := &appsv1.StatefulSet{} | ||
Expect(k8sClient.Get(ctx, names.GenStsName(vdb, &vdb.Spec.Subclusters[0]), newSts)).Should(Succeed()) | ||
Expect(*newSts.Spec.Replicas).Should(Equal(int32(0))) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters