github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/env/env.go (about)

     1  /*
     2  Copyright 2020 The OpenEBS 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 env
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"strings"
    23  
    24  	v1 "k8s.io/api/core/v1"
    25  )
    26  
    27  const (
    28  	// IMAGE_PULL_SECRETS_ENV is the environment variable used to pass the image pull secrets
    29  	IMAGE_PULL_SECRETS_ENV = "OPENEBS_IO_IMAGE_PULL_SECRETS"
    30  
    31  	// WATCH_NAMESPACE is the namespace to watch for resources
    32  	WATCH_NAMESPACE_ENV = "WATCH_NAMESPACE"
    33  )
    34  
    35  // GetOpenEBSImagePullSecrets is used to get the image pull secrets from the environment variable
    36  func GetOpenEBSImagePullSecrets() []v1.LocalObjectReference {
    37  	secrets := strings.TrimSpace(os.Getenv(IMAGE_PULL_SECRETS_ENV))
    38  
    39  	list := make([]v1.LocalObjectReference, 0)
    40  
    41  	if len(secrets) == 0 {
    42  		return list
    43  	}
    44  	arr := strings.Split(secrets, ",")
    45  	for _, item := range arr {
    46  		if len(item) > 0 {
    47  			l := v1.LocalObjectReference{Name: strings.TrimSpace(item)}
    48  			list = append(list, l)
    49  		}
    50  	}
    51  	return list
    52  }
    53  
    54  // GetWatchNamespace gets the namespace to watch for resources
    55  func GetWatchNamespace() (string, error) {
    56  	ns := strings.TrimSpace(os.Getenv(WATCH_NAMESPACE_ENV))
    57  	if len(ns) == 0 {
    58  		return "", fmt.Errorf("WATCH_NAMESPACE env not set")
    59  	}
    60  	return ns, nil
    61  }