7
7
"regexp"
8
8
"strings"
9
9
10
- "github.com/golang/glog"
11
-
10
+ kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
12
11
kexec "k8s.io/utils/exec"
13
12
)
14
13
@@ -27,8 +26,8 @@ type Runtime struct {
27
26
Endpoint string
28
27
}
29
28
30
- func GetRuntime () (* Runtime , error ) {
31
- runtimeName , runtimeType , runtimeEndpoint , err := getDefaultRuntimeEndpoint ( )
29
+ func GetRuntime (kubeClient kclientset. Interface ) (* Runtime , error ) {
30
+ runtimeName , runtimeType , runtimeEndpoint , err := getRuntimeEndpoint ( kubeClient )
32
31
if err != nil {
33
32
return nil , err
34
33
}
@@ -114,42 +113,44 @@ func (r *Runtime) GetRuntimeVersion() (string, error) {
114
113
return versionInfo , nil
115
114
}
116
115
117
- func getDefaultRuntimeEndpoint ( ) (string , string , string , error ) {
118
- isCRIO , err := filePathExists ( defaultCRIOShimSocket )
116
+ func getRuntimeEndpoint ( kubeClient kclientset. Interface ) (string , string , string , error ) {
117
+ nodes , err := GetNodes ( kubeClient )
119
118
if err != nil {
120
119
return "" , "" , "" , err
121
120
}
122
-
123
- isDocker , err := filePathExists (defaultDockerShimSocket )
124
- if err != nil {
125
- return "" , "" , "" , err
121
+ if len (nodes ) == 0 {
122
+ return "" , "" , "" , fmt .Errorf ("no nodes found to detect the runtime" )
126
123
}
127
124
128
- // TODO: Instead of trying to detect the runtime make this as config option
129
- if isDocker && isCRIO {
130
- glog .Warningf ("Found both crio and docker socket files, defaulting to crio" )
131
- return crioRuntimeName , crioRuntimeType , defaultCRIOShimSocket , nil
132
- } else if isCRIO {
133
- return crioRuntimeName , crioRuntimeType , defaultCRIOShimSocket , nil
134
- } else if isDocker {
135
- return dockerRuntimeName , dockerRuntimeType , defaultDockerShimSocket , nil
125
+ for _ , node := range nodes {
126
+ if len (node .Status .NodeInfo .ContainerRuntimeVersion ) > 0 {
127
+ runtimeTokens := strings .Split (node .Status .NodeInfo .ContainerRuntimeVersion , "://" )
128
+ switch runtimeTokens [0 ] {
129
+ case crioRuntimeType :
130
+ if err := filePathExists (defaultCRIOShimSocket ); err != nil {
131
+ return "" , "" , "" , fmt .Errorf ("detected crio runtime but validation of socket file %q failed: %v" , defaultCRIOShimSocket , err )
132
+ }
133
+ return crioRuntimeName , crioRuntimeType , defaultCRIOShimSocket , nil
134
+ case dockerRuntimeType :
135
+ if err := filePathExists (defaultDockerShimSocket ); err != nil {
136
+ return "" , "" , "" , fmt .Errorf ("detected docker runtime but validation of socket file %q failed: %v" , defaultDockerShimSocket , err )
137
+ }
138
+ return dockerRuntimeName , dockerRuntimeType , defaultDockerShimSocket , nil
139
+ default :
140
+ return "" , "" , "" , fmt .Errorf ("runtime %q is not supported" , runtimeTokens [0 ])
141
+ }
142
+ }
136
143
}
137
144
138
- return "" , "" , "" , fmt .Errorf ("supported runtime socket files not found" )
145
+ return "" , "" , "" , fmt .Errorf ("supported runtime not found" )
139
146
}
140
147
141
- func filePathExists (endpoint string ) ( bool , error ) {
148
+ func filePathExists (endpoint string ) error {
142
149
u , err := url .Parse (endpoint )
143
150
if err != nil {
144
- return false , err
145
- }
146
-
147
- if _ , err := os .Stat (u .Path ); err != nil {
148
- if os .IsNotExist (err ) {
149
- return false , nil
150
- }
151
- return false , err
151
+ return err
152
152
}
153
153
154
- return true , nil
154
+ _ , err = os .Stat (u .Path )
155
+ return err
155
156
}
0 commit comments