-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: E2E Framework [Kubernetes steps] [3/6] (#2529)
* add kubernetes steps * update port forwarder * go mod
- Loading branch information
Showing
11 changed files
with
1,270 additions
and
113 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
157 changes: 157 additions & 0 deletions
157
test/e2e/framework/kubernetes/create-agnhost-statefulset.go
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,157 @@ | ||
package k8s | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
"time" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
var ErrLabelMissingFromPod = fmt.Errorf("label missing from pod") | ||
|
||
const ( | ||
AgnhostHTTPPort = 80 | ||
AgnhostReplicas = 1 | ||
) | ||
|
||
type CreateAgnhostStatefulSet struct { | ||
AgnhostName string | ||
AgnhostNamespace string | ||
KubeConfigFilePath string | ||
} | ||
|
||
func (c *CreateAgnhostStatefulSet) Run() error { | ||
config, err := clientcmd.BuildConfigFromFlags("", c.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() | ||
|
||
agnhostStatefulest := c.getAgnhostDeployment() | ||
|
||
err = CreateResource(ctx, agnhostStatefulest, clientset) | ||
if err != nil { | ||
return fmt.Errorf("error agnhost component: %w", err) | ||
} | ||
|
||
selector, exists := agnhostStatefulest.Spec.Selector.MatchLabels["app"] | ||
if !exists { | ||
return fmt.Errorf("missing label \"app=%s\" from agnhost statefulset: %w", c.AgnhostName, ErrLabelMissingFromPod) | ||
} | ||
|
||
labelSelector := fmt.Sprintf("app=%s", selector) | ||
err = WaitForPodReady(ctx, clientset, c.AgnhostNamespace, labelSelector) | ||
if err != nil { | ||
return fmt.Errorf("error waiting for agnhost pod to be ready: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *CreateAgnhostStatefulSet) Prevalidate() error { | ||
return nil | ||
} | ||
|
||
func (c *CreateAgnhostStatefulSet) Postvalidate() error { | ||
return nil | ||
} | ||
|
||
func (c *CreateAgnhostStatefulSet) getAgnhostDeployment() *appsv1.StatefulSet { | ||
reps := int32(AgnhostReplicas) | ||
|
||
return &appsv1.StatefulSet{ | ||
TypeMeta: metaV1.TypeMeta{ | ||
Kind: "Deployment", | ||
APIVersion: "apps/v1", | ||
}, | ||
ObjectMeta: metaV1.ObjectMeta{ | ||
Name: c.AgnhostName, | ||
Namespace: c.AgnhostNamespace, | ||
}, | ||
Spec: appsv1.StatefulSetSpec{ | ||
Replicas: &reps, | ||
Selector: &metaV1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
"app": c.AgnhostName, | ||
"k8s-app": "agnhost", | ||
}, | ||
}, | ||
Template: v1.PodTemplateSpec{ | ||
ObjectMeta: metaV1.ObjectMeta{ | ||
Labels: map[string]string{ | ||
"app": c.AgnhostName, | ||
"k8s-app": "agnhost", | ||
}, | ||
Annotations: map[string]string{ | ||
"policy.cilium.io/proxy-visibility": "<Egress/53/UDP/DNS>", | ||
}, | ||
}, | ||
|
||
Spec: v1.PodSpec{ | ||
Affinity: &v1.Affinity{ | ||
PodAntiAffinity: &v1.PodAntiAffinity{ | ||
// prefer an even spread across the cluster to avoid scheduling on the same node | ||
PreferredDuringSchedulingIgnoredDuringExecution: []v1.WeightedPodAffinityTerm{ | ||
{ | ||
Weight: MaxAffinityWeight, | ||
PodAffinityTerm: v1.PodAffinityTerm{ | ||
TopologyKey: "kubernetes.io/hostname", | ||
LabelSelector: &metaV1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
"k8s-app": "agnhost", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Containers: []v1.Container{ | ||
{ | ||
Name: c.AgnhostName, | ||
Image: "acnpublic.azurecr.io/agnhost:2.40", | ||
Resources: v1.ResourceRequirements{ | ||
Requests: v1.ResourceList{ | ||
"memory": resource.MustParse("20Mi"), | ||
}, | ||
Limits: v1.ResourceList{ | ||
"memory": resource.MustParse("20Mi"), | ||
}, | ||
}, | ||
Command: []string{ | ||
"/agnhost", | ||
}, | ||
Args: []string{ | ||
"serve-hostname", | ||
"--http", | ||
"--port", | ||
strconv.Itoa(AgnhostHTTPPort), | ||
}, | ||
|
||
Ports: []v1.ContainerPort{ | ||
{ | ||
ContainerPort: AgnhostHTTPPort, | ||
}, | ||
}, | ||
Env: []v1.EnvVar{}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
Oops, something went wrong.