github.com/argoproj-labs/argocd-operator@v0.10.0/controllers/argoutil/api.go (about)

     1  // Copyright 2019 ArgoCD Operator Developers
     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 argoutil
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"k8s.io/apimachinery/pkg/runtime/schema"
    21  	"k8s.io/client-go/discovery"
    22  	"k8s.io/client-go/kubernetes"
    23  	"sigs.k8s.io/controller-runtime/pkg/client/config"
    24  )
    25  
    26  // VerifyAPI will verify that the given group/version is present in the cluster.
    27  func VerifyAPI(group string, version string) (bool, error) {
    28  	cfg, err := config.GetConfig()
    29  	if err != nil {
    30  		log.Error(err, "unable to get k8s config")
    31  		return false, err
    32  	}
    33  
    34  	k8s, err := kubernetes.NewForConfig(cfg)
    35  	if err != nil {
    36  		log.Error(err, "unable to create k8s client")
    37  		return false, err
    38  	}
    39  
    40  	gv := schema.GroupVersion{
    41  		Group:   group,
    42  		Version: version,
    43  	}
    44  
    45  	if err = discovery.ServerSupportsVersion(k8s, gv); err != nil {
    46  		// error, API not available
    47  		return false, nil
    48  	}
    49  
    50  	log.Info(fmt.Sprintf("%s/%s API verified", group, version))
    51  	return true, nil
    52  }