Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions test/e2e/framework/kubernetes/create-agnhost-statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ func (c *CreateAgnhostStatefulSet) getAgnhostDeployment() *appsv1.StatefulSet {
},
},
},
NodeSelector: map[string]string{
"kubernetes.io/os": "linux",
},
Containers: []v1.Container{
{
Name: c.AgnhostName,
Expand Down
16 changes: 16 additions & 0 deletions test/e2e/framework/kubernetes/create-resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,22 @@ func CreateResource(ctx context.Context, obj runtime.Object, clientset *kubernet
return fmt.Errorf("failed to create/update NetworkPolicy \"%s\" in namespace \"%s\": %w", o.Name, o.Namespace, err)
}

case *v1.Secret:
log.Printf("Creating/Updating Secret \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
client := clientset.CoreV1().Secrets(o.Namespace)
_, err := client.Get(ctx, o.Name, metaV1.GetOptions{})
if errors.IsNotFound(err) {
_, err = client.Create(ctx, o, metaV1.CreateOptions{})
if err != nil {
return fmt.Errorf("failed to create Secret \"%s\" in namespace \"%s\": %w", o.Name, o.Namespace, err)
}
return nil
}
_, err = client.Update(ctx, o, metaV1.UpdateOptions{})
if err != nil {
return fmt.Errorf("failed to create/update Secret \"%s\" in namespace \"%s\": %w", o.Name, o.Namespace, err)
}

default:
return fmt.Errorf("unknown object type: %T, err: %w", obj, ErrUnknownResourceType)
}
Expand Down
211 changes: 201 additions & 10 deletions test/e2e/framework/kubernetes/delete-resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"time"

appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
Expand All @@ -13,10 +14,188 @@ import (
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)

var ErrDeleteNilResource = fmt.Errorf("cannot create nil resource")

type ResourceType string

const (
DaemonSet ResourceType = "DaemonSet"
Deployment ResourceType = "Deployment"
StatefulSet ResourceType = "StatefulSet"
Service ResourceType = "Service"
ServiceAccount ResourceType = "ServiceAccount"
Role ResourceType = "Role"
RoleBinding ResourceType = "RoleBinding"
ClusterRole ResourceType = "ClusterRole"
ClusterRoleBinding ResourceType = "ClusterRoleBinding"
ConfigMap ResourceType = "ConfigMap"
NetworkPolicy ResourceType = "NetworkPolicy"
Secret ResourceType = "Secret"
Unknown ResourceType = "Unknown"
)

// Parameters can only be strings, heres to help add guardrails
func TypeString(resourceType ResourceType) string {
ResourceTypes := map[ResourceType]string{
DaemonSet: "DaemonSet",
Deployment: "Deployment",
StatefulSet: "StatefulSet",
Service: "Service",
ServiceAccount: "ServiceAccount",
Role: "Role",
RoleBinding: "RoleBinding",
ClusterRole: "ClusterRole",
ClusterRoleBinding: "ClusterRoleBinding",
ConfigMap: "ConfigMap",
NetworkPolicy: "NetworkPolicy",
Secret: "Secret",
Unknown: "Unknown",
}
str, ok := ResourceTypes[resourceType]
if !ok {
return ResourceTypes[Unknown]
}
return str
}

type DeleteKubernetesResource struct {
ResourceType string // can't use enum, breaks parameter parsing, all must be strings
ResourceName string
ResourceNamespace string
KubeConfigFilePath string
}

func (d *DeleteKubernetesResource) Run() error {
config, err := clientcmd.BuildConfigFromFlags("", d.KubeConfigFilePath)
if err != nil {
return fmt.Errorf("error building kubeconfig: %w", err)
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return fmt.Errorf("error creating Kubernetes client: %w", err)
}

ctx, cancel := context.WithTimeout(context.Background(), defaultTimeoutSeconds*time.Second)
defer cancel()

res := ResourceType(d.ResourceType)

var resource runtime.Object

switch res {
case DaemonSet:
resource = &appsv1.DaemonSet{
ObjectMeta: metaV1.ObjectMeta{
Name: d.ResourceName,
Namespace: d.ResourceNamespace,
},
}
case Deployment:
resource = &appsv1.Deployment{
ObjectMeta: metaV1.ObjectMeta{
Name: d.ResourceName,
Namespace: d.ResourceNamespace,
},
}
case StatefulSet:
resource = &appsv1.StatefulSet{
ObjectMeta: metaV1.ObjectMeta{
Name: d.ResourceName,
Namespace: d.ResourceNamespace,
},
}
case Service:
resource = &v1.Service{
ObjectMeta: metaV1.ObjectMeta{
Name: d.ResourceName,
Namespace: d.ResourceNamespace,
},
}
case ServiceAccount:
resource = &v1.ServiceAccount{
ObjectMeta: metaV1.ObjectMeta{
Name: d.ResourceName,
Namespace: d.ResourceNamespace,
},
}
case Role:
resource = &rbacv1.Role{
ObjectMeta: metaV1.ObjectMeta{
Name: d.ResourceName,
Namespace: d.ResourceNamespace,
},
}
case RoleBinding:
resource = &rbacv1.RoleBinding{
ObjectMeta: metaV1.ObjectMeta{
Name: d.ResourceName,
Namespace: d.ResourceNamespace,
},
}
case ClusterRole:
resource = &rbacv1.ClusterRole{
ObjectMeta: metaV1.ObjectMeta{
Name: d.ResourceName,
},
}
case ClusterRoleBinding:
resource = &rbacv1.ClusterRoleBinding{
ObjectMeta: metaV1.ObjectMeta{
Name: d.ResourceName,
},
}
case ConfigMap:
resource = &v1.ConfigMap{
ObjectMeta: metaV1.ObjectMeta{
Name: d.ResourceName,
Namespace: d.ResourceNamespace,
},
}
case NetworkPolicy:
resource = &networkingv1.NetworkPolicy{
ObjectMeta: metaV1.ObjectMeta{
Name: d.ResourceName,
Namespace: d.ResourceNamespace,
},
}
case Secret:
resource = &v1.Secret{
ObjectMeta: metaV1.ObjectMeta{
Name: d.ResourceName,
Namespace: d.ResourceNamespace,
},
}
case Unknown:
return fmt.Errorf("unknown resource type: %s: %w", d.ResourceType, ErrUnknownResourceType)
default:
return ErrUnknownResourceType
}

err = DeleteResource(ctx, resource, clientset)
if err != nil {
return fmt.Errorf("error deleting resource: %w", err)
}

return nil
}

func (d *DeleteKubernetesResource) Stop() error {
return nil
}

func (d *DeleteKubernetesResource) Prevalidate() error {
restype := ResourceType(d.ResourceType)
if restype == Unknown {
return ErrUnknownResourceType
}

return nil
}

func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernetes.Clientset) error { //nolint:gocyclo //this is just boilerplate code
if obj == nil {
return ErrCreateNilResource
Expand All @@ -36,7 +215,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
}

case *appsv1.Deployment:
log.Printf("Creating/Updating Deployment \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
log.Printf("Deleting Deployment \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
client := clientset.AppsV1().Deployments(o.Namespace)
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
if err != nil {
Expand All @@ -48,7 +227,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
}

case *appsv1.StatefulSet:
log.Printf("Creating/Updating StatefulSet \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
log.Printf("Deleting StatefulSet \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
client := clientset.AppsV1().StatefulSets(o.Namespace)
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
if err != nil {
Expand All @@ -60,7 +239,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
}

case *v1.Service:
log.Printf("Creating/Updating Service \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
log.Printf("Deleting Service \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
client := clientset.CoreV1().Services(o.Namespace)
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
if err != nil {
Expand All @@ -72,7 +251,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
}

case *v1.ServiceAccount:
log.Printf("Creating/Updating ServiceAccount \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
log.Printf("Deleting ServiceAccount \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
client := clientset.CoreV1().ServiceAccounts(o.Namespace)
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
if err != nil {
Expand All @@ -84,7 +263,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
}

case *rbacv1.Role:
log.Printf("Creating/Updating Role \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
log.Printf("Deleting Role \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
client := clientset.RbacV1().Roles(o.Namespace)
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
if err != nil {
Expand All @@ -96,7 +275,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
}

case *rbacv1.RoleBinding:
log.Printf("Creating/Updating RoleBinding \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
log.Printf("Deleting RoleBinding \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
client := clientset.RbacV1().RoleBindings(o.Namespace)
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
if err != nil {
Expand All @@ -108,7 +287,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
}

case *rbacv1.ClusterRole:
log.Printf("Creating/Updating ClusterRole \"%s\"...\n", o.Name)
log.Printf("Deleting ClusterRole \"%s\"...\n", o.Name)
client := clientset.RbacV1().ClusterRoles()
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
if err != nil {
Expand All @@ -120,7 +299,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
}

case *rbacv1.ClusterRoleBinding:
log.Printf("Creating/Updating ClusterRoleBinding \"%s\"...\n", o.Name)
log.Printf("Deleting ClusterRoleBinding \"%s\"...\n", o.Name)
client := clientset.RbacV1().ClusterRoleBindings()
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
if err != nil {
Expand All @@ -132,7 +311,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
}

case *v1.ConfigMap:
log.Printf("Creating/Updating ConfigMap \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
log.Printf("Deleting ConfigMap \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
client := clientset.CoreV1().ConfigMaps(o.Namespace)
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
if err != nil {
Expand All @@ -144,7 +323,7 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
}

case *networkingv1.NetworkPolicy:
log.Printf("Creating/Updating NetworkPolicy \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
log.Printf("Deleting NetworkPolicy \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
client := clientset.NetworkingV1().NetworkPolicies(o.Namespace)
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
if err != nil {
Expand All @@ -155,6 +334,18 @@ func DeleteResource(ctx context.Context, obj runtime.Object, clientset *kubernet
return fmt.Errorf("failed to delete NetworkPolicy \"%s\" in namespace \"%s\": %w", o.Name, o.Namespace, err)
}

case *v1.Secret:
log.Printf("Deleting Secret \"%s\" in namespace \"%s\"...\n", o.Name, o.Namespace)
client := clientset.CoreV1().Secrets(o.Namespace)
err := client.Delete(ctx, o.Name, metaV1.DeleteOptions{})
if err != nil {
if errors.IsNotFound(err) {
log.Printf("Secret \"%s\" in namespace \"%s\" does not exist\n", o.Name, o.Namespace)
return nil
}
return fmt.Errorf("failed to delete Secret \"%s\" in namespace \"%s\": %w", o.Name, o.Namespace, err)
}

default:
return fmt.Errorf("unknown object type: %T, err: %w", obj, ErrUnknownResourceType)
}
Expand Down
1 change: 0 additions & 1 deletion test/e2e/framework/kubernetes/port-forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func (p *PortForward) Run() error {
}

portForwardFn := func() error {

// if we have a pod name (likely from affinity above), use it, otherwise use label selector
opts := k8s.PortForwardingOpts{
Namespace: p.Namespace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,25 @@ import (
"sync"
"testing"
"time"

"github.com/Azure/azure-container-networking/test/e2e/framework/types"
)

func TestFramework(t *testing.T) {
job := types.NewJob("Validate that drop metrics are present in the prometheus endpoint")
runner := types.NewRunner(t, job)
job := NewJob("Validate that drop metrics are present in the prometheus endpoint")
runner := NewRunner(t, job)
defer runner.Run()

job.AddStep(&TestBackground{
CounterName: "Example Counter",
}, &types.StepOptions{
}, &StepOptions{
ExpectError: false,
RunInBackgroundWithID: "TestStep",
})

job.AddStep(&types.Sleep{
job.AddStep(&Sleep{
Duration: 1 * time.Second,
}, nil)

job.AddStep(&types.Stop{
job.AddStep(&Stop{
BackgroundID: "TestStep",
}, nil)
}
Expand Down
Loading