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

     1  /*
     2  Copyright 2020 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 webhook
    18  
    19  import (
    20  	"crypto/tls"
    21  	"fmt"
    22  	"os"
    23  	"strconv"
    24  )
    25  
    26  const (
    27  	portEnvKey = "WEBHOOK_PORT"
    28  
    29  	// Webhook is the name of the override key used inside of the logging config for Webhook Controller.
    30  	webhookNameEnvKey = "WEBHOOK_NAME"
    31  
    32  	secretNameEnvKey = "WEBHOOK_SECRET_NAME" //nolint:gosec // This is not a hardcoded credential
    33  
    34  	tlsMinVersionEnvKey = "WEBHOOK_TLS_MIN_VERSION"
    35  
    36  	disableNamespaceOwnershipEnvKey = "WEBHOOK_DISABLE_NAMESPACE_OWNERSHIP"
    37  )
    38  
    39  // PortFromEnv returns the webhook port set by portEnvKey, or default port if env var is not set.
    40  func PortFromEnv(defaultPort int) int {
    41  	if os.Getenv(portEnvKey) == "" {
    42  		return defaultPort
    43  	}
    44  	port, err := strconv.Atoi(os.Getenv(portEnvKey))
    45  	if err != nil {
    46  		panic(fmt.Sprintf("failed to convert the environment variable %q : %v", portEnvKey, err))
    47  	} else if port == 0 {
    48  		panic(fmt.Sprintf("the environment variable %q can't be zero", portEnvKey))
    49  	}
    50  	return port
    51  }
    52  
    53  func NameFromEnv() string {
    54  	if webhook := os.Getenv(webhookNameEnvKey); webhook != "" {
    55  		return webhook
    56  	}
    57  
    58  	panic(fmt.Sprintf(`The environment variable %[1]q is not set.
    59  This should be unique for the webhooks in a namespace
    60  If this is a process running on Kubernetes, then initialize this variable via:
    61    env:
    62    - name: %[1]s
    63      value: webhook
    64  `, webhookNameEnvKey))
    65  }
    66  
    67  func SecretNameFromEnv(defaultSecretName string) string {
    68  	secret := os.Getenv(secretNameEnvKey)
    69  	if secret == "" {
    70  		return defaultSecretName
    71  	}
    72  	return secret
    73  }
    74  
    75  // Deprecated: Use knative.dev/pkg/network/tls.DefaultConfigFromEnv instead.
    76  // TLS configuration is now read automatically inside webhook.New via the shared tls package.
    77  func TLSMinVersionFromEnv(defaultTLSMinVersion uint16) uint16 {
    78  	switch tlsMinVersion := os.Getenv(tlsMinVersionEnvKey); tlsMinVersion {
    79  	case "1.2":
    80  		return tls.VersionTLS12
    81  	case "1.3":
    82  		return tls.VersionTLS13
    83  	case "":
    84  		return defaultTLSMinVersion
    85  	default:
    86  		panic(fmt.Sprintf("the environment variable %q has to be either '1.2' or '1.3'", tlsMinVersionEnvKey))
    87  	}
    88  }
    89  
    90  func DisableNamespaceOwnershipFromEnv() *bool {
    91  	disableNamespaceOwnership := os.Getenv(disableNamespaceOwnershipEnvKey)
    92  	if disableNamespaceOwnership == "" {
    93  		return nil
    94  	}
    95  	disableNamespaceOwnershipBool, err := strconv.ParseBool(disableNamespaceOwnership)
    96  	if err != nil {
    97  		panic(fmt.Sprintf("failed to convert the environment variable %q : %v", disableNamespaceOwnershipEnvKey, err))
    98  	}
    99  	return &disableNamespaceOwnershipBool
   100  }