github.com/kubeflow/training-operator@v1.7.0/pkg/util/k8sutil/client.go (about)

     1  /*
     2  Copyright 2023 The Kubeflow Authors.
     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  
    17  package k8sutil
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"net/http"
    23  
    24  	"github.com/kubeflow/training-operator/pkg/util"
    25  	metav1unstructured "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	"k8s.io/apimachinery/pkg/runtime/schema"
    28  	"k8s.io/apimachinery/pkg/runtime/serializer"
    29  	"k8s.io/client-go/kubernetes/scheme"
    30  	"k8s.io/client-go/rest"
    31  )
    32  
    33  // CRDRestClient defines an interface for working with CRDs using the REST client.
    34  // In most cases we want to use the auto-generated clientset for specific CRDs.
    35  // The only exception is when the CRD spec is invalid and we can't parse the type into the corresponding
    36  // go struct.
    37  type CRDClient interface {
    38  	// Update a Job.
    39  	Update(obj *metav1unstructured.Unstructured) error
    40  }
    41  
    42  // CRDRestClient uses the Kubernetes rest interface to talk to the CRD.
    43  type CRDRestClient struct {
    44  	restcli *rest.RESTClient
    45  }
    46  
    47  func NewCRDRestClient(version *schema.GroupVersion) (*CRDRestClient, error) {
    48  	config, err := GetClusterConfig()
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	config.GroupVersion = version
    53  	config.APIPath = "/apis"
    54  	config.ContentType = runtime.ContentTypeJSON
    55  	config.NegotiatedSerializer = serializer.WithoutConversionCodecFactory{CodecFactory: scheme.Codecs}
    56  
    57  	restcli, err := rest.RESTClientFor(config)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	cli := &CRDRestClient{
    63  		restcli: restcli,
    64  	}
    65  	return cli, nil
    66  }
    67  
    68  // HttpClient returns the http client used.
    69  func (c *CRDRestClient) Client() *http.Client {
    70  	return c.restcli.Client
    71  }
    72  
    73  func (c *CRDRestClient) Update(obj *metav1unstructured.Unstructured, plural string) error {
    74  	logger := util.LoggerForUnstructured(obj, obj.GetKind())
    75  	// TODO(jlewi): Can we just call obj.GetKind() to get the kind? I think that will return the singular
    76  	// not plural will that work?
    77  	if plural == "" {
    78  		logger.Errorf("Could not issue update because plural not set.")
    79  		return fmt.Errorf("plural must be set")
    80  	}
    81  	r := c.restcli.Put().Resource(plural).Namespace(obj.GetNamespace()).Name(obj.GetName()).Body(obj)
    82  	_, err := r.DoRaw(context.TODO())
    83  	if err != nil {
    84  		logger.Errorf("Could not issue update using URL: %v; error; %v", r.URL().String(), err)
    85  	}
    86  	return err
    87  }
    88  
    89  func (c *CRDRestClient) UpdateStatus(obj *metav1unstructured.Unstructured, plural string) error {
    90  	logger := util.LoggerForUnstructured(obj, obj.GetKind())
    91  	if plural == "" {
    92  		logger.Errorf("Could not issue update because plural not set.")
    93  		return fmt.Errorf("plural must be set")
    94  	}
    95  	r := c.restcli.Put().Resource(plural).Namespace(obj.GetNamespace()).Name(obj.GetName()).SubResource("status").Body(obj)
    96  	_, err := r.DoRaw(context.TODO())
    97  	if err != nil {
    98  		logger.Errorf("Could not issue update using URL: %v; error; %v", r.URL().String(), err)
    99  	}
   100  	return err
   101  }