func TestGenerateStorageVersionMap(t *testing.T) {
	testCases := []struct {
		legacyVersion   string
		storageVersions string
		defaultVersions string
		expectedMap     map[string]schema.GroupVersion
	}{
		{
			legacyVersion:   "v1",
			storageVersions: "v1,extensions/v1beta1",
			expectedMap: map[string]schema.GroupVersion{
				api.GroupName:        {Version: "v1"},
				extensions.GroupName: {Group: "extensions", Version: "v1beta1"},
			},
		},
		{
			legacyVersion:   "",
			storageVersions: "extensions/v1beta1,v1",
			expectedMap: map[string]schema.GroupVersion{
				api.GroupName:        {Version: "v1"},
				extensions.GroupName: {Group: "extensions", Version: "v1beta1"},
			},
		},
		{
			legacyVersion:   "",
			storageVersions: "autoscaling=extensions/v1beta1,v1",
			defaultVersions: "extensions/v1beta1,v1,autoscaling/v1",
			expectedMap: map[string]schema.GroupVersion{
				api.GroupName:         {Version: "v1"},
				autoscaling.GroupName: {Group: "extensions", Version: "v1beta1"},
				extensions.GroupName:  {Group: "extensions", Version: "v1beta1"},
			},
		},
		{
			legacyVersion:   "",
			storageVersions: "",
			expectedMap:     map[string]schema.GroupVersion{},
		},
	}
	for i, test := range testCases {
		s := options.ServerRunOptions{
			StorageVersions:        test.storageVersions,
			DefaultStorageVersions: test.defaultVersions,
		}
		output, err := s.StorageGroupsToEncodingVersion()
		if err != nil {
			t.Errorf("%v: unexpected error: %v", i, err)
		}
		if !reflect.DeepEqual(test.expectedMap, output) {
			t.Errorf("%v: unexpected error. expect: %v, got: %v", i, test.expectedMap, output)
		}
	}
}
示例#2
0
func (s *CloudProviderOptions) DefaultExternalHost(genericoptions *genericoptions.ServerRunOptions) error {
	if len(genericoptions.ExternalHost) != 0 {
		return nil
	}

	// TODO: extend for other providers
	if s.CloudProvider == "gce" || s.CloudProvider == "aws" {
		cloud, err := cloudprovider.InitCloudProvider(s.CloudProvider, s.CloudConfigFile)
		if err != nil {
			return fmt.Errorf("%q cloud provider could not be initialized: %v", s.CloudProvider, err)
		}
		instances, supported := cloud.Instances()
		if !supported {
			return fmt.Errorf("%q cloud provider has no instances", s.CloudProvider)
		}
		hostname, err := os.Hostname()
		if err != nil {
			return fmt.Errorf("failed to get hostname: %v", err)
		}
		nodeName, err := instances.CurrentNodeName(hostname)
		if err != nil {
			return fmt.Errorf("failed to get NodeName from %q cloud provider: %v", s.CloudProvider, err)
		}
		addrs, err := instances.NodeAddresses(nodeName)
		if err != nil {
			return fmt.Errorf("failed to get external host address from %q cloud provider: %v", s.CloudProvider, err)
		} else {
			for _, addr := range addrs {
				if addr.Type == v1.NodeExternalIP {
					genericoptions.ExternalHost = addr.Address
				}
			}
		}
	}

	return nil
}