This repository was archived by the owner on Feb 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cri: handle the migration to the new configuration format
Ensure that the local files of the user are properly migrated to the new cri-o configuration format. Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
- Loading branch information
Showing
9 changed files
with
248 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright (c) 2020 SUSE LLC. | ||
* | ||
* 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 cluster | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
|
||
"github.com/SUSE/skuba/internal/pkg/skuba/kubernetes" | ||
"github.com/SUSE/skuba/pkg/skuba" | ||
clusterinit "github.com/SUSE/skuba/pkg/skuba/actions/cluster/init" | ||
) | ||
|
||
// CriMigrate migrates the old configuration of cri-o < 1.17 to the new format. | ||
func CriMigrate() error { | ||
if err := criGenerateLocalConfiguration(); err != nil { | ||
return err | ||
} | ||
|
||
if err := criRemoveLocalOldFile(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func criGenerateLocalConfiguration() error { | ||
cfg := clusterinit.InitConfiguration{ | ||
PauseImage: kubernetes.ComponentContainerImageForClusterVersion(kubernetes.Pause, kubernetes.LatestVersion()), | ||
StrictCapDefaults: !criHadStrictCapDefaults(), | ||
} | ||
|
||
files := clusterinit.CriScaffoldFiles["criconfig"] | ||
for _, file := range files { | ||
if err := clusterinit.WriteScaffoldFile(file, cfg); err != nil { | ||
_ = os.RemoveAll(skuba.CriConfDir()) | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func criHadStrictCapDefaults() bool { | ||
data, err := ioutil.ReadFile(skuba.CriDockerDefaultsConfFile()) | ||
if err != nil { | ||
return false | ||
} | ||
return strings.Contains(string(data), "--default-capabilities") | ||
} | ||
|
||
func criRemoveLocalOldFile() error { | ||
_, err := os.Stat(skuba.CriDockerDefaultsConfFile()) | ||
if os.IsNotExist(err) { | ||
return nil | ||
} | ||
return os.Remove(skuba.CriDockerDefaultsConfFile()) | ||
} |
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,92 @@ | ||
/* | ||
* Copyright (c) 2020 SUSE LLC. | ||
* | ||
* 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 cluster | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/SUSE/skuba/pkg/skuba" | ||
) | ||
|
||
func TestCriMigrate(t *testing.T) { | ||
// First of all, create the new working directory as it will be taken by | ||
// skuba. | ||
|
||
dir, err := ioutil.TempDir("/tmp", "skuba-cri-test") | ||
if err != nil { | ||
t.Fatalf("Could not initialize test: %v", err) | ||
} | ||
defer os.RemoveAll(dir) | ||
destPath := filepath.Join(dir, "addons", "cri") | ||
err = os.MkdirAll(destPath, os.ModePerm) | ||
if err != nil { | ||
t.Fatalf("Could not initialize test: %v", err) | ||
} | ||
|
||
// Move the old `default_flags` we have in `testdata` into the new temporary | ||
// working directory. | ||
|
||
b, err := ioutil.ReadFile(filepath.Join("testdata", "addons", "cri", "default_flags")) | ||
if err != nil { | ||
t.Fatalf("Could not initialize test: %v", err) | ||
} | ||
err = ioutil.WriteFile(filepath.Join(destPath, "default_flags"), b, 0644) | ||
if err != nil { | ||
t.Fatalf("Could not initialize test: %v", err) | ||
} | ||
|
||
// Now it's safe to change the working directory. | ||
|
||
wd, _ := os.Getwd() | ||
defer func() { | ||
_ = os.Chdir(wd) | ||
}() | ||
_ = os.Chdir(dir) | ||
|
||
// Test: before calling the function, check that the expected file exists | ||
// there. Then, upon calling the tested function, we should have the new | ||
// configuration schema (with `default_capabilities` as given in the old | ||
// configuration). | ||
|
||
_, err = os.Stat(skuba.CriDockerDefaultsConfFile()) | ||
if err != nil { | ||
t.Fatalf("File should exist, got '%v' instead", err) | ||
} | ||
|
||
err = CriMigrate() | ||
if err != nil { | ||
t.Fatalf("Function should've run correctly") | ||
} | ||
|
||
_, err = os.Stat(skuba.CriDockerDefaultsConfFile()) | ||
if err == nil || !os.IsNotExist(err) { | ||
t.Fatalf("File should not exist, got '%v' instead", err) | ||
} | ||
|
||
b, err = ioutil.ReadFile(filepath.Join(destPath, "conf.d", "01-caasp.conf")) | ||
if err != nil { | ||
t.Fatalf("File should be readable, got '%v' instead", err) | ||
} | ||
if !strings.Contains(string(b), "default_capabilities") { | ||
t.Fatalf("New file should include default capabilities") | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
internal/pkg/skuba/upgrade/cluster/testdata/addons/cri/default_flags
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,7 @@ | ||
## Path : System/Management | ||
## Description : Extra cli switches for crio daemon | ||
## Type : string | ||
## Default : "" | ||
## ServiceRestart : crio | ||
# | ||
CRIO_OPTIONS=--pause-image=registry.suse.com/caasp/v4/pause:3.1 --default-capabilities="CHOWN,DAC_OVERRIDE,FSETID,FOWNER,NET_RAW,SETGID,SETUID,SETPCAP,NET_BIND_SERVICE,SYS_CHROOT,KILL,MKNOD,AUDIT_WRITE,SETFCAP" |
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
Oops, something went wrong.