...

Source file src/kubevirt.io/client-go/kubecli/version.go

Documentation: kubevirt.io/client-go/kubecli

     1  /*
     2   * This file is part of the KubeVirt project
     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   * Copyright 2018 Red Hat, Inc.
    17   *
    18   */
    19  
    20  package kubecli
    21  
    22  import (
    23  	"context"
    24  
    25  	"encoding/json"
    26  	"fmt"
    27  	"net/url"
    28  
    29  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    30  	"k8s.io/client-go/rest"
    31  
    32  	v1 "kubevirt.io/api/core/v1"
    33  
    34  	"kubevirt.io/client-go/version"
    35  )
    36  
    37  const (
    38  	ApiGroupName = "/apis/" + v1.SubresourceGroupName
    39  )
    40  
    41  func (k *kubevirt) ServerVersion() ServerVersionInterface {
    42  	return &ServerVersion{
    43  		restClient: k.restClient,
    44  		resource:   "version",
    45  	}
    46  }
    47  
    48  type ServerVersion struct {
    49  	restClient *rest.RESTClient
    50  	resource   string
    51  }
    52  
    53  func (v *ServerVersion) Get() (*version.Info, error) {
    54  
    55  	var group metav1.APIGroup
    56  	// First, find out which version to query
    57  	uri := ApiGroupName
    58  	result := v.restClient.Get().AbsPath(uri).Do(context.Background())
    59  	if data, err := result.Raw(); err != nil {
    60  		connErr, isConnectionErr := err.(*url.Error)
    61  
    62  		if isConnectionErr {
    63  			return nil, connErr.Err
    64  		}
    65  
    66  		return nil, err
    67  	} else if err = json.Unmarshal(data, &group); err != nil {
    68  		return nil, err
    69  	}
    70  
    71  	// Now, query the preferred version
    72  	uri = fmt.Sprintf("/apis/%s/version", group.PreferredVersion.GroupVersion)
    73  	var serverInfo version.Info
    74  
    75  	result = v.restClient.Get().AbsPath(uri).Do(context.Background())
    76  	if data, err := result.Raw(); err != nil {
    77  		connErr, isConnectionErr := err.(*url.Error)
    78  
    79  		if isConnectionErr {
    80  			return nil, connErr.Err
    81  		}
    82  
    83  		return nil, err
    84  	} else if err = json.Unmarshal(data, &serverInfo); err != nil {
    85  		return nil, err
    86  	}
    87  	return &serverInfo, nil
    88  }
    89  

View as plain text