9
9
"net"
10
10
"net/http"
11
11
"os"
12
- "path"
12
+ "path/filepath "
13
13
"strings"
14
14
15
15
"github.com/golang/glog"
@@ -44,9 +44,12 @@ import (
44
44
// removed and re-created with 0700 permissions each time openshift-node is
45
45
// started.
46
46
47
- // Default CNIServer unix domain socket path which the OpenShift SDN CNI
48
- // plugin uses to talk to the CNIServer
49
- const CNIServerSocketPath string = "/var/run/openshift-sdn/cni-server.sock"
47
+ // Default directory for CNIServer runtime files
48
+ const CNIServerRunDir string = "/var/run/openshift-sdn"
49
+
50
+ // CNIServer socket name, and default full path
51
+ const CNIServerSocketName string = "cni-server.sock"
52
+ const CNIServerSocketPath string = CNIServerRunDir + "/" + CNIServerSocketName
50
53
51
54
// Explicit type for CNI commands the server handles
52
55
type CNICommand string
@@ -95,19 +98,18 @@ type cniRequestFunc func(request *PodRequest) ([]byte, error)
95
98
type CNIServer struct {
96
99
http.Server
97
100
requestFunc cniRequestFunc
98
- path string
101
+ rundir string
99
102
}
100
103
101
- // Create and return a new CNIServer object which will listen on the given
102
- // socket path
103
- func NewCNIServer (socketPath string ) * CNIServer {
104
+ // Create and return a new CNIServer object which will listen on a socket in the given path
105
+ func NewCNIServer (rundir string ) * CNIServer {
104
106
router := mux .NewRouter ()
105
107
106
108
s := & CNIServer {
107
109
Server : http.Server {
108
110
Handler : router ,
109
111
},
110
- path : socketPath ,
112
+ rundir : rundir ,
111
113
}
112
114
router .NotFoundHandler = http .HandlerFunc (http .NotFound )
113
115
router .HandleFunc ("/" , s .handleCNIRequest ).Methods ("POST" )
@@ -125,25 +127,25 @@ func (s *CNIServer) Start(requestFunc cniRequestFunc) error {
125
127
s .requestFunc = requestFunc
126
128
127
129
// Remove and re-create the socket directory with root-only permissions
128
- dirName := path .Dir (s .path )
129
- if err := os .RemoveAll (s .path ); err != nil && ! os .IsNotExist (err ) {
130
+ if err := os .RemoveAll (s .rundir ); err != nil && ! os .IsNotExist (err ) {
130
131
utilruntime .HandleError (fmt .Errorf ("failed to remove old pod info socket: %v" , err ))
131
132
}
132
- if err := os .RemoveAll (dirName ); err != nil && ! os .IsNotExist (err ) {
133
+ if err := os .RemoveAll (s . rundir ); err != nil && ! os .IsNotExist (err ) {
133
134
utilruntime .HandleError (fmt .Errorf ("failed to remove contents of socket directory: %v" , err ))
134
135
}
135
- if err := os .MkdirAll (dirName , 0700 ); err != nil {
136
+ if err := os .MkdirAll (s . rundir , 0700 ); err != nil {
136
137
return fmt .Errorf ("failed to create pod info socket directory: %v" , err )
137
138
}
138
139
139
140
// On Linux the socket is created with the permissions of the directory
140
141
// it is in, so as long as the directory is root-only we can avoid
141
142
// racy umask manipulation.
142
- l , err := net .Listen ("unix" , s .path )
143
+ socketPath := filepath .Join (s .rundir , CNIServerSocketName )
144
+ l , err := net .Listen ("unix" , socketPath )
143
145
if err != nil {
144
146
return fmt .Errorf ("failed to listen on pod info socket: %v" , err )
145
147
}
146
- if err := os .Chmod (s . path , 0600 ); err != nil {
148
+ if err := os .Chmod (socketPath , 0600 ); err != nil {
147
149
l .Close ()
148
150
return fmt .Errorf ("failed to set pod info socket mode: %v" , err )
149
151
}
0 commit comments