@@ -18,6 +18,7 @@ package integration
1818
1919import (
2020 "context"
21+ "encoding/json"
2122 "flag"
2223 "fmt"
2324 "os/exec"
@@ -28,12 +29,15 @@ import (
2829 "github.com/containerd/containerd"
2930 "github.com/pkg/errors"
3031 "github.com/sirupsen/logrus"
32+ "google.golang.org/grpc"
3133 "k8s.io/kubernetes/pkg/kubelet/apis/cri"
3234 runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
3335 "k8s.io/kubernetes/pkg/kubelet/remote"
36+ kubeletutil "k8s.io/kubernetes/pkg/kubelet/util"
3437
3538 api "github.com/containerd/cri/pkg/api/v1"
3639 "github.com/containerd/cri/pkg/client"
40+ criconfig "github.com/containerd/cri/pkg/config"
3741 "github.com/containerd/cri/pkg/constants"
3842 "github.com/containerd/cri/pkg/util"
3943)
@@ -100,6 +104,7 @@ func ConnectDaemons() error {
100104// Opts sets specific information in pod sandbox config.
101105type PodSandboxOpts func (* runtime.PodSandboxConfig )
102106
107+ // Set host network.
103108func WithHostNetwork (p * runtime.PodSandboxConfig ) {
104109 if p .Linux == nil {
105110 p .Linux = & runtime.LinuxPodSandboxConfig {}
@@ -114,6 +119,13 @@ func WithHostNetwork(p *runtime.PodSandboxConfig) {
114119 }
115120}
116121
122+ // Add pod log directory.
123+ func WithPodLogDirectory (dir string ) PodSandboxOpts {
124+ return func (p * runtime.PodSandboxConfig ) {
125+ p .LogDirectory = dir
126+ }
127+ }
128+
117129// PodSandboxConfig generates a pod sandbox config for test.
118130func PodSandboxConfig (name , ns string , opts ... PodSandboxOpts ) * runtime.PodSandboxConfig {
119131 config := & runtime.PodSandboxConfig {
@@ -137,52 +149,59 @@ func PodSandboxConfig(name, ns string, opts ...PodSandboxOpts) *runtime.PodSandb
137149type ContainerOpts func (* runtime.ContainerConfig )
138150
139151func WithTestLabels () ContainerOpts {
140- return func (cf * runtime.ContainerConfig ) {
141- cf .Labels = map [string ]string {"key" : "value" }
152+ return func (c * runtime.ContainerConfig ) {
153+ c .Labels = map [string ]string {"key" : "value" }
142154 }
143155}
144156
145157func WithTestAnnotations () ContainerOpts {
146- return func (cf * runtime.ContainerConfig ) {
147- cf .Annotations = map [string ]string {"a.b.c" : "test" }
158+ return func (c * runtime.ContainerConfig ) {
159+ c .Annotations = map [string ]string {"a.b.c" : "test" }
148160 }
149161}
150162
151163// Add container resource limits.
152164func WithResources (r * runtime.LinuxContainerResources ) ContainerOpts {
153- return func (cf * runtime.ContainerConfig ) {
154- if cf .Linux == nil {
155- cf .Linux = & runtime.LinuxContainerConfig {}
165+ return func (c * runtime.ContainerConfig ) {
166+ if c .Linux == nil {
167+ c .Linux = & runtime.LinuxContainerConfig {}
156168 }
157- cf .Linux .Resources = r
169+ c .Linux .Resources = r
158170 }
159171}
160172
161173// Add container command.
162- func WithCommand (c string , args ... string ) ContainerOpts {
163- return func (cf * runtime.ContainerConfig ) {
164- cf .Command = []string {c }
165- cf .Args = args
174+ func WithCommand (cmd string , args ... string ) ContainerOpts {
175+ return func (c * runtime.ContainerConfig ) {
176+ c .Command = []string {cmd }
177+ c .Args = args
166178 }
167179}
168180
169181// Add pid namespace mode.
170182func WithPidNamespace (mode runtime.NamespaceMode ) ContainerOpts {
171- return func (cf * runtime.ContainerConfig ) {
172- if cf .Linux == nil {
173- cf .Linux = & runtime.LinuxContainerConfig {}
183+ return func (c * runtime.ContainerConfig ) {
184+ if c .Linux == nil {
185+ c .Linux = & runtime.LinuxContainerConfig {}
174186 }
175- if cf .Linux .SecurityContext == nil {
176- cf .Linux .SecurityContext = & runtime.LinuxContainerSecurityContext {}
187+ if c .Linux .SecurityContext == nil {
188+ c .Linux .SecurityContext = & runtime.LinuxContainerSecurityContext {}
177189 }
178- if cf .Linux .SecurityContext .NamespaceOptions == nil {
179- cf .Linux .SecurityContext .NamespaceOptions = & runtime.NamespaceOption {}
190+ if c .Linux .SecurityContext .NamespaceOptions == nil {
191+ c .Linux .SecurityContext .NamespaceOptions = & runtime.NamespaceOption {}
180192 }
181- cf .Linux .SecurityContext .NamespaceOptions .Pid = mode
193+ c .Linux .SecurityContext .NamespaceOptions .Pid = mode
182194 }
183195
184196}
185197
198+ // Add container log path.
199+ func WithLogPath (path string ) ContainerOpts {
200+ return func (c * runtime.ContainerConfig ) {
201+ c .LogPath = path
202+ }
203+ }
204+
186205// ContainerConfig creates a container config given a name and image name
187206// and additional container config options
188207func ContainerConfig (name , image string , opts ... ContainerOpts ) * runtime.ContainerConfig {
@@ -247,3 +266,27 @@ func PidOf(name string) (int, error) {
247266 }
248267 return strconv .Atoi (output )
249268}
269+
270+ // CRIConfig gets current cri config from containerd.
271+ func CRIConfig () (* criconfig.Config , error ) {
272+ addr , dialer , err := kubeletutil .GetAddressAndDialer (* criEndpoint )
273+ if err != nil {
274+ return nil , errors .Wrap (err , "failed to get dialer" )
275+ }
276+ ctx , cancel := context .WithTimeout (context .Background (), timeout )
277+ defer cancel ()
278+ conn , err := grpc .DialContext (ctx , addr , grpc .WithInsecure (), grpc .WithDialer (dialer ))
279+ if err != nil {
280+ return nil , errors .Wrap (err , "failed to connect cri endpoint" )
281+ }
282+ client := runtime .NewRuntimeServiceClient (conn )
283+ resp , err := client .Status (ctx , & runtime.StatusRequest {Verbose : true })
284+ if err != nil {
285+ return nil , errors .Wrap (err , "failed to get status" )
286+ }
287+ config := & criconfig.Config {}
288+ if err := json .Unmarshal ([]byte (resp .Info ["config" ]), config ); err != nil {
289+ return nil , errors .Wrap (err , "failed to unmarshal config" )
290+ }
291+ return config , nil
292+ }
0 commit comments