@@ -236,6 +236,10 @@ func (k kubeClientsets) MainClientset() (*kubernetes.Clientset, error) {
236236 return k .mainClientset , nil
237237 }
238238
239+ if err := checkConfigurationValid (k .configData ); err != nil {
240+ return nil , err
241+ }
242+
239243 if k .config != nil {
240244 kc , err := kubernetes .NewForConfig (k .config )
241245 if err != nil {
@@ -260,6 +264,52 @@ func (k kubeClientsets) AggregatorClientset() (*aggregator.Clientset, error) {
260264 return k .aggregatorClientset , nil
261265}
262266
267+ var apiTokenMountPath = "/var/run/secrets/kubernetes.io/serviceaccount"
268+
269+ func inCluster () bool {
270+ host , port := os .Getenv ("KUBERNETES_SERVICE_HOST" ), os .Getenv ("KUBERNETES_SERVICE_PORT" )
271+ if host == "" || port == "" {
272+ return false
273+ }
274+
275+ if _ , err := os .Stat (apiTokenMountPath ); err != nil {
276+ return false
277+ }
278+ return true
279+ }
280+
281+ var authDocumentationURL = "https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs#authentication"
282+
283+ func checkConfigurationValid (d * schema.ResourceData ) error {
284+ if inCluster () {
285+ log .Printf ("[DEBUG] Terraform appears to be running inside the Kubernetes cluster" )
286+ return nil
287+ }
288+
289+ if os .Getenv ("KUBE_CONFIG_PATHS" ) != "" {
290+ return nil
291+ }
292+
293+ atLeastOneOf := []string {
294+ "host" ,
295+ "config_path" ,
296+ "config_paths" ,
297+ "client_certificate" ,
298+ "token" ,
299+ "exec" ,
300+ }
301+ for _ , a := range atLeastOneOf {
302+ if _ , ok := d .GetOk (a ); ok {
303+ return nil
304+ }
305+ }
306+
307+ return fmt .Errorf (`provider not configured: you must configure a path to your kubeconfig
308+ or explicitly supply credentials via the provider block or environment variables.
309+
310+ See our documentation at: %s` , authDocumentationURL )
311+ }
312+
263313func providerConfigure (ctx context.Context , d * schema.ResourceData , terraformVersion string ) (interface {}, diag.Diagnostics ) {
264314 // Config initialization
265315 cfg , err := initializeConfiguration (d )
0 commit comments