Skip to content

Commit f5f0858

Browse files
committed
Add legacy code that is still required
Always need to do a fake migration from V0 -> V1 -> V3 And in order to that, also need a copy of none driver.
1 parent 29e463f commit f5f0858

File tree

6 files changed

+382
-1
lines changed

6 files changed

+382
-1
lines changed

pkg/libmachine/host/driver.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package host
18+
19+
import (
20+
"fmt"
21+
22+
"k8s.io/minikube/pkg/libmachine/drivers"
23+
"k8s.io/minikube/pkg/libmachine/mcnflag"
24+
"k8s.io/minikube/pkg/libmachine/state"
25+
)
26+
27+
const driverName = "none"
28+
29+
// Driver is the driver used when no driver is selected.
30+
type Driver struct {
31+
*drivers.BaseDriver
32+
}
33+
34+
func NewDriver(hostName, storePath string) *Driver {
35+
return &Driver{
36+
BaseDriver: &drivers.BaseDriver{
37+
MachineName: hostName,
38+
StorePath: storePath,
39+
},
40+
}
41+
}
42+
43+
func (d *Driver) GetCreateFlags() []mcnflag.Flag {
44+
return []mcnflag.Flag{}
45+
}
46+
47+
func (d *Driver) Create() error {
48+
return nil
49+
}
50+
51+
func (d *Driver) DriverName() string {
52+
return driverName
53+
}
54+
55+
func (d *Driver) GetIP() (string, error) {
56+
return d.IPAddress, nil
57+
}
58+
59+
func (d *Driver) GetSSHHostname() (string, error) {
60+
return "", nil
61+
}
62+
63+
func (d *Driver) GetSSHKeyPath() string {
64+
return ""
65+
}
66+
67+
func (d *Driver) GetSSHPort() (int, error) {
68+
return 0, nil
69+
}
70+
71+
func (d *Driver) GetSSHUsername() string {
72+
return ""
73+
}
74+
75+
func (d *Driver) GetURL() (string, error) {
76+
return "", nil
77+
}
78+
79+
func (d *Driver) GetState() (state.State, error) {
80+
return state.Running, nil
81+
}
82+
83+
func (d *Driver) Kill() error {
84+
return fmt.Errorf("hosts without a driver cannot be killed")
85+
}
86+
87+
func (d *Driver) Remove() error {
88+
return nil
89+
}
90+
91+
func (d *Driver) Restart() error {
92+
return fmt.Errorf("hosts without a driver cannot be restarted")
93+
}
94+
95+
func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
96+
return nil
97+
}
98+
99+
func (d *Driver) Start() error {
100+
return fmt.Errorf("hosts without a driver cannot be started")
101+
}
102+
103+
func (d *Driver) Stop() error {
104+
return fmt.Errorf("hosts without a driver cannot be stopped")
105+
}

pkg/libmachine/host/host_v0.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package host
18+
19+
import "k8s.io/minikube/pkg/libmachine/drivers"
20+
21+
type V0 struct {
22+
Name string `json:"-"`
23+
Driver drivers.Driver
24+
DriverName string
25+
ConfigVersion int
26+
HostOptions *Options
27+
28+
StorePath string
29+
CaCertPath string
30+
PrivateKeyPath string
31+
ServerCertPath string
32+
ServerKeyPath string
33+
ClientCertPath string
34+
SwarmHost string
35+
SwarmMaster bool
36+
SwarmDiscovery string
37+
ClientKeyPath string
38+
}
39+
40+
type MetadataV0 struct {
41+
HostOptions Options
42+
DriverName string
43+
44+
ConfigVersion int
45+
StorePath string
46+
CaCertPath string
47+
PrivateKeyPath string
48+
ServerCertPath string
49+
ServerKeyPath string
50+
ClientCertPath string
51+
}

pkg/libmachine/host/host_v1.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package host
18+
19+
import (
20+
"k8s.io/minikube/pkg/libmachine/drivers"
21+
"k8s.io/minikube/pkg/libmachine/engine"
22+
"k8s.io/minikube/pkg/libmachine/swarm"
23+
)
24+
25+
type AuthOptionsV1 struct {
26+
StorePath string
27+
CaCertPath string
28+
CaCertRemotePath string
29+
ServerCertPath string
30+
ServerKeyPath string
31+
ClientKeyPath string
32+
ServerCertRemotePath string
33+
ServerKeyRemotePath string
34+
PrivateKeyPath string
35+
ClientCertPath string
36+
}
37+
38+
type OptionsV1 struct {
39+
Driver string
40+
Memory int
41+
Disk int
42+
EngineOptions *engine.Options
43+
SwarmOptions *swarm.Options
44+
AuthOptions *AuthOptionsV1
45+
}
46+
47+
type V1 struct {
48+
ConfigVersion int
49+
Driver drivers.Driver
50+
DriverName string
51+
HostOptions *OptionsV1
52+
Name string `json:"-"`
53+
StorePath string
54+
}
55+

pkg/libmachine/host/migrate.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package host
18+
19+
import (
20+
"encoding/json"
21+
"fmt"
22+
"path/filepath"
23+
)
24+
25+
type RawDataDriver struct {
26+
*Driver
27+
Data []byte // passed directly back when invoking json.Marshal on this type
28+
}
29+
30+
func (r *RawDataDriver) MarshalJSON() ([]byte, error) {
31+
return r.Data, nil
32+
}
33+
34+
func (r *RawDataDriver) UnmarshalJSON(data []byte) error {
35+
r.Data = data
36+
return nil
37+
}
38+
39+
func getMigratedHostMetadata(data []byte) (*Metadata, error) {
40+
// HostMetadata is for a "first pass" so we can then load the driver
41+
var (
42+
hostMetadata *MetadataV0
43+
)
44+
45+
if err := json.Unmarshal(data, &hostMetadata); err != nil {
46+
return &Metadata{}, err
47+
}
48+
49+
migratedHostMetadata := MigrateHostMetadataV0ToHostMetadataV1(hostMetadata)
50+
51+
return migratedHostMetadata, nil
52+
}
53+
54+
func MigrateHost(h *Host, data []byte) (*Host, bool, error) {
55+
var (
56+
migrationPerformed = false
57+
)
58+
59+
migratedHostMetadata, err := getMigratedHostMetadata(data)
60+
if err != nil {
61+
return nil, false, err
62+
}
63+
64+
globalStorePath := filepath.Dir(filepath.Dir(migratedHostMetadata.HostOptions.AuthOptions.StorePath))
65+
66+
driver := &RawDataDriver{NewDriver(h.Name, globalStorePath), nil}
67+
68+
h.Driver = driver
69+
if err := json.Unmarshal(data, &h); err != nil {
70+
return nil, migrationPerformed, fmt.Errorf("Error unmarshalling most recent host version: %s", err)
71+
}
72+
73+
h.RawDriver = driver.Data
74+
75+
return h, migrationPerformed, nil
76+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package host
18+
19+
import (
20+
"k8s.io/minikube/pkg/libmachine/auth"
21+
"k8s.io/minikube/pkg/libmachine/engine"
22+
"k8s.io/minikube/pkg/libmachine/swarm"
23+
)
24+
25+
// In the 0.1.0 => 0.2.0 transition, the JSON representation of
26+
// machines changed from a "flat" to a more "nested" structure
27+
// for various options and configuration settings. To preserve
28+
// compatibility with existing machines, these migration functions
29+
// have been introduced. They preserve backwards compat at the expense
30+
// of some duplicated information.
31+
32+
// MigrateHostV0ToHostV1 validates host config and modifies if needed
33+
// this is used for configuration updates
34+
func MigrateHostV0ToHostV1(hostV0 *V0) *V1 {
35+
hostV1 := &V1{
36+
Driver: hostV0.Driver,
37+
DriverName: hostV0.DriverName,
38+
}
39+
40+
hostV1.HostOptions = &OptionsV1{}
41+
hostV1.HostOptions.EngineOptions = &engine.Options{
42+
TLSVerify: true,
43+
InstallURL: "https://get.docker.com",
44+
}
45+
hostV1.HostOptions.SwarmOptions = &swarm.Options{
46+
Address: "",
47+
Discovery: hostV0.SwarmDiscovery,
48+
Host: hostV0.SwarmHost,
49+
Master: hostV0.SwarmMaster,
50+
}
51+
hostV1.HostOptions.AuthOptions = &AuthOptionsV1{
52+
StorePath: hostV0.StorePath,
53+
CaCertPath: hostV0.CaCertPath,
54+
CaCertRemotePath: "",
55+
ServerCertPath: hostV0.ServerCertPath,
56+
ServerKeyPath: hostV0.ServerKeyPath,
57+
ClientKeyPath: hostV0.ClientKeyPath,
58+
ServerCertRemotePath: "",
59+
ServerKeyRemotePath: "",
60+
PrivateKeyPath: hostV0.PrivateKeyPath,
61+
ClientCertPath: hostV0.ClientCertPath,
62+
}
63+
64+
return hostV1
65+
}
66+
67+
// MigrateHostMetadataV0ToHostMetadataV1 fills nested host metadata and modifies if needed
68+
// this is used for configuration updates
69+
func MigrateHostMetadataV0ToHostMetadataV1(m *MetadataV0) *Metadata {
70+
hostMetadata := &Metadata{}
71+
hostMetadata.DriverName = m.DriverName
72+
hostMetadata.HostOptions.EngineOptions = &engine.Options{}
73+
hostMetadata.HostOptions.AuthOptions = &auth.Options{
74+
StorePath: m.StorePath,
75+
CaCertPath: m.CaCertPath,
76+
CaCertRemotePath: "",
77+
ServerCertPath: m.ServerCertPath,
78+
ServerKeyPath: m.ServerKeyPath,
79+
ClientKeyPath: "",
80+
ServerCertRemotePath: "",
81+
ServerKeyRemotePath: "",
82+
CaPrivateKeyPath: m.PrivateKeyPath,
83+
ClientCertPath: m.ClientCertPath,
84+
}
85+
86+
hostMetadata.ConfigVersion = m.ConfigVersion
87+
88+
return hostMetadata
89+
}

pkg/libmachine/persist/filestore.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,12 @@ func (s Filestore) loadConfig(h *host.Host) error {
133133
// struct in the migration.
134134
name := h.Name
135135

136-
migrationPerformed := false
136+
migratedHost, migrationPerformed, err := host.MigrateHost(h, data)
137+
if err != nil {
138+
return fmt.Errorf("Error getting migrated host: %s", err)
139+
}
140+
141+
*h = *migratedHost
137142

138143
h.Name = name
139144

0 commit comments

Comments
 (0)