8000 Fix sts recreate stuck delete by realyota · Pull Request #1996 · Altinity/clickhouse-operator · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions pkg/controller/common/statefulset/statefulset-reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ func (r *Reconciler) recreateStatefulSet(ctx context.Context, host *api.Host, re
r.a.V(2).M(host).S().Info(util.NamespaceNameString(host.GetCR()))
defer r.a.V(2).M(host).E().Info(util.NamespaceNameString(host.GetCR()))

_ = r.doDeleteStatefulSet(ctx, host)
if err := r.doDeleteStatefulSet(ctx, host); err != nil {
return common.ErrCRUDAbort
}
_ = r.storage.ReconcilePVCs(ctx, host, api.DesiredStatefulSet)
return r.createStatefulSet(ctx, host, register, opts)
}
Expand Down Expand Up @@ -386,9 +388,10 @@ func (r *Reconciler) shouldAbortOrContinueCreateStatefulSet(action error, host *
return nil

case common.ErrCRUDRecreate:
// Continue
r.a.V(1).M(host).Warning("Got recreate action. Ignore and continue for now")
return nil
// Recreate is not actionable from the create path. Treat it as failed
// instead of marking an uncreated StatefulSet as reconciled.
r.a.V(1).M(host).Warning("Got recreate action while creating StatefulSet. Abort")
return common.ErrCRUDAbort

case common.ErrCRUDUnexpectedFlow:
// Continue
Expand All @@ -410,7 +413,7 @@ func (r *Reconciler) doCreateStatefulSet(ctx context.Context, host *api.Host, op
log.V(1).Info("Create StatefulSet: %s", util.NamespaceNameString(statefulSet))
if _, err := r.sts.Create(ctx, statefulSet); err != nil {
log.V(1).M(host).F().Error("StatefulSet create failed. err: %v", err)
return common.ErrCRUDRecreate
return common.ErrCRUDAbort
}

// StatefulSet created, wait until host is launched
Expand Down Expand Up @@ -514,6 +517,7 @@ func (r *Reconciler) doDeleteStatefulSet(ctx context.Context, host *api.Host) er
// Unable to fetch cur StatefulSet, but this is not necessarily an error yet
if apiErrors.IsNotFound(err) {
log.V(1).M(host).Info("NEUTRAL not found StatefulSet %s/%s", namespace, name)
return nil
} else {
log.V(1).M(host).F().Error("FAIL get StatefulSet %s/%s err:%v", namespace, name, err)
}
Expand All @@ -539,6 +543,7 @@ func (r *Reconciler) doDeleteStatefulSet(ctx context.Context, host *api.Host) er
log.V(1).M(host).Info("NEUTRAL not found StatefulSet %s/%s", namespace, name)
} else {
log.V(1).M(host).F().Error("FAIL delete StatefulSet %s/%s err: %v", namespace, name, err)
return err
}

return nil
Expand Down
142 changes: 142 additions & 0 deletions pkg/controller/common/statefulset/statefulset-reconciler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright 2019 Altinity Ltd and/or its affiliates. All rights reserved.
//
// 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 statefulset

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/require"
apps "k8s.io/api/apps/v1"
apiErrors "k8s.io/apimachinery/pkg/api/errors"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"

api "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
"github.com/altinity/clickhouse-operator/pkg/controller/common"
"github.com/altinity/clickhouse-operator/pkg/interfaces"
)

type fakeSTS struct {
cur *apps.StatefulSet
createErr error
deleteErr error
}

func (s *fakeSTS) Get(context.Context, ...any) (*apps.StatefulSet, error) {
return s.cur, nil
}

func (s *fakeSTS) Create(context.Context, *apps.StatefulSet) (*apps.StatefulSet, error) {
return nil, s.createErr
}

func (s *fakeSTS) Update(context.Context, *apps.StatefulSet) (*apps.StatefulSet, error) {
return s.cur, nil
}

func (s *fakeSTS) Delete(context.Context, string, string) error {
return s.deleteErr
}

func (s *fakeSTS) List(context.Context, string, meta.ListOptions) ([]apps.StatefulSet, error) {
return nil, nil
}

type testNamer struct{}

func (testNamer) Names(interfaces.NameType, ...any) []string {
return nil
}

func (testNamer) Name(interfaces.NameType, ...any) string {
return "chi-test-cluster-0-0"
}

type noopHostObjectsPoller struct{}

func (noopHostObjectsPoller) WaitHostStatefulSetReady(context.Context, *api.Host) error {
return nil
}

func (noopHostObjectsPoller) WaitHostPodStarted(context.Context, *api.Host) error {
return nil
}

func testHostWithDesiredSTS() *api.Host {
host := &api.Host{}
host.Runtime.SetCR(&api.ClickHouseInstallation{
ObjectMeta: meta.ObjectMeta{
Namespace: "default",
Name: "test",
},
})
host.Runtime.Address.Namespace = "default"
host.Runtime.DesiredStatefulSet = &apps.StatefulSet{
ObjectMeta: meta.ObjectMeta{
Namespace: "default",
Name: "chi-test-cluster-0-0",
},
}
return host
}

func TestDoCreateStatefulSetCreateErrorAborts(t *testing.T) {
err := apiErrors.NewAlreadyExists(
schema.GroupResource{Group: "apps", Resource: "statefulsets"},
"chi-test-cluster-0-0",
)
reconciler := &Reconciler{
sts: &fakeSTS{createErr: err},
}

action := reconciler.doCreateStatefulSet(context.Background(), testHostWithDesiredSTS(), nil)

require.ErrorIs(t, action, common.ErrCRUDAbort)
}

func TestShouldAbortOrContinueCreateStatefulSetRecreateActionAborts(t *testing.T) {
reconciler := &Reconciler{}

err := reconciler.shouldAbortOrContinueCreateStatefulSet(common.ErrCRUDRecreate, testHostWithDesiredSTS())

require.ErrorIs(t, err, common.ErrCRUDAbort)
}

func TestRecreateStatefulSetDeleteErrorAborts(t *testing.T) {
replicas := int32(1)
errDelete := errors.New("delete timed out")
reconciler := &Reconciler{
hostObjectsPoller: noopHostObjectsPoller{},
namer: testNamer{},
sts: &fakeSTS{
cur: &apps.StatefulSet{
ObjectMeta: meta.ObjectMeta{
Namespace: "default",
Name: "chi-test-cluster-0-0",
},
Spec: apps.StatefulSetSpec{
Replicas: &replicas,
},
},
deleteErr: errDelete,
},
}

err := reconciler.recreateStatefulSet(context.Background(), testHostWithDesiredSTS(), true, nil)

require.ErrorIs(t, err, common.ErrCRUDAbort)
}
35 changes: 17 additions & 18 deletions pkg/model/chi/config/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,17 @@ func (c *Generator) getRemoteServersReplica(host *chi.Host, b *bytes.Buffer) {
util.Iline(b, 16, "</replica>")
}

func (c *Generator) addRemoteServersClusterSecret(b *bytes.Buffer, indent int, secret *chi.ClusterSecret) {
switch secret.Source() {
case chi.ClusterSecretSourcePlaintext:
// Secret value is explicitly specified
util.Iline(b, indent, "<secret>%s</secret>", secret.Value)
case chi.ClusterSecretSourceSecretRef, chi.ClusterSecretSourceAuto:
// Use secret via ENV var from secret
util.Iline(b, indent, `<secret from_env="%s" />`, InternodeClusterSecretEnvName)
}
}

// getRemoteServers creates "remote_servers.xml" content and calculates data generation parameters for other sections
func (c *Generator) getRemoteServers(selector *config.HostSelector) string {
if selector == nil {
Expand Down Expand Up @@ -320,14 +331,7 @@ func (c *Generator) getRemoteServerClusters(selector *config.HostSelector, inden
util.Iline(b, indent, "<%s>", cluster.GetName())

// <secret>VALUE</secret>
switch cluster.GetSecret().Source() {
case chi.ClusterSecretSourcePlaintext:
// Secret value is explicitly specified
util.Iline(b, indent+4, "<secret>%s</secret>", cluster.GetSecret().Value)
case chi.ClusterSecretSourceSecretRef, chi.ClusterSecretSourceAuto:
// Use secret via ENV var from secret
util.Iline(b, indent+4, `<secret from_env="%s" />`, InternodeClusterSecretEnvName)
}
c.addRemoteServersClusterSecret(b, indent+4, cluster.GetSecret())

// Build each shard XML
cluster.WalkShards(func(index int, shard chi.IShard) error {
Expand Down Expand Up @@ -380,13 +384,16 @@ func (c *Generator) getRemoteClustersAutogenerated(selector *config.HostSelector
// We have at least one host - render clusters
util.Iline(b, indent, "<!-- Autogenerated clusters -->")

cluster := c.cr.FindCluster(0)

// One Shard All Replicas

// <all-replicated>
// <shard>
// <internal_replication>
clusterName := OneShardAllReplicasClusterName
util.Iline(b, indent, "<%s>", clusterName)
c.addRemoteServersClusterSecret(b, indent+4, cluster.GetSecret())
util.Iline(b, indent, " <shard>")
util.Iline(b, indent, " <internal_replication>true</internal_replication>")
c.cr.WalkHosts(func(host *chi.Host) error {
Expand All @@ -407,17 +414,8 @@ func (c *Generator) getRemoteClustersAutogenerated(selector *config.HostSelector
clusterName = AllShardsOneReplicaClusterName
util.Iline(b, indent, "<%s>", clusterName)

// Add secret to all-sharded from the first cluster if present
cluster := c.cr.FindCluster(0)
// <secret>VALUE</secret>
switch cluster.GetSecret().Source() {
case chi.ClusterSecretSourcePlaintext:
// Secret value is explicitly specified
util.Iline(b, indent+4, "<secret>%s</secret>", cluster.GetSecret().Value)
case chi.ClusterSecretSourceSecretRef, chi.ClusterSecretSourceAuto:
// Use secret via ENV var from secret
util.Iline(b, indent+4, `<secret from_env="%s" />`, InternodeClusterSecretEnvName)
}
c.addRemoteServersClusterSecret(b, indent+4, cluster.GetSecret())

c.cr.WalkHosts(func(host *chi.Host) error {
if selector.Include(host) {
Expand All @@ -441,6 +439,7 @@ func (c *Generator) getRemoteClustersAutogenerated(selector *config.HostSelector
// <all-clusters>
clusterName = AllClustersClusterName
util.Iline(b, indent, "<%s>", clusterName)
c.addRemoteServersClusterSecret(b, indent+4, cluster.GetSecret())
c.cr.WalkClusters(func(cluster chi.ICluster) error {
cluster.WalkShards(func(index int, shard chi.IShard) error {
if c.shardHostsNum(shard, selector) < 1 {
Expand Down
Loading
0