(ctx devspacecontext.Context, deployment *appsv1.Deployment, devPod *latest.DevPod)
| 127 | } |
| 128 | |
| 129 | func updateNeeded(ctx devspacecontext.Context, deployment *appsv1.Deployment, devPod *latest.DevPod) (recreateNeeded bool, err error) { |
| 130 | if deployment.Annotations == nil || deployment.Annotations[TargetKindAnnotation] == "" || deployment.Annotations[TargetNameAnnotation] == "" { |
| 131 | return true, deleteDeployment(ctx, deployment) |
| 132 | } |
| 133 | |
| 134 | target, err := findTargetByKindName(ctx, deployment.Annotations[TargetKindAnnotation], deployment.Namespace, deployment.Annotations[TargetNameAnnotation]) |
| 135 | if err != nil { |
| 136 | if kerrors.IsNotFound(err) { |
| 137 | return true, deleteDeployment(ctx, deployment) |
| 138 | } |
| 139 | |
| 140 | ctx.Log().Debugf("error getting target for deployment %s/%s: %v", deployment.Namespace, deployment.Name, err) |
| 141 | return false, err |
| 142 | } |
| 143 | |
| 144 | newDeployment, err := buildDeployment(ctx, deployment.Name, target, devPod) |
| 145 | if err != nil { |
| 146 | return false, err |
| 147 | } |
| 148 | |
| 149 | configHash, err := hashConfig(devPod) |
| 150 | if err != nil { |
| 151 | return false, errors.Wrap(err, "hash config") |
| 152 | } |
| 153 | |
| 154 | err = scaleDownTarget(ctx, target) |
| 155 | if err != nil { |
| 156 | ctx.Log().Warnf("Error scaling down target: %v", err) |
| 157 | } |
| 158 | |
| 159 | // update deployment |
| 160 | originalDeployment := deployment.DeepCopy() |
| 161 | deployment.Spec.Replicas = ptr.Int32(1) |
| 162 | deployment.Spec.Selector = newDeployment.Spec.Selector |
| 163 | deployment.Spec.Template = newDeployment.Spec.Template |
| 164 | deployment.Annotations = newDeployment.Annotations |
| 165 | deployment.Annotations[DevPodConfigHashAnnotation] = configHash |
| 166 | deployment.Labels = newDeployment.Labels |
| 167 | patch := patch2.MergeFrom(originalDeployment) |
| 168 | patchBytes, err := patch.Data(deployment) |
| 169 | if err != nil { |
| 170 | return false, err |
| 171 | } else if string(patchBytes) == "{}" { |
| 172 | ctx.Log().Debugf("No changes required in replaced deployment %s", deployment.Name) |
| 173 | return false, nil |
| 174 | } |
| 175 | |
| 176 | ctx.Log().Debugf("Update replaced deployment with patch:\n %s", string(patchBytes)) |
| 177 | |
| 178 | deployment, err = ctx.KubeClient().KubeClient().AppsV1().Deployments(deployment.Namespace).Patch(ctx.Context(), deployment.Name, patch.Type(), patchBytes, metav1.PatchOptions{}) |
| 179 | if err != nil { |
| 180 | if kerrors.IsInvalid(err) { |
| 181 | ctx.Log().Debugf("Recreate deployment because it is invalid: %v", err) |
| 182 | return true, deleteDeployment(ctx, deployment) |
| 183 | } |
| 184 | |
| 185 | return false, err |
| 186 | } |
no test coverage detected