-
Notifications
You must be signed in to change notification settings - Fork 38.7k
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 #107986 from wzshiming/promote/shutdown-based-on-p…
…od-priority Promote graceful shutdown based on pod priority to beta
- Loading branch information
Showing
8 changed files
with
260 additions
and
5 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
Copyright 2022 The Kubernetes Authors. | ||
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 nodeshutdown | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
) | ||
|
||
type storage interface { | ||
Store(data interface{}) (err error) | ||
Load(data interface{}) (err error) | ||
} | ||
|
||
type localStorage struct { | ||
Path string | ||
} | ||
|
||
func (l localStorage) Store(data interface{}) (err error) { | ||
b, err := json.Marshal(data) | ||
if err != nil { | ||
return err | ||
} | ||
return atomicWrite(l.Path, b, 0644) | ||
} | ||
|
||
func (l localStorage) Load(data interface{}) (err error) { | ||
b, err := os.ReadFile(l.Path) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return nil | ||
} | ||
return err | ||
} | ||
return json.Unmarshal(b, data) | ||
} | ||
|
||
func timestamp(t time.Time) float64 { | ||
if t.IsZero() { | ||
return 0 | ||
} | ||
return float64(t.Unix()) | ||
} | ||
|
||
type state struct { | ||
StartTime time.Time `json:"startTime"` | ||
EndTime time.Time `json:"endTime"` | ||
} | ||
|
||
// atomicWrite atomically writes data to a file specified by filename. | ||
func atomicWrite(filename string, data []byte, perm os.FileMode) error { | ||
f, err := ioutil.TempFile(filepath.Dir(filename), ".tmp-"+filepath.Base(filename)) | ||
if err != nil { | ||
return err | ||
} | ||
err = os.Chmod(f.Name(), perm) | ||
if err != nil { | ||
f.Close() | ||
return err | ||
} | ||
n, err := f.Write(data) | ||
if err != nil { | ||
f.Close() | ||
return err | ||
} | ||
if n < len(data) { | ||
f.Close() | ||
return io.ErrShortWrite | ||
} | ||
if err := f.Sync(); err != nil { | ||
f.Close() | ||
return err | ||
} | ||
if err := f.Close(); err != nil { | ||
return err | ||
} | ||
return os.Rename(f.Name(), filename) | ||
} |
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,69 @@ | ||
/* | ||
Copyright 2022 The Kubernetes Authors. | ||
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 nodeshutdown | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestLocalStorage(t *testing.T) { | ||
var localStorageStateFileName = "graceful_node_shutdown_state" | ||
tempdir := os.TempDir() | ||
path := filepath.Join(tempdir, localStorageStateFileName) | ||
l := localStorage{ | ||
Path: path, | ||
} | ||
now := time.Now() | ||
want := state{ | ||
StartTime: now, | ||
EndTime: now, | ||
} | ||
err := l.Store(want) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
got := state{} | ||
err = l.Load(&got) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
if !want.StartTime.Equal(got.StartTime) || !want.EndTime.Equal(got.EndTime) { | ||
t.Errorf("got %+v, want %+v", got, want) | ||
return | ||
} | ||
|
||
raw, err := os.ReadFile(path) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
nowStr := now.Format(time.RFC3339Nano) | ||
wantRaw := fmt.Sprintf(`{"startTime":"` + nowStr + `","endTime":"` + nowStr + `"}`) | ||
if string(raw) != wantRaw { | ||
t.Errorf("got %s, want %s", string(raw), wantRaw) | ||
return | ||
} | ||
|
||
} |
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