From 35a96120414d56f6b2bbda844d851c43c2da20bc Mon Sep 17 00:00:00 2001 From: Rakshith R Date: Tue, 1 Jun 2021 15:36:45 +0530 Subject: [PATCH] ceph: remove obsolete statefulset functions This commit removes obsolete statefulset create, delete, addlabel, templateToStatefulset functions which were part of deprecated csi support for k8s 1.13 and should have been removed as part of https://github.com/rook/rook/pull/5982. It also adds Unit test for templateToDeployment func. Signed-off-by: Rakshith R --- pkg/operator/ceph/csi/util.go | 14 ------ pkg/operator/ceph/csi/util_test.go | 23 +++++----- pkg/operator/k8sutil/statefulset.go | 68 ----------------------------- 3 files changed, 13 insertions(+), 92 deletions(-) delete mode 100644 pkg/operator/k8sutil/statefulset.go diff --git a/pkg/operator/ceph/csi/util.go b/pkg/operator/ceph/csi/util.go index e8c7d53f5..8aac9057f 100644 --- a/pkg/operator/ceph/csi/util.go +++ b/pkg/operator/ceph/csi/util.go @@ -64,20 +64,6 @@ func templateToService(name, templatePath string, p templateParam) (*corev1.Serv return &svc, nil } -func templateToStatefulSet(name, templatePath string, p templateParam) (*apps.StatefulSet, error) { - var ss apps.StatefulSet - t, err := loadTemplate(name, templatePath, p) - if err != nil { - return nil, errors.Wrap(err, "failed to load statefulset template") - } - - err = yaml.Unmarshal([]byte(t), &ss) - if err != nil { - return nil, errors.Wrap(err, "failed to unmarshal statefulset template") - } - return &ss, nil -} - func templateToDaemonSet(name, templatePath string, p templateParam) (*apps.DaemonSet, error) { var ds apps.DaemonSet t, err := loadTemplate(name, templatePath, p) diff --git a/pkg/operator/ceph/csi/util_test.go b/pkg/operator/ceph/csi/util_test.go index 47080d7c8..d211afd97 100644 --- a/pkg/operator/ceph/csi/util_test.go +++ b/pkg/operator/ceph/csi/util_test.go @@ -50,13 +50,14 @@ spec: - name: cephfsplugin image: {{ .CSIPluginImage }} `) - testSSTemplate = []byte(` -kind: StatefulSet + testDepTemplate = []byte(` +kind: Deployment apiVersion: apps/v1 metadata: name: test-label namespace: {{ .Namespace }} spec: + replicas: {{ .ProvisionerReplicas }} selector: matchLabels: app: test-label @@ -67,13 +68,15 @@ spec: spec: serviceAccount: test-sa containers: - - name: csi-rbdplugin-attacher + - name: csi-attacher image: {{ .AttacherImage }} - - name: provisioner + - name: csi-snapshotter + image: {{ .SnapshotterImage }} + - name: csi-resizer + image: {{ .ResizerImage }} + - name: csi-provisioner image: {{ .ProvisionerImage }} - - name: rbdplugin - image: {{ .CSIPluginImage }} - - name: cephfsplugin + - name: csi-cephfsplugin image: {{ .CSIPluginImage }} `) ) @@ -97,13 +100,13 @@ func TestDaemonSetTemplate(t *testing.T) { assert.Nil(t, err) } -func TestStatefulSetTemplate(t *testing.T) { +func TestDeploymentTemplate(t *testing.T) { tmp, err := ioutil.TempFile("", "yaml") assert.Nil(t, err) defer os.Remove(tmp.Name()) - _, err = tmp.Write(testSSTemplate) + _, err = tmp.Write(testDepTemplate) assert.Nil(t, err) err = tmp.Close() assert.Nil(t, err) @@ -112,7 +115,7 @@ func TestStatefulSetTemplate(t *testing.T) { Param: CSIParam, Namespace: "foo", } - _, err = templateToStatefulSet("test-ss", tmp.Name(), tp) + _, err = templateToDeployment("test-dep", tmp.Name(), tp) assert.Nil(t, err) } diff --git a/pkg/operator/k8sutil/statefulset.go b/pkg/operator/k8sutil/statefulset.go deleted file mode 100644 index 97da6fe78..000000000 --- a/pkg/operator/k8sutil/statefulset.go +++ /dev/null @@ -1,68 +0,0 @@ -/* -Copyright 2019 The Rook Authors. 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 k8sutil - -import ( - "context" - "fmt" - - apps "k8s.io/api/apps/v1" - k8serrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/kubernetes" -) - -// create a apps.statefulset -func CreateStatefulSet(clientset kubernetes.Interface, name, namespace string, ss *apps.StatefulSet) error { - ctx := context.TODO() - _, err := clientset.AppsV1().StatefulSets(namespace).Create(ctx, ss, metav1.CreateOptions{}) - if err != nil { - if k8serrors.IsAlreadyExists(err) { - _, err = clientset.AppsV1().StatefulSets(namespace).Update(ctx, ss, metav1.UpdateOptions{}) - } - if err != nil { - return fmt.Errorf("failed to start %s statefulset: %+v\n%+v", name, err, ss) - } - } - return nil -} - -// DeleteStatefulset makes a best effort at deleting a statefulset and its pods, then waits for them to be deleted -func DeleteStatefulset(clientset kubernetes.Interface, namespace, name string) error { - ctx := context.TODO() - logger.Debugf("removing %s statefulset if it exists", name) - deleteAction := func(options *metav1.DeleteOptions) error { - return clientset.AppsV1().StatefulSets(namespace).Delete(ctx, name, *options) - } - getAction := func() error { - _, err := clientset.AppsV1().StatefulSets(namespace).Get(ctx, name, metav1.GetOptions{}) - return err - } - return deleteResourceAndWait(namespace, name, "statefulset", deleteAction, getAction) -} - -// AddRookVersionLabelToStatefulSet adds or updates a label reporting the Rook version which last -// modified a apps.statefulset. -func AddRookVersionLabelToStatefulSet(ss *apps.StatefulSet) { - if ss == nil { - return - } - if ss.Labels == nil { - ss.Labels = map[string]string{} - } - addRookVersionLabel(ss.Labels) -}