2022-03-15 17:29:56 +00:00
|
|
|
package tests
|
2021-07-01 23:08:35 +00:00
|
|
|
|
|
|
|
import (
|
2021-10-07 19:47:00 +00:00
|
|
|
"net"
|
2021-07-01 23:08:35 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/daemons/config"
|
|
|
|
"github.com/k3s-io/k3s/pkg/daemons/control/deps"
|
2021-07-01 23:08:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GenerateDataDir creates a temporary directory at "/tmp/k3s/<RANDOM_STRING>/".
|
|
|
|
// The latest directory created with this function is soft linked to "/tmp/k3s/latest/".
|
|
|
|
// This allows tests to replicate the "/var/lib/rancher/k3s" directory structure.
|
|
|
|
func GenerateDataDir(cnf *config.Control) error {
|
|
|
|
if err := os.MkdirAll(cnf.DataDir, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
testDir, err := os.MkdirTemp(cnf.DataDir, "*")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Remove old symlink and add new one
|
|
|
|
os.Remove(filepath.Join(cnf.DataDir, "latest"))
|
|
|
|
if err = os.Symlink(testDir, filepath.Join(cnf.DataDir, "latest")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cnf.DataDir = testDir
|
|
|
|
cnf.DataDir, err = filepath.Abs(cnf.DataDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanupDataDir removes the associated "/tmp/k3s/<RANDOM_STRING>"
|
2021-10-12 06:13:10 +00:00
|
|
|
// directory along with the 'latest' symlink that points at it.
|
2021-07-01 23:08:35 +00:00
|
|
|
func CleanupDataDir(cnf *config.Control) {
|
2021-10-12 06:13:10 +00:00
|
|
|
os.Remove(filepath.Join(cnf.DataDir, "..", "latest"))
|
2021-07-01 23:08:35 +00:00
|
|
|
os.RemoveAll(cnf.DataDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateRuntime creates a temporary data dir and configures
|
|
|
|
// config.ControlRuntime with all the appropriate certificate keys.
|
|
|
|
func GenerateRuntime(cnf *config.Control) error {
|
2022-02-24 19:01:14 +00:00
|
|
|
cnf.Runtime = &config.ControlRuntime{}
|
2021-07-01 23:08:35 +00:00
|
|
|
if err := GenerateDataDir(cnf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-04 21:54:50 +00:00
|
|
|
os.MkdirAll(filepath.Join(cnf.DataDir, "etc"), 0700)
|
2021-07-01 23:08:35 +00:00
|
|
|
os.MkdirAll(filepath.Join(cnf.DataDir, "tls"), 0700)
|
|
|
|
os.MkdirAll(filepath.Join(cnf.DataDir, "cred"), 0700)
|
|
|
|
|
2022-02-24 19:01:14 +00:00
|
|
|
deps.CreateRuntimeCertFiles(cnf)
|
2021-07-01 23:08:35 +00:00
|
|
|
|
2022-02-24 19:01:14 +00:00
|
|
|
return deps.GenServerDeps(cnf)
|
2021-07-01 23:08:35 +00:00
|
|
|
}
|
2021-10-07 19:47:00 +00:00
|
|
|
|
|
|
|
func ClusterIPNet() *net.IPNet {
|
|
|
|
_, clusterIPNet, _ := net.ParseCIDR("10.42.0.0/16")
|
|
|
|
return clusterIPNet
|
|
|
|
}
|
|
|
|
|
|
|
|
func ServiceIPNet() *net.IPNet {
|
|
|
|
_, serviceIPNet, _ := net.ParseCIDR("10.43.0.0/16")
|
|
|
|
return serviceIPNet
|
|
|
|
}
|