-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set instance_id in flintlock #394
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package cloudinit | ||
|
||
const ( | ||
// InstanceDataKey is the metdata key name to use for instance data. | ||
InstanceDataKey = "meta-data" | ||
// UserdataKey is the metadata key name to use for user data. | ||
UserdataKey = "user-data" | ||
// VendorDataKey is the metadata key name to use for vendor data. | ||
VendorDataKey = "vendor-data" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package instance | ||
|
||
// These constants represent standard instance metadata key names. | ||
const ( | ||
// CloudNameKey is the instance metdata key name representing the cloud name. | ||
CloudNameKey = "cloud_name" | ||
// InstanceIDKey is the instance metdata key name representing the unique instance id mof the instance. | ||
InstanceIDKey = "instance_id" | ||
// LocalHostnameKey is the instance metdata key name representing the host name of the instance. | ||
LocalHostnameKey = "local_hostname" | ||
// PlatformKey is the instance metdata key name representing the hosting platform of the instance. | ||
PlatformKey = "platform" | ||
) | ||
|
||
// These constants represents custom instance metadata names | ||
const ( | ||
// ClusterNameKey is the instance metdata key name representing the cluster name of the instance. | ||
ClusterNameKey = "cluster_name" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package instance | ||
|
||
// New creates a new instance metadata | ||
func New(opts ...MetadataOption) Metadata { | ||
m := map[string]string{} | ||
|
||
for _, opt := range opts { | ||
opt(m) | ||
} | ||
|
||
return m | ||
} | ||
|
||
// Metadata represents the cloud-init instance metadata. | ||
// See https://cloudinit.readthedocs.io/en/latest/topics/instancedata.html | ||
type Metadata map[string]string | ||
|
||
// HasItem returns true/false if the specific metadata item exists. | ||
func (m Metadata) HasItem(name string) bool { | ||
if len(m) == 0 { | ||
return false | ||
} | ||
_, ok := m[name] | ||
|
||
return ok | ||
} | ||
|
||
// MetadataOption is an option when creating an instance of Metadata | ||
type MetadataOption func(Metadata) | ||
|
||
// WithInstanceID will set the instance id metadata. | ||
func WithInstanceID(instanceID string) MetadataOption { | ||
return func(im Metadata) { | ||
im[InstanceIDKey] = instanceID | ||
} | ||
} | ||
|
||
// WithCloudName will set the cloud name metadata. | ||
func WithCloudName(name string) MetadataOption { | ||
return func(im Metadata) { | ||
im[CloudNameKey] = name | ||
} | ||
} | ||
|
||
// WithLocalHostname will set the local hostname metadata. | ||
func WithLocalHostname(name string) MetadataOption { | ||
return func(im Metadata) { | ||
im[LocalHostnameKey] = name | ||
} | ||
} | ||
|
||
// WithPlatform will set the platform metadata. | ||
func WithPlatform(name string) MetadataOption { | ||
return func(im Metadata) { | ||
im[PlatformKey] = name | ||
} | ||
} | ||
|
||
// WithClusterName will set the cluster name metadata. | ||
func WithClusterName(name string) MetadataOption { | ||
return func(im Metadata) { | ||
im[ClusterNameKey] = name | ||
} | ||
} | ||
|
||
// WithExisting will set the metadata keys/values based on an existing Metadata. | ||
func WithExisting(existing Metadata) MetadataOption { | ||
return func(im Metadata) { | ||
for k, v := range existing { | ||
im[k] = v | ||
} | ||
} | ||
} | ||
|
||
// WithKeyValue will set the metadata with the specified key and value. | ||
func WithKeyValue(key, value string) MetadataOption { | ||
return func(im Metadata) { | ||
im[key] = value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package instance_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
|
||
"github.com/weaveworks/flintlock/client/cloudinit/instance" | ||
) | ||
|
||
const ( | ||
testCloudName = "nocloud" | ||
testClusterName = "cluster1" | ||
testInstanceID = "i-123456" | ||
testHostName = "host1" | ||
testPlatformName = "liquidmetal" | ||
) | ||
|
||
func TestInstanceMetadata_NewNoOptions(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
m := instance.New() | ||
Expect(m).NotTo(BeNil()) | ||
Expect(m).To(HaveLen(0)) | ||
} | ||
|
||
func TestInstanceMetadata_NewWithOptions(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
m := instance.New( | ||
instance.WithCloudName(testCloudName), | ||
instance.WithClusterName(testClusterName), | ||
instance.WithInstanceID(testInstanceID), | ||
instance.WithLocalHostname(testHostName), | ||
instance.WithPlatform(testPlatformName), | ||
) | ||
Expect(m).NotTo(BeNil()) | ||
Expect(m).To(HaveLen(5)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could simplify this to just HaveLen(5) because it can't be both nil and have len 5 The one above is fine ( (There is another below). |
||
|
||
Expect(m[instance.CloudNameKey]).To(Equal(testCloudName)) | ||
Expect(m[instance.ClusterNameKey]).To(Equal(testClusterName)) | ||
Expect(m[instance.InstanceIDKey]).To(Equal(testInstanceID)) | ||
Expect(m[instance.LocalHostnameKey]).To(Equal(testHostName)) | ||
Expect(m[instance.PlatformKey]).To(Equal(testPlatformName)) | ||
} | ||
|
||
func TestInstanceMetadata_NewOptionsExisting(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
existing := instance.New( | ||
instance.WithCloudName(testCloudName), | ||
instance.WithClusterName(testClusterName), | ||
instance.WithInstanceID(testInstanceID), | ||
instance.WithLocalHostname(testHostName), | ||
instance.WithPlatform(testPlatformName), | ||
) | ||
|
||
m := instance.New( | ||
instance.WithExisting(existing), | ||
instance.WithInstanceID("changed"), | ||
) | ||
|
||
Expect(m).NotTo(BeNil()) | ||
Expect(m).To(HaveLen(5)) | ||
|
||
Expect(m[instance.CloudNameKey]).To(Equal(testCloudName)) | ||
Expect(m[instance.ClusterNameKey]).To(Equal(testClusterName)) | ||
Expect(m[instance.InstanceIDKey]).To(Equal("changed")) | ||
Expect(m[instance.LocalHostnameKey]).To(Equal(testHostName)) | ||
Expect(m[instance.PlatformKey]).To(Equal(testPlatformName)) | ||
} |
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
client/cloudinit/network.go → client/cloudinit/network/network.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cloudinit | ||
package network | ||
|
||
type Network struct { | ||
Version int `yaml:"version"` | ||
|
2 changes: 1 addition & 1 deletion
2
client/cloudinit/userdata.go → client/cloudinit/userdata/userdata.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cloudinit | ||
package userdata | ||
|
||
type UserData struct { | ||
HostName string `yaml:"hostname,omitempty"` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a custom
WithKeyVaue
orWithCustomField
would be useful. Right now we are not using anything else, but in production ppl might want to define more data.potentially all
WithXXX
can use that as well, so if we need validation we can do it in one place (like don't add if the value is empty)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thats a good point. Let me change that.