MCPcopy Index your code
hub / github.com/devspace-sh/devspace / updateNeeded

Function updateNeeded

pkg/devspace/services/podreplace/replace.go:129–195  ·  view source on GitHub ↗
(ctx devspacecontext.Context, deployment *appsv1.Deployment, devPod *latest.DevPod)

Source from the content-addressed store, hash-verified

127}
128
129func 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 }

Callers 1

ReplacePodMethod · 0.85

Calls 15

Int32Function · 0.92
deleteDeploymentFunction · 0.85
findTargetByKindNameFunction · 0.85
buildDeploymentFunction · 0.85
hashConfigFunction · 0.85
scaleDownTargetFunction · 0.85
updatePVCFunction · 0.85
LogMethod · 0.65
DeepCopyMethod · 0.65
DataMethod · 0.65
KubeClientMethod · 0.65
ContextMethod · 0.65

Tested by

no test coverage detected