...

Source file src/kubevirt.io/client-go/kubecli/vm.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  	"net/url"
    25  
    26  	k8smetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/types"
    28  	"k8s.io/client-go/rest"
    29  
    30  	v1 "kubevirt.io/api/core/v1"
    31  	kvcorev1 "kubevirt.io/client-go/generated/kubevirt/clientset/versioned/typed/core/v1"
    32  )
    33  
    34  func (k *kubevirt) VirtualMachine(namespace string) VirtualMachineInterface {
    35  	return &vm{
    36  		VirtualMachineInterface: k.GeneratedKubeVirtClient().KubevirtV1().VirtualMachines(namespace),
    37  		restClient:              k.restClient,
    38  		config:                  k.config,
    39  		namespace:               namespace,
    40  		resource:                "virtualmachines",
    41  	}
    42  }
    43  
    44  type vm struct {
    45  	kvcorev1.VirtualMachineInterface
    46  	restClient *rest.RESTClient
    47  	config     *rest.Config
    48  	namespace  string
    49  	resource   string
    50  }
    51  
    52  // Create new VirtualMachine in the cluster to specified namespace
    53  func (v *vm) Create(ctx context.Context, vm *v1.VirtualMachine, opts k8smetav1.CreateOptions) (*v1.VirtualMachine, error) {
    54  	newVm, err := v.VirtualMachineInterface.Create(ctx, vm, opts)
    55  	newVm.SetGroupVersionKind(v1.VirtualMachineGroupVersionKind)
    56  
    57  	return newVm, err
    58  }
    59  
    60  // Get the Virtual machine from the cluster by its name and namespace
    61  func (v *vm) Get(ctx context.Context, name string, options k8smetav1.GetOptions) (*v1.VirtualMachine, error) {
    62  	newVm, err := v.VirtualMachineInterface.Get(ctx, name, options)
    63  	newVm.SetGroupVersionKind(v1.VirtualMachineGroupVersionKind)
    64  
    65  	return newVm, err
    66  }
    67  
    68  // Update the VirtualMachine instance in the cluster in given namespace
    69  func (v *vm) Update(ctx context.Context, vm *v1.VirtualMachine, opts k8smetav1.UpdateOptions) (*v1.VirtualMachine, error) {
    70  	updatedVm, err := v.VirtualMachineInterface.Update(ctx, vm, opts)
    71  	updatedVm.SetGroupVersionKind(v1.VirtualMachineGroupVersionKind)
    72  
    73  	return updatedVm, err
    74  }
    75  
    76  // Delete the defined VirtualMachine in the cluster in defined namespace
    77  func (v *vm) Delete(ctx context.Context, name string, options k8smetav1.DeleteOptions) error {
    78  	return v.VirtualMachineInterface.Delete(ctx, name, options)
    79  }
    80  
    81  // List all VirtualMachines in given namespace
    82  func (v *vm) List(ctx context.Context, options k8smetav1.ListOptions) (*v1.VirtualMachineList, error) {
    83  	newVmList, err := v.VirtualMachineInterface.List(ctx, options)
    84  	for i := range newVmList.Items {
    85  		newVmList.Items[i].SetGroupVersionKind(v1.VirtualMachineGroupVersionKind)
    86  	}
    87  
    88  	return newVmList, err
    89  }
    90  
    91  func (v *vm) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, patchOptions k8smetav1.PatchOptions, subresources ...string) (result *v1.VirtualMachine, err error) {
    92  	return v.VirtualMachineInterface.Patch(ctx, name, pt, data, patchOptions, subresources...)
    93  }
    94  
    95  func (v *vm) UpdateStatus(ctx context.Context, vmi *v1.VirtualMachine, opts k8smetav1.UpdateOptions) (result *v1.VirtualMachine, err error) {
    96  	result, err = v.VirtualMachineInterface.UpdateStatus(ctx, vmi, opts)
    97  	result.SetGroupVersionKind(v1.VirtualMachineGroupVersionKind)
    98  	return
    99  }
   100  
   101  func (v *vm) PortForward(name string, port int, protocol string) (kvcorev1.StreamInterface, error) {
   102  	return kvcorev1.AsyncSubresourceHelper(v.config, v.resource, v.namespace, name, buildPortForwardResourcePath(port, protocol), url.Values{})
   103  }
   104  

View as plain text