forked from kubeflow/kubeflow
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #436 from jiridanek/jd_webhook_no_restart
RHOAIENG-15335: feat(odh-nbc/webhook): don't initiate updates that would restart a running pod
- Loading branch information
Showing
5 changed files
with
245 additions
and
30 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
80 changes: 80 additions & 0 deletions
80
components/odh-notebook-controller/controllers/notebook_webhook_utils.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,80 @@ | ||
/* | ||
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 ( | ||
"context" | ||
"fmt" | ||
"github.com/google/go-cmp/cmp" | ||
|
||
"github.com/go-logr/logr" | ||
) | ||
|
||
// UpdatesPending is either NoPendingUpdates, or a new value providing a Reason for the update. | ||
type UpdatesPending struct { | ||
Reason string | ||
} | ||
|
||
var ( | ||
NoPendingUpdates = &UpdatesPending{} | ||
) | ||
|
||
// FirstDifferenceReporter is a custom go-cmp reporter that only records the first difference. | ||
type FirstDifferenceReporter struct { | ||
path cmp.Path | ||
diff string | ||
} | ||
|
||
func (r *FirstDifferenceReporter) PushStep(ps cmp.PathStep) { | ||
r.path = append(r.path, ps) | ||
} | ||
|
||
func (r *FirstDifferenceReporter) Report(rs cmp.Result) { | ||
if r.diff == "" && !rs.Equal() { | ||
vx, vy := r.path.Last().Values() | ||
r.diff = fmt.Sprintf("%#v: %+v != %+v", r.path, vx, vy) | ||
} | ||
} | ||
|
||
func (r *FirstDifferenceReporter) PopStep() { | ||
r.path = r.path[:len(r.path)-1] | ||
} | ||
|
||
func (r *FirstDifferenceReporter) String() string { | ||
return r.diff | ||
} | ||
|
||
// getStructDiff compares a and b, reporting the first difference it found in a human-readable single-line string. | ||
func getStructDiff(ctx context.Context, a any, b any) (result string) { | ||
log := logr.FromContextOrDiscard(ctx) | ||
|
||
// calling cmp.Equal may panic, get ready for it | ||
result = "failed to compute the reason for why there is a pending restart" | ||
defer func() { | ||
if r := recover(); r != nil { | ||
log.Error(fmt.Errorf("failed to compute struct difference: %+v", r), "Cannot determine reason for restart") | ||
} | ||
}() | ||
|
||
var comparator FirstDifferenceReporter | ||
eq := cmp.Equal(a, b, cmp.Reporter(&comparator)) | ||
if eq { | ||
log.Error(nil, "Unexpectedly attempted to diff structs that are actually equal") | ||
} | ||
result = comparator.String() | ||
|
||
return | ||
} |
64 changes: 64 additions & 0 deletions
64
components/odh-notebook-controller/controllers/notebook_webhook_utils_test.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,64 @@ | ||
/* | ||
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 ( | ||
"context" | ||
"testing" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFirstDifferenceReporter(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
a any | ||
b any | ||
diff string | ||
}{ | ||
{"", 42, 42, ""}, | ||
{"", v1.Pod{Spec: v1.PodSpec{NodeName: "node1"}}, v1.Pod{Spec: v1.PodSpec{NodeName: "node2"}}, "{v1.Pod}.Spec.NodeName: node1 != node2"}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var reporter FirstDifferenceReporter | ||
eq := cmp.Equal(tt.a, tt.b, cmp.Reporter(&reporter)) | ||
assert.Equal(t, tt.diff == "", eq) | ||
assert.Equal(t, tt.diff, reporter.String()) | ||
}) | ||
} | ||
} | ||
|
||
func TestGetStructDiff(t *testing.T) { | ||
var tests = []struct { | ||
name string | ||
a any | ||
b any | ||
expected string | ||
}{ | ||
{"simple numbers", 42, 42, ""}, | ||
{"differing pods", v1.Pod{Spec: v1.PodSpec{NodeName: "node1"}}, v1.Pod{Spec: v1.PodSpec{NodeName: "node2"}}, "{v1.Pod}.Spec.NodeName: node1 != node2"}, | ||
} | ||
|
||
for _, v := range tests { | ||
t.Run(v.name, func(t *testing.T) { | ||
diff := getStructDiff(context.Background(), v.a, v.b) | ||
assert.Equal(t, diff, v.expected) | ||
}) | ||
} | ||
} |
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