-
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.
- Loading branch information
1 parent
b7859b0
commit 15f64ea
Showing
10 changed files
with
256 additions
and
27 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
Binary file not shown.
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,33 @@ | ||
package calico | ||
|
||
import ( | ||
"path" | ||
|
||
"github.com/canonical/k8s/pkg/client/helm" | ||
) | ||
|
||
var ( | ||
// chartCalico represents manifests to deploy Calico. | ||
chartCalico = helm.InstallableChart{ | ||
Name: "ck-network", | ||
Namespace: "tigera-operator", | ||
ManifestPath: path.Join("charts", "tigera-operator-v3.28.0.tgz"), | ||
} | ||
|
||
// tigeraOperatorRepo represents the repo to fetch the tigera-operator image for calico. | ||
// Note: Tigera is the company behind Calico and the tigera-operator is the operator for Calico. | ||
// TODO: use ROCKs instead of upstream | ||
tigeraOperatorRegistry = "quay.io" | ||
|
||
// tigeraOperatorImage represents the image to fetch for calico. | ||
tigeraOperatorImage = "tigera/operator" | ||
|
||
// tigeraOperatorVersion is the version to use for the tigera-operator image. | ||
tigeraOperatorVersion = "v1.34.0" | ||
|
||
// calicoCtlImage represents the image to fetch for calicoctl. | ||
// TODO: use ROCKs instead of upstream | ||
calicoCtlImage = "docker.io/calico/ctl" | ||
// calicoCtlTag represents the tag to use for the calicoctl image. | ||
calicoCtlTag = "v3.28.0" | ||
) |
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,79 @@ | ||
package calico | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/canonical/k8s/pkg/client/helm" | ||
"github.com/canonical/k8s/pkg/k8sd/types" | ||
"github.com/canonical/k8s/pkg/snap" | ||
"github.com/canonical/k8s/pkg/utils" | ||
) | ||
|
||
// ApplyNetwork will deploy Calico when cfg.Enabled is true. | ||
// ApplyNetwork will remove Calico when cfg.Enabled is false. | ||
// ApplyNetwork returns an error if anything fails. | ||
func ApplyNetwork(ctx context.Context, snap snap.Snap, cfg types.Network, _ types.Annotations) error { | ||
m := snap.HelmClient() | ||
|
||
if !cfg.GetEnabled() { | ||
if _, err := m.Apply(ctx, chartCalico, helm.StateDeleted, nil); err != nil { | ||
return fmt.Errorf("failed to uninstall network: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
podIpPools := []map[string]any{} | ||
ipv4PodCIDR, ipv6PodCIDR, err := utils.ParseCIDRs(cfg.GetPodCIDR()) | ||
if err != nil { | ||
return fmt.Errorf("invalid pod cidr: %v", err) | ||
} | ||
if ipv4PodCIDR != "" { | ||
podIpPools = append(podIpPools, map[string]any{ | ||
"name": "ipv4-ippool", | ||
"cidr": ipv4PodCIDR, | ||
}) | ||
} | ||
if ipv6PodCIDR != "" { | ||
podIpPools = append(podIpPools, map[string]any{ | ||
"name": "ipv6-ippool", | ||
"cidr": ipv6PodCIDR, | ||
}) | ||
} | ||
|
||
serviceCIDRs := []string{} | ||
ipv4ServiceCIDR, ipv6ServiceCIDR, err := utils.ParseCIDRs(cfg.GetPodCIDR()) | ||
if err != nil { | ||
return fmt.Errorf("invalid service cidr: %v", err) | ||
} | ||
if ipv4ServiceCIDR != "" { | ||
serviceCIDRs = append(serviceCIDRs, ipv4ServiceCIDR) | ||
} | ||
if ipv6ServiceCIDR != "" { | ||
serviceCIDRs = append(serviceCIDRs, ipv6ServiceCIDR) | ||
} | ||
|
||
values := map[string]any{ | ||
"tigeraOperator": map[string]any{ | ||
"registry": tigeraOperatorRegistry, | ||
"image": tigeraOperatorImage, | ||
"version": tigeraOperatorVersion, | ||
}, | ||
"calicoctl": map[string]any{ | ||
"image": calicoCtlImage, | ||
"tag": calicoCtlTag, | ||
}, | ||
"installation": map[string]any{ | ||
"calicoNetwork": map[string]any{ | ||
"ipPools": podIpPools, | ||
}, | ||
}, | ||
"serviceCIDRs": serviceCIDRs, | ||
} | ||
|
||
if _, err := m.Apply(ctx, chartCalico, helm.StatePresent, values); err != nil { | ||
return fmt.Errorf("failed to enable network: %w", err) | ||
} | ||
|
||
return 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,49 @@ | ||
package calico | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/canonical/k8s/pkg/snap" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// CheckNetwork checks the status of the Calico pods in the Kubernetes cluster. | ||
// It verifies if all the Calico pods in the "tigera-operator" namespace are ready. | ||
// If any pod is not ready, it returns false. Otherwise, it returns true. | ||
func CheckNetwork(ctx context.Context, snap snap.Snap) (bool, error) { | ||
client, err := snap.KubernetesClient("calico-system") | ||
if err != nil { | ||
return false, fmt.Errorf("failed to create kubernetes client: %w", err) | ||
} | ||
|
||
operatorReady, err := client.IsPodReady(ctx, "kube-system", "tigera-operator", metav1.ListOptions{}) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to get calico pods: %w", err) | ||
} | ||
if !operatorReady { | ||
return false, nil | ||
} | ||
|
||
calicoPods, err := client.ListPods(ctx, "calico-system", metav1.ListOptions{}) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to get calico pods: %w", err) | ||
} | ||
calicoApiserverPods, err := client.ListPods(ctx, "calico-apiserver", metav1.ListOptions{}) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to get calico-apiserver pods: %w", err) | ||
} | ||
|
||
for _, pod := range append(calicoPods, calicoApiserverPods...) { | ||
isReady, err := client.IsPodReady(ctx, pod.Name, "calico-system", metav1.ListOptions{}) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to check if pod %q is ready: %w", pod.Name, 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
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
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