Fix the dependency issue (#231)

This commit is contained in:
Robbie Zhang
2018-06-21 12:09:42 -07:00
committed by GitHub
parent 027b76651d
commit 6ec1098bb8
16629 changed files with 74837 additions and 4975021 deletions

View File

@@ -1,75 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 errors
import (
"fmt"
)
// InternalError is returned when there is internal issue
type InternalError struct {
Message string
}
func (e InternalError) Error() string {
return e.Message
}
type DataTypeError struct {
ExpectedType string
}
func (e DataTypeError) Error() string {
return fmt.Sprintf("Data type is not %s", e.ExpectedType)
}
type KeyNotFound struct {
Key string
Message string
}
func (e KeyNotFound) Error() string {
return fmt.Sprintf("key %s is not found: %s", e.Key, e.Message)
}
type InvalidMigrationVersion struct {
Version string
Err error
}
func (e InvalidMigrationVersion) Error() string {
return fmt.Sprintf("Data migration version is invalid %s: %s", e.Version, e.Err)
}
type DecodeError struct {
Err error
}
func (e DecodeError) Error() string {
if e.Err != nil {
return fmt.Sprintf("Failed to decode data: %s", e.Err)
}
return fmt.Sprintf("Failed to decode data")
}
type ValueFormatError struct {
Key string
Value interface{}
}
func (e ValueFormatError) Error() string {
return fmt.Sprintf("Failed to convert value of key %s: %#v", e.Key, e.Value)
}

View File

@@ -28,6 +28,10 @@ const (
// create time is stored in nanoseconds (previously seconds) in the portlayer.
ContainerCreateTimestampVersion
// VCHFolderSupportVersion represents the VCH version that first introduced
// VM folder support for the VCH.
VCHFolderSupportVersion
// Add new feature flag here
// MaxPluginVersion must be the last

View File

@@ -1,144 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 manager
import (
"context"
"fmt"
"sort"
"sync"
log "github.com/Sirupsen/logrus"
"github.com/vmware/vic/lib/migration/errors"
"github.com/vmware/vic/lib/migration/feature"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/vsphere/session"
)
const (
ApplianceConfigure = "ApplianceConfigure"
ContainerConfigure = "ContainerConfigure"
ApplianceVersionKey = "guestinfo.vice./init/version/PluginVersion"
ContainerVersionKey = "guestinfo.vice./version/PluginVersion"
)
var (
Migrator = NewDataMigrator()
)
type Plugin interface {
Migrate(ctx context.Context, s *session.Session, data interface{}) error
}
type DataMigration interface {
// Register plugin to data migration system
Register(version int, target string, plugin Plugin) error
// Migrate data with current version ID, return maximum ID number of executed plugins
Migrate(ctx context.Context, s *session.Session, target string, currentVersion int, data interface{}) (int, error)
// LatestVersion return the latest plugin version for specified target
LatestVersion(target string) int
}
type DataMigrator struct {
targetVers map[string][]int
verPlugins map[int]Plugin
once sync.Once
}
func NewDataMigrator() DataMigration {
return &DataMigrator{
targetVers: make(map[string][]int),
verPlugins: make(map[int]Plugin),
}
}
// Register plugin to data migration system
func (m *DataMigrator) Register(ver int, target string, plugin Plugin) error {
defer trace.End(trace.Begin(fmt.Sprintf("plugin %s:%d", target, ver)))
// assert if plugin version less than max plugin version, which is forcing deveoper to change MaxPluginVersion variable everytime new plugin is added
if plugin == nil {
return &errors.InternalError{
Message: "Empty Plugin object is not allowed",
}
}
if ver > feature.MaxPluginVersion {
return &errors.InternalError{
Message: fmt.Sprintf("Plugin %d is bigger than Max Plugin Version %d", ver, feature.MaxPluginVersion),
}
}
if m.verPlugins[ver] != nil {
return &errors.InternalError{
Message: fmt.Sprintf("Plugin %d is conflict with another plugin, please make sure the plugin Version is unique and ascending", ver),
}
}
m.targetVers[target] = append(m.targetVers[target], ver)
m.verPlugins[ver] = plugin
return nil
}
func (m *DataMigrator) sortVersions() {
m.once.Do(func() {
sort.Ints(m.targetVers[ApplianceConfigure])
sort.Ints(m.targetVers[ContainerConfigure])
})
}
// Migrate data with current version ID, return true if has any plugin executed
func (m *DataMigrator) Migrate(ctx context.Context, s *session.Session, target string, currentVersion int, data interface{}) (int, error) {
defer trace.End(trace.Begin(fmt.Sprintf("migrate %s from %d", target, currentVersion)))
m.sortVersions()
pluginVers := m.targetVers[target]
if len(pluginVers) == 0 {
log.Debugf("No plugins registered for %s", target)
return currentVersion, nil
}
i := sort.SearchInts(pluginVers, currentVersion)
if i >= len(pluginVers) {
log.Debugf("No plugins greater than %d", currentVersion)
return currentVersion, nil
}
latestVer := currentVersion
j := i
if pluginVers[i] == currentVersion {
j = i + 1
}
for ; j < len(pluginVers); j++ {
ver := pluginVers[j]
p := m.verPlugins[ver]
err := p.Migrate(ctx, s, data)
if err != nil {
return latestVer, err
}
latestVer = ver
}
return latestVer, nil
}
// LatestVersion return the latest plugin version for specified target
func (m *DataMigrator) LatestVersion(target string) int {
pluginVers := m.targetVers[target]
l := len(pluginVers)
if l == 0 {
log.Debugf("No plugin registered for %s", target)
return 0
}
return pluginVers[l-1]
}

View File

@@ -1,105 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 manager
import (
"context"
"fmt"
"testing"
log "github.com/Sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/vsphere/session"
)
var testMap map[int]bool
type TestPlugin struct {
Version int
}
func NewTestPlugin(version int) *TestPlugin {
return &TestPlugin{version}
}
func (p *TestPlugin) Migrate(ctx context.Context, s *session.Session, data interface{}) error {
testMap[p.Version] = true
return nil
}
func setUp() {
log.SetLevel(log.DebugLevel)
trace.Logger.Level = log.DebugLevel
testMap = make(map[int]bool)
}
func TestInsertID(t *testing.T) {
setUp()
tester := &DataMigrator{
targetVers: make(map[string][]int),
verPlugins: make(map[int]Plugin),
}
tester.targetVers[ApplianceConfigure] = []int{1, 11, 9, 5, 8, 2, 4}
tester.sortVersions()
assert.Equal(t, []int{1, 2, 4, 5, 8, 9, 11}, tester.targetVers[ApplianceConfigure], "Should have expected array")
tester.targetVers[ApplianceConfigure] = append(tester.targetVers[ApplianceConfigure], []int{20, 15}...)
// sort will only execute once
tester.sortVersions()
assert.NotEqual(t, []int{1, 2, 4, 5, 8, 9, 11, 15, 20}, tester.targetVers[ApplianceConfigure], "Should have expected array")
}
func TestMigratePluginExecution(t *testing.T) {
setUp()
tester := &DataMigrator{
targetVers: make(map[string][]int),
verPlugins: make(map[int]Plugin),
}
ids := []int{1, 2, 3, 4, 5}
var err error
for _, id := range ids {
if err = tester.Register(id, ApplianceConfigure, NewTestPlugin(id)); err != nil {
t.Errorf("Failed to register plugin %d: %s", id, err)
}
}
dataID, err := tester.Migrate(nil, nil, ApplianceConfigure, 0, nil)
assert.Equal(t, 5, dataID, "migrated id mismatch")
for _, id := range ids {
assert.True(t, testMap[id], fmt.Sprintf("plugin %d should be executed", id))
}
testMap = make(map[int]bool)
dataID, err = tester.Migrate(nil, nil, ApplianceConfigure, 3, nil)
assert.Equal(t, 5, dataID, "migrated id mismatch")
for _, id := range ids[:3] {
assert.False(t, testMap[id], fmt.Sprintf("plugin %d should not be executed", id))
}
for _, id := range ids[3:] {
assert.True(t, testMap[id], fmt.Sprintf("plugin %d should be executed", id))
}
testMap = make(map[int]bool)
dataID, err = tester.Migrate(nil, nil, ApplianceConfigure, 20, nil)
assert.Equal(t, 20, dataID, "migrated id mismatch")
for _, id := range ids {
assert.False(t, testMap[id], fmt.Sprintf("plugin %d should not be executed", id))
}
}

View File

@@ -1,116 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 migration
import (
"context"
"strconv"
"github.com/vmware/vic/lib/migration/errors"
"github.com/vmware/vic/lib/migration/feature"
"github.com/vmware/vic/lib/migration/manager"
// imported for the side effect
_ "github.com/vmware/vic/lib/migration/plugins"
"github.com/vmware/vic/pkg/vsphere/session"
)
// MigrateApplianceConfigure migrate VCH appliance configuration, including guestinfo, keyvaluestore, or any other configuration related change.
//
// Note: Input map conf is VCH appliance guestinfo map, and returned map is the new guestinfo.
// Returns false without error means no need to migrate, and returned map is the copy of input map
// If there is error returned, returns true and half-migrated value
func MigrateApplianceConfig(ctx context.Context, s *session.Session, conf map[string]string) (map[string]string, bool, error) {
return migrateConfig(ctx, s, conf, manager.ApplianceConfigure, manager.ApplianceVersionKey)
}
// MigrateContainerConfigure migrate container configuration
//
// Note: Migrated data will be returned in map, and input object is not changed.
// Returns false without error means no need to migrate, and returned map is the copy of input map
// If there is error returned, returns true and half-migrated value
func MigrateContainerConfig(conf map[string]string) (map[string]string, bool, error) {
return migrateConfig(nil, nil, conf, manager.ContainerConfigure, manager.ContainerVersionKey)
}
// ContainerDataIsOlder returns true if input container config is older than latest version. If error happens, always returns false
func ContainerDataIsOlder(conf map[string]string) (bool, error) {
return dataIsOlder(conf, manager.ContainerConfigure, manager.ContainerVersionKey)
}
// ApplianceDataIsOlder returns true if input appliance config is older than latest version. If error happens, always returns false
func ApplianceDataIsOlder(conf map[string]string) (bool, error) {
return dataIsOlder(conf, manager.ApplianceConfigure, manager.ApplianceVersionKey)
}
// ContainerDataVersion returns container data version
func ContainerDataVersion(conf map[string]string) (int, error) {
return getCurrentID(conf, manager.ContainerVersionKey)
}
// dataIsOlder returns true if data is older than latest. If error happens, always returns false
func dataIsOlder(data map[string]string, target string, verKey string) (bool, error) {
var currentID int
var err error
if currentID, err = getCurrentID(data, verKey); err != nil {
return false, err
}
latestVer := manager.Migrator.LatestVersion(target)
return latestVer > currentID, nil
}
func migrateConfig(ctx context.Context, s *session.Session, data map[string]string, target string, verKey string) (map[string]string, bool, error) {
dst := make(map[string]string)
for k, v := range data {
dst[k] = v
}
if len(data) == 0 {
return dst, false, nil
}
var currentID int
var err error
if currentID, err = getCurrentID(data, verKey); err != nil {
return dst, false, err
}
latestVer := manager.Migrator.LatestVersion(target)
if latestVer <= currentID {
return dst, false, nil
}
_, err = manager.Migrator.Migrate(ctx, s, target, currentID, dst)
dst[verKey] = strconv.Itoa(feature.MaxPluginVersion - 1)
return dst, true, err
}
func getCurrentID(data map[string]string, verKey string) (int, error) {
var currentID int
var err error
strID := data[verKey]
if strID == "" {
return 0, nil
}
if currentID, err = strconv.Atoi(strID); err != nil {
return 0, &errors.InvalidMigrationVersion{
Version: strID,
Err: err,
}
}
return currentID, nil
}

View File

@@ -1,153 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 migration
import (
"os"
"strconv"
"strings"
"testing"
log "github.com/Sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/vmware/vic/lib/config"
"github.com/vmware/vic/lib/config/executor"
"github.com/vmware/vic/lib/migration/feature"
"github.com/vmware/vic/lib/migration/manager"
"github.com/vmware/vic/lib/migration/samples/plugins/plugin1"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/vsphere/extraconfig"
)
var (
MaxPluginVersion = feature.MaxPluginVersion
)
func setUp() {
// register sample plugin into test
log.SetLevel(log.DebugLevel)
trace.Logger.Level = log.DebugLevel
if err := manager.Migrator.Register(MaxPluginVersion, manager.ApplianceConfigure, &plugin1.ApplianceStopSignalRename{}); err != nil {
log.Errorf("Failed to register plugin %s:%d, %s", manager.ApplianceConfigure, MaxPluginVersion, err)
panic(err)
}
}
func TestMigrateConfigure(t *testing.T) {
conf := &config.VirtualContainerHostConfigSpec{
ExecutorConfig: executor.ExecutorConfig{
Sessions: map[string]*executor.SessionConfig{
"abc": {
Attach: true,
StopSignal: "2",
},
"def": {
Attach: false,
StopSignal: "10",
},
},
},
Network: config.Network{
BridgeNetwork: "VM Network",
},
}
mapData := make(map[string]string)
extraconfig.Encode(extraconfig.MapSink(mapData), conf)
t.Logf("Old data: %#v", mapData)
newData, migrated, err := MigrateApplianceConfig(nil, nil, mapData)
if err != nil {
t.Errorf("migration failed: %s", err)
assert.Fail(t, "migration failed")
}
assert.True(t, migrated, "should be migrated")
latestVer := newData[manager.ApplianceVersionKey]
assert.Equal(t, strconv.Itoa(feature.MaxPluginVersion-1), latestVer, "upgrade version mismatch")
// check new data
var found bool
for k := range newData {
if strings.Contains(k, "stopSignal") {
assert.Fail(t, "key %s still exists in migrated data", k)
}
if strings.Contains(k, "forceStopSignal") {
found = true
}
}
assert.True(t, found, "Should found migrated data")
// verify old data
found = false
for k := range mapData {
if strings.Contains(k, "stopSignal") {
found = true
}
if strings.Contains(k, "forceStopSignal") {
assert.Fail(t, "key %s is found in old data", k)
}
}
assert.True(t, found, "Should found old data")
t.Logf("New data: %#v", newData)
newConf := &config.VirtualContainerHostConfigSpec{}
extraconfig.Decode(extraconfig.MapSource(newData), newConf)
assert.Equal(t, feature.MaxPluginVersion-1, newConf.Version.PluginVersion, "should not be migrated")
t.Logf("other version fields: %s", newConf.Version.String())
}
func TestIsDataOlder(t *testing.T) {
conf := &config.VirtualContainerHostConfigSpec{
ExecutorConfig: executor.ExecutorConfig{
Sessions: map[string]*executor.SessionConfig{
"abc": {
Attach: true,
StopSignal: "2",
},
"def": {
Attach: false,
StopSignal: "10",
},
},
},
Network: config.Network{
BridgeNetwork: "VM Network",
},
}
mapData := make(map[string]string)
extraconfig.Encode(extraconfig.MapSink(mapData), conf)
t.Logf("Old appliance data: %#v", mapData)
older, err := ApplianceDataIsOlder(mapData)
assert.Equal(t, nil, err, "should not have error")
assert.True(t, older, "Test data should be older than latest")
mapData = make(map[string]string)
extraconfig.Encode(extraconfig.MapSink(mapData), conf.ExecutorConfig)
t.Logf("Old container data: %#v", mapData)
older, err = ContainerDataIsOlder(mapData)
assert.Equal(t, nil, err, "should not have error")
assert.True(t, older, "Test data should be older than latest since a container update plugin has been registered")
}
func TestMain(m *testing.M) {
setUp()
code := m.Run()
os.Exit(code)
}

View File

@@ -1,26 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 plugins
// import all plugin packages here to register plugins
import (
// imported for the side effect
_ "github.com/vmware/vic/lib/migration/plugins/plugin1"
_ "github.com/vmware/vic/lib/migration/plugins/plugin2"
_ "github.com/vmware/vic/lib/migration/plugins/plugin5"
_ "github.com/vmware/vic/lib/migration/plugins/plugin7"
_ "github.com/vmware/vic/lib/migration/plugins/plugin8"
_ "github.com/vmware/vic/lib/migration/plugins/plugin9"
)

View File

@@ -1,128 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 plugin1
import (
"context"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/vmware/vic/lib/migration/errors"
"github.com/vmware/vic/lib/migration/feature"
"github.com/vmware/vic/lib/migration/manager"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/vsphere/extraconfig"
"github.com/vmware/vic/pkg/vsphere/session"
)
const (
target = manager.ApplianceConfigure
)
func init() {
defer trace.End(trace.Begin(fmt.Sprintf("Registering plugin %s:%d", target, feature.AddCommonSpecForVCHVersion)))
if err := manager.Migrator.Register(feature.AddCommonSpecForVCHVersion, target, &AddCommonSpecForVCH{}); err != nil {
log.Errorf("Failed to register plugin %s:%d, %s", target, feature.AddCommonSpecForVCHVersion, err)
panic(err)
}
}
// AddCommonSpecForVCH is plugin for vic 0.8.0-GA version upgrade
type AddCommonSpecForVCH struct {
}
type VirtualContainerHostConfigSpec struct {
ExecutorConfig `vic:"0.1" scope:"read-only" key:"init"`
}
type ExecutorConfig struct {
Common `vic:"0.1" scope:"read-only" key:"common"`
}
type Common struct {
// A reference to the components hosting execution environment, if any
ExecutionEnvironment string
// Unambiguous ID with meaning in the context of its hosting execution environment
ID string `vic:"0.1" scope:"read-only" key:"id"`
// Convenience field to record a human readable name
Name string `vic:"0.1" scope:"read-only" key:"name"`
// Freeform notes related to the entity
Notes string `vic:"0.1" scope:"hidden" key:"notes"`
}
type UpdatedVCHConfigSpec struct {
UpdatedExecutorConfig `vic:"0.1" scope:"read-only" key:"init"`
}
type UpdatedExecutorConfig struct {
UpdatedCommon `vic:"0.1" scope:"read-only" key:"common"`
}
type UpdatedCommon struct {
// A reference to the components hosting execution environment, if any
ExecutionEnvironment string
// Unambiguous ID with meaning in the context of its hosting execution environment
ID string `vic:"0.1" scope:"read-only" key:"id"`
// Convenience field to record a human readable name
Name string `vic:"0.1" scope:"hidden" key:"name"`
// Freeform notes related to the entity
Notes string `vic:"0.1" scope:"hidden" key:"notes"`
}
func (p *AddCommonSpecForVCH) Migrate(ctx context.Context, s *session.Session, data interface{}) error {
defer trace.End(trace.Begin(fmt.Sprintf("AddCommonSpecForVCH version: %d", feature.AddCommonSpecForVCHVersion)))
if data == nil {
return nil
}
mapData, ok := data.(map[string]string)
if !ok {
// Log the error here and return nil so that other plugins can proceed
log.Errorf("Migration data format is not map: %+v", data)
return nil
}
oldStruct := &VirtualContainerHostConfigSpec{}
result := extraconfig.Decode(extraconfig.MapSource(mapData), oldStruct)
log.Debugf("The oldStruct is %+v", oldStruct)
if result == nil {
return &errors.DecodeError{Err: fmt.Errorf("decode oldStruct %+v failed", oldStruct)}
}
newStruct := &UpdatedVCHConfigSpec{
UpdatedExecutorConfig: UpdatedExecutorConfig{
UpdatedCommon: UpdatedCommon{
Name: oldStruct.Name,
ID: oldStruct.ID,
ExecutionEnvironment: oldStruct.ExecutionEnvironment,
Notes: oldStruct.Notes,
},
},
}
cfg := make(map[string]string)
extraconfig.Encode(extraconfig.MapSink(cfg), newStruct)
for k, v := range cfg {
log.Debugf("New data: %s:%s", k, v)
mapData[k] = v
}
return nil
}

View File

@@ -1,117 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 plugin2
import (
"context"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/vmware/vic/lib/migration/errors"
"github.com/vmware/vic/lib/migration/feature"
"github.com/vmware/vic/lib/migration/manager"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/vsphere/extraconfig"
"github.com/vmware/vic/pkg/vsphere/session"
)
const (
target = manager.ContainerConfigure
)
func init() {
defer trace.End(trace.Begin(fmt.Sprintf("Registering plugin %s:%d", target, feature.AddCommonSpecForContainerVersion)))
if err := manager.Migrator.Register(feature.AddCommonSpecForContainerVersion, target, &AddCommonSpecForContainer{}); err != nil {
log.Errorf("Failed to register plugin %s:%d, %s", target, feature.AddCommonSpecForContainerVersion, err)
panic(err)
}
}
// AddCommonSpecForContainer is plugin for vic 0.8.0-GA version upgrade
type AddCommonSpecForContainer struct {
}
type ExecutorConfig struct {
Common `vic:"0.1" scope:"read-only" key:"common"`
}
type Common struct {
// A reference to the components hosting execution environment, if any
ExecutionEnvironment string
// Unambiguous ID with meaning in the context of its hosting execution environment
ID string `vic:"0.1" scope:"read-only" key:"id"`
// Convenience field to record a human readable name
Name string `vic:"0.1" scope:"read-only" key:"name"`
// Freeform notes related to the entity
Notes string `vic:"0.1" scope:"hidden" key:"notes"`
}
type UpdatedExecutorConfig struct {
UpdatedCommon `vic:"0.1" scope:"read-only" key:"common"`
}
type UpdatedCommon struct {
// A reference to the components hosting execution environment, if any
ExecutionEnvironment string
// Unambiguous ID with meaning in the context of its hosting execution environment
ID string `vic:"0.1" scope:"read-only" key:"id"`
// Convenience field to record a human readable name
Name string `vic:"0.1" scope:"hidden" key:"name"`
// Freeform notes related to the entity
Notes string `vic:"0.1" scope:"hidden" key:"notes"`
}
func (p *AddCommonSpecForContainer) Migrate(ctx context.Context, s *session.Session, data interface{}) error {
defer trace.End(trace.Begin(fmt.Sprintf("AddCommonSpecForContainer version %d", feature.AddCommonSpecForContainerVersion)))
if data == nil {
return nil
}
mapData, ok := data.(map[string]string)
if !ok {
// Log the error here and return nil so that other plugins can proceed
log.Errorf("Migration data format is not map: %+v", data)
return nil
}
oldStruct := &ExecutorConfig{}
result := extraconfig.Decode(extraconfig.MapSource(mapData), oldStruct)
log.Debugf("The oldStruct is %+v", oldStruct)
if result == nil {
return &errors.DecodeError{Err: fmt.Errorf("decode oldStruct %+v failed", oldStruct)}
}
newStruct := &UpdatedExecutorConfig{
UpdatedCommon: UpdatedCommon{
Name: oldStruct.Name,
ID: oldStruct.ID,
Notes: oldStruct.Notes,
ExecutionEnvironment: oldStruct.ExecutionEnvironment,
}}
cfg := make(map[string]string)
extraconfig.Encode(extraconfig.MapSink(cfg), newStruct)
for k, v := range cfg {
log.Debugf("New data: %s:%s", k, v)
mapData[k] = v
}
return nil
}

View File

@@ -1,120 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 plugin5 Plugin to migrate urls from go1.7 to go1.8
// Issue# https://github.com/vmware/vic/issues/4388
package plugin5
import (
"context"
"fmt"
"net/url"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/vmware/vic/lib/migration/errors"
"github.com/vmware/vic/lib/migration/feature"
"github.com/vmware/vic/lib/migration/manager"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/vsphere/extraconfig"
"github.com/vmware/vic/pkg/vsphere/session"
)
const (
target = manager.ApplianceConfigure
)
// VirtualContainerHostConfigSpec holds the metadata for a
// Virtual Container Host that should be visible inside the appliance VM.
type VirtualContainerHostConfigSpec struct {
// Registry configuration for Imagec
Registry `vic:"0.1" scope:"read-only" key:"registry"`
}
// Registry defines the registries virtual container host can talk to
type Registry struct {
// Insecure registries
InsecureRegistries []url.URL `vic:"0.1" scope:"read-only" key:"insecure_registries"`
}
func init() {
defer trace.End(trace.Begin(fmt.Sprintf("Registering plugin %s:%d", target, feature.MigrateRegistryVersion)))
if err := manager.Migrator.Register(feature.MigrateRegistryVersion, target, &MigrateRegistry{}); err != nil {
log.Errorf("Failed to register plugin %s:%d, %s", target, feature.MigrateRegistryVersion, err)
panic(err)
}
}
// MigrateRegistry is plugin for vic 0.9.0-GA version upgrade
type MigrateRegistry struct {
}
func (p *MigrateRegistry) Migrate(ctx context.Context, s *session.Session, data interface{}) error {
defer trace.End(trace.Begin(fmt.Sprintf("MigrateRegistry version %d", feature.MigrateRegistryVersion)))
if data == nil {
log.Debugf("No data received plugin %s:%d", target, feature.MigrateRegistryVersion)
return nil
}
mapData, ok := data.(map[string]string)
if !ok {
// Log the error here and return nil so that other plugins can proceed
log.Errorf("Migration data format is not map: %+v", data)
return nil
}
oldVCHSpec := &VirtualContainerHostConfigSpec{}
result := extraconfig.Decode(extraconfig.MapSource(mapData), oldVCHSpec)
if result == nil {
log.Errorf("Error decoding vchspec: %+v", oldVCHSpec)
return &errors.DecodeError{}
}
log.Debugf("The oldVCHSpec is %+v", oldVCHSpec)
newVCHSpec := &VirtualContainerHostConfigSpec{}
for _, registry := range oldVCHSpec.InsecureRegistries {
log.Debugf("Checking insecure registry url: %v", registry.String())
if registry.Host == "" {
log.Debugf("Fixing insecure registry url: %v", registry.String())
// split host:port/path
sp := strings.SplitN(registry.Path, "/", 2)
// Fix host from the first index of split
registry.Host = sp[0]
// if a path was present, in the second index of split
if len(sp) > 1 {
registry.Path = sp[1]
} else {
// else set path as empty
registry.Path = ""
}
}
newVCHSpec.InsecureRegistries = append(newVCHSpec.InsecureRegistries, registry)
}
cfg := make(map[string]string)
extraconfig.Encode(extraconfig.MapSink(cfg), newVCHSpec)
for k, v := range cfg {
log.Debugf("New data: %s:%s", k, v)
mapData[k] = v
}
return nil
}

View File

@@ -1,65 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 plugin5
import (
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/vmware/vic/pkg/vsphere/extraconfig"
)
func TestMigrateRegistry(t *testing.T) {
inConfig := []url.URL{
{Path: "1.1.1.1:5000"},
{Path: "2.2.2.2"},
{Host: "3.3.3.3:6000"},
{Host: "3.3.3.3"},
{Host: "vic.vmware.com"},
{Path: "[1234:1234:0:1234::11]:7000"},
{Host: "[1234:1234:0:1234::11]:7000"},
{Path: "3.3.3.3/foo/bar"},
}
outConfig := []url.URL{
{Host: "1.1.1.1:5000"},
{Host: "2.2.2.2"},
{Host: "3.3.3.3:6000"},
{Host: "3.3.3.3"},
{Host: "vic.vmware.com"},
{Host: "[1234:1234:0:1234::11]:7000"},
{Host: "[1234:1234:0:1234::11]:7000"},
{Host: "3.3.3.3", Path: "foo/bar"},
}
m := MigrateRegistry{}
vch := VirtualContainerHostConfigSpec{
Registry{
InsecureRegistries: inConfig,
}}
mapData := make(map[string]string)
extraconfig.Encode(extraconfig.MapSink(mapData), vch)
err := m.Migrate(nil, nil, mapData)
assert.NoError(t, err)
vchMigrated := extraconfig.Decode(extraconfig.MapSource(mapData), vch)
assert.Equal(t, outConfig, vchMigrated.(VirtualContainerHostConfigSpec).InsecureRegistries)
}

View File

@@ -1,111 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 plugin7
import (
"context"
"fmt"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/vmware/vic/lib/migration/errors"
"github.com/vmware/vic/lib/migration/feature"
"github.com/vmware/vic/lib/migration/manager"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/vsphere/extraconfig"
"github.com/vmware/vic/pkg/vsphere/session"
)
// Translates the proxy environment variables from the old appliance to the new appliance for vic admin
const (
target = manager.ApplianceConfigure
oldHProxy = "HTTP_PROXY"
newHProxy = "VICADMIN_HTTP_PROXY"
oldSProxy = "HTTPS_PROXY"
newSProxy = "VICADMIN_HTTPS_PROXY"
sessionName = "vicadmin"
)
func init() {
defer trace.End(trace.Begin(fmt.Sprintf("Registering plugin %s:%d", target, feature.VicadminProxyVarRenameVersion)))
if err := manager.Migrator.Register(feature.VicadminProxyVarRenameVersion, target, &VicadminProxyVarRename{}); err != nil {
log.Errorf("Failed to register plugin %s:%d, %s", target, feature.VicadminProxyVarRenameVersion, err)
}
}
// VicadminProxyVarRename is plugin for vic 0.8.0-GA version upgrade
type VicadminProxyVarRename struct {
}
type VCHConfig struct {
ExecutorConfig `vic:"0.1" scope:"read-only" key:"init"`
}
type ExecutorConfig struct {
Sessions map[string]*SessionConfig `vic:"0.1" scope:"read-only" key:"sessions"`
}
type SessionConfig struct {
Cmd Cmd `vic:"0.1" scope:"read-only" key:"cmd"`
}
type Cmd struct {
Env []string `vic:"0.1" scope:"read-only" key:"Env"`
}
func (p *VicadminProxyVarRename) Migrate(ctx context.Context, s *session.Session, data interface{}) error {
defer trace.End(trace.Begin(fmt.Sprintf("%d", feature.VicadminProxyVarRenameVersion)))
if data == nil {
return nil
}
mapData := data.(map[string]string)
oldStruct := &VCHConfig{}
result := extraconfig.Decode(extraconfig.MapSource(mapData), oldStruct)
if result == nil {
return &errors.DecodeError{}
}
// translate old proxy env var keys into to proxy env var keys
// skip upgrading if the proxy isn't defined or something is wrong with the the vicadmin executor
if oldStruct.Sessions == nil || oldStruct.Sessions[sessionName] == nil || oldStruct.Sessions[sessionName].Cmd.Env == nil {
log.Debugln("vicadmin session not found. skipping proxy rename")
} else {
var newEnvs []string
for _, envVar := range oldStruct.Sessions[sessionName].Cmd.Env {
envVarArgs := strings.Split(envVar, "=")
envVarValue := ""
if len(envVarArgs) > 1 {
envVarValue = envVarArgs[1]
}
if strings.Contains(envVar, oldHProxy) {
newEnvs = append(newEnvs, fmt.Sprintf("%s=%s", newHProxy, envVarValue))
} else if strings.Contains(envVar, oldSProxy) {
newEnvs = append(newEnvs, fmt.Sprintf("%s=%s", newSProxy, envVarValue))
} else {
newEnvs = append(newEnvs, envVar)
}
}
oldStruct.Sessions[sessionName].Cmd.Env = newEnvs
}
cfg := make(map[string]string)
extraconfig.Encode(extraconfig.MapSink(cfg), oldStruct)
for k, v := range cfg {
log.Debugf("New data: %s:%s", k, v)
mapData[k] = v
}
return nil
}

View File

@@ -1,90 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 plugin8
import (
"context"
"fmt"
"net/url"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/vmware/vic/lib/migration/errors"
"github.com/vmware/vic/lib/migration/feature"
"github.com/vmware/vic/lib/migration/manager"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/vsphere/extraconfig"
"github.com/vmware/vic/pkg/vsphere/session"
)
// Translates the proxy environment variables from the old appliance to the new appliance for vic admin
const (
target = manager.ApplianceConfigure
)
func init() {
defer trace.End(trace.Begin(fmt.Sprintf("Registering plugin %s:%d", target, feature.InsecureRegistriesTypeChangeVersion)))
if err := manager.Migrator.Register(feature.InsecureRegistriesTypeChangeVersion, target, &InsecureRegistriesTypeChange{}); err != nil {
log.Errorf("Failed to register plugin %s:%d, %s", target, feature.InsecureRegistriesTypeChangeVersion, err)
}
}
// InsecureRegistriesTypeChange
type InsecureRegistriesTypeChange struct {
}
type OldVCHConfig struct {
OldRegistry `vic:"0.1" scope:"read-only" key:"registry"`
}
type OldRegistry struct {
InsecureRegistries []url.URL `vic:"0.1" scope:"read-only" key:"insecure_registries"`
}
type VCHConfig struct {
Registry `vic:"0.1" scope:"read-only" key:"registry"`
}
type Registry struct {
InsecureRegistries []string `vic:"0.1" scope:"read-only" key:"insecure_registries"`
}
func (p *InsecureRegistriesTypeChange) Migrate(ctx context.Context, s *session.Session, data interface{}) error {
defer trace.End(trace.Begin(fmt.Sprintf("%d", feature.InsecureRegistriesTypeChangeVersion)))
if data == nil {
return nil
}
mapData := data.(map[string]string)
oldStruct := &OldVCHConfig{}
result := extraconfig.Decode(extraconfig.MapSource(mapData), oldStruct)
if result == nil {
return &errors.DecodeError{}
}
vchConfig := &VCHConfig{}
for _, r := range oldStruct.InsecureRegistries {
vchConfig.InsecureRegistries = append(vchConfig.InsecureRegistries, strings.TrimPrefix(r.String(), r.Scheme+"://"))
}
cfg := make(map[string]string)
extraconfig.Encode(extraconfig.MapSink(cfg), vchConfig)
for k, v := range cfg {
log.Debugf("New data: %s:%s", k, v)
mapData[k] = v
}
return nil
}

View File

@@ -1,81 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 plugin9
import (
"context"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/vmware/vic/lib/migration/errors"
"github.com/vmware/vic/lib/migration/feature"
"github.com/vmware/vic/lib/migration/manager"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/vsphere/extraconfig"
"github.com/vmware/vic/pkg/vsphere/session"
)
const target = manager.ContainerConfigure
func init() {
defer trace.End(trace.Begin(fmt.Sprintf("Registering plugin %s:%d", target, feature.ContainerCreateTimestampVersion)))
if err := manager.Migrator.Register(feature.ContainerCreateTimestampVersion, target, &ContainerCreateTimestampVersion{}); err != nil {
log.Errorf("Failed to register plugin %s:%d, %s", target, feature.ContainerCreateTimestampVersion, err)
panic(err)
}
}
// ContainerCreateTimestampVersion is a plugin to convert stored container create timestamps
// in seconds to nanoseconds in the container configuration.
type ContainerCreateTimestampVersion struct {
}
// ExecutorConfig is used to update the container create time from seconds to nanoseconds.
type ExecutorConfig struct {
CreateTime int64 `vic:"0.1" scope:"read-write" key:"createtime"`
}
// Migrate converts the stored container create timestamp (in seconds) to nanoseconds.
func (c *ContainerCreateTimestampVersion) Migrate(ctx context.Context, s *session.Session, data interface{}) error {
defer trace.End(trace.Begin(fmt.Sprintf("ContainerCreateTimestampVersion version %d", feature.ContainerCreateTimestampVersion)))
if data == nil {
return nil
}
mapData, ok := data.(map[string]string)
if !ok {
// Log the error here and return nil so that other plugins can proceed.
log.Errorf("Migration data format is not map: %+v", data)
return nil
}
oldStruct := &ExecutorConfig{}
result := extraconfig.Decode(extraconfig.MapSource(mapData), oldStruct)
log.Debugf("The oldStruct is %+v", oldStruct)
if result == nil {
return &errors.DecodeError{Err: fmt.Errorf("decode oldStruct %+v failed", oldStruct)}
}
// Convert create timestamp to nanoseconds for older containers that use seconds.
oldStruct.CreateTime *= 1e9
cfg := make(map[string]string)
extraconfig.Encode(extraconfig.MapSink(cfg), oldStruct)
for k, v := range cfg {
log.Debugf("New data: %s:%s", k, v)
mapData[k] = v
}
return nil
}

View File

@@ -1,49 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 plugin9
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/vmware/vic/lib/config/executor"
"github.com/vmware/vic/pkg/vsphere/extraconfig"
)
// TestMigrateContainerCreateTimestamp tests this package's ContainerCreateTimestampVersion.Migrate.
func TestMigrateContainerCreateTimestamp(t *testing.T) {
secSinceEpoch := int64(1510950922)
oldCreateTime := secSinceEpoch
newCreateTime := secSinceEpoch * 1e9
layerID := "abcdefg"
execConfig := executor.ExecutorConfig{
CreateTime: oldCreateTime,
// Supply an extra field that's not accessed in the plugin9 migrator to
// ensure that unneeded fields aren't dropped from the returned data.
LayerID: layerID,
}
c := ContainerCreateTimestampVersion{}
mapData := make(map[string]string)
extraconfig.Encode(extraconfig.MapSink(mapData), execConfig)
err := c.Migrate(nil, nil, mapData)
assert.NoError(t, err)
newConf := extraconfig.Decode(extraconfig.MapSource(mapData), execConfig)
assert.Equal(t, newCreateTime, newConf.(executor.ExecutorConfig).CreateTime)
assert.Equal(t, layerID, newConf.(executor.ExecutorConfig).LayerID)
}

View File

@@ -1,45 +0,0 @@
# Developer guideline to write a data migration plugin
## Who and When to develop a data migration plugin
As [upgrade design](../../../doc/design/vic-machine/upgrade.md) mentioned, every developer who changed configuration from VCH appliance configuration, container vm configuration, or key/value store, whatever it is, they should add data migration plugin here.
## How to develop a data migration plugin
### VM guestinfo changes including VCH appliance and container VM
- Check if there is a file conflict with someone else's commit. If there is a conflict, that means there's another plugin being developed at the time - ensure that you do not use same plugin version number.
- Create a new package in the _lib/migration/plugins_ directory for your new plugin, say _pluginX_.
- Add a new constant for the plugin version in _lib/migration/feature/feature.go_.
- Add plugin files to the new package.
- Your plugin might rely on previous version and current version, if you need the whole VCH appliance or container configuration definition, copy them to package lib/migration/config, with correct new package created. For example, if you are working on migration from version 4 to version 5, create a package named v4 if there is no one existing, and copy all old configuration files to there, and create a new package v5, copy all new configuration files to v5 package. Remember to update package name.
- If only a few attributes are touched, you could define that piece of attribute definition in your plugin package, without whole configuration files copied in the above step. Which can save binary size, and also configuration encodeing/decoding time.
- Develop plugin based on your specific change, read the input data, decoding to acceptable format, and change value inside of them. stop_singal_rename_sample.go is one VCH appliance configuration change sample, you can follow that to write your own plugin.
- Register your plugin to data migration framework, and put the function in package _pluginX_'s init() method.
* Register appliance data migration plugin to manager.ApplianceConfigure category
* Register container data migration plugin to manager.ContainerConfigure category
* The two plugin targets are for different data, ensure that the type is selected correctly in registration.
- *Important:* add import of your _pluginX_ package in init.go of the _plugin_ package to ensure that _pluginX_ is registered dynamically.
### key/value store changes
- Check if there is a file conflict with someone else's commit. If there is a conflict, that means there's another plugin being developed at the time - ensure that you do not use same plugin version number.
- Create a new package in the _lib/migration/plugins_ directory for your new plugin, say _pluginX_.
- Add a new constant for the plugin version in _lib/migration/feature/feature.go_.
- Add your plugin files to the new package.
- Develop plugin based on your specific change.
* Create new key/value store file in datastore, instead of change in existing file. The new file for new version, e.g. v4, could use file name as XXX.v4, to differentiate with old version's file.
* Copy all existing key/values to the new file, and update to the new file directly.
* Write to new datastore file only, because old file is still correct if migration failed eventually.
- Register your plugin to data migration framework, and put the function in package _pluginX_'s init() method.
* Register key/value store data migration plugin to manager.ApplianceConfigure category.
- *Important:* add import of your _pluginX_ package in init.go of the _plugin_ package to ensure that _pluginX_ is registered dynamically.
Note:
- Plugin version should be greater than 0
- If you changed both key/value store and VCH appliance configuration, please add two separate plugins for them. Eventually, data migration framework will execute both of them, but separation will make the code easy to read.
- While copy configuration files, remove unnecessary methods from that file, to reduce binary file size
## Add integration test
Be sure to add test scenario into upgrade test group, to cover your changes, to make sure after upgrade, your function works well

View File

@@ -1,218 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 executor
import (
"net/url"
"time"
"github.com/vmware/vic/pkg/version"
)
type State int
const (
STARTED State = iota
EXITED
KILLED
)
// Common data between managed entities, across execution environments
type Common struct {
// A reference to the components hosting execution environment, if any
ExecutionEnvironment string
// Unambiguous ID with meaning in the context of its hosting execution environment
ID string `vic:"0.1" scope:"read-only" key:"id"`
// Convenience field to record a human readable name
Name string `vic:"0.1" scope:"read-only" key:"name"`
// Freeform notes related to the entity
Notes string `vic:"0.1" scope:"hidden" key:"notes"`
}
// Diagnostics records some basic control and lifecycle information for diagnostic purposes
type Diagnostics struct {
// Should debugging be enabled on whatever component this is and at what level
DebugLevel int `vic:"0.1" scope:"read-only" key:"debug"`
// RessurectionCount is a log of how many times the entity has been restarted due
// to error exit
ResurrectionCount int `vic:"0.1" scope:"read-write" key:"resurrections"`
// ExitLogs is a best effort record of the time of process death and the cause for
// restartable entities
ExitLogs []ExitLog `vic:"0.1" scope:"read-write" key:"exitlogs"`
}
// ExitLog records some basic diagnostics about anomalous exit for restartable entities
type ExitLog struct {
Time time.Time
ExitStatus int
Message string
}
// MountSpec details a mount that must be executed within the executor
// A mount is a URI -> path mapping with a credential of some kind
// In the case of a labeled disk:
// label://<label name> => </mnt/path>
type MountSpec struct {
// A URI->path mapping, e.g.
// May contain credentials
Source url.URL `vic:"0.1" scope:"read-only" key:"source"`
// The path in the executor at which this should be mounted
Path string `vic:"0.1" scope:"read-only" key:"dest"`
// Freeform mode string, which could translate directly to mount options
// We may want to turn this into a more structured form eventually
Mode string `vic:"0.1" scope:"read-only" key:"mode"`
}
// ContainerVM holds that data tightly associated with a containerVM, but that should not
// be visible to the guest. This is the external complement to ExecutorConfig.
type ContainerVM struct {
Common
// The version of the bootstrap image that this container was booted from.
Version string
// Name aliases for this specific container, Maps alias to unambiguous name
// This uses unambiguous name rather than reified network endpoint to persist
// the intent rather than a point-in-time manifesting of that intent.
Aliases map[string]string
// The location of the interaction service that the tether should connect to. Examples:
// * tcp://x.x.x.x:2377
// * vmci://moid - should this be an moid or a VMCI CID? Does one insulate us from reboots?
Interaction url.URL
// Key is the host key used during communicate back with the Interaction endpoint if any
// Used if the vSocket agent is responsible for authenticating the connection
AgentKey []byte
}
// ExecutorConfig holds the data tightly associated with an Executor. This is distinct from Sessions
// in that there is no process inherently associated - this is closer to a ThreadPool than a Thread and
// is the owner of the shared filesystem environment. This is the guest visible complement to ContainerVM.
type ExecutorConfig struct {
Common `vic:"0.1" scope:"read-only" key:"common"`
// CreateTime stamp
CreateTime int64 `vic:"0.1" scope:"read-write" key:"createtime"`
// Diagnostics holds basic diagnostics data
Diagnostics Diagnostics `vic:"0.1" scope:"read-only" key:"diagnostics"`
// Sessions is the set of sessions currently hosted by this executor
// These are keyed by session ID
Sessions map[string]*SessionConfig `vic:"0.1" scope:"read-only" key:"sessions"`
// Maps the mount name to the detail mount specification
Mounts map[string]MountSpec `vic:"0.1" scope:"read-only" key:"mounts"`
// This describes an executors presence on a network, and contains sufficient
// information to configure the interface in the guest.
Networks map[string]*NetworkEndpoint `vic:"0.1" scope:"read-only" key:"networks"`
// Key is the host key used during communicate back with the Interaction endpoint if any
// Used if the in-guest tether is responsible for authenticating the connection
Key []byte `vic:"0.1" scope:"read-only" key:"key"`
// Layer id that is backing this container VM
LayerID string `vic:"0.1" scope:"read-only" key:"layerid"`
// Blob metadata for the caller
Annotations map[string]string `vic:"0.1" scope:"hidden" key:"annotations"`
// Repository requested by user
// TODO: a bit docker specific
RepoName string `vic:"0.1" scope:"read-only" key:"repo"`
// version
Version *version.Build `vic:"0.1" scope:"read-only" key:"version"`
}
// Cmd is here because the encoding packages seem to have issues with the full exec.Cmd struct
type Cmd struct {
// Path is the command to run
Path string `vic:"0.1" scope:"read-only" key:"Path"`
// Args is the command line arguments including the command in Args[0]
Args []string `vic:"0.1" scope:"read-only" key:"Args"`
// Env specifies the environment of the process
Env []string `vic:"0.1" scope:"read-only" key:"Env"`
// Dir specifies the working directory of the command
Dir string `vic:"0.1" scope:"read-only" key:"Dir"`
}
// SessionConfig defines the content of a session - this maps to the root of a process tree
// inside an executor
// This is close to but not perfectly aligned with the new docker/docker/daemon/execdriver/driver:CommonProcessConfig
type SessionConfig struct {
// The primary session may have the same ID as the executor owning it
Common `vic:"0.1" scope:"read-only" key:"common"`
Detail `vic:"0.1" scope:"read-write" key:"detail"`
// The primary process for the session
Cmd Cmd `vic:"0.1" scope:"read-only" key:"cmd"`
// Allow attach
Attach bool `vic:"0.1" scope:"read-only" key:"attach"`
OpenStdin bool `vic:"0.1" scope:"read-only" key:"openstdin"`
// Delay launching the Cmd until an attach request comes
RunBlock bool `vic:"0.1" scope:"read-only" key:"runblock"`
// Allocate a tty or not
Tty bool `vic:"0.1" scope:"read-only" key:"tty"`
ExitStatus int `vic:"0.1" scope:"read-write" key:"status"`
Started string `vic:"0.1" scope:"read-write" key:"started"`
Restart bool `vic:"0.1" scope:"read-only" key:"restart"`
// StopSignal is the signal name or number used to stop container session
StopSignal string `vic:"0.1" scope:"read-only" key:"stopSignal"`
// Diagnostics holds basic diagnostics data
Diagnostics Diagnostics `vic:"0.1" scope:"read-only" key:"diagnostics"`
// Maps the intent to the signal for this specific app
// Signals map[int]int
// Use struct composition to add in the guest specific portions
// http://attilaolah.eu/2014/09/10/json-and-struct-composition-in-go/
// ulimits
// user
// rootfs - within the container context
// User and group for setuid programs.
// Need to go here since UID/GID resolution must be done on appliance
User string `vic:"0.1" scope:"read-only" key:"User"`
Group string `vic:"0.1" scope:"read-only" key:"Group"`
}
type Detail struct {
// creation, started & stopped timestamps
CreateTime int64 `vic:"0.1" scope:"read-write" key:"createtime"`
StartTime int64 `vic:"0.1" scope:"read-write" key:"starttime"`
StopTime int64 `vic:"0.1" scope:"read-write" key:"stoptime"`
}

View File

@@ -1,81 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 executor
import (
"net"
"github.com/vmware/vic/pkg/ip"
)
// NetworkEndpoint describes a network presence in the form a vNIC in sufficient detail that it can be:
// a. created - the vNIC added to a VM
// b. identified - the guestOS can determine which interface it corresponds to
// c. configured - the guestOS can configure the interface correctly
type NetworkEndpoint struct {
// Common.Name - the nic alias requested (only one name and one alias possible in linux)
// Common.ID - pci slot of the vnic allowing for interface identifcation in-guest
Common
// Whether this endpoint's IP was specified by the client (true if it was)
Static bool `vic:"0.1" scope:"read-only" key:"static"`
// IP address to assign
IP *net.IPNet `vic:"0.1" scope:"read-only" key:"ip"`
// Actual IP address assigned
Assigned net.IPNet `vic:"0.1" scope:"read-write" key:"assigned"`
// The network in which this information should be interpreted. This is embedded directly rather than
// as a pointer so that we can ensure the data is consistent
Network ContainerNetwork `vic:"0.1" scope:"read-only" key:"network"`
// The list of exposed ports on the container
Ports []string `vic:"0.1" scope:"read-only" key:"ports"`
}
// ContainerNetwork is the data needed on a per container basis both for vSphere to ensure it's attached
// to the correct network, and in the guest to ensure the interface is correctly configured.
type ContainerNetwork struct {
// Common.Name - the symbolic name for the network, e.g. web or backend
// Common.ID - identifier of the underlay for the network
Common
Type string `vic:"0.1" scope:"read-write" key:"type"`
// Destinations is a list of CIDRs used for routing traffic to the gateway
Destinations []net.IPNet `vic:"0.1" scope:"read-only" key:"destinations"`
// The network scope the IP belongs to.
// The IP address is the default gateway
Gateway net.IPNet `vic:"0.1" scope:"read-only" key:"gateway"`
// Should this gateway be the default route for containers on the network
Default bool `vic:"0.1" scope:"read-only" key:"default"`
// The set of nameservers associated with this network - may be empty
Nameservers []net.IP `vic:"0.1" scope:"read-only" key:"dns"`
// The IP ranges for this network
Pools []ip.Range `vic:"0.1" scope:"read-only" key:"pools"`
// set of network wide links and aliases for this container on this network
Aliases []string `vic:"0.1" scope:"hidden" key:"aliases"`
Assigned struct {
Gateway net.IPNet `vic:"0.1" scope:"read-write" key:"gateway"`
Nameservers []net.IP `vic:"0.1" scope:"read-write" key:"dns"`
} `vic:"0.1" scope:"read-write" key:"assigned"`
}

View File

@@ -1,190 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 v1
import (
"net"
"net/mail"
"net/url"
"time"
"github.com/vmware/govmomi/vim25/types"
"github.com/vmware/vic/lib/config/executor"
)
// PatternToken is a set of tokens that can be placed into string constants
// for containerVMs that will be replaced with the specific values
type PatternToken string
const (
// VM is the VM name - i.e. [ds] {vm}/{vm}.vmx
VM PatternToken = "{vm}"
// ID is the container ID for the VM
ID = "{id}"
// Name is the container name of the VM
Name = "{name}"
)
// Can we just treat the VCH appliance as a containerVM booting off a specific bootstrap image
// It has many of the same requirements (around networks being attached, version recorded,
// volumes mounted, et al). Each of the components can easily be captured as a Session given they
// are simply processes.
// This would require that the bootstrap read session record for the VM and relaunch them - that
// actually aligns very well with containerVMs restarting their processes if restarted directly
// (this is obviously a behaviour we'd want to toggles for in regular containers).
// VirtualContainerHostConfigSpec holds the metadata for a Virtual Container Host that should be visible inside the appliance VM.
type VirtualContainerHostConfigSpec struct {
// The base config for the appliance. This includes the networks that are to be attached
// and disks to be mounted.
// Networks are keyed by interface name
executor.ExecutorConfig `vic:"0.1" scope:"read-only" key:"init"`
// vSphere connection configuration
Connection `vic:"0.1" scope:"read-only" key:"connect"`
// basic contact information
Contacts `vic:"0.1" scope:"read-only" key:"contact"`
// certificate configuration, for both inbound and outbound access
Certificate `vic:"0.1" scope:"read-only" key:"cert"`
// Port Layer - storage
Storage `vic:"0.1" scope:"read-only" key:"storage"`
// Port Layer - network
Network `vic:"0.1" scope:"read-only" key:"network"`
// Port Layer - exec
Container `vic:"0.1" scope:"read-only" key:"container"`
// Registry configuration for Imagec
Registry `vic:"0.1" scope:"read-only" key:"registry"`
// configuration for vic-machine
CreateBridgeNetwork bool `vic:"0.1" scope:"read-only" key:"create_bridge_network"`
}
// ContainerConfig holds the container configuration for a virtual container host
type Container struct {
// Default containerVM capacity
ContainerVMSize Resources `vic:"0.1" scope:"read-only" recurse:"depth=0"`
// Resource pools under which all containers will be created
ComputeResources []types.ManagedObjectReference `vic:"0.1" scope:"read-only"`
// Path of the ISO to use for bootstrapping containers
BootstrapImagePath string `vic:"0.1" scope:"read-only" key:"bootstrap_image_path"`
// Allow custom naming convention for containerVMs
ContainerNameConvention string
// Permitted datastore URLs for container storage for this virtual container host
ContainerStores []url.URL `vic:"0.1" scope:"read-only" recurse:"depth=0"`
}
// RegistryConfig defines the registries virtual container host can talk to
type Registry struct {
// Whitelist of registries
RegistryWhitelist []url.URL `vic:"0.1" scope:"read-only" recurse:"depth=0"`
// Blacklist of registries
RegistryBlacklist []url.URL `vic:"0.1" scope:"read-only" recurse:"depth=0"`
// Insecure registries
InsecureRegistries []url.URL `vic:"0.1" scope:"read-only" key:"insecure_registries"`
}
// NetworkConfig defines the network configuration of virtual container host
type Network struct {
// The network to use by default to provide access to the world
BridgeNetwork string `vic:"0.1" scope:"read-only" key:"bridge_network"`
// Published networks available for containers to join, keyed by consumption name
ContainerNetworks map[string]*executor.ContainerNetwork `vic:"0.1" scope:"read-only" key:"container_networks"`
// The IP range for the bridge networks
BridgeIPRange *net.IPNet `vic:"0.1" scope:"read-only" key:"bridge-ip-range"`
// The width of each new bridge network
BridgeNetworkWidth *net.IPMask `vic:"0.1" scope:"read-only" key:"bridge-net-width"`
}
// StorageConfig defines the storage configuration including images and volumes
type Storage struct {
// Datastore URLs for image stores - the top layer is [0], the bottom layer is [len-1]
ImageStores []url.URL `vic:"0.1" scope:"read-only" key:"image_stores"`
// Permitted datastore URL roots for volumes
// Keyed by the volume store name (which is used by the docker user to
// refer to the datstore + path), valued by the datastores and the path.
VolumeLocations map[string]*url.URL `vic:"0.1" scope:"read-only"`
// default size for root image
ScratchSize int64 `vic:"0.1" scope:"read-only" key:"scratch_size"`
}
type Certificate struct {
// Certificates for user authentication - this needs to be expanded to allow for directory server auth
UserCertificates []*RawCertificate
// Certificates for general outgoing network access, keyed by CIDR (IPNet.String())
NetworkCertificates map[string]*RawCertificate
// The certificate used to validate the appliance to clients
HostCertificate *RawCertificate `vic:"0.1" scope:"read-only"`
// The CAs to validate client connections
CertificateAuthorities []byte `vic:"0.1" scope:"read-only"`
// The CAs to validate docker registry connections
RegistryCertificateAuthorities []byte `vic:"0.1" scope:"read-only"`
// Certificates for specific system access, keyed by FQDN
HostCertificates map[string]*RawCertificate
}
// Connection holds the vSphere connection configuration
type Connection struct {
// The sdk URL
Target string `vic:"0.1" scope:"read-only" key:"target"`
// Username for target login
Username string `vic:"0.1" scope:"read-only" key:"username"`
// Token is an SSO token or password
Token string `vic:"0.1" scope:"secret" key:"token"`
// TargetThumbprint is the SHA-1 digest of the Target's public certificate
TargetThumbprint string `vic:"0.1" scope:"read-only" key:"target_thumbprint"`
// The session timeout
Keepalive time.Duration `vic:"0.1" scope:"read-only" key:"keepalive"`
}
type Contacts struct {
// Administrative contact for the Virtual Container Host
Admin []mail.Address
// Administrative contact for hosting infrastructure
InfrastructureAdmin []mail.Address
}
// RawCertificate is present until we add extraconfig support for [][]byte slices that are present
// in tls.Certificate
type RawCertificate struct {
Key []byte `vic:"0.1" scope:"secret"`
Cert []byte
}
// CustomerExperienceImprovementProgram provides configuration for the phone home mechanism
// This is broken out so that we can have more granular configuration in here in the future
// and so that it is insulated from changes to Virtual Container Host structure
type CustomerExperienceImprovementProgram struct {
// The server target is as follows, where the uuid is the raw number, no dashes
// "https://vcsa.vmware.com/ph-stg/api/hyper/send?_v=1.0&_c=vic.1_0&_i="+vc.uuid
// If this is non-nil then it's enabled
CEIPGateway url.URL
}
// Resources is used instead of the ResourceAllocation structs in govmomi as
// those don't currently hold IO or storage related data.
type Resources struct {
CPU types.ResourceAllocationInfo
Memory types.ResourceAllocationInfo
IO types.ResourceAllocationInfo
Storage types.ResourceAllocationInfo
}
// Remove all methods from this file to reduce binary size

View File

@@ -1,218 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 executor
import (
"net/url"
"time"
"github.com/vmware/vic/pkg/version"
)
type State int
const (
STARTED State = iota
EXITED
KILLED
)
// Common data between managed entities, across execution environments
type Common struct {
// A reference to the components hosting execution environment, if any
ExecutionEnvironment string
// Unambiguous ID with meaning in the context of its hosting execution environment
ID string `vic:"0.1" scope:"read-only" key:"id"`
// Convenience field to record a human readable name
Name string `vic:"0.1" scope:"read-only" key:"name"`
// Freeform notes related to the entity
Notes string `vic:"0.1" scope:"hidden" key:"notes"`
}
// Diagnostics records some basic control and lifecycle information for diagnostic purposes
type Diagnostics struct {
// Should debugging be enabled on whatever component this is and at what level
DebugLevel int `vic:"0.1" scope:"read-only" key:"debug"`
// RessurectionCount is a log of how many times the entity has been restarted due
// to error exit
ResurrectionCount int `vic:"0.1" scope:"read-write" key:"resurrections"`
// ExitLogs is a best effort record of the time of process death and the cause for
// restartable entities
ExitLogs []ExitLog `vic:"0.1" scope:"read-write" key:"exitlogs"`
}
// ExitLog records some basic diagnostics about anomalous exit for restartable entities
type ExitLog struct {
Time time.Time
ExitStatus int
Message string
}
// MountSpec details a mount that must be executed within the executor
// A mount is a URI -> path mapping with a credential of some kind
// In the case of a labeled disk:
// label://<label name> => </mnt/path>
type MountSpec struct {
// A URI->path mapping, e.g.
// May contain credentials
Source url.URL `vic:"0.1" scope:"read-only" key:"source"`
// The path in the executor at which this should be mounted
Path string `vic:"0.1" scope:"read-only" key:"dest"`
// Freeform mode string, which could translate directly to mount options
// We may want to turn this into a more structured form eventually
Mode string `vic:"0.1" scope:"read-only" key:"mode"`
}
// ContainerVM holds that data tightly associated with a containerVM, but that should not
// be visible to the guest. This is the external complement to ExecutorConfig.
type ContainerVM struct {
Common
// The version of the bootstrap image that this container was booted from.
Version string
// Name aliases for this specific container, Maps alias to unambiguous name
// This uses unambiguous name rather than reified network endpoint to persist
// the intent rather than a point-in-time manifesting of that intent.
Aliases map[string]string
// The location of the interaction service that the tether should connect to. Examples:
// * tcp://x.x.x.x:2377
// * vmci://moid - should this be an moid or a VMCI CID? Does one insulate us from reboots?
Interaction url.URL
// Key is the host key used during communicate back with the Interaction endpoint if any
// Used if the vSocket agent is responsible for authenticating the connection
AgentKey []byte
}
// ExecutorConfig holds the data tightly associated with an Executor. This is distinct from Sessions
// in that there is no process inherently associated - this is closer to a ThreadPool than a Thread and
// is the owner of the shared filesystem environment. This is the guest visible complement to ContainerVM.
type ExecutorConfig struct {
Common `vic:"0.1" scope:"read-only" key:"common"`
// CreateTime stamp
CreateTime int64 `vic:"0.1" scope:"read-write" key:"createtime"`
// Diagnostics holds basic diagnostics data
Diagnostics Diagnostics `vic:"0.1" scope:"read-only" key:"diagnostics"`
// Sessions is the set of sessions currently hosted by this executor
// These are keyed by session ID
Sessions map[string]*SessionConfig `vic:"0.1" scope:"read-only" key:"sessions"`
// Maps the mount name to the detail mount specification
Mounts map[string]MountSpec `vic:"0.1" scope:"read-only" key:"mounts"`
// This describes an executors presence on a network, and contains sufficient
// information to configure the interface in the guest.
Networks map[string]*NetworkEndpoint `vic:"0.1" scope:"read-only" key:"networks"`
// Key is the host key used during communicate back with the Interaction endpoint if any
// Used if the in-guest tether is responsible for authenticating the connection
Key []byte `vic:"0.1" scope:"read-only" key:"key"`
// Layer id that is backing this container VM
LayerID string `vic:"0.1" scope:"read-only" key:"layerid"`
// Blob metadata for the caller
Annotations map[string]string `vic:"0.1" scope:"hidden" key:"annotations"`
// Repository requested by user
// TODO: a bit docker specific
RepoName string `vic:"0.1" scope:"read-only" key:"repo"`
// version
Version *version.Build `vic:"0.1" scope:"read-only" key:"version"`
}
// Cmd is here because the encoding packages seem to have issues with the full exec.Cmd struct
type Cmd struct {
// Path is the command to run
Path string `vic:"0.1" scope:"read-only" key:"Path"`
// Args is the command line arguments including the command in Args[0]
Args []string `vic:"0.1" scope:"read-only" key:"Args"`
// Env specifies the environment of the process
Env []string `vic:"0.1" scope:"read-only" key:"Env"`
// Dir specifies the working directory of the command
Dir string `vic:"0.1" scope:"read-only" key:"Dir"`
}
// SessionConfig defines the content of a session - this maps to the root of a process tree
// inside an executor
// This is close to but not perfectly aligned with the new docker/docker/daemon/execdriver/driver:CommonProcessConfig
type SessionConfig struct {
// The primary session may have the same ID as the executor owning it
Common `vic:"0.1" scope:"read-only" key:"common"`
Detail `vic:"0.1" scope:"read-write" key:"detail"`
// The primary process for the session
Cmd Cmd `vic:"0.1" scope:"read-only" key:"cmd"`
// Allow attach
Attach bool `vic:"0.1" scope:"read-only" key:"attach"`
OpenStdin bool `vic:"0.1" scope:"read-only" key:"openstdin"`
// Delay launching the Cmd until an attach request comes
RunBlock bool `vic:"0.1" scope:"read-only" key:"runblock"`
// Allocate a tty or not
Tty bool `vic:"0.1" scope:"read-only" key:"tty"`
ExitStatus int `vic:"0.1" scope:"read-write" key:"status"`
Started string `vic:"0.1" scope:"read-write" key:"started"`
Restart bool `vic:"0.1" scope:"read-only" key:"restart"`
// StopSignal is the signal name or number used to stop container session
StopSignal string `vic:"0.1" scope:"read-only" key:"stopSignal"`
// Diagnostics holds basic diagnostics data
Diagnostics Diagnostics `vic:"0.1" scope:"read-only" key:"diagnostics"`
// Maps the intent to the signal for this specific app
// Signals map[int]int
// Use struct composition to add in the guest specific portions
// http://attilaolah.eu/2014/09/10/json-and-struct-composition-in-go/
// ulimits
// user
// rootfs - within the container context
// User and group for setuid programs.
// Need to go here since UID/GID resolution must be done on appliance
User string `vic:"0.1" scope:"read-only" key:"User"`
Group string `vic:"0.1" scope:"read-only" key:"Group"`
}
type Detail struct {
// creation, started & stopped timestamps
CreateTime int64 `vic:"0.1" scope:"read-write" key:"createtime"`
StartTime int64 `vic:"0.1" scope:"read-write" key:"starttime"`
StopTime int64 `vic:"0.1" scope:"read-write" key:"stoptime"`
}

View File

@@ -1,81 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 executor
import (
"net"
"github.com/vmware/vic/pkg/ip"
)
// NetworkEndpoint describes a network presence in the form a vNIC in sufficient detail that it can be:
// a. created - the vNIC added to a VM
// b. identified - the guestOS can determine which interface it corresponds to
// c. configured - the guestOS can configure the interface correctly
type NetworkEndpoint struct {
// Common.Name - the nic alias requested (only one name and one alias possible in linux)
// Common.ID - pci slot of the vnic allowing for interface identifcation in-guest
Common
// Whether this endpoint's IP was specified by the client (true if it was)
Static bool `vic:"0.1" scope:"read-only" key:"static"`
// IP address to assign
IP *net.IPNet `vic:"0.1" scope:"read-only" key:"ip"`
// Actual IP address assigned
Assigned net.IPNet `vic:"0.1" scope:"read-write" key:"assigned"`
// The network in which this information should be interpreted. This is embedded directly rather than
// as a pointer so that we can ensure the data is consistent
Network ContainerNetwork `vic:"0.1" scope:"read-only" key:"network"`
// The list of exposed ports on the container
Ports []string `vic:"0.1" scope:"read-only" key:"ports"`
}
// ContainerNetwork is the data needed on a per container basis both for vSphere to ensure it's attached
// to the correct network, and in the guest to ensure the interface is correctly configured.
type ContainerNetwork struct {
// Common.Name - the symbolic name for the network, e.g. web or backend
// Common.ID - identifier of the underlay for the network
Common
Type string `vic:"0.1" scope:"read-write" key:"type"`
// Destinations is a list of CIDRs used for routing traffic to the gateway
Destinations []net.IPNet `vic:"0.1" scope:"read-only" key:"destinations"`
// The network scope the IP belongs to.
// The IP address is the default gateway
Gateway net.IPNet `vic:"0.1" scope:"read-only" key:"gateway"`
// Should this gateway be the default route for containers on the network
Default bool `vic:"0.1" scope:"read-only" key:"default"`
// The set of nameservers associated with this network - may be empty
Nameservers []net.IP `vic:"0.1" scope:"read-only" key:"dns"`
// The IP ranges for this network
Pools []ip.Range `vic:"0.1" scope:"read-only" key:"pools"`
// set of network wide links and aliases for this container on this network
Aliases []string `vic:"0.1" scope:"hidden" key:"aliases"`
Assigned struct {
Gateway net.IPNet `vic:"0.1" scope:"read-write" key:"gateway"`
Nameservers []net.IP `vic:"0.1" scope:"read-write" key:"dns"`
} `vic:"0.1" scope:"read-write" key:"assigned"`
}

View File

@@ -1,190 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 v2
import (
"net"
"net/mail"
"net/url"
"time"
"github.com/vmware/govmomi/vim25/types"
"github.com/vmware/vic/lib/config/executor"
)
// PatternToken is a set of tokens that can be placed into string constants
// for containerVMs that will be replaced with the specific values
type PatternToken string
const (
// VM is the VM name - i.e. [ds] {vm}/{vm}.vmx
VM PatternToken = "{vm}"
// ID is the container ID for the VM
ID = "{id}"
// Name is the container name of the VM
Name = "{name}"
)
// Can we just treat the VCH appliance as a containerVM booting off a specific bootstrap image
// It has many of the same requirements (around networks being attached, version recorded,
// volumes mounted, et al). Each of the components can easily be captured as a Session given they
// are simply processes.
// This would require that the bootstrap read session record for the VM and relaunch them - that
// actually aligns very well with containerVMs restarting their processes if restarted directly
// (this is obviously a behaviour we'd want to toggles for in regular containers).
// VirtualContainerHostConfigSpec holds the metadata for a Virtual Container Host that should be visible inside the appliance VM.
type VirtualContainerHostConfigSpec struct {
// The base config for the appliance. This includes the networks that are to be attached
// and disks to be mounted.
// Networks are keyed by interface name
executor.ExecutorConfig `vic:"0.1" scope:"read-only" key:"init"`
// vSphere connection configuration
Connection `vic:"0.1" scope:"read-only" key:"connect"`
// basic contact information
Contacts `vic:"0.1" scope:"read-only" key:"contact"`
// certificate configuration, for both inbound and outbound access
Certificate `vic:"0.1" scope:"read-only" key:"cert"`
// Port Layer - storage
Storage `vic:"0.1" scope:"read-only" key:"storage"`
// Port Layer - network
Network `vic:"0.1" scope:"read-only" key:"network"`
// Port Layer - exec
Container `vic:"0.1" scope:"read-only" key:"container"`
// Registry configuration for Imagec
Registry `vic:"0.1" scope:"read-only" key:"registry"`
// configuration for vic-machine
CreateBridgeNetwork bool `vic:"0.1" scope:"read-only" key:"create_bridge_network"`
}
// ContainerConfig holds the container configuration for a virtual container host
type Container struct {
// Default containerVM capacity
ContainerVMSize Resources `vic:"0.1" scope:"read-only" recurse:"depth=0"`
// Resource pools under which all containers will be created
ComputeResources []types.ManagedObjectReference `vic:"0.1" scope:"read-only"`
// Path of the ISO to use for bootstrapping containers
BootstrapImagePath string `vic:"0.1" scope:"read-only" key:"bootstrap_image_path"`
// Allow custom naming convention for containerVMs
ContainerNameConvention string
// Permitted datastore URLs for container storage for this virtual container host
ContainerStores []url.URL `vic:"0.1" scope:"read-only" recurse:"depth=0"`
}
// RegistryConfig defines the registries virtual container host can talk to
type Registry struct {
// Whitelist of registries
RegistryWhitelist []url.URL `vic:"0.1" scope:"read-only" recurse:"depth=0"`
// Blacklist of registries
RegistryBlacklist []url.URL `vic:"0.1" scope:"read-only" recurse:"depth=0"`
// Insecure registries
InsecureRegistries []url.URL `vic:"0.1" scope:"read-only" key:"insecure_registries"`
}
// NetworkConfig defines the network configuration of virtual container host
type Network struct {
// The network to use by default to provide access to the world
BridgeNetwork string `vic:"0.1" scope:"read-only" key:"bridge_network"`
// Published networks available for containers to join, keyed by consumption name
ContainerNetworks map[string]*executor.ContainerNetwork `vic:"0.1" scope:"read-only" key:"container_networks"`
// The IP range for the bridge networks
BridgeIPRange *net.IPNet `vic:"0.1" scope:"read-only" key:"bridge-ip-range"`
// The width of each new bridge network
BridgeNetworkWidth *net.IPMask `vic:"0.1" scope:"read-only" key:"bridge-net-width"`
}
// StorageConfig defines the storage configuration including images and volumes
type Storage struct {
// Datastore URLs for image stores - the top layer is [0], the bottom layer is [len-1]
ImageStores []url.URL `vic:"0.1" scope:"read-only" key:"image_stores"`
// Permitted datastore URL roots for volumes
// Keyed by the volume store name (which is used by the docker user to
// refer to the datstore + path), valued by the datastores and the path.
VolumeLocations map[string]*url.URL `vic:"0.1" scope:"read-only"`
// default size for root image
ScratchSize int64 `vic:"0.1" scope:"read-only" key:"scratch_size"`
}
type Certificate struct {
// Certificates for user authentication - this needs to be expanded to allow for directory server auth
UserCertificates []*RawCertificate
// Certificates for general outgoing network access, keyed by CIDR (IPNet.String())
NetworkCertificates map[string]*RawCertificate
// The certificate used to validate the appliance to clients
HostCertificate *RawCertificate `vic:"0.1" scope:"read-only"`
// The CAs to validate client connections
CertificateAuthorities []byte `vic:"0.1" scope:"read-only"`
// The CAs to validate docker registry connections
RegistryCertificateAuthorities []byte `vic:"0.1" scope:"read-only"`
// Certificates for specific system access, keyed by FQDN
HostCertificates map[string]*RawCertificate
}
// Connection holds the vSphere connection configuration
type Connection struct {
// The sdk URL
Target string `vic:"0.1" scope:"read-only" key:"target"`
// Username for target login
Username string `vic:"0.1" scope:"read-only" key:"username"`
// Token is an SSO token or password
Token string `vic:"0.1" scope:"secret" key:"token"`
// TargetThumbprint is the SHA-1 digest of the Target's public certificate
TargetThumbprint string `vic:"0.1" scope:"read-only" key:"target_thumbprint"`
// The session timeout
Keepalive time.Duration `vic:"0.1" scope:"read-only" key:"keepalive"`
}
type Contacts struct {
// Administrative contact for the Virtual Container Host
Admin []mail.Address
// Administrative contact for hosting infrastructure
InfrastructureAdmin []mail.Address
}
// RawCertificate is present until we add extraconfig support for [][]byte slices that are present
// in tls.Certificate
type RawCertificate struct {
Key []byte `vic:"0.1" scope:"secret"`
Cert []byte
}
// CustomerExperienceImprovementProgram provides configuration for the phone home mechanism
// This is broken out so that we can have more granular configuration in here in the future
// and so that it is insulated from changes to Virtual Container Host structure
type CustomerExperienceImprovementProgram struct {
// The server target is as follows, where the uuid is the raw number, no dashes
// "https://vcsa.vmware.com/ph-stg/api/hyper/send?_v=1.0&_c=vic.1_0&_i="+vc.uuid
// If this is non-nil then it's enabled
CEIPGateway url.URL
}
// Resources is used instead of the ResourceAllocation structs in govmomi as
// those don't currently hold IO or storage related data.
type Resources struct {
CPU types.ResourceAllocationInfo
Memory types.ResourceAllocationInfo
IO types.ResourceAllocationInfo
Storage types.ResourceAllocationInfo
}
// Remove all methods from this file to reduce binary size

View File

@@ -1,23 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 plugins
// import all plugin packages here to register plugins
import (
// imported for the side effect
_ "github.com/vmware/vic/lib/migration/samples/plugins/plugin1"
_ "github.com/vmware/vic/lib/migration/samples/plugins/plugin2"
)

View File

@@ -1,111 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 plugin1
import (
"context"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/vmware/vic/lib/migration/errors"
"github.com/vmware/vic/lib/migration/manager"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/vsphere/extraconfig"
"github.com/vmware/vic/pkg/vsphere/session"
)
// sample plugin to migrate data in appliance configuration VirtualContainerHost
// If only a couple of items changed in the configuration, you don't have to copy all VirtualContainerHost. Only define the few items used by
// this upgrade plugin will simplify the extraconfig encoding/decoding process
const (
version = 1
target = manager.ApplianceConfigure
)
func init() {
defer trace.End(trace.Begin(fmt.Sprintf("Registering plugin %s:%d", target, version)))
if err := manager.Migrator.Register(version, target, &ApplianceStopSignalRename{}); err != nil {
log.Errorf("Failed to register plugin %s:%d, %s", target, version, err)
}
}
// ApplianceStopSignalRename is plugin for vic 0.8.0-GA version upgrade
type ApplianceStopSignalRename struct {
}
type OldStopSignal struct {
ExecutorConfig `vic:"0.1" scope:"read-only" key:"init"`
}
type ExecutorConfig struct {
Sessions map[string]*SessionConfig `vic:"0.1" scope:"read-only" key:"sessions"`
}
type SessionConfig struct {
StopSignal string `vic:"0.1" scope:"read-only" key:"stopSignal"`
}
type NewStopSignal struct {
NewExecutorConfig `vic:"0.1" scope:"read-only" key:"init"`
}
type NewExecutorConfig struct {
Sessions map[string]*NewSessionConfig `vic:"0.1" scope:"read-only" key:"sessions"`
}
type NewSessionConfig struct {
StopSignal string `vic:"0.1" scope:"read-only" key:"forceStopSignal"`
}
func (p *ApplianceStopSignalRename) Migrate(ctx context.Context, s *session.Session, data interface{}) error {
defer trace.End(trace.Begin(fmt.Sprintf("%d", version)))
if data == nil {
return nil
}
mapData := data.(map[string]string)
oldStruct := &OldStopSignal{}
result := extraconfig.Decode(extraconfig.MapSource(mapData), oldStruct)
if result == nil {
return &errors.DecodeError{}
}
keys := extraconfig.CalculateKeys(oldStruct, "ExecutorConfig.Sessions.*.StopSignal", "")
for _, key := range keys {
log.Debugf("old %s:%s", key, mapData[key])
}
newStruct := &NewStopSignal{}
if len(oldStruct.ExecutorConfig.Sessions) == 0 {
return nil
}
newStruct.Sessions = make(map[string]*NewSessionConfig)
for id, sess := range oldStruct.ExecutorConfig.Sessions {
newSess := &NewSessionConfig{}
newSess.StopSignal = sess.StopSignal
newStruct.Sessions[id] = newSess
}
cfg := make(map[string]string)
extraconfig.Encode(extraconfig.MapSink(cfg), newStruct)
// remove old data
for _, key := range keys {
delete(mapData, key)
}
for k, v := range cfg {
log.Debugf("New data: %s:%s", k, v)
mapData[k] = v
}
return nil
}

View File

@@ -1,123 +0,0 @@
// Copyright 2017 VMware, Inc. All Rights Reserved.
//
// 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 plugin2
import (
"context"
"fmt"
"os"
log "github.com/Sirupsen/logrus"
"github.com/vmware/vic/lib/migration/errors"
"github.com/vmware/vic/lib/migration/manager"
"github.com/vmware/vic/lib/migration/samples/config/v2"
"github.com/vmware/vic/pkg/kvstore"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/vsphere/datastore"
"github.com/vmware/vic/pkg/vsphere/extraconfig"
"github.com/vmware/vic/pkg/vsphere/session"
)
// Sample plugin to migrate data in keyvalue store
// If there is any key/value change, should create a new keyvalue store file with version appendix, like .v2, to differentiate with old keyvalue store file
// Migrate keyvalue plugin should read configuration from input VirtualContainerHost configuration, and then read from old keyvalue store file directly
// After migration, write back to new datastore file with version appendix
// Data migration framework is not responsible for data roll back. With versioned datastore file, even roll back happens, old version's datastore file is still useable by old binary
// Make sure to delete existing new version datastore file, which might be a left over of last failed data migration attempt.
const (
version = 2
target = manager.ApplianceConfigure
KVStoreFolder = "kvStores"
APIKV = "apiKV"
oldKey = "image.name"
newKey = "image.tag"
)
func init() {
log.Debugf("Registering plugin %s:%d", target, version)
if err := manager.Migrator.Register(version, target, &NewImageMeta{}); err != nil {
log.Errorf("Failed to register plugin %s:%d, %s", target, version, err)
}
}
// NewImageMeta is plugin for vic 0.8.0-GA version upgrade
type NewImageMeta struct {
}
func (p *NewImageMeta) Migrate(ctx context.Context, s *session.Session, data interface{}) error {
defer trace.End(trace.Begin(fmt.Sprintf("%d", version)))
if data == nil {
return nil
}
vchConfMap := data.(map[string]string)
// No plugin query keyvalue store yet, load from datastore file
// get a ds helper for this ds url
vchConf := &v2.VirtualContainerHostConfigSpec{}
extraconfig.Decode(extraconfig.MapSource(vchConfMap), vchConf)
imageURL := vchConf.ImageStores[0]
// TODO: sample code, should get datastore from imageURL
dsHelper, err := datastore.NewHelper(trace.NewOperation(ctx, "datastore helper creation"), s,
s.Datastore, fmt.Sprintf("%s/%s", imageURL.Path, KVStoreFolder))
if err != nil {
return &errors.InternalError{
Message: fmt.Sprintf("unable to get datastore helper for %s store creation: %s", APIKV, err.Error()),
}
}
// restore the modified K/V store
oldKeyValStore, err := kvstore.NewKeyValueStore(ctx, kvstore.NewDatastoreBackend(dsHelper), APIKV)
if err != nil && !os.IsExist(err) {
return &errors.InternalError{
Message: fmt.Sprintf("unable to create %s datastore backed store: %s", APIKV, err.Error()),
}
}
// create new k/v store with version appendix v2
newDsFile := fmt.Sprintf("%s.v%d", APIKV, version)
// try to remove new k/v store file in case it's created already
dsHelper.Rm(ctx, newDsFile)
newKeyValueStore, err := kvstore.NewKeyValueStore(ctx, kvstore.NewDatastoreBackend(dsHelper), newDsFile)
if err != nil && !os.IsExist(err) {
return &errors.InternalError{
Message: fmt.Sprintf("unable to create %s datastore backed store: %s", newDsFile, err.Error()),
}
}
// copy all key/value from old k/v store
allKeyVals, err := oldKeyValStore.List(".*")
if err != nil {
return &errors.InternalError{
Message: fmt.Sprintf("unable to list key/value store %s: %s", APIKV, err.Error()),
}
}
for key, val := range allKeyVals {
newKeyValueStore.Put(ctx, key, val)
}
val, err := newKeyValueStore.Get(oldKey)
if err != nil && err != kvstore.ErrKeyNotFound {
return &errors.InternalError{
Message: fmt.Sprintf("failed to get %s from store %s: %s", oldKey, APIKV, err.Error()),
}
}
// put the new key/value to store, and leave the old key/value there, in case upgrade failed, old binary still works well with half-changed store
newKeyValueStore.Put(ctx, newKey, []byte(fmt.Sprintf("%s:%s", val, "latest")))
// persist new data back to vsphere, framework does not take of it
newKeyValueStore.Save(ctx)
return nil
}