Skip to content

Commit

Permalink
Remove legacy runtime config migration
Browse files Browse the repository at this point in the history
The legacy config has been replaced for quite some releases already. So
this migration code is no longer required.

Signed-off-by: Tom Wieczorek <[email protected]>
  • Loading branch information
twz123 committed Jan 8, 2025
1 parent 0c4f33e commit 27a4471
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 71 deletions.
39 changes: 0 additions & 39 deletions pkg/config/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ limitations under the License.
package config

import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/k0sproject/k0s/internal/pkg/dir"
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
Expand Down Expand Up @@ -68,12 +65,6 @@ func LoadRuntimeConfig(k0sVars *CfgVars) (*RuntimeConfigSpec, error) {
return nil, err
}

// "migrate" old runtime config to allow running commands on a new binary while an old version is still running.
// the legacy runtime config gets deleted when the server running on the old binary is stopped.
if isLegacy(content) {
return migrateLegacyRuntimeConfig(k0sVars, content)
}

config := &RuntimeConfig{}
if err := yaml.Unmarshal(content, config); err != nil {
return nil, err
Expand Down Expand Up @@ -106,36 +97,6 @@ func LoadRuntimeConfig(k0sVars *CfgVars) (*RuntimeConfigSpec, error) {
return spec, nil
}

func migrateLegacyRuntimeConfig(k0sVars *CfgVars, content []byte) (*RuntimeConfigSpec, error) {
cfg := &v1beta1.ClusterConfig{}

if err := yaml.Unmarshal(content, cfg); err != nil {
return nil, fmt.Errorf("failed to unmarshal legacy runtime config: %w", err)
}

// generate a new runtime config
return &RuntimeConfigSpec{K0sVars: k0sVars, NodeConfig: cfg, Pid: os.Getpid()}, nil
}

func isLegacy(data []byte) bool {
scanner := bufio.NewScanner(bytes.NewReader(data))

for scanner.Scan() {
line := scanner.Text()

if strings.HasPrefix(line, "kind:") {
value := strings.TrimSpace(strings.TrimPrefix(line, "kind:"))
return value != RuntimeConfigKind
}
}

if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "error scanning runtime config:", err)
}

return false
}

func NewRuntimeConfig(k0sVars *CfgVars) (*RuntimeConfigSpec, error) {
if _, err := LoadRuntimeConfig(k0sVars); err == nil {
return nil, ErrK0sAlreadyRunning
Expand Down
32 changes: 0 additions & 32 deletions pkg/config/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestLoadRuntimeConfig_Legacy(t *testing.T) {
tmpfile, err := os.CreateTemp("", "runtime-config")
assert.NoError(t, err)
defer os.Remove(tmpfile.Name())

// prepare k0sVars
k0sVars := &CfgVars{
DataDir: "/var/lib/k0s-custom",
RuntimeConfigPath: tmpfile.Name(),
}

content := []byte(`apiVersion: k0s.k0sproject.io/v1beta1
kind: ClusterConfig
metadata:
name: k0s
spec:
api:
address: 10.2.3.4
`)
err = os.WriteFile(k0sVars.RuntimeConfigPath, content, 0644)
assert.NoError(t, err)

spec, err := LoadRuntimeConfig(k0sVars)
assert.NoError(t, err)
assert.NotNil(t, spec)
assert.Equal(t, "/var/lib/k0s-custom", spec.K0sVars.DataDir)
assert.Equal(t, os.Getpid(), spec.Pid)
assert.NotNil(t, spec.NodeConfig)
assert.NotNil(t, spec.NodeConfig.Spec.API)
assert.Equal(t, "10.2.3.4", spec.NodeConfig.Spec.API.Address)
}

func TestLoadRuntimeConfig_K0sNotRunning(t *testing.T) {
// create a temporary file for runtime config
tmpfile, err := os.CreateTemp("", "runtime-config")
Expand Down

0 comments on commit 27a4471

Please sign in to comment.