forked from aws/amazon-vpc-cni-k8s
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "fix: get container ID from kube rather than docker (aws#371)"
This reverts commit 14de538.
- Loading branch information
1 parent
7eab704
commit de8e10f
Showing
8 changed files
with
228 additions
and
9 deletions.
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
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
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
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,91 @@ | ||
package docker | ||
|
||
import ( | ||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/client" | ||
"golang.org/x/net/context" | ||
|
||
"github.com/pkg/errors" | ||
|
||
log "github.com/cihub/seelog" | ||
) | ||
|
||
// ContainerInfo provides container information | ||
type ContainerInfo struct { | ||
ID string | ||
Name string | ||
K8SUID string | ||
} | ||
|
||
// APIs provides Docker API | ||
type APIs interface { | ||
GetRunningContainers() (map[string]*ContainerInfo, error) | ||
} | ||
|
||
type Client struct{} | ||
|
||
func New() *Client { | ||
return &Client{} | ||
} | ||
|
||
func (c *Client) GetRunningContainers() (map[string]*ContainerInfo, error) { | ||
var containerInfos = make(map[string]*ContainerInfo) | ||
|
||
cli, err := client.NewEnvClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer cli.Close() | ||
|
||
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, container := range containers { | ||
log.Infof("GetRunningContainers: Discovered running docker: %s %s %s State: %s Status: %s ", | ||
container.ID, container.Names[0], container.Labels["io.kubernetes.pod.uid"], container.State, container.Status) | ||
|
||
containerType, ok := container.Labels["io.kubernetes.docker.type"] | ||
if !ok { | ||
log.Infof("GetRunningContainers: skip non pause container") | ||
continue | ||
} | ||
|
||
if containerType != "podsandbox" { | ||
log.Infof("GetRunningContainers: skip container type: %s", containerType) | ||
continue | ||
} | ||
|
||
log.Debugf("GetRunningContainers: containerType %s", containerType) | ||
|
||
if container.State != "running" { | ||
log.Infof("GetRunningContainers: skip container who is not running") | ||
continue | ||
} | ||
|
||
uid := container.Labels["io.kubernetes.pod.uid"] | ||
_, ok = containerInfos[uid] | ||
if !ok { | ||
containerInfos[uid] = &ContainerInfo{ | ||
ID: container.ID, | ||
Name: container.Names[0], | ||
K8SUID: uid} | ||
continue | ||
} | ||
|
||
if container.Names[0] != containerInfos[uid].Name { | ||
log.Infof("GetRunningContainers: same uid matched by container:%s, %s container id %s", | ||
containerInfos[uid].Name, container.Names[0], container.ID) | ||
continue | ||
} | ||
|
||
if container.Names[0] == containerInfos[uid].Name { | ||
log.Errorf("GetRunningContainers: Conflict container id %s for container %s", | ||
container.ID, containerInfos[uid].Name) | ||
return nil, errors.New("conflict docker runtime info") | ||
} | ||
} | ||
|
||
return containerInfos, nil | ||
} |
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,16 @@ | ||
// Copyright 2017 Amazon.com, Inc. or its affiliates. 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. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file 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 docker | ||
|
||
//go:generate go run ../../scripts/mockgen.go github.com/aws/amazon-vpc-cni-k8s/pkg/docker APIs mocks/docker_mocks.go |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.