Skip to content

Commit

Permalink
feat: changes to integrate keploy with Foo controller
Browse files Browse the repository at this point in the history
Signed-off-by: Sarthak160 <rocksarthak45@gmail.com>
  • Loading branch information
Sarthak160 committed Nov 30, 2023
1 parent 679aa01 commit 44c078e
Show file tree
Hide file tree
Showing 5 changed files with 8,457 additions and 7 deletions.
Binary file added .fuse_hidden00000a9300000024
Binary file not shown.
4 changes: 2 additions & 2 deletions artifacts/examples/example-foo.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: samplecontroller.k8s.io/v1alpha1
kind: Foo
metadata:
name: example-foo
name: example-foo-9
spec:
deploymentName: example-foo
deploymentName: example-foo-9
replicas: 1
20 changes: 15 additions & 5 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func NewController(
}

logger.Info("Setting up event handlers")
// var ResourceVersion string
// Set up an event handler for when Foo resources change
fooInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueFoo,
Expand All @@ -140,6 +141,8 @@ func NewController(
UpdateFunc: func(old, new interface{}) {
newDepl := new.(*appsv1.Deployment)
oldDepl := old.(*appsv1.Deployment)
// fmt.Println("newDepl.ResourceVersion", newDepl.ResourceVersion)
// fmt.Println("oldDepl.ResourceVersion", oldDepl.ResourceVersion)
if newDepl.ResourceVersion == oldDepl.ResourceVersion {
// Periodic resync will send update events for all known Deployments.
// Two different versions of the same Deployment will always have different RVs.
Expand Down Expand Up @@ -275,6 +278,13 @@ func (c *Controller) syncHandler(ctx context.Context, key string) error {
return err
}

// set ResourceVersion in ctx
rv := foo.GetResourceVersion()

if rv!= "" {
ctx = context.WithValue(ctx, "ResourceVersion", rv)
}

deploymentName := foo.Spec.DeploymentName
if deploymentName == "" {
// We choose to absorb the error here as the worker would requeue the
Expand All @@ -288,7 +298,7 @@ func (c *Controller) syncHandler(ctx context.Context, key string) error {
deployment, err := c.deploymentsLister.Deployments(foo.Namespace).Get(deploymentName)
// If the resource doesn't exist, we'll create it
if errors.IsNotFound(err) {
deployment, err = c.kubeclientset.AppsV1().Deployments(foo.Namespace).Create(context.TODO(), newDeployment(foo), metav1.CreateOptions{})
deployment, err = c.kubeclientset.AppsV1().Deployments(foo.Namespace).Create(ctx, newDeployment(foo), metav1.CreateOptions{})
}

// If an error occurs during Get/Create, we'll requeue the item so we can
Expand All @@ -311,7 +321,7 @@ func (c *Controller) syncHandler(ctx context.Context, key string) error {
// should update the Deployment resource.
if foo.Spec.Replicas != nil && *foo.Spec.Replicas != *deployment.Spec.Replicas {
logger.V(4).Info("Update deployment resource", "currentReplicas", *foo.Spec.Replicas, "desiredReplicas", *deployment.Spec.Replicas)
deployment, err = c.kubeclientset.AppsV1().Deployments(foo.Namespace).Update(context.TODO(), newDeployment(foo), metav1.UpdateOptions{})
deployment, err = c.kubeclientset.AppsV1().Deployments(foo.Namespace).Update(ctx, newDeployment(foo), metav1.UpdateOptions{})
}

// If an error occurs during Update, we'll requeue the item so we can
Expand All @@ -323,7 +333,7 @@ func (c *Controller) syncHandler(ctx context.Context, key string) error {

// Finally, we update the status block of the Foo resource to reflect the
// current state of the world
err = c.updateFooStatus(foo, deployment)
err = c.updateFooStatus(foo, deployment, ctx)
if err != nil {
return err
}
Expand All @@ -332,7 +342,7 @@ func (c *Controller) syncHandler(ctx context.Context, key string) error {
return nil
}

func (c *Controller) updateFooStatus(foo *samplev1alpha1.Foo, deployment *appsv1.Deployment) error {
func (c *Controller) updateFooStatus(foo *samplev1alpha1.Foo, deployment *appsv1.Deployment, ctx context.Context) error {
// NEVER modify objects from the store. It's a read-only, local cache.
// You can use DeepCopy() to make a deep copy of original object and modify this copy
// Or create a copy manually for better performance
Expand All @@ -342,7 +352,7 @@ func (c *Controller) updateFooStatus(foo *samplev1alpha1.Foo, deployment *appsv1
// we must use Update instead of UpdateStatus to update the Status block of the Foo resource.
// UpdateStatus will not allow changes to the Spec of the resource,
// which is ideal for ensuring nothing other than resource status has been updated.
_, err := c.sampleclientset.SamplecontrollerV1alpha1().Foos(foo.Namespace).UpdateStatus(context.TODO(), fooCopy, metav1.UpdateOptions{})
_, err := c.sampleclientset.SamplecontrollerV1alpha1().Foos(foo.Namespace).UpdateStatus(ctx, fooCopy, metav1.UpdateOptions{})
return err
}

Expand Down
Loading

0 comments on commit 44c078e

Please sign in to comment.