@@ -17,6 +17,8 @@ limitations under the License.
1717package integration
1818
1919import (
20+ "context"
21+ "encoding/json"
2022 "flag"
2123 "fmt"
2224 "os/exec"
@@ -27,12 +29,15 @@ import (
2729 "github.com/containerd/containerd"
2830 "github.com/pkg/errors"
2931 "github.com/sirupsen/logrus"
32+ "google.golang.org/grpc"
3033 "k8s.io/kubernetes/pkg/kubelet/apis/cri"
3134 runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
3235 "k8s.io/kubernetes/pkg/kubelet/remote"
36+ kubeletutil "k8s.io/kubernetes/pkg/kubelet/util"
3337
3438 api "github.com/containerd/cri/pkg/api/v1"
3539 "github.com/containerd/cri/pkg/client"
40+ criconfig "github.com/containerd/cri/pkg/config"
3641 "github.com/containerd/cri/pkg/constants"
3742 "github.com/containerd/cri/pkg/util"
3843)
@@ -97,6 +102,7 @@ func ConnectDaemons() error {
97102// Opts sets specific information in pod sandbox config.
98103type PodSandboxOpts func (* runtime.PodSandboxConfig )
99104
105+ // Set host network.
100106func WithHostNetwork (p * runtime.PodSandboxConfig ) {
101107 if p .Linux == nil {
102108 p .Linux = & runtime.LinuxPodSandboxConfig {}
@@ -111,6 +117,13 @@ func WithHostNetwork(p *runtime.PodSandboxConfig) {
111117 }
112118}
113119
120+ // Add pod log directory.
121+ func WithPodLogDirectory (dir string ) PodSandboxOpts {
122+ return func (p * runtime.PodSandboxConfig ) {
123+ p .LogDirectory = dir
124+ }
125+ }
126+
114127// PodSandboxConfig generates a pod sandbox config for test.
115128func PodSandboxConfig (name , ns string , opts ... PodSandboxOpts ) * runtime.PodSandboxConfig {
116129 config := & runtime.PodSandboxConfig {
@@ -134,52 +147,59 @@ func PodSandboxConfig(name, ns string, opts ...PodSandboxOpts) *runtime.PodSandb
134147type ContainerOpts func (* runtime.ContainerConfig )
135148
136149func WithTestLabels () ContainerOpts {
137- return func (cf * runtime.ContainerConfig ) {
138- cf .Labels = map [string ]string {"key" : "value" }
150+ return func (c * runtime.ContainerConfig ) {
151+ c .Labels = map [string ]string {"key" : "value" }
139152 }
140153}
141154
142155func WithTestAnnotations () ContainerOpts {
143- return func (cf * runtime.ContainerConfig ) {
144- cf .Annotations = map [string ]string {"a.b.c" : "test" }
156+ return func (c * runtime.ContainerConfig ) {
157+ c .Annotations = map [string ]string {"a.b.c" : "test" }
145158 }
146159}
147160
148161// Add container resource limits.
149162func WithResources (r * runtime.LinuxContainerResources ) ContainerOpts {
150- return func (cf * runtime.ContainerConfig ) {
151- if cf .Linux == nil {
152- cf .Linux = & runtime.LinuxContainerConfig {}
163+ return func (c * runtime.ContainerConfig ) {
164+ if c .Linux == nil {
165+ c .Linux = & runtime.LinuxContainerConfig {}
153166 }
154- cf .Linux .Resources = r
167+ c .Linux .Resources = r
155168 }
156169}
157170
158171// Add container command.
159- func WithCommand (c string , args ... string ) ContainerOpts {
160- return func (cf * runtime.ContainerConfig ) {
161- cf .Command = []string {c }
162- cf .Args = args
172+ func WithCommand (cmd string , args ... string ) ContainerOpts {
173+ return func (c * runtime.ContainerConfig ) {
174+ c .Command = []string {cmd }
175+ c .Args = args
163176 }
164177}
165178
166179// Add pid namespace mode.
167180func WithPidNamespace (mode runtime.NamespaceMode ) ContainerOpts {
168- return func (cf * runtime.ContainerConfig ) {
169- if cf .Linux == nil {
170- cf .Linux = & runtime.LinuxContainerConfig {}
181+ return func (c * runtime.ContainerConfig ) {
182+ if c .Linux == nil {
183+ c .Linux = & runtime.LinuxContainerConfig {}
171184 }
172- if cf .Linux .SecurityContext == nil {
173- cf .Linux .SecurityContext = & runtime.LinuxContainerSecurityContext {}
185+ if c .Linux .SecurityContext == nil {
186+ c .Linux .SecurityContext = & runtime.LinuxContainerSecurityContext {}
174187 }
175- if cf .Linux .SecurityContext .NamespaceOptions == nil {
176- cf .Linux .SecurityContext .NamespaceOptions = & runtime.NamespaceOption {}
188+ if c .Linux .SecurityContext .NamespaceOptions == nil {
189+ c .Linux .SecurityContext .NamespaceOptions = & runtime.NamespaceOption {}
177190 }
178- cf .Linux .SecurityContext .NamespaceOptions .Pid = mode
191+ c .Linux .SecurityContext .NamespaceOptions .Pid = mode
179192 }
180193
181194}
182195
196+ // Add container log path.
197+ func WithLogPath (path string ) ContainerOpts {
198+ return func (c * runtime.ContainerConfig ) {
199+ c .LogPath = path
200+ }
201+ }
202+
183203// ContainerConfig creates a container config given a name and image name
184204// and additional container config options
185205func ContainerConfig (name , image string , opts ... ContainerOpts ) * runtime.ContainerConfig {
@@ -244,3 +264,27 @@ func PidOf(name string) (int, error) {
244264 }
245265 return strconv .Atoi (output )
246266}
267+
268+ // CRIConfig gets current cri config from containerd.
269+ func CRIConfig () (* criconfig.Config , error ) {
270+ addr , dialer , err := kubeletutil .GetAddressAndDialer (* criEndpoint )
271+ if err != nil {
272+ return nil , errors .Wrap (err , "failed to get dialer" )
273+ }
274+ ctx , cancel := context .WithTimeout (context .Background (), timeout )
275+ defer cancel ()
276+ conn , err := grpc .DialContext (ctx , addr , grpc .WithInsecure (), grpc .WithDialer (dialer ))
277+ if err != nil {
278+ return nil , errors .Wrap (err , "failed to connect cri endpoint" )
279+ }
280+ client := runtime .NewRuntimeServiceClient (conn )
281+ resp , err := client .Status (ctx , & runtime.StatusRequest {Verbose : true })
282+ if err != nil {
283+ return nil , errors .Wrap (err , "failed to get status" )
284+ }
285+ config := & criconfig.Config {}
286+ if err := json .Unmarshal ([]byte (resp .Info ["config" ]), config ); err != nil {
287+ return nil , errors .Wrap (err , "failed to unmarshal config" )
288+ }
289+ return config , nil
290+ }
0 commit comments