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

cmd: get ep with labels, pod / container name, ID #1139

Merged
merged 2 commits into from
Jul 20, 2017

Conversation

ianvernon
Copy link
Member

@ianvernon ianvernon commented Jul 18, 2017

cmd: get ep with labels, pod / container name, ID

  • daemon: add handlers for new /endpoints API, update endpoint structures with container ID / pod name
  • pkg/endpoint: support new API models
  • pkg/endpointmanager: add pod name, container name entries as keys mapping to endpoints
  • pkg/labels: change Orchestration labels -> OrchestrationIdentity labels (labels used in computing a security identity) and OrchrestationInfo labels (labels not used in computing a security identity)

cilium endpoint get now supports the following:

  • cilium endpoint get -l <set of labels>
  • cilium endpoint get <eID, pod name, container name, etc.>

api: add pod / container name, labels for GET ep

  • add new fields to Endpoint (pod-name, container-name)
  • GET /endpoint now takes an optional array of labels to return a list of endpoints whose labels match the provided array.

Signed-off by: Ian Vernon ian@covalent.io

return mutable
}

func (bo *BoolOptions) GetImmutableModel() models.ConfigurationMap {

Choose a reason for hiding this comment

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

exported method BoolOptions.GetImmutableModel should have comment or be unexported

@@ -121,6 +121,25 @@ type BoolOptions struct {
Library *OptionLibrary `json:"-"`
}

func (bo *BoolOptions) GetMutableModel() models.ConfigurationMap {

Choose a reason for hiding this comment

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

exported method BoolOptions.GetMutableModel should have comment or be unexported

@@ -180,6 +218,16 @@ func LookupLocked(id string) (*endpoint.Endpoint, error) {
case endpoint.DockerEndpointPrefix:
return lookupDockerEndpointLocked(eid), nil

case endpoint.ContainerNamePrefix:
log.Warningf("looking up container name: %s", eid)
for k, _ := range endpointsAux {

Choose a reason for hiding this comment

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

should omit 2nd value from range; this loop is equivalent to for k := range ...

if ep, ok := endpointsAux[endpoint.NewID(endpoint.ContainerIdPrefix, id)]; ok {
return ep
}


for k, _ := range endpointsAux {

Choose a reason for hiding this comment

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

should omit 2nd value from range; this loop is equivalent to for k := range ...

d *Daemon
}

func NewGetEndpointsHandler(d *Daemon) GetEndpointsHandler {

Choose a reason for hiding this comment

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

exported function NewGetEndpointsHandler should have comment or be unexported

)

//"github.com/cilium/cilium/pkg/nodeaddress"

Choose a reason for hiding this comment

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

comment on exported method Daemon.SetEndpointIdentity should be of the form "SetEndpointIdentity ..."

if ep, ok := endpointsAux[endpoint.NewID(endpoint.ContainerIdPrefix, id)]; ok {
return ep
}

for k, _ := range endpointsAux {

Choose a reason for hiding this comment

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

should omit 2nd value from range; this loop is equivalent to for k := range ...

)

//"github.com/cilium/cilium/pkg/nodeaddress"

Choose a reason for hiding this comment

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

comment on exported method Daemon.SetEndpointIdentity should be of the form "SetEndpointIdentity ..."

)

//"github.com/cilium/cilium/pkg/nodeaddress"

Choose a reason for hiding this comment

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

comment on exported method Daemon.SetEndpointIdentity should be of the form "SetEndpointIdentity ..."

)

//"github.com/cilium/cilium/pkg/nodeaddress"

Choose a reason for hiding this comment

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

comment on exported method Daemon.SetEndpointIdentity should be of the form "SetEndpointIdentity ..."

return mutable
}

// GetMutableModel returns immutable configuration options as a ConfigurationMap model.

Choose a reason for hiding this comment

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

comment on exported method BoolOptions.GetImmutableModel should be of the form "GetImmutableModel ..."

d *Daemon
}

// NewGetEndpoints handler returns a new getEndpoints populated with provided Daemon.

Choose a reason for hiding this comment

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

comment on exported function NewGetEndpointsHandler should be of the form "NewGetEndpointsHandler ..."

return mutable
}

// GetMutableModel returns immutable configuration options as a ConfigurationMap model.

Choose a reason for hiding this comment

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

comment on exported method BoolOptions.GetImmutableModel should be of the form "GetImmutableModel ..."

d *Daemon
}

// NewGetEndpoints handler returns a new getEndpoints populated with provided Daemon.

Choose a reason for hiding this comment

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

comment on exported function NewGetEndpointsHandler should be of the form "NewGetEndpointsHandler ..."

return mutable
}

// GetMutableModel returns immutable configuration options as a ConfigurationMap model.

Choose a reason for hiding this comment

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

comment on exported method BoolOptions.GetImmutableModel should be of the form "GetImmutableModel ..."

d *Daemon
}

// NewGetEndpoints handler returns a new getEndpoints populated with provided Daemon.

Choose a reason for hiding this comment

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

comment on exported function NewGetEndpointsHandler should be of the form "NewGetEndpointsHandler ..."

return mutable
}

// GetMutableModel returns immutable configuration options as a ConfigurationMap model.

Choose a reason for hiding this comment

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

comment on exported method BoolOptions.GetImmutableModel should be of the form "GetImmutableModel ..."

d *Daemon
}

// NewGetEndpoints handler returns a new getEndpoints populated with provided Daemon.

Choose a reason for hiding this comment

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

comment on exported function NewGetEndpointsHandler should be of the form "NewGetEndpointsHandler ..."

return mutable
}

// GetMutableModel returns immutable configuration options as a ConfigurationMap model.

Choose a reason for hiding this comment

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

comment on exported method BoolOptions.GetImmutableModel should be of the form "GetImmutableModel ..."

d *Daemon
}

// NewGetEndpoints handler returns a new getEndpoints populated with provided Daemon.

Choose a reason for hiding this comment

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

comment on exported function NewGetEndpointsHandler should be of the form "NewGetEndpointsHandler ..."

@ianvernon ianvernon force-pushed the 675-endpoint-get-enhancements branch 2 times, most recently from f8293f8 to c54bfbf Compare July 18, 2017 23:38
@ianvernon ianvernon changed the title WIP - 675 endpoint get enhancements cmd: get ep with labels, pod / container name, ID Jul 18, 2017
Copy link
Member

@tgraf tgraf left a comment

Choose a reason for hiding this comment

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

This will be awesome!!! 👍

@@ -249,6 +255,21 @@ func (d *Daemon) handleCreateContainer(id string, retry bool) {
}
}

if ep != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Just move this to below the ep ==nil condition so you don't have to check for ep != nil

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

if d.conf.IsK8sEnabled() {
if dockerContainer.Config != nil {
podNamespace := k8sDockerLbls.GetPodNamespace(dockerContainer.Config.Labels)
podName := k8sDockerLbls.GetPodName(dockerContainer.Config.Labels)
Copy link
Member

Choose a reason for hiding this comment

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

Does this trigger another API call to the API server? If so, can we restructure the code to retrieve this when we already fetch them via getFilteredLabels()?

Copy link
Member Author

Choose a reason for hiding this comment

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

These calls to GetPodNamespace and GetPodName are lookups into the map dockerContainer.Config.Labels for io.kubernetes.pod.name and io.kubernetes.pod.namespace - so they are not calls to the API server.

args.Add("label", v)
}

containers, err := h.d.dockerClient.ContainerList(ctx.Background(), types.ContainerListOptions{
Copy link
Member

Choose a reason for hiding this comment

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

This is not a good idea. This will potentially block this API call for a long time. We should fetch and store the docker labels with the endpoint when we receive the create event and then just look for local endpoints which match the labels.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

State string
ID uint16 // Endpoint ID.
Mutex sync.RWMutex // Protects all variables from this structure below this line
DockerContainerName string // Docker container name.
Copy link
Member

Choose a reason for hiding this comment

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

Can we call this ContainerName? We will rename the DockerID to ContainerID as well later.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@@ -47,6 +49,9 @@ func NewID(prefix PrefixType, id string) string {
func SplitID(id string) (PrefixType, string) {
if s := strings.Split(id, ":"); len(s) == 2 {
return PrefixType(s[0]), s[1]
} else if len(s) == 3 {
// PodNamePrefix case.
Copy link
Member

Choose a reason for hiding this comment

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

Can you add an example here on how the id looks like to make it clear?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@@ -224,6 +224,27 @@ paths:
x-go-name: Failed
schema:
"$ref": "#/definitions/Error"
"/endpoints/":
Copy link
Member

Choose a reason for hiding this comment

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

Why not extending the /endpoint/ on line 170?

Copy link
Member Author

Choose a reason for hiding this comment

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

/endpoint/ returns one endpoint, while /endpoints/ returns an array of endpoints. I'm open to moving this under /endpoint/, though and changing /endpoint/ to return a list of endpoints as well.

Copy link
Member

Choose a reason for hiding this comment

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

   "/endpoint":
     get:
       summary: Get list of all endpoints
       description: |
         Returns an array of all local endpoints.

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right :) I've made this change as part of my most recent commit.

if len(lbls) > 0 {
params := endpointApi.NewGetEndpointsParams().WithLabels(lbls)
if e, err := client.Endpoint.GetEndpoints(params); err != nil {
Fatalf("Cannot get endpoint for given list of labels %s: %s\n", lbls, err)
Copy link
Member

Choose a reason for hiding this comment

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

"endpoint", as a single endpoint? We can have multiple endpoints using the same list of labels.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

"github.com/go-openapi/runtime/middleware"
ctx "golang.org/x/net/context"
Copy link
Member

Choose a reason for hiding this comment

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

can't you replace with ctx "context"?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

}

containers, err := h.d.dockerClient.ContainerList(ctx.Background(), types.ContainerListOptions{
Size: true,
Copy link
Member

Choose a reason for hiding this comment

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

Do you need the Size of the containers?

Copy link
Member Author

Choose a reason for hiding this comment

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

I have removed interacting with the Docker client in my most recent upload.


containers, err := h.d.dockerClient.ContainerList(ctx.Background(), types.ContainerListOptions{
Size: true,
All: true,
Copy link
Member

Choose a reason for hiding this comment

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

All includes dead containers, not sure if we need it as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have removed interacting with the Docker client in my most recent upload.

@@ -75,6 +75,8 @@ func configDaemon(cmd *cobra.Command, opts []string) {

Copy link
Member

Choose a reason for hiding this comment

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

Why part of the cilium config command? I think it should make more sense as part of the cilium status on the K8s api server

Copy link
Member Author

Choose a reason for hiding this comment

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

I had added this change initially as part of this commit, but ended up not needing it. Figured it might be useful if we added more visibility into what the configuration of Cilium is as part of the API, but I'm open to removing it as part of this patch. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed this in most recent upload.

if !found {
log.Warningf("HasLabels: label %v not found, returning false", v)
return false
} else {

Choose a reason for hiding this comment

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

if block ends with a return statement, so drop this else and outdent its block

@ianvernon ianvernon requested review from tgraf and aanm July 19, 2017 23:52
endpointmanager.Mutex.RUnlock()
wg.Wait()
return NewGetEndpointOK().WithPayload(eps)
} else {

Choose a reason for hiding this comment

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

if block ends with a return statement, so drop this else and outdent its block

@ianvernon ianvernon force-pushed the 675-endpoint-get-enhancements branch from bd5370f to fc41fa4 Compare July 20, 2017 00:59
* add new fields to Endpoint (pod-name, container-name)
* GET /endpoint now takes an optional array of labels to return a list of endpoints whose labels match the provided array.

Signed-off by: Ian Vernon <ian@covalent.io>
@ianvernon ianvernon force-pushed the 675-endpoint-get-enhancements branch 3 times, most recently from 6dcea37 to 8b59d43 Compare July 20, 2017 02:50
* daemon: add handlers for new /endpoints API, update endpoint structures with container ID / pod name
* pkg/endpoint: support new API models
* pkg/endpointmanager: add pod name, container name entries as keys mapping to endpoints
* pkg/labels: change Orchestration labels -> OrchestrationIdentity labels (labels used in computing a security identity) and OrchrestationInfo labels (labels not used in computing a security identity)

`cilium endpoint get` now supports the following:
* `cilium endpoint get -l <set of labels>`
* `cilium endpoint get <eID, pod name, container name, etc.>`

Signed-off by: Ian Vernon <ian@covalent.io>
@ianvernon ianvernon force-pushed the 675-endpoint-get-enhancements branch from 8b59d43 to df64441 Compare July 20, 2017 02:59
@tgraf tgraf merged commit d9a265e into master Jul 20, 2017
@tgraf tgraf deleted the 675-endpoint-get-enhancements branch July 20, 2017 03:53
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.

4 participants