@@ -16,6 +16,7 @@ import (
16
16
"github.com/pkg/errors"
17
17
"github.com/rancher/k3s/pkg/daemons/config"
18
18
"github.com/rancher/k3s/pkg/daemons/control"
19
+ "github.com/rancher/k3s/pkg/datadir"
19
20
"github.com/rancher/k3s/pkg/deploy"
20
21
"github.com/rancher/k3s/pkg/helm"
21
22
"github.com/rancher/k3s/pkg/servicelb"
@@ -147,8 +148,19 @@ func startNorman(ctx context.Context, config *Config) (string, error) {
147
148
}
148
149
}
149
150
150
- func HomeKubeConfig () (string , error ) {
151
- return resolvehome .Resolve ("${HOME}/.kube/k3s.yaml" )
151
+ func HomeKubeConfig (write bool ) (string , error ) {
152
+ if write {
153
+ if os .Getuid () == 0 {
154
+ return datadir .GlobalConfig , nil
155
+ }
156
+ return resolvehome .Resolve (datadir .HomeConfig )
157
+ }
158
+
159
+ if _ , err := os .Stat (datadir .GlobalConfig ); err == nil {
160
+ return datadir .GlobalConfig , nil
161
+ }
162
+
163
+ return resolvehome .Resolve (datadir .HomeConfig )
152
164
}
153
165
154
166
func printTokens (certs , advertiseIP string , tlsConfig * dynamiclistener.UserConfig , config * config.Control ) {
@@ -177,7 +189,7 @@ func printTokens(certs, advertiseIP string, tlsConfig *dynamiclistener.UserConfi
177
189
func writeKubeConfig (certs string , tlsConfig * dynamiclistener.UserConfig , config * config.Control ) {
178
190
clientToken := FormatToken (config .Runtime .ClientToken , certs )
179
191
url := fmt .Sprintf ("https://localhost:%d" , tlsConfig .HTTPSPort )
180
- kubeConfig , err := HomeKubeConfig ()
192
+ kubeConfig , err := HomeKubeConfig (true )
181
193
def := true
182
194
if err != nil {
183
195
kubeConfig = filepath .Join (config .DataDir , "kubeconfig-k3s.yaml" )
@@ -199,6 +211,8 @@ func writeKubeConfig(certs string, tlsConfig *dynamiclistener.UserConfig, config
199
211
} else {
200
212
logrus .Errorf ("failed to set %s to mode %s: %v" , kubeConfig , os .FileMode (mode ), err )
201
213
}
214
+ } else {
215
+ os .Chmod (kubeConfig , os .FileMode (0644 ))
202
216
}
203
217
204
218
logrus .Infof ("Wrote kubeconfig %s" , kubeConfig )
0 commit comments