@@ -15,6 +15,7 @@ import (
1515
1616 "github.com/Microsoft/hcsshim/internal/guest/network"
1717 specGuest "github.com/Microsoft/hcsshim/internal/guest/spec"
18+ "github.com/Microsoft/hcsshim/internal/log"
1819 "github.com/Microsoft/hcsshim/internal/oc"
1920 "github.com/Microsoft/hcsshim/pkg/annotations"
2021)
@@ -23,22 +24,37 @@ func getSandboxHostnamePath(id string) string {
2324 return filepath .Join (specGuest .SandboxRootDir (id ), "hostname" )
2425}
2526
27+ func getVirtualPodAwareSandboxHostnamePath (id , virtualSandboxID string ) string {
28+ return filepath .Join (specGuest .VirtualPodAwareSandboxRootDir (id , virtualSandboxID ), "hostname" )
29+ }
30+
2631func getSandboxHostsPath (id string ) string {
2732 return filepath .Join (specGuest .SandboxRootDir (id ), "hosts" )
2833}
2934
35+ func getVirtualPodAwareSandboxHostsPath (id , virtualSandboxID string ) string {
36+ return filepath .Join (specGuest .VirtualPodAwareSandboxRootDir (id , virtualSandboxID ), "hosts" )
37+ }
38+
3039func getSandboxResolvPath (id string ) string {
3140 return filepath .Join (specGuest .SandboxRootDir (id ), "resolv.conf" )
3241}
3342
43+ func getVirtualPodAwareSandboxResolvPath (id , virtualSandboxID string ) string {
44+ return filepath .Join (specGuest .VirtualPodAwareSandboxRootDir (id , virtualSandboxID ), "resolv.conf" )
45+ }
46+
3447func setupSandboxContainerSpec (ctx context.Context , id string , spec * oci.Spec ) (err error ) {
3548 ctx , span := oc .StartSpan (ctx , "hcsv2::setupSandboxContainerSpec" )
3649 defer span .End ()
3750 defer func () { oc .SetSpanStatus (span , err ) }()
3851 span .AddAttributes (trace .StringAttribute ("cid" , id ))
3952
40- // Generate the sandbox root dir
41- rootDir := specGuest .SandboxRootDir (id )
53+ // Check if this is a virtual pod to use appropriate root directory
54+ virtualSandboxID := spec .Annotations [annotations .VirtualPodID ]
55+
56+ // Generate the sandbox root dir - virtual pod aware
57+ rootDir := specGuest .VirtualPodAwareSandboxRootDir (id , virtualSandboxID )
4258 if err := os .MkdirAll (rootDir , 0755 ); err != nil {
4359 return errors .Wrapf (err , "failed to create sandbox root directory %q" , rootDir )
4460 }
@@ -58,39 +74,55 @@ func setupSandboxContainerSpec(ctx context.Context, id string, spec *oci.Spec) (
5874 }
5975 }
6076
61- sandboxHostnamePath := getSandboxHostnamePath (id )
77+ sandboxHostnamePath := getVirtualPodAwareSandboxHostnamePath (id , virtualSandboxID )
6278 if err := os .WriteFile (sandboxHostnamePath , []byte (hostname + "\n " ), 0644 ); err != nil {
6379 return errors .Wrapf (err , "failed to write hostname to %q" , sandboxHostnamePath )
6480 }
6581
6682 // Write the hosts
6783 sandboxHostsContent := network .GenerateEtcHostsContent (ctx , hostname )
68- sandboxHostsPath := getSandboxHostsPath (id )
84+ sandboxHostsPath := getVirtualPodAwareSandboxHostsPath (id , virtualSandboxID )
6985 if err := os .WriteFile (sandboxHostsPath , []byte (sandboxHostsContent ), 0644 ); err != nil {
7086 return errors .Wrapf (err , "failed to write sandbox hosts to %q" , sandboxHostsPath )
7187 }
7288
89+ log .G (ctx ).Debug ("quick setup network namespace, cflick" )
90+ // Check if this is a virtual pod sandbox container by comparing container ID with virtual pod ID
91+ isVirtualPodSandbox := virtualSandboxID != "" && id == virtualSandboxID
92+ if strings .EqualFold (spec .Annotations [annotations .SkipPodNetworking ], "true" ) || isVirtualPodSandbox {
93+ ns := GetOrAddNetworkNamespace (specGuest .GetNetworkNamespaceID (spec ))
94+ err := ns .Sync (ctx )
95+ if err != nil {
96+ return err
97+ }
98+ }
7399 // Write resolv.conf
100+ log .G (ctx ).Debug ("sandbox resolv.conf, cflick" )
74101 ns , err := getNetworkNamespace (specGuest .GetNetworkNamespaceID (spec ))
75102 if err != nil {
76- return err
77- }
78- var searches , servers []string
79- for _ , n := range ns .Adapters () {
80- if len (n .DNSSuffix ) > 0 {
81- searches = network .MergeValues (searches , strings .Split (n .DNSSuffix , "," ))
103+ if ! strings .EqualFold (spec .Annotations [annotations .SkipPodNetworking ], "true" ) {
104+ return err
82105 }
83- if len (n .DNSServerList ) > 0 {
84- servers = network .MergeValues (servers , strings .Split (n .DNSServerList , "," ))
106+ // Networking is skipped, do not error out
107+ log .G (ctx ).Infof ("setupSandboxContainerSpec: Did not find NS spec %v, err %v" , spec , err )
108+ } else {
109+ var searches , servers []string
110+ for _ , n := range ns .Adapters () {
111+ if len (n .DNSSuffix ) > 0 {
112+ searches = network .MergeValues (searches , strings .Split (n .DNSSuffix , "," ))
113+ }
114+ if len (n .DNSServerList ) > 0 {
115+ servers = network .MergeValues (servers , strings .Split (n .DNSServerList , "," ))
116+ }
117+ }
118+ resolvContent , err := network .GenerateResolvConfContent (ctx , searches , servers , nil )
119+ if err != nil {
120+ return errors .Wrap (err , "failed to generate sandbox resolv.conf content" )
121+ }
122+ sandboxResolvPath := getVirtualPodAwareSandboxResolvPath (id , virtualSandboxID )
123+ if err := os .WriteFile (sandboxResolvPath , []byte (resolvContent ), 0644 ); err != nil {
124+ return errors .Wrap (err , "failed to write sandbox resolv.conf" )
85125 }
86- }
87- resolvContent , err := network .GenerateResolvConfContent (ctx , searches , servers , nil )
88- if err != nil {
89- return errors .Wrap (err , "failed to generate sandbox resolv.conf content" )
90- }
91- sandboxResolvPath := getSandboxResolvPath (id )
92- if err := os .WriteFile (sandboxResolvPath , []byte (resolvContent ), 0644 ); err != nil {
93- return errors .Wrap (err , "failed to write sandbox resolv.conf" )
94126 }
95127
96128 // User.Username is generally only used on Windows, but as there's no (easy/fast at least) way to grab
@@ -113,8 +145,14 @@ func setupSandboxContainerSpec(ctx context.Context, id string, spec *oci.Spec) (
113145 // also has a concept of a sandbox/shm file when the IPC NamespaceMode !=
114146 // NODE.
115147
116- // Force the parent cgroup into our /containers root
117- spec .Linux .CgroupsPath = "/containers/" + id
148+ // Set cgroup path - check if this is a virtual pod
149+ if virtualSandboxID != "" {
150+ // Virtual pod sandbox gets its own cgroup under /containers/virtual-pods using the virtual pod ID
151+ spec .Linux .CgroupsPath = "/containers/virtual-pods/" + virtualSandboxID
152+ } else {
153+ // Traditional sandbox goes under /containers
154+ spec .Linux .CgroupsPath = "/containers/" + id
155+ }
118156
119157 // Clear the windows section as we dont want to forward to runc
120158 spec .Windows = nil
0 commit comments