-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add check if knuu was initialized Signed-off-by: Smuu <18609909+Smuu@users.noreply.github.com> * feat: add networking capabilities Signed-off-by: Smuu <18609909+Smuu@users.noreply.github.com> * doc: add note about executor Signed-off-by: Smuu <18609909+Smuu@users.noreply.github.com> --------- Signed-off-by: Smuu <18609909+Smuu@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
142 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package k8s | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
v1 "k8s.io/api/networking/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"time" | ||
) | ||
|
||
// CreateNetworkPolicy creates a new NetworkPolicy resource. | ||
func CreateNetworkPolicy(namespace string, name string, selectorMap map[string]string, ingressSelectorMap map[string]string, egressSelectorMap map[string]string) error { | ||
var ingress []v1.NetworkPolicyIngressRule | ||
if ingressSelectorMap != nil { | ||
ingress = []v1.NetworkPolicyIngressRule{ | ||
{ | ||
From: []v1.NetworkPolicyPeer{ | ||
{ | ||
PodSelector: &metav1.LabelSelector{ | ||
MatchLabels: ingressSelectorMap, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
var egress []v1.NetworkPolicyEgressRule | ||
if egressSelectorMap != nil { | ||
egress = []v1.NetworkPolicyEgressRule{ | ||
{ | ||
To: []v1.NetworkPolicyPeer{ | ||
{ | ||
PodSelector: &metav1.LabelSelector{ | ||
MatchLabels: egressSelectorMap, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
np := &v1.NetworkPolicy{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: namespace, | ||
Name: name, | ||
}, | ||
Spec: v1.NetworkPolicySpec{ | ||
PodSelector: metav1.LabelSelector{ | ||
MatchLabels: selectorMap, | ||
}, | ||
PolicyTypes: []v1.PolicyType{ | ||
v1.PolicyTypeIngress, | ||
v1.PolicyTypeEgress, | ||
}, | ||
Ingress: ingress, | ||
Egress: egress, | ||
}, | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) | ||
defer cancel() | ||
|
||
if !IsInitialized() { | ||
return fmt.Errorf("knuu is not initialized") | ||
} | ||
np, err := Clientset().NetworkingV1().NetworkPolicies(namespace).Create(ctx, np, metav1.CreateOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("error creating network policy %s: %w", name, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// DeleteNetworkPolicy removes a NetworkPolicy resource. | ||
func DeleteNetworkPolicy(namespace string, name string) error { | ||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) | ||
defer cancel() | ||
|
||
if !IsInitialized() { | ||
return fmt.Errorf("knuu is not initialized") | ||
} | ||
err := Clientset().NetworkingV1().NetworkPolicies(namespace).Delete(ctx, name, metav1.DeleteOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("error deleting network policy %s: %w", name, 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package knuu | ||
|
||
// InstanceType represents the type of the instance | ||
type InstanceType int | ||
|
||
// Possible types of the instance | ||
const ( | ||
BasicInstance InstanceType = iota | ||
ExecutorInstance | ||
) | ||
|
||
// String returns the string representation of the type | ||
func (s InstanceType) String() string { | ||
if s < 0 || s > 2 { | ||
return "Unknown" | ||
} | ||
return [...]string{"BasicInstance", "ExecutorInstance"}[s] | ||
} |