Skip to content

Commit

Permalink
Fix the upload directory for different caes
Browse files Browse the repository at this point in the history
  • Loading branch information
johscheuer committed Sep 6, 2024
1 parent a72ca75 commit bc83edf
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 2 deletions.
36 changes: 34 additions & 2 deletions kubectl-fdb/cmd/recover_multi_region_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2021 Apple Inc. and the FoundationDB project authors
* Copyright 2021-2024 Apple Inc. and the FoundationDB project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -256,8 +256,10 @@ func recoverMultiRegionCluster(cmd *cobra.Command, opts recoverMultiRegionCluste
}

for _, target := range needsUpload {
targetDataDir := getDataDir(dataDir, target, cluster)

for idx, coordinatorFile := range coordinatorFiles {
err = uploadCoordinatorFile(cmd, opts.client, opts.config, target, tmpCoordinatorFiles[idx], path.Join(dataDir, coordinatorFile))
err = uploadCoordinatorFile(cmd, opts.client, opts.config, target, tmpCoordinatorFiles[idx], path.Join(targetDataDir, coordinatorFile))
if err != nil {
return err
}
Expand Down Expand Up @@ -345,6 +347,36 @@ func recoverMultiRegionCluster(cmd *cobra.Command, opts recoverMultiRegionCluste
return nil
}

// getDataDir will return the target data directory to upload the coordinator files to. The directory can be different, depending
// on the used image type and if more than one process should be running inside the Pod.
func getDataDir(dataDir string, pod *corev1.Pod, cluster *fdbv1beta2.FoundationDBCluster) string {
baseDir := dataDir
// If the dataDir has a suffix for the process we remove it.
if dataDir != "/var/fdb/data" {
baseDir = path.Dir(dataDir)
}

// If the unified image is used we can simply return /var/fdb/data/1, as the unified image will always add the process
// directory, even if only a single process is running inside the Pod.
if cluster.UseUnifiedImage() {
return path.Join(baseDir, "/1")
}

// In this path we use the split image, so the process directory is only added if more than one process should be running
processClass := internal.GetProcessClassFromMeta(cluster, pod.ObjectMeta)

if processClass.IsLogProcess() && cluster.GetLogServersPerPod() > 1 {
return path.Join(baseDir, "/1")
}

if processClass == fdbv1beta2.ProcessClassStorage && cluster.GetStorageServersPerPod() > 1 {
return path.Join(baseDir, "/1")
}

// This is the default case if we are running one process per Pod for this storage class and using the split image.
return baseDir
}

func downloadCoordinatorFile(cmd *cobra.Command, kubeClient client.Client, config *rest.Config, pod *corev1.Pod, src string, dst string) error {
tmpCoordinatorFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
Expand Down
127 changes: 127 additions & 0 deletions kubectl-fdb/cmd/recover_multi_region_cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* recover_multi_region_cluster_test.go
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2021-2024 Apple Inc. and the FoundationDB project authors
*
* 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 cmd

import (
fdbv1beta2 "github.com/FoundationDB/fdb-kubernetes-operator/api/v1beta2"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var _ = Describe("[plugin] running the recover multi-region cluster command", func() {
var imageTypeUnified = fdbv1beta2.ImageTypeUnified
type dataDirTest struct {
input string
pod *corev1.Pod
cluster *fdbv1beta2.FoundationDBCluster
expected string
}

DescribeTable("when getting the data dir for uploading files", func(test dataDirTest) {
Expect(getDataDir(test.input, test.pod, test.cluster)).To(Equal(test.expected))
},
Entry("data dir is already correct",
dataDirTest{
input: "/var/fdb/data",
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
fdbv1beta2.FDBProcessClassLabel: string(fdbv1beta2.ProcessClassStorage),
},
},
},
cluster: &fdbv1beta2.FoundationDBCluster{},
expected: "/var/fdb/data",
},
),
Entry("data dir has process directory and target pod has single process",
dataDirTest{
input: "/var/fdb/data/2",
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
fdbv1beta2.FDBProcessClassLabel: string(fdbv1beta2.ProcessClassStorage),
},
},
},
cluster: &fdbv1beta2.FoundationDBCluster{},
expected: "/var/fdb/data",
},
),
Entry("data dir has process directory and target pod has multiple processes",
dataDirTest{
input: "/var/fdb/data/2",
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
fdbv1beta2.FDBProcessClassLabel: string(fdbv1beta2.ProcessClassStorage),
},
},
},
cluster: &fdbv1beta2.FoundationDBCluster{
Spec: fdbv1beta2.FoundationDBClusterSpec{
StorageServersPerPod: 2,
},
},
expected: "/var/fdb/data/1",
},
),
Entry("data dir has process directory and target pod has single process with unified image",
dataDirTest{
input: "/var/fdb/data/2",
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
fdbv1beta2.FDBProcessClassLabel: string(fdbv1beta2.ProcessClassStorage),
},
},
},
cluster: &fdbv1beta2.FoundationDBCluster{
Spec: fdbv1beta2.FoundationDBClusterSpec{
ImageType: &imageTypeUnified,
},
},
expected: "/var/fdb/data/1",
},
),
Entry("data dir has process directory and target pod has multiple processes with unified image",
dataDirTest{
input: "/var/fdb/data/2",
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
fdbv1beta2.FDBProcessClassLabel: string(fdbv1beta2.ProcessClassStorage),
},
},
},
cluster: &fdbv1beta2.FoundationDBCluster{
Spec: fdbv1beta2.FoundationDBClusterSpec{
ImageType: &imageTypeUnified,
StorageServersPerPod: 2,
},
},
expected: "/var/fdb/data/1",
},
),
)
})

0 comments on commit bc83edf

Please sign in to comment.