Skip to content

Commit bf4cfa0

Browse files
authored
fix post-completion/failure longhorn prometheus scale code (#4887)
* fix post-failure longhorn prometheus scale code * the longhorn reset scale process should never block continuation of the script if the migration succeeded * include prometheus in longhorn to openebs migration test
1 parent eb62413 commit bf4cfa0

File tree

3 files changed

+18
-41
lines changed

3 files changed

+18
-41
lines changed

pkg/cli/longhorn.go

+9-39
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cli
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"log"
87
"strconv"
@@ -20,16 +19,13 @@ import (
2019
"sigs.k8s.io/controller-runtime/pkg/client/config"
2120

2221
lhv1b1 "github.com/longhorn/longhorn-manager/k8s/pkg/apis/longhorn/v1beta1"
23-
promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
2422
"github.com/spf13/cobra"
2523
)
2624

2725
var scaleDownReplicasWaitTime = 5 * time.Minute
2826

2927
const (
3028
prometheusNamespace = "monitoring"
31-
prometheusName = "k8s"
32-
prometheusStatefulSetName = "prometheus-k8s"
3329
ekcoNamespace = "kurl"
3430
ekcoDeploymentName = "ekc-operator"
3531
pvmigrateScaleDownAnnotation = "kurl.sh/pvcmigrate-scale"
@@ -64,7 +60,6 @@ func NewLonghornRollbackMigrationReplicas(cli CLI) *cobra.Command {
6460
return fmt.Errorf("error creating client: %s", err)
6561
}
6662
lhv1b1.AddToScheme(cli.Scheme())
67-
promv1.AddToScheme(cli.Scheme())
6863

6964
var l1b1Volumes lhv1b1.VolumeList
7065
if err := cli.List(cmd.Context(), &l1b1Volumes, client.InNamespace(longhornNamespace)); err != nil {
@@ -136,7 +131,6 @@ func NewLonghornPrepareForMigration(cli CLI) *cobra.Command {
136131
return fmt.Errorf("error creating client: %s", err)
137132
}
138133
lhv1b1.AddToScheme(cli.Scheme())
139-
promv1.AddToScheme(cli.Scheme())
140134

141135
var scaledDown bool
142136
var nodes corev1.NodeList
@@ -256,46 +250,22 @@ func isPrometheusInstalled(ctx context.Context, cli client.Client) (bool, error)
256250
return true, nil
257251
}
258252

259-
// scaleUpPrometheus scales up prometheus.
253+
// scaleUpPrometheus scales up the prometheus operator.
260254
func scaleUpPrometheus(ctx context.Context, cli client.Client) error {
261255
if installed, err := isPrometheusInstalled(ctx, cli); err != nil {
262-
return fmt.Errorf("error scaling down prometheus: %w", err)
256+
return fmt.Errorf("error scaling up prometheus: %w", err)
263257
} else if !installed {
264258
return nil
265259
}
266260

267-
nsn := types.NamespacedName{Namespace: prometheusNamespace, Name: prometheusName}
268-
var prometheus promv1.Prometheus
269-
if err := cli.Get(ctx, nsn, &prometheus); err != nil {
270-
if errors.IsNotFound(err) {
271-
return nil
272-
}
273-
return fmt.Errorf("error getting prometheus: %w", err)
274-
}
275-
replicasStr, ok := prometheus.Annotations[pvmigrateScaleDownAnnotation]
276-
if !ok {
277-
return fmt.Errorf("error reading original replicas from the prometheus annotation: not found")
278-
}
279-
origReplicas, err := strconv.Atoi(replicasStr)
280-
if err != nil {
281-
return fmt.Errorf("error converting replicas annotation to integer: %w", err)
282-
}
283-
patch := map[string]interface{}{
284-
"metadata": map[string]interface{}{
285-
"annotations": map[string]interface{}{
286-
pvmigrateScaleDownAnnotation: nil,
287-
},
288-
},
289-
"spec": map[string]interface{}{
290-
"replicas": origReplicas,
291-
},
292-
}
293-
rawPatch, err := json.Marshal(patch)
294-
if err != nil {
295-
return fmt.Errorf("error creating prometheus patch: %w", err)
261+
var dep appsv1.Deployment
262+
if err := cli.Get(ctx, types.NamespacedName{Namespace: prometheusNamespace, Name: "prometheus-operator"}, &dep); err != nil {
263+
return fmt.Errorf("error getting prometheus operator deployment: %w", err)
296264
}
297-
if err := cli.Patch(ctx, &prometheus, client.RawPatch(types.MergePatchType, rawPatch)); err != nil {
298-
return fmt.Errorf("error scaling prometheus: %w", err)
265+
266+
dep.Spec.Replicas = ptr.To(int32(1))
267+
if err := cli.Update(ctx, &dep); err != nil {
268+
return fmt.Errorf("error scaling up prometheus operator: %w", err)
299269
}
300270
return nil
301271
}

scripts/common/longhorn.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ function longhorn_to_sc_migration() {
157157
kubectl annotate storageclass "$longhornStorageClass" storageclass.kubernetes.io/is-default-class-
158158
done
159159

160-
longhorn_restore_migration_replicas
160+
if ! longhorn_restore_migration_replicas; then
161+
log "Failed to restore the scale of Longhorn volumes and replicas. All data was successfully migrated to $destStorageClass and no action needs to be taken."
162+
fi
161163

162164
# reset ekco scale
163165
if [ "$ekcoScaledDown" = "1" ] ; then

testgrid/specs/storage-migration.yaml

+6-1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
fi
163163
- name: Migrate from Longhorn + Minio to OpenEBS + Minio
164164
flags: "yes"
165+
cpu: 6
165166
installerSpec:
166167
kubernetes:
167168
version: 1.24.x
@@ -179,6 +180,8 @@
179180
version: 1.3.x
180181
minio:
181182
version: latest
183+
prometheus:
184+
version: latest
182185
upgradeSpec:
183186
kubernetes:
184187
version: 1.24.x
@@ -193,11 +196,13 @@
193196
ekco:
194197
version: latest
195198
openebs:
196-
version: 3.4.x
199+
version: latest
197200
isLocalPVEnabled: true
198201
localPVStorageClassName: local
199202
minio:
200203
version: latest
204+
prometheus:
205+
version: latest
201206
postInstallScript: |
202207
source /opt/kurl-testgrid/testhelpers.sh
203208
create_deployment_with_mounted_volume "migration-test" "default" "/data" "registry:2.8.1"

0 commit comments

Comments
 (0)