Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: kstatus/status returns incorrect "Current" result immediately after DaemonSet apply #548

Closed
rohitagarwal003 opened this issue Feb 17, 2022 · 2 comments · Fixed by #550

Comments

@rohitagarwal003
Copy link
Contributor

We have a use case where we want to create a DaemonSet and wait for its replicas to be ready before moving forward with the rest of the work. I was trying to use the kstatus/status library to do that. I thought I would apply the DaemonSet and then continuously check the result returned by status.Compute().

However, immediately after applying the DaemonSet, the status.Compute() method returns a Current result seemingly because the status is: status:map[currentNumberScheduled:0 desiredNumberScheduled:0 numberMisscheduled:0 numberReady:0]]}

Is this expected? Should we add an exception case to return an InProgress result when desiredNumberScheduled == 0?

Here's the code I was using:

package main

import (
	"context"
	"flag"
	"log"
	"os"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
	serializeryaml "k8s.io/apimachinery/pkg/runtime/serializer/yaml"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/client-go/discovery"
	"k8s.io/client-go/discovery/cached/memory"
	"k8s.io/client-go/dynamic"
	"k8s.io/client-go/restmapper"
	"k8s.io/client-go/tools/clientcmd"
	"sigs.k8s.io/cli-utils/pkg/kstatus/status"
)

func main() {
	kubeconfig := flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
	manifest := flag.String("manifest", "", "absolute path to the daemonset manifest")
	flag.Parse()

	dsBytes, err := os.ReadFile(*manifest)
	if err != nil {
		log.Fatalf("Can't read %q: %v", *manifest, err)
	}

	obj := &unstructured.Unstructured{}
	_, gvk, err := serializeryaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode(dsBytes, nil, obj)
	if err != nil {
		log.Fatalf("Can't decode yaml: %v", err)
	}
	result, err := status.Compute(obj)
	if err != nil {
		log.Fatalf("Error computing status: %v", err)
	}
	log.Printf("Result Before: %v", result)

	config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
	if err != nil {
		log.Fatalf("Can't create *rest.Config to talk to the cluster: %v", err)
	}

	dynamicClient, err := dynamic.NewForConfig(config)
	if err != nil {
		log.Fatalf("Can't create a dynamic client: %v", err)
	}

	discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
	if err != nil {
		log.Fatalf("Can't create a discovery client: %v", err)
	}
	mapper := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(discoveryClient))

	mapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
	if err != nil {
		log.Fatalf("Can't find GVR for GVK: %v", err)
	}

	if newObj, err := dynamicClient.Resource(mapping.Resource).Namespace(obj.GetNamespace()).Patch(context.TODO(), obj.GetName(), types.ApplyPatchType, dsBytes, metav1.PatchOptions{FieldManager: "deployer"}); err != nil {
		log.Fatalf("Failed to apply patch: %v", err)
	} else {
		result, err := status.Compute(newObj)
		if err != nil {
			log.Fatalf("Error computing new status: %v", err)
		}
		log.Printf("Result After: %v", result)
	}
}

Here's the output of the run:

$ ./main --kubeconfig admin.conf --manifest=ds.yaml
2022/02/17 08:37:15 Result Before: &{InProgress Missing .status.desiredNumberScheduled [{Reconciling True NoDesiredNumber Missing .status.desiredNumberScheduled}]}
2022/02/17 08:37:15 Result After: &{Current All replicas scheduled as expected. Replicas: 0 []}

Because I had a "bad" DaemonSet that didn't start, if I run the program again, it worked correctly:

$ ./main --kubeconfig admin.conf --manifest=ds.yaml
2022/02/17 08:37:30 Result Before: &{InProgress Missing .status.desiredNumberScheduled [{Reconciling True NoDesiredNumber Missing .status.desiredNumberScheduled}]}
2022/02/17 08:37:30 Result After: &{InProgress Available: 0/2 [{Reconciling True LessAvailable Available: 0/2}]}
@rohitagarwal003
Copy link
Contributor Author

Maybe we can use status.desiredNumberScheduled == 0 && status.observedGeneration == 0 as a signal that DaemonSet controller hasn't acted on the manifest?

@rohitagarwal003
Copy link
Contributor Author

/cc @mortent @ash2k @karlkfi

rohitagarwal003 added a commit to rohitagarwal003/cli-utils that referenced this issue Feb 18, 2022
In status.Compute() -> checkGenericProperties() -> checkGeneration(),
we check that metadata.generation is equal to status.observedGeneration
but don't return an InProgress status if either of them is unset.

This may be reasonable for generic resource where we are unsure if these
fields would be set, but for daemonsets, we *know* that these fields get
set by the controller and so we should ensure that.

This fixes kubernetes-sigs#548.
rohitagarwal003 added a commit to rohitagarwal003/cli-utils that referenced this issue Feb 18, 2022
In status.Compute() -> checkGenericProperties() -> checkGeneration(),
we check that metadata.generation is equal to status.observedGeneration
but don't return an InProgress status if either of them is unset.

This may be reasonable for generic resource where we are unsure if these
fields would be set, but for daemonsets, we *know* that these fields get
set by the controller and so we should ensure that.

This addresses kubernetes-sigs#548
rohitagarwal003 added a commit to rohitagarwal003/cli-utils that referenced this issue Feb 18, 2022
In status.Compute() -> checkGenericProperties() -> checkGeneration(),
we check that metadata.generation is equal to status.observedGeneration
but don't return an InProgress status if either of them is unset.

This may be reasonable for generic resource where we are unsure if these
fields would be set, but for daemonsets, we *know* that these fields get
set by the controller and so we should ensure that.

This addresses kubernetes-sigs#548
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant