-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement XWaitFor for Features (#458)
Add `x-wait-for` command to allow the snap to wait until features are available and ready in the cluster.
- Loading branch information
1 parent
6526622
commit f0bed04
Showing
8 changed files
with
178 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package k8s | ||
|
||
import ( | ||
cmdutil "github.com/canonical/k8s/cmd/util" | ||
"github.com/canonical/k8s/pkg/k8sd/features" | ||
"github.com/canonical/k8s/pkg/utils/control" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newXWaitForCmd(env cmdutil.ExecutionEnvironment) *cobra.Command { | ||
waitForDNSCmd := &cobra.Command{ | ||
Use: "dns", | ||
Short: "Wait for DNS to be ready", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := control.WaitUntilReady(cmd.Context(), func() (bool, error) { | ||
return features.StatusChecks.CheckDNS(cmd.Context(), env.Snap) | ||
}) | ||
if err != nil { | ||
cmd.PrintErrf("Error: failed to wait for DNS to be ready: %v\n", err) | ||
env.Exit(1) | ||
} | ||
}, | ||
} | ||
|
||
waitForNetworkCmd := &cobra.Command{ | ||
Use: "network", | ||
Short: "Wait for Network to be ready", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := control.WaitUntilReady(cmd.Context(), func() (bool, error) { | ||
return features.StatusChecks.CheckNetwork(cmd.Context(), env.Snap) | ||
}) | ||
if err != nil { | ||
cmd.PrintErrf("Error: failed to wait for DNS to be ready: %v\n", err) | ||
env.Exit(1) | ||
} | ||
}, | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "x-wait-for", | ||
Short: "Wait for the cluster's feature to be in a ready state", | ||
Hidden: true, | ||
} | ||
|
||
cmd.AddCommand(waitForDNSCmd) | ||
cmd.AddCommand(waitForNetworkCmd) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// IsPodReady checks if a pod is ready. | ||
func (c *Client) IsPodReady(ctx context.Context, name, namespace string, listOptions metav1.ListOptions) (bool, error) { | ||
pods, err := c.CoreV1().Pods(namespace).List(ctx, listOptions) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to list pods: %w", err) | ||
} | ||
|
||
for _, pod := range pods.Items { | ||
if strings.Contains(pod.Name, name) { | ||
if pod.Status.Phase != corev1.PodRunning { | ||
return false, nil | ||
} | ||
|
||
for _, condition := range pod.Status.Conditions { | ||
if condition.Type == corev1.PodReady && condition.Status == corev1.ConditionTrue { | ||
return true, nil | ||
} | ||
} | ||
} | ||
} | ||
|
||
return false, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cilium | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/canonical/k8s/pkg/snap" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func CheckNetwork(ctx context.Context, snap snap.Snap) (bool, error) { | ||
client, err := snap.KubernetesClient("kube-system") | ||
if err != nil { | ||
return false, fmt.Errorf("failed to create kubernetes client: %w", err) | ||
} | ||
|
||
ciliumPods := map[string]string{ | ||
"cilium-operator": "io.cilium/app=operator", | ||
"cilium": "k8s-app=cilium", | ||
} | ||
|
||
for ciliumPod, selector := range ciliumPods { | ||
isReady, err := client.IsPodReady(ctx, ciliumPod, "kube-system", metav1.ListOptions{LabelSelector: selector}) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to check if pod %q is ready: %w", ciliumPod, err) | ||
} | ||
if !isReady { | ||
return false, nil | ||
} | ||
} | ||
|
||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package coredns | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/canonical/k8s/pkg/snap" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// CheckDNS checks the CoreDNS deployment in the cluster. | ||
func CheckDNS(ctx context.Context, snap snap.Snap) (bool, error) { | ||
client, err := snap.KubernetesClient("kube-system") | ||
if err != nil { | ||
return false, fmt.Errorf("failed to create kubernetes client: %w", err) | ||
} | ||
|
||
isReady, err := client.IsPodReady(ctx, "coredns", "kube-system", metav1.ListOptions{LabelSelector: "app.kubernetes.io/name=coredns"}) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to wait for CoreDNS pod to be ready: %w", err) | ||
} | ||
|
||
return isReady, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package features | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/canonical/k8s/pkg/snap" | ||
) | ||
|
||
type StatusInterface interface { | ||
CheckDNS(context.Context, snap.Snap) (bool, error) | ||
CheckNetwork(context.Context, snap.Snap) (bool, error) | ||
} | ||
|
||
type statusChecks struct { | ||
checkDNS func(context.Context, snap.Snap) (bool, error) | ||
checkNetwork func(context.Context, snap.Snap) (bool, error) | ||
} | ||
|
||
func (s *statusChecks) CheckDNS(ctx context.Context, snap snap.Snap) (bool, error) { | ||
return s.checkDNS(ctx, snap) | ||
} | ||
|
||
func (s *statusChecks) CheckNetwork(ctx context.Context, snap snap.Snap) (bool, error) { | ||
return s.checkNetwork(ctx, snap) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters