@@ -21,6 +21,7 @@ import (
21
21
"bytes"
22
22
"io"
23
23
"os"
24
+ "path/filepath"
24
25
25
26
"github.com/pkg/errors"
26
27
"github.com/spf13/pflag"
@@ -71,7 +72,7 @@ func enforceRequirements(flagSet *pflag.FlagSet, flags *applyPlanFlags, args []s
71
72
}
72
73
}
73
74
74
- client , err := getClient (flags .kubeConfigPath , * isDryRun )
75
+ client , err := getClient (flags .kubeConfigPath , * isDryRun , printer )
75
76
if err != nil {
76
77
return nil , nil , nil , nil , errors .Wrapf (err , "couldn't create a Kubernetes client from file %q" , flags .kubeConfigPath )
77
78
}
@@ -137,7 +138,7 @@ func enforceRequirements(flagSet *pflag.FlagSet, flags *applyPlanFlags, args []s
137
138
}
138
139
139
140
// Run healthchecks against the cluster
140
- if err := upgrade .CheckClusterHealth (client , & initCfg .ClusterConfiguration , ignorePreflightErrorsSet , printer ); err != nil {
141
+ if err := upgrade .CheckClusterHealth (client , & initCfg .ClusterConfiguration , ignorePreflightErrorsSet , dryRun , printer ); err != nil {
141
142
return nil , nil , nil , nil , errors .Wrap (err , "[upgrade/health] FATAL" )
142
143
}
143
144
@@ -189,32 +190,57 @@ func runPreflightChecks(client clientset.Interface, ignorePreflightErrors sets.S
189
190
}
190
191
191
192
// getClient gets a real or fake client depending on whether the user is dry-running or not
192
- func getClient (file string , dryRun bool ) (clientset.Interface , error ) {
193
+ func getClient (file string , dryRun bool , printer output. Printer ) (clientset.Interface , error ) {
193
194
if dryRun {
195
+ // Default the server version to the kubeadm version.
196
+ serverVersion := constants .CurrentKubernetesVersion .Info ()
197
+
194
198
dryRun := apiclient .NewDryRun ()
195
- if err := dryRun .WithKubeConfigFile (file ); err != nil {
196
- return nil , err
197
- }
198
199
dryRun .WithDefaultMarshalFunction ().
199
200
WithWriter (os .Stdout ).
200
201
PrependReactor (dryRun .HealthCheckJobReactor ()).
201
202
PrependReactor (dryRun .PatchNodeReactor ())
202
203
203
- // In order for fakeclient.Discovery().ServerVersion() to return the backing API Server's
204
- // real version; we have to do some clever API machinery tricks. First, we get the real
205
- // API Server's version.
206
- realServerVersion , err := dryRun .Client ().Discovery ().ServerVersion ()
207
- if err != nil {
208
- return nil , errors .Wrap (err , "failed to get server version" )
204
+ // If the kubeconfig exists, construct a real client from it and get the real serverVersion.
205
+ if _ , err := os .Stat (file ); err == nil {
206
+ _ , _ = printer .Printf ("[dryrun] Creating a real client from %q\n " , file )
207
+ if err := dryRun .WithKubeConfigFile (file ); err != nil {
208
+ return nil , err
209
+ }
210
+ serverVersion , err = dryRun .Client ().Discovery ().ServerVersion ()
211
+ if err != nil {
212
+ return nil , errors .Wrap (err , "failed to get server version" )
213
+ }
214
+ } else if os .IsNotExist (err ) {
215
+ // If the file (supposedly admin.conf) does not exist, add more reactors.
216
+ // Knowing the node name is required by the ListPodsReactor. For that we try to use
217
+ // the kubelet.conf client, if it exists. If not, it falls back to hostname.
218
+ _ , _ = printer .Printf ("[dryrun] Dryrunning without a real client\n " )
219
+ kubeconfigPath := filepath .Join (constants .KubernetesDir , constants .KubeletKubeConfigFileName )
220
+ nodeName , err := configutil .GetNodeName (kubeconfigPath )
221
+ if err != nil {
222
+ return nil , err
223
+ }
224
+ dryRun .PrependReactor (dryRun .GetKubeadmConfigReactor ()).
225
+ PrependReactor (dryRun .GetKubeletConfigReactor ()).
226
+ PrependReactor (dryRun .GetKubeProxyConfigReactor ()).
227
+ PrependReactor (dryRun .GetNodeReactor ()).
228
+ PrependReactor (dryRun .ListPodsReactor (nodeName )).
229
+ PrependReactor (dryRun .GetCoreDNSConfigReactor ()).
230
+ PrependReactor (dryRun .ListDeploymentsReactor ())
231
+ } else {
232
+ // Throw an error if the file exists but there was a different stat error.
233
+ return nil , errors .Wrapf (err , "could not create a client from %q" , file )
209
234
}
235
+
210
236
// Obtain the FakeDiscovery object for this fake client.
211
237
fakeClient := dryRun .FakeClient ()
212
238
fakeClientDiscovery , ok := fakeClient .Discovery ().(* fakediscovery.FakeDiscovery )
213
239
if ! ok {
214
240
return nil , errors .New ("could not set fake discovery's server version" )
215
241
}
216
- // Lastly, set the right server version to be used .
217
- fakeClientDiscovery .FakedServerVersion = realServerVersion
242
+ // Set the right server version for it .
243
+ fakeClientDiscovery .FakedServerVersion = serverVersion
218
244
219
245
return fakeClient , nil
220
246
}
0 commit comments