knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/system/env.go (about)

     1  /*
     2  Copyright 2019 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package system
    18  
    19  import (
    20  	"cmp"
    21  	"fmt"
    22  	"os"
    23  )
    24  
    25  const (
    26  	// NamespaceEnvKey is the environment variable that specifies the system namespace.
    27  	NamespaceEnvKey = "SYSTEM_NAMESPACE"
    28  
    29  	// ResourceLabelEnvKey is the environment variable that specifies the system resource
    30  	// label. This label should be used to limit the number of configmaps that are watched
    31  	// in the system namespace.
    32  	ResourceLabelEnvKey = "SYSTEM_RESOURCE_LABEL"
    33  )
    34  
    35  // Namespace returns the name of the K8s namespace where our system components
    36  // run.
    37  func Namespace() string {
    38  	if ns := os.Getenv(NamespaceEnvKey); ns != "" {
    39  		return ns
    40  	}
    41  
    42  	panic(fmt.Sprintf(`The environment variable %q is not set
    43  
    44  If this is a process running on Kubernetes, then it should be using the downward
    45  API to initialize this variable via:
    46  
    47    env:
    48    - name: %s
    49      valueFrom:
    50        fieldRef:
    51          fieldPath: metadata.namespace
    52  
    53  If this is a Go unit test consuming system.Namespace() then it should add the
    54  following import:
    55  
    56  import (
    57  	_ "knative.dev/pkg/system/testing"
    58  )`, NamespaceEnvKey, NamespaceEnvKey))
    59  }
    60  
    61  // ResourceLabel returns the label key identifying K8s objects our system
    62  // components source their configuration from.
    63  func ResourceLabel() string {
    64  	return os.Getenv(ResourceLabelEnvKey)
    65  }
    66  
    67  // PodName will read various env vars to determine the name of the running
    68  // pod before falling back
    69  //
    70  // First it will read 'POD_NAME' this is expected to be populated using the
    71  // Kubernetes downward API.
    72  //
    73  //	env:
    74  //	- name: MY_POD_NAME
    75  //	  valueFrom:
    76  //	    fieldRef:
    77  //	      fieldPath: metadata.name
    78  //
    79  // As a fallback it will read HOSTNAME. This is undocumented
    80  // Kubernetes behaviour that podman, cri-o and containerd have
    81  // inherited from docker.
    82  //
    83  // If none of these env-vars is set PodName will return an
    84  // empty string
    85  func PodName() string {
    86  	return cmp.Or(
    87  		os.Getenv("POD_NAME"),
    88  		os.Getenv("HOSTNAME"),
    89  	)
    90  }