Skip to content
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

Add ID fields to Resource Objects for easy lookup and validations #481

Merged
merged 1 commit into from
Jul 26, 2017

Conversation

mgleung
Copy link
Contributor

@mgleung mgleung commented Jul 24, 2017

Description

Adds in an ID field for every Resource Object and validations on top of those ID fields to make it easier to validate that important fields were not changed.

Will be of help to finishing projectcalico/calicoctl#1564

Todos

  • Tests
  • Documentation (none required)
  • Release note (none required)

Release Note

None required

@@ -37,6 +37,10 @@ func (t BGPPeer) GetResourceMetadata() unversioned.ResourceMetadata {
return t.Metadata
}

func (t BGPPeer) GetResourceID() string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're better off having this method return a user friendly name that identifies this resource. That way it could be returned to the user as well. e.g. "BGP Peer (PeerIP=xxx, Scope=Global)"

@@ -80,3 +82,39 @@ func ValidateMetadataIDsAssigned(rm unversioned.ResourceMetadata) error {

return nil
}

// ValidateIDFieldsSame is used to validate that the Resource Objects
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is only useful for calicoctl, so I'd not include this in libcalico-go.

// GetIDFields is a helper function that returns the field labels
// on the metadata objects that are ID fields excluding Labels,
// Annotations, and Tags
func GetIDFields(rm unversioned.ResourceMetadata) []string {
Copy link
Contributor

@robbrockbank robbrockbank Jul 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this. I think if you really want to get a slice of IDs as strings, then the getResourceID() should probably have been getResourceIDs() returning a slice of strings.

Also, if you are going to go to the trouble of reading the tags, then it would be better to add a new tag type to identify the field as an ID. Skipping over some types and assuming the rest are IDs isn't very maintainable if we start padding out the Metadata with other fields.

Personally I don't think that's necessary - if you have a method that returns a nice user friendly name of the resource then you can craft some text that says Resource X should be Resource Y.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. My thought was to basically write a function that would work for all the resources without needing to create more individual methods on each that would be need to be maintained should something change. To that end, adding a new tag type would probably be best as far as that line of thinking goes. You're right though; all of this is probably unnecessary if I can get a user friendly name working so then I can parse those values. WorkloadEndpoint might have a little bit of a long string name though....

@@ -37,6 +39,10 @@ func (t BGPPeer) GetResourceMetadata() unversioned.ResourceMetadata {
return t.Metadata
}

func (t BGPPeer) String() string {
return fmt.Sprintf("BGPPeer(IP=%s, Scope=%s, Node=%s)", t.Metadata.PeerIP.IP.String(), t.Metadata.Scope, t.Metadata.Node)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IP->PeerIP

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not too fussed about this, but perhaps if scope is node and node=="" we could omit node from the name.

@@ -23,6 +23,7 @@ type Resource interface {
type ResourceObject interface {
Resource
GetResourceMetadata() ResourceMetadata
String() string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the very specific behavior of this String() method, I think it is worth adding a doc comment.

@@ -31,6 +31,10 @@ func (t WorkloadEndpoint) GetResourceMetadata() unversioned.ResourceMetadata {
return t.Metadata
}

func (t WorkloadEndpoint) String() string {
return fmt.Sprintf("WorkloadEndpoint(Node=%s, Orchestrator=%s, Workload=%s, Name=%s, ActiveInstanceID=%s)", t.Metadata.Node, t.Metadata.Orchestrator, t.Metadata.Workload, t.Metadata.Name, t.Metadata.ActiveInstanceID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove the Active Instance ID, or perhaps only include it if it is set to something - it's used under very specific circumstances.

@robbrockbank robbrockbank merged commit 73b7a64 into projectcalico:master Jul 26, 2017
@mgleung mgleung deleted the id_string branch July 26, 2017 23:30
song-jiang pushed a commit to song-jiang/libcalico-go that referenced this pull request Jul 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants