...

Source file src/kubevirt.io/client-go/kubecli/guestfs.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 2021 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  
    33  type GuestfsInfo struct {
    34  	Registry    string `json:"registry"`
    35  	Tag         string `json:"tag"`
    36  	Digest      string `json:"digest"`
    37  	ImagePrefix string `json:"imagePrefix"`
    38  	GsImage     string `json:"gsImage"`
    39  }
    40  
    41  func (k *kubevirt) GuestfsVersion() *GuestfsVersion {
    42  	return &GuestfsVersion{
    43  		restClient: k.restClient,
    44  		resource:   "guestfs",
    45  	}
    46  }
    47  
    48  type GuestfsVersion struct {
    49  	restClient *rest.RESTClient
    50  	resource   string
    51  }
    52  
    53  func (v *GuestfsVersion) Get() (*GuestfsInfo, error) {
    54  	var group metav1.APIGroup
    55  	// First, find out which version to query
    56  	uri := ApiGroupName
    57  	result := v.restClient.Get().AbsPath(uri).Do(context.Background())
    58  	if data, err := result.Raw(); err != nil {
    59  		connErr, isConnectionErr := err.(*url.Error)
    60  
    61  		if isConnectionErr {
    62  			return nil, connErr.Err
    63  		}
    64  
    65  		return nil, err
    66  	} else if err = json.Unmarshal(data, &group); err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	// Now, query the preferred version
    71  	uri = fmt.Sprintf("/apis/%s/guestfs", group.PreferredVersion.GroupVersion)
    72  	var info GuestfsInfo
    73  
    74  	result = v.restClient.Get().AbsPath(uri).Do(context.Background())
    75  	if data, err := result.Raw(); err != nil {
    76  		connErr, isConnectionErr := err.(*url.Error)
    77  
    78  		if isConnectionErr {
    79  			return nil, connErr.Err
    80  		}
    81  
    82  		return nil, err
    83  	} else if err = json.Unmarshal(data, &info); err != nil {
    84  		return nil, err
    85  	}
    86  	return &info, nil
    87  }
    88  

View as plain text