-
Notifications
You must be signed in to change notification settings - Fork 217
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
fix(test): configuration changes and fixes needed to scale-test #1085
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,10 @@ import ( | |
"k8s.io/client-go/util/retry" | ||
) | ||
|
||
const ( | ||
deleteNamespaceTimeoutSeconds = 1200 | ||
) | ||
|
||
type DeleteNamespace struct { | ||
Namespace string | ||
KubeConfigFilePath string | ||
|
@@ -30,7 +34,7 @@ func (d *DeleteNamespace) Run() error { | |
return fmt.Errorf("error creating Kubernetes client: %w", err) | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeoutSeconds*time.Second) | ||
ctx, cancel := context.WithTimeout(context.Background(), deleteNamespaceTimeoutSeconds*time.Second) | ||
defer cancel() | ||
|
||
err = clientset.CoreV1().Namespaces().Delete(ctx, d.Namespace, metaV1.DeleteOptions{}) | ||
|
@@ -40,8 +44,10 @@ func (d *DeleteNamespace) Run() error { | |
} | ||
} | ||
|
||
numberOfSteps := 9 | ||
|
||
backoff := wait.Backoff{ | ||
Steps: 6, | ||
Steps: numberOfSteps, | ||
Comment on lines
+47
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing here, the constant just obscures what's going on more than it helps. Steps: 9 |
||
Duration: 10 * time.Second, | ||
Factor: 2.0, | ||
// Jitter: 0.1, | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |||||||||
"context" | ||||||||||
"encoding/json" | ||||||||||
"fmt" | ||||||||||
"log" | ||||||||||
"time" | ||||||||||
|
||||||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||||||||
|
@@ -12,6 +13,10 @@ import ( | |||||||||
"k8s.io/client-go/tools/clientcmd" | ||||||||||
) | ||||||||||
|
||||||||||
const ( | ||||||||||
timeoutToLabelAllPodsMinutes = 120 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, please use
Suggested change
|
||||||||||
) | ||||||||||
|
||||||||||
type patchStringValue struct { | ||||||||||
Op string `json:"op"` | ||||||||||
Path string `json:"path"` | ||||||||||
|
@@ -50,32 +55,21 @@ func (a *AddSharedLabelsToAllPods) Run() error { | |||||||||
return fmt.Errorf("error creating Kubernetes client: %w", err) | ||||||||||
} | ||||||||||
|
||||||||||
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeoutSeconds*time.Second) | ||||||||||
ctx, cancel := contextToLabelAllPods() | ||||||||||
defer cancel() | ||||||||||
|
||||||||||
resources, err := clientset.CoreV1().Pods(a.Namespace).List(ctx, metav1.ListOptions{}) | ||||||||||
|
||||||||||
patch := []patchStringValue{} | ||||||||||
|
||||||||||
for i := 0; i < a.NumSharedLabelsPerPod; i++ { | ||||||||||
patch = append(patch, patchStringValue{ | ||||||||||
Op: "add", | ||||||||||
Path: "/metadata/labels/shared-lab-" + fmt.Sprintf("%05d", i), | ||||||||||
Value: "val", | ||||||||||
}) | ||||||||||
} | ||||||||||
|
||||||||||
patchBytes, err := json.Marshal(patch) | ||||||||||
patchBytes, err := getSharedLabelsPatch(a.NumSharedLabelsPerPod) | ||||||||||
if err != nil { | ||||||||||
return fmt.Errorf("error marshalling patch: %w", err) | ||||||||||
return fmt.Errorf("error getting label patch: %w", err) | ||||||||||
} | ||||||||||
|
||||||||||
for _, resource := range resources.Items { | ||||||||||
clientset.CoreV1().Pods(a.Namespace).Patch(ctx, resource.Name, | ||||||||||
types.JSONPatchType, | ||||||||||
patchBytes, | ||||||||||
metav1.PatchOptions{}, | ||||||||||
) | ||||||||||
err = patchLabel(ctx, clientset, a.Namespace, resource.Name, patchBytes) | ||||||||||
if err != nil { | ||||||||||
log.Printf("Error adding shared labels to pod %s: %s\n", resource.Name, err) | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
return nil | ||||||||||
|
@@ -85,3 +79,38 @@ func (a *AddSharedLabelsToAllPods) Run() error { | |||||||||
func (a *AddSharedLabelsToAllPods) Stop() error { | ||||||||||
return nil | ||||||||||
} | ||||||||||
|
||||||||||
func patchLabel(ctx context.Context, clientset *kubernetes.Clientset, namespace, podName string, patchBytes []byte) error { | ||||||||||
log.Println("Labeling Pod", podName) | ||||||||||
_, err := clientset.CoreV1().Pods(namespace).Patch(ctx, podName, | ||||||||||
types.JSONPatchType, | ||||||||||
patchBytes, | ||||||||||
metav1.PatchOptions{}, | ||||||||||
) | ||||||||||
if err != nil { | ||||||||||
return fmt.Errorf("error patching pod: %w", err) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I wrap errors, the word "error" is typically redundant (it's an error, so it will be logged as such). It's sufficient to just write what you were trying to do when the error occurred, as you've done:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agree that "error" is redundant, but I prefer to write
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
return nil | ||||||||||
} | ||||||||||
|
||||||||||
func getSharedLabelsPatch(numLabels int) ([]byte, error) { | ||||||||||
patch := []patchStringValue{} | ||||||||||
for i := 0; i < numLabels; i++ { | ||||||||||
patch = append(patch, patchStringValue{ | ||||||||||
Op: "add", | ||||||||||
Path: "/metadata/labels/shared-lab-" + fmt.Sprintf("%05d", i), | ||||||||||
Value: "val", | ||||||||||
}) | ||||||||||
} | ||||||||||
b, err := json.Marshal(patch) | ||||||||||
if err != nil { | ||||||||||
return nil, fmt.Errorf("error marshalling patch: %w", err) | ||||||||||
} | ||||||||||
|
||||||||||
return b, nil | ||||||||||
} | ||||||||||
|
||||||||||
func contextToLabelAllPods() (context.Context, context.CancelFunc) { | ||||||||||
return context.WithTimeout(context.Background(), timeoutToLabelAllPodsMinutes*time.Minute) | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
package scaletest | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
@@ -44,35 +41,23 @@ func (a *AddUniqueLabelsToAllPods) Run() error { | |
return fmt.Errorf("error creating Kubernetes client: %w", err) | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeoutSeconds*time.Second) | ||
ctx, cancel := contextToLabelAllPods() | ||
defer cancel() | ||
|
||
resources, err := clientset.CoreV1().Pods(a.Namespace).List(ctx, metav1.ListOptions{}) | ||
|
||
count := 0 | ||
|
||
for _, resource := range resources.Items { | ||
patch := []patchStringValue{} | ||
|
||
for i := 0; i < a.NumUniqueLabelsPerPod; i++ { | ||
patch = append(patch, patchStringValue{ | ||
Op: "add", | ||
Path: "/metadata/labels/uni-lab-" + fmt.Sprintf("%05d", count), | ||
Value: "val", | ||
}) | ||
count++ | ||
patchBytes, err := getUniqueLabelsPatch(a.NumUniqueLabelsPerPod, &count) | ||
if err != nil { | ||
return fmt.Errorf("error getting label patch: %w", err) | ||
} | ||
|
||
patchBytes, err := json.Marshal(patch) | ||
err = patchLabel(ctx, clientset, a.Namespace, resource.Name, patchBytes) | ||
if err != nil { | ||
return fmt.Errorf("error marshalling patch: %w", err) | ||
return fmt.Errorf("error adding unique label to pod: %w", err) | ||
} | ||
|
||
clientset.CoreV1().Pods(a.Namespace).Patch(ctx, resource.Name, | ||
types.JSONPatchType, | ||
patchBytes, | ||
metav1.PatchOptions{}, | ||
) | ||
} | ||
|
||
return nil | ||
|
@@ -82,3 +67,23 @@ func (a *AddUniqueLabelsToAllPods) Run() error { | |
func (a *AddUniqueLabelsToAllPods) Stop() error { | ||
return nil | ||
} | ||
|
||
func getUniqueLabelsPatch(numLabels int, counter *int) ([]byte, error) { | ||
patch := []patchStringValue{} | ||
|
||
for i := 0; i < numLabels; i++ { | ||
patch = append(patch, patchStringValue{ | ||
Op: "add", | ||
Path: "/metadata/labels/uni-lab-" + fmt.Sprintf("%05d", *counter), | ||
Value: "val", | ||
}) | ||
(*counter)++ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just return this? Or let the caller deal with JSON marshalling so they can just look at the |
||
} | ||
|
||
b, err := json.Marshal(patch) | ||
if err != nil { | ||
return nil, fmt.Errorf("error marshalling patch: %w", err) | ||
} | ||
|
||
return b, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, just inline this. The const makes this less readable (mute the linter if it complains). Also, (even though we're not doing this) the best practice with Go durations is
const deleteNamespaceTimeout = 1200 * time.Second
. Embedding a unit likeSeconds
in the name is an antipattern.