| 359 | } |
| 360 | |
| 361 | func scaleDownTarget(ctx devspacecontext.Context, obj runtime.Object) error { |
| 362 | cloned := obj.DeepCopyObject() |
| 363 | |
| 364 | // update object based on type |
| 365 | switch t := obj.(type) { |
| 366 | case *appsv1.ReplicaSet: |
| 367 | if t.Annotations == nil { |
| 368 | t.Annotations = map[string]string{} |
| 369 | } |
| 370 | |
| 371 | replicas := 1 |
| 372 | if t.Spec.Replicas != nil { |
| 373 | replicas = int(*t.Spec.Replicas) |
| 374 | } |
| 375 | |
| 376 | if replicas == 0 { |
| 377 | return nil |
| 378 | } |
| 379 | |
| 380 | t.Annotations[ReplicasAnnotation] = strconv.Itoa(replicas) |
| 381 | t.Spec.Replicas = ptr.Int32(0) |
| 382 | patch := patch2.MergeFrom(cloned) |
| 383 | bytes, err := patch.Data(t) |
| 384 | if err != nil { |
| 385 | return err |
| 386 | } |
| 387 | |
| 388 | _, err = ctx.KubeClient().KubeClient().AppsV1().ReplicaSets(t.Namespace).Patch(ctx.Context(), t.Name, patch.Type(), bytes, metav1.PatchOptions{}) |
| 389 | if err != nil { |
| 390 | return err |
| 391 | } |
| 392 | |
| 393 | return nil |
| 394 | case *appsv1.Deployment: |
| 395 | if t.Annotations == nil { |
| 396 | t.Annotations = map[string]string{} |
| 397 | } |
| 398 | |
| 399 | replicas := 1 |
| 400 | if t.Spec.Replicas != nil { |
| 401 | replicas = int(*t.Spec.Replicas) |
| 402 | } |
| 403 | |
| 404 | if replicas == 0 { |
| 405 | return nil |
| 406 | } |
| 407 | |
| 408 | t.Annotations[ReplicasAnnotation] = strconv.Itoa(replicas) |
| 409 | t.Spec.Replicas = ptr.Int32(0) |
| 410 | patch := patch2.MergeFrom(cloned) |
| 411 | bytes, err := patch.Data(t) |
| 412 | if err != nil { |
| 413 | return err |
| 414 | } |
| 415 | |
| 416 | _, err = ctx.KubeClient().KubeClient().AppsV1().Deployments(t.Namespace).Patch(ctx.Context(), t.Name, patch.Type(), bytes, metav1.PatchOptions{}) |
| 417 | if err != nil { |
| 418 | return err |