Repository navigation
Implement GPU Operator high availability #3103
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
Merged
+383
−41
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,55 @@ | ||
| /** | ||
| # Copyright (c) NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| **/ | ||
|
|
||
| package controllers | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| promcli "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/stretchr/testify/require" | ||
| "sigs.k8s.io/controller-runtime/pkg/metrics" | ||
| ) | ||
|
|
||
| func TestOperatorMetricsExposedOnlyAfterRegistration(t *testing.T) { | ||
| globalBefore, err := metrics.Registry.Gather() | ||
| require.NoError(t, err) | ||
| m := InitOperatorMetrics() | ||
| globalAfter, err := metrics.Registry.Gather() | ||
| require.NoError(t, err) | ||
| require.Len(t, globalAfter, len(globalBefore), "standby construction must not register metrics") | ||
|
|
||
| registry := promcli.NewRegistry() | ||
| const name = "gpu_operator_reconciliation_status" | ||
|
|
||
| metricFamilies, err := registry.Gather() | ||
| require.NoError(t, err) | ||
| for _, family := range metricFamilies { | ||
| require.NotEqual(t, name, family.GetName(), "standby must not expose reconciliation status") | ||
| } | ||
|
|
||
| m.register(registry) | ||
| metricFamilies, err = registry.Gather() | ||
| require.NoError(t, err) | ||
| for _, family := range metricFamilies { | ||
| if family.GetName() == name { | ||
| require.Len(t, family.GetMetric(), 1) | ||
| require.Zero(t, family.GetMetric()[0].GetGauge().GetValue()) | ||
| return | ||
| } | ||
| } | ||
| t.Fatal("leader did not expose reconciliation status after registration") | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -20,3 +20,5 @@ | |
| .idea/ | ||
| *.tmproj | ||
| .vscode/ | ||
| # Go unit tests for the chart | ||
| *_test.go | ||
This file contains hidden or 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,184 @@ | ||
| /** | ||
| # Copyright (c) NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| **/ | ||
|
|
||
| package gpuoperator | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os/exec" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| appsv1 "k8s.io/api/apps/v1" | ||
| policyv1 "k8s.io/api/policy/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/util/intstr" | ||
| "sigs.k8s.io/yaml" | ||
| ) | ||
|
|
||
| func TestOperatorRejectsInvalidReplicaCount(t *testing.T) { | ||
| if _, err := exec.LookPath("helm"); err != nil { | ||
| t.Skip("helm binary not found in PATH") | ||
| } | ||
|
|
||
| for _, replicas := range []int{0, -1} { | ||
| t.Run(fmt.Sprint(replicas), func(t *testing.T) { | ||
| cmd := exec.Command("helm", "template", "gpu-operator", ".", "--values", "-") | ||
| cmd.Stdin = strings.NewReader(fmt.Sprintf("operator:\n replicaCount: %d\n", replicas)) | ||
| out, err := cmd.CombinedOutput() | ||
| require.Error(t, err) | ||
| require.Contains(t, string(out), "operator.replicaCount must be at least 1") | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // renderChart returns the operator Deployment and, if rendered, its PodDisruptionBudget. | ||
| func renderChart(t *testing.T, values string) (*appsv1.Deployment, *policyv1.PodDisruptionBudget) { | ||
| t.Helper() | ||
| if _, err := exec.LookPath("helm"); err != nil { | ||
| t.Skip("helm binary not found in PATH") | ||
| } | ||
|
|
||
| cmd := exec.Command("helm", "template", "gpu-operator", ".", "--namespace", "gpu-operator", "--values", "-") | ||
| cmd.Stdin = strings.NewReader(values) | ||
| var stderr strings.Builder | ||
| cmd.Stderr = &stderr | ||
| out, err := cmd.Output() | ||
| require.NoError(t, err, stderr.String()) | ||
|
|
||
| var deployment *appsv1.Deployment | ||
| var pdb *policyv1.PodDisruptionBudget | ||
| for doc := range strings.SplitSeq(string(out), "\n---\n") { | ||
| var meta metav1.PartialObjectMetadata | ||
| require.NoError(t, yaml.Unmarshal([]byte(doc), &meta)) | ||
| if meta.Name != "gpu-operator" { | ||
| continue | ||
| } | ||
| switch meta.Kind { | ||
| case "Deployment": | ||
| deployment = &appsv1.Deployment{} | ||
| require.NoError(t, yaml.Unmarshal([]byte(doc), deployment)) | ||
| case "PodDisruptionBudget": | ||
| pdb = &policyv1.PodDisruptionBudget{} | ||
| require.NoError(t, yaml.Unmarshal([]byte(doc), pdb)) | ||
| } | ||
| } | ||
| require.NotNil(t, deployment) | ||
| return deployment, pdb | ||
| } | ||
|
|
||
| func requireSoftPodAntiAffinity(t *testing.T, deployment *appsv1.Deployment) { | ||
| t.Helper() | ||
| podAntiAffinity := deployment.Spec.Template.Spec.Affinity.PodAntiAffinity | ||
| require.NotNil(t, podAntiAffinity) | ||
| require.Empty(t, podAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution) | ||
| require.Len(t, podAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution, 1) | ||
| term := podAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution[0].PodAffinityTerm | ||
| require.Equal(t, "kubernetes.io/hostname", term.TopologyKey) | ||
| require.Equal(t, deployment.Spec.Selector, term.LabelSelector) | ||
| } | ||
|
|
||
| func TestOperatorHighAvailability(t *testing.T) { | ||
| testCases := map[string]struct { | ||
| values string | ||
| check func(t *testing.T, deployment *appsv1.Deployment, pdb *policyv1.PodDisruptionBudget) | ||
| }{ | ||
| "defaults render one replica, no anti-affinity and no PDB": { | ||
| check: func(t *testing.T, deployment *appsv1.Deployment, pdb *policyv1.PodDisruptionBudget) { | ||
| require.Equal(t, int32(1), *deployment.Spec.Replicas) | ||
| require.Nil(t, deployment.Spec.Template.Spec.Affinity.PodAntiAffinity) | ||
| require.Nil(t, pdb) | ||
| }, | ||
| }, | ||
| "two replicas render soft anti-affinity and a PDB": { | ||
| values: "operator: {replicaCount: 2}", | ||
| check: func(t *testing.T, deployment *appsv1.Deployment, pdb *policyv1.PodDisruptionBudget) { | ||
| require.NotNil(t, deployment.Spec.Replicas) | ||
| require.Equal(t, int32(2), *deployment.Spec.Replicas) | ||
| requireSoftPodAntiAffinity(t, deployment) | ||
| require.NotNil(t, pdb) | ||
| require.Equal(t, intstr.FromInt32(1), *pdb.Spec.MinAvailable) | ||
| require.NotNil(t, pdb.Spec.UnhealthyPodEvictionPolicy) | ||
| require.Equal(t, policyv1.AlwaysAllow, *pdb.Spec.UnhealthyPodEvictionPolicy) | ||
| require.Equal(t, deployment.Spec.Selector, pdb.Spec.Selector) | ||
| }, | ||
| }, | ||
| "two replicas with user-provided affinity render both the user's node affinity and HA anti-affinity": { | ||
| // This is the user's intended node affinity | ||
| values: ` | ||
| operator: | ||
| replicaCount: 2 | ||
| affinity: | ||
| nodeAffinity: | ||
| preferredDuringSchedulingIgnoredDuringExecution: | ||
| - weight: 1 | ||
| preference: | ||
| matchExpressions: | ||
| - {key: example.com/pool, operator: Exists} | ||
| `, | ||
| check: func(t *testing.T, deployment *appsv1.Deployment, _ *policyv1.PodDisruptionBudget) { | ||
| require.NotNil(t, deployment.Spec.Replicas) | ||
| require.Equal(t, int32(2), *deployment.Spec.Replicas) | ||
| // Check if the rendered helm template includes user's intended node affinity | ||
| terms := deployment.Spec.Template.Spec.Affinity.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution | ||
| require.Len(t, terms, 1) | ||
| require.Equal(t, "example.com/pool", terms[0].Preference.MatchExpressions[0].Key) | ||
| // Check if the rendered helm template includes the HA node affinity | ||
| requireSoftPodAntiAffinity(t, deployment) | ||
| }, | ||
| }, | ||
| "two replicas with HA keys missing (--reuse-values from an older chart) render soft anti-affinity and a PDB": { | ||
| // A null value deletes the key from the merged values. This copies the | ||
| // values that an older release has: the release has no podAntiAffinity | ||
| // or podDisruptionBudget keys. | ||
| values: ` | ||
| operator: | ||
| replicaCount: 2 | ||
| podAntiAffinity: null | ||
| podDisruptionBudget: null | ||
| `, | ||
| check: func(t *testing.T, deployment *appsv1.Deployment, pdb *policyv1.PodDisruptionBudget) { | ||
| require.NotNil(t, deployment.Spec.Replicas) | ||
| require.Equal(t, int32(2), *deployment.Spec.Replicas) | ||
| requireSoftPodAntiAffinity(t, deployment) | ||
| require.NotNil(t, pdb) | ||
| require.Equal(t, deployment.Spec.Selector, pdb.Spec.Selector) | ||
| }, | ||
| }, | ||
| "two replicas with HA features disabled render no anti-affinity and no PDB": { | ||
| values: ` | ||
| operator: | ||
| replicaCount: 2 | ||
| podAntiAffinity: {enabled: false} | ||
| podDisruptionBudget: {enabled: false} | ||
| `, | ||
| check: func(t *testing.T, deployment *appsv1.Deployment, pdb *policyv1.PodDisruptionBudget) { | ||
| require.NotNil(t, deployment.Spec.Replicas) | ||
| require.Equal(t, int32(2), *deployment.Spec.Replicas) | ||
| require.Nil(t, deployment.Spec.Template.Spec.Affinity.PodAntiAffinity) | ||
| require.Nil(t, pdb) | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for name, tc := range testCases { | ||
| t.Run(name, func(t *testing.T) { | ||
| deployment, pdb := renderChart(t, tc.values) | ||
| tc.check(t, deployment, pdb) | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.