容器标签使用小记

docker cli使用举例

1
2
3
4
5
docker ps -a --filter "label=org.hyperledger.fabric.chaincode.id.name=test-chaincode" --filter "label=org.hyperledger.fabric.chaincode.id.version=v0.0.1"

或者

docker ps -a -f "label=org.hyperledger.fabric.chaincode.id.name=test-chaincode" -f "label=org.hyperledger.fabric.chaincode.id.version=v0.0.1"

如下图所示:

docker_label

docker client go编码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
...
import (
"github.com/docker/docker/api/types"

"github.com/docker/docker/api/types/filters"
)
...
...
flagArgs := []string{
fmt.Sprintf("label=%s=%s", ChainCodeNameLabelKey, contractName), // "label=org.hyperledger.fabric.chaincode.id.name=wx-hy001"
fmt.Sprintf("label=%s=%s", ChainCodeVersionLabelKey, contractVersion), // "label=org.hyperledger.fabric.chaincode.id.version=1.2.0"
}
args := filters.NewArgs()
for i := range flagArgs {
args, err = filters.ParseFlag(flagArgs[i], args)
if err != nil {
glog.Error(err)
return contractChainCodes, err
}
}
containerList, err := cli.ContainerList(types.ContainerListOptions{
Filters: args,
})
if err != nil {
glog.Error(err)
return contractChainCodes, err
}
for j := 0; j < len(containerList); j++ {
container := containerList[j]
containerId := container.ID
containerName := container.Names[0][1 : ]
contractChainCode := module.ContractChainCode{
ContainerId: containerId,
ContainerName: containerName,
ContainerHost: hostIP,
}
contractChainCodes = append(contractChainCodes, contractChainCode)
}
...

如下图所示:

docker_label

kubernetes cli使用举例

1
2
3
4
5
kubectl get pod --all-namespaces -o wide -l baas.yonghui.cn/network-name=test-network

或者

kubectl get pod --all-namespaces -o wide -l baas.yonghui.cn/network-name=test-network,baas.yonghui.cn/network-component=peer

如下图所示:

kubernetes_label

kubernetes client go编码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
...
import (
module "yonghui.cn/blockchain/yhbkas/module/network"
pkglabels "k8s.io/apimachinery/pkg/labels"
)
...
...
networkNameKey := string(sc.NetworkNameKey)
labels := map[string]string{
networkNameKey: networkName,
}
selector := pkglabels.FormatLabels(labels)
opts := metav1.ListOptions{
LabelSelector: selector,
}

podList, err := clientSet.CoreV1().Pods(metav1.NamespaceAll).List(opts)
if err != nil {
return module.GetNetworkComponentsResponse{
CommonResponse: module.CommonResponse{
Status: http.StatusInternalServerError,
Message: "GetNetworkComponents unsuccessfully.",
},
NetworkComponents: networkComponents,
}
}

for i := 0; i < len(podList.Items); i++ {
pod := podList.Items[i]
podContainers := make([]module.PodContainer, 0)
containers := pod.Spec.Containers
for j := 0; j < len(containers); j++ {
container := containers[j]
podContainer := module.PodContainer{
Name: container.Name,
}
podContainers = append(podContainers, podContainer)
}
labels := pod.Labels
componentTypeKey := string(sc.NetworkComponentTypeKey)
componentTypeValue := labels[componentTypeKey]
networkComponent := module.NetworkComponent{
Id: string(pod.UID),
Name: pod.Name,
Namespace: pod.Namespace,
Containers: podContainers,
Role: componentTypeValue,
}
networkComponents = append(networkComponents, networkComponent)
}
...

如下图所示:

kubernetes_label