github.com/jmrodri/operator-sdk@v0.5.0/pkg/k8sutil/k8sutil.go (about)

     1  // Copyright 2018 The Operator-SDK Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package k8sutil
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"os"
    21  	"strings"
    22  
    23  	discovery "k8s.io/client-go/discovery"
    24  	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
    25  )
    26  
    27  var log = logf.Log.WithName("k8sutil")
    28  
    29  // GetWatchNamespace returns the namespace the operator should be watching for changes
    30  func GetWatchNamespace() (string, error) {
    31  	ns, found := os.LookupEnv(WatchNamespaceEnvVar)
    32  	if !found {
    33  		return "", fmt.Errorf("%s must be set", WatchNamespaceEnvVar)
    34  	}
    35  	return ns, nil
    36  }
    37  
    38  // errNoNS indicates that a namespace could not be found for the current
    39  // environment
    40  var ErrNoNamespace = fmt.Errorf("namespace not found for current environment")
    41  
    42  // GetOperatorNamespace returns the namespace the operator should be running in.
    43  func GetOperatorNamespace() (string, error) {
    44  	nsBytes, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
    45  	if err != nil {
    46  		if os.IsNotExist(err) {
    47  			return "", ErrNoNamespace
    48  		}
    49  		return "", err
    50  	}
    51  	ns := strings.TrimSpace(string(nsBytes))
    52  	log.V(1).Info("Found namespace", "Namespace", ns)
    53  	return ns, nil
    54  }
    55  
    56  // GetOperatorName return the operator name
    57  func GetOperatorName() (string, error) {
    58  	operatorName, found := os.LookupEnv(OperatorNameEnvVar)
    59  	if !found {
    60  		return "", fmt.Errorf("%s must be set", OperatorNameEnvVar)
    61  	}
    62  	if len(operatorName) == 0 {
    63  		return "", fmt.Errorf("%s must not be empty", OperatorNameEnvVar)
    64  	}
    65  	return operatorName, nil
    66  }
    67  
    68  // ResourceExists returns true if the given resource kind exists
    69  // in the given api groupversion
    70  func ResourceExists(dc discovery.DiscoveryInterface, apiGroupVersion, kind string) (bool, error) {
    71  	apiLists, err := dc.ServerResources()
    72  	if err != nil {
    73  		return false, err
    74  	}
    75  	for _, apiList := range apiLists {
    76  		if apiList.GroupVersion == apiGroupVersion {
    77  			for _, r := range apiList.APIResources {
    78  				if r.Kind == kind {
    79  					return true, nil
    80  				}
    81  			}
    82  		}
    83  	}
    84  	return false, nil
    85  }