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

Adding Networkpolicy to ClusterSets #125

Merged
merged 8 commits into from
Oct 15, 2024
Merged

Conversation

galal-hussein
Copy link
Collaborator

@galal-hussein galal-hussein commented Oct 1, 2024

The PR:

  • Adds clusterset type by @MbolotSuse
  • Adds a default networkpolicy for each clusterset which does the following:
    • Accepts all ingress traffic
    • allow all egress except for:
      • podCIDRS of the current nodes in the system with an exception for the pods in the same namespace of the clusterset and the coredns pod
  • Adds a node controller to track new nodes and update the netpolicies with their pod CIDR.

Issue:

MbolotSuse and others added 2 commits October 2, 2024 02:09
Adds types for cluster sets, which allows constraining a few elements of
clusters including: overall resource usage, and which nodes it can use.
@galal-hussein galal-hussein changed the title Netpol Adding Networkpolicy to ClusterSets Oct 1, 2024
main.go Outdated Show resolved Hide resolved
Signed-off-by: galal-hussein <[email protected]>
Signed-off-by: galal-hussein <[email protected]>
@@ -11,6 +11,10 @@ imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""

host:
# clusterCIDR specifies the clusterCIDR that will be added to the default networkpolicy for clustersets
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to comment/define the behavior for cases where people don't supply the clusterCIDR but do try to enable network policies (i.e. do we then try to extract information from the available nodes)?

Also I think it would be good to provide more information to help people connect this to the specific values in the k8s distribution that they are using. Meaning, this is a pod CIDR - so it would be good to state that specifically to make it easier for people to look it up on the cluster.

main.go Outdated

klog.Info("adding networkpolicy node controller")
if err := clusterset.AddNodeController(ctx, mgr, clusterCIDR); err != nil {
klog.Fatalf("Failed to add the clusterset controller: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
klog.Fatalf("Failed to add the clusterset controller: %v", err)
klog.Fatalf("Failed to add the clusterset node controller: %v", err)

main.go Outdated
Comment on lines 44 to 46
if clusterCIDR == "" {
clusterCIDR = os.Getenv(clusterCIDREnvVar)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CLI uses urfave/cli to allow us to read values either from the env var or the CLI. This part of the code doesn't do that, and because of that needs to more manually parse from both places. I think that we should do what the CLI does here - to unify the approaches and avoid duplicating the code.

}

func (c *ClusterSetReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
klog.Infof("%#v", req)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think that we can remove this debug statement now.

return reconcile.Result{}, fmt.Errorf("unable to get the clusterset: %w", err)
}

klog.Infof("got a clusterset: %v", clusterSet)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think that we can remove this debug statement now.

if clusterSet.Spec.MaxLimits != nil {
quota := v1.ResourceQuota{
ObjectMeta: metav1.ObjectMeta{
Name: "clusterset-quota",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now there's no limit on the number of clustersets that user can make for a given namespace. However, each clusterset will try to make a clusterset with the same name, which could cause problems.

},
}
quota.Spec.Hard = clusterSet.Spec.MaxLimits
if err := c.Client.Create(ctx, &quota); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this needs to handle the case where the quota already exists but we wanted to update the limits (i.e. check for conflict, and on conflict update). Do see the above note though on the name - if we don't fix the name to be exclusive to one clusterset that could cause problems.

pkg/apis/k3k.io/v1alpha1/set_types.go Show resolved Hide resolved
Comment on lines +107 to +110
TypeMeta: metav1.TypeMeta{
Kind: "NetworkPolicy",
APIVersion: "networking.k8s.io/v1",
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Is this necessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied that convention from all wrangler controller code, not really sure if its different in controller-runtime client, but in wrangler they always write the GVK in the resources they create programmatically

pkg/controller/clusterset/clusterset.go Show resolved Hide resolved
Signed-off-by: galal-hussein <[email protected]>
Signed-off-by: galal-hussein <[email protected]>
@galal-hussein galal-hussein merged commit bf1fe2a into rancher:main Oct 15, 2024
1 check passed
briandowns pushed a commit to briandowns/k3k that referenced this pull request Dec 4, 2024
* Adding cluster set types

Adds types for cluster sets, which allows constraining a few elements of
clusters including: overall resource usage, and which nodes it can use.

* Add networkpolicy to clustersets

Signed-off-by: galal-hussein <[email protected]>

* Fix comments

Signed-off-by: galal-hussein <[email protected]>

* Fix linting issues

Signed-off-by: galal-hussein <[email protected]>

* fixing node controller logic and nit fixes

Signed-off-by: galal-hussein <[email protected]>

* more fixes

Signed-off-by: galal-hussein <[email protected]>

* fix main cli

Signed-off-by: galal-hussein <[email protected]>

* Comment the resource quota for clustersets

Signed-off-by: galal-hussein <[email protected]>

---------

Signed-off-by: galal-hussein <[email protected]>
Co-authored-by: Michael Bolot <[email protected]>
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 this pull request may close these issues.

3 participants