From c6088d76920674b40413c57079d7e5ea7bba915a Mon Sep 17 00:00:00 2001 From: Fabrice Jammes Date: Fri, 5 Jan 2024 14:46:21 +0100 Subject: [PATCH] Update current feature --- cmd/exist.go | 96 +++++++++++++++++++++++++++++++++++++ resources/install-argocd.sh | 2 +- 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 cmd/exist.go diff --git a/cmd/exist.go b/cmd/exist.go new file mode 100644 index 0000000..6cc84a5 --- /dev/null +++ b/cmd/exist.go @@ -0,0 +1,96 @@ +/* +Copyright © 2023 Fabrice Jammmes fabrice.jammes@k8s-school.fr +*/ +package cmd + +import ( + "github.com/spf13/cobra" +) + +// deskCmd represents the desk command +var existCmd = &cobra.Command{ + Use: "exist", + Aliases: []string{"e"}, + Short: "Wait for a kubernetes resource to exist", + Long: `Lauch a interactive shell with k8s client tools installed, inside a docker container, it mounts host directories $HOME/.kube in $HOME/.kube and homefs/ in $HOME`, + Run: func(cmd *cobra.Command, args []string) { + + // TODO + + }, +} + +func init() { + rootCmd.AddCommand(existCmd) + +} + +// package main + +// import ( +// "context" +// "flag" +// "fmt" +// "os" +// "time" + +// corev1 "k8s.io/api/core/v1" +// metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +// "k8s.io/client-go/kubernetes" +// "k8s.io/client-go/tools/clientcmd" +// ) + +// func waitForExist(clientset *kubernetes.Clientset, ns string, resourceType string, maxWaitSecs int, resource string) error { +// intervalSecs := 2 * time.Second +// startTime := time.Now() + +// for { +// fmt.Printf("Waiting for %s %s\n", resourceType, resource) + +// if time.Since(startTime).Seconds() > float64(maxWaitSecs) { +// return fmt.Errorf("waited for %s in namespace \"%s\" to exist for %d seconds without luck", resourceType, ns, maxWaitSecs) +// } + +// _, err := clientset.CoreV1().Namespaces().Get(context.Background(), resource, metav1.GetOptions{}) + +// if err == nil { +// break +// } else { +// time.Sleep(intervalSecs) +// } +// } + +// return nil +// } + +// func main() { +// kubeconfig := flag.String("kubeconfig", "~/.kube/config", "path to the kubeconfig file") +// ns := flag.String("namespace", "", "the namespace") +// resourceType := flag.String("type", "", "the resource type") +// maxWaitSecs := flag.Int("timeout", 120, "the maximum wait time in seconds") +// flag.Parse() +// args := flag.Args() + +// if len(args) < 1 { +// fmt.Println("Please provide a resource to check") +// os.Exit(1) +// } + +// resource := args[0] + +// config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig) +// if err != nil { +// panic(err) +// } + +// clientset, err := kubernetes.NewForConfig(config) +// if err != nil { +// panic(err) +// } + +// err = waitForExist(clientset, *ns, *resourceType, *maxWaitSecs, resource) +// if err != nil { +// fmt.Println(err) +// os.Exit(1) +// } +// } diff --git a/resources/install-argocd.sh b/resources/install-argocd.sh index 066cd8e..f5c9ab7 100755 --- a/resources/install-argocd.sh +++ b/resources/install-argocd.sh @@ -99,7 +99,7 @@ curl -sSL -o /tmp/argocd-linux-amd64 https://github.com/argoproj/argo-cd/release sudo install -m 555 /tmp/argocd-linux-amd64 /usr/local/bin/argocd rm /tmp/argocd-linux-amd64 -wait_for_exist argocd statefulset.apps 120 +wait_for_exist argocd statefulset.apps 120 for obj in statefulset.apps/argocd-application-controller deployment.apps/argocd-redis deployment.apps/argocd-repo-server deployment.apps/argocd-server; do echo "Wait for $obj to be ready" kubectl rollout status $obj --timeout="${WAIT_TIMEOUT}s" -n argocd