syncHandler compares the actual state with the desired, and attempts to converge the two. It then updates the Status block of the Foo resource with the current status of the resource.
(ctx context.Context, objectRef cache.ObjectName)
| 239 | // converge the two. It then updates the Status block of the Foo resource |
| 240 | // with the current status of the resource. |
| 241 | func (c *Controller) syncHandler(ctx context.Context, objectRef cache.ObjectName) error { |
| 242 | logger := klog.LoggerWithValues(klog.FromContext(ctx), "objectRef", objectRef) |
| 243 | |
| 244 | // Get the Foo resource with this namespace/name |
| 245 | foo, err := c.foosLister.Foos(objectRef.Namespace).Get(objectRef.Name) |
| 246 | if err != nil { |
| 247 | // The Foo resource may no longer exist, in which case we stop |
| 248 | // processing. |
| 249 | if errors.IsNotFound(err) { |
| 250 | utilruntime.HandleErrorWithContext(ctx, err, "Foo referenced by item in work queue no longer exists", "objectReference", objectRef) |
| 251 | return nil |
| 252 | } |
| 253 | |
| 254 | return err |
| 255 | } |
| 256 | |
| 257 | deploymentName := foo.Spec.DeploymentName |
| 258 | if deploymentName == "" { |
| 259 | // We choose to absorb the error here as the worker would requeue the |
| 260 | // resource otherwise. Instead, the next time the resource is updated |
| 261 | // the resource will be queued again. |
| 262 | utilruntime.HandleErrorWithContext(ctx, nil, "Deployment name missing from object reference", "objectReference", objectRef) |
| 263 | return nil |
| 264 | } |
| 265 | |
| 266 | // Get the deployment with the name specified in Foo.spec |
| 267 | deployment, err := c.deploymentsLister.Deployments(foo.Namespace).Get(deploymentName) |
| 268 | // If the resource doesn't exist, we'll create it |
| 269 | if errors.IsNotFound(err) { |
| 270 | deployment, err = c.kubeclientset.AppsV1().Deployments(foo.Namespace).Create(ctx, newDeployment(foo), metav1.CreateOptions{FieldManager: FieldManager}) |
| 271 | } |
| 272 | |
| 273 | // If an error occurs during Get/Create, we'll requeue the item so we can |
| 274 | // attempt processing again later. This could have been caused by a |
| 275 | // temporary network failure, or any other transient reason. |
| 276 | if err != nil { |
| 277 | return err |
| 278 | } |
| 279 | |
| 280 | // If the Deployment is not controlled by this Foo resource, we should log |
| 281 | // a warning to the event recorder and return error msg. |
| 282 | if !metav1.IsControlledBy(deployment, foo) { |
| 283 | msg := fmt.Sprintf(MessageResourceExists, deployment.Name) |
| 284 | c.recorder.Event(foo, corev1.EventTypeWarning, ErrResourceExists, msg) |
| 285 | return fmt.Errorf("%s", msg) |
| 286 | } |
| 287 | |
| 288 | // If this number of the replicas on the Foo resource is specified, and the |
| 289 | // number does not equal the current desired replicas on the Deployment, we |
| 290 | // should update the Deployment resource. |
| 291 | if foo.Spec.Replicas != nil && *foo.Spec.Replicas != *deployment.Spec.Replicas { |
| 292 | logger.V(4).Info("Update deployment resource", "currentReplicas", *deployment.Spec.Replicas, "desiredReplicas", *foo.Spec.Replicas) |
| 293 | deployment, err = c.kubeclientset.AppsV1().Deployments(foo.Namespace).Update(ctx, newDeployment(foo), metav1.UpdateOptions{FieldManager: FieldManager}) |
| 294 | } |
| 295 | |
| 296 | // If an error occurs during Update, we'll requeue the item so we can |
| 297 | // attempt processing again later. This could have been caused by a |
| 298 | // temporary network failure, or any other transient reason. |