github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/factory/setk8sdefaults.go (about)

     1  // Copyright 2019 The Kubernetes Authors.
     2  // Copyright 2019-2021 The Inspektor Gadget 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  package factory
    17  
    18  import (
    19  	"k8s.io/apimachinery/pkg/runtime/schema"
    20  	"k8s.io/apimachinery/pkg/runtime/serializer"
    21  	"k8s.io/client-go/kubernetes/scheme"
    22  	"k8s.io/client-go/rest"
    23  )
    24  
    25  // SetKubernetesDefaults sets default values on the provided client config for accessing the
    26  // Kubernetes API or returns an error if any of the defaults are impossible or invalid.
    27  // TODO this isn't what we want.  Each clientset should be setting defaults as it sees fit.
    28  func SetKubernetesDefaults(config *rest.Config) error {
    29  	// TODO remove this hack.  This is allowing the GetOptions to be serialized.
    30  	config.GroupVersion = &schema.GroupVersion{Group: "", Version: "v1"}
    31  
    32  	if config.APIPath == "" {
    33  		config.APIPath = "/api"
    34  	}
    35  	if config.NegotiatedSerializer == nil {
    36  		// This codec factory ensures the resources are not converted. Therefore, resources
    37  		// will not be round-tripped through internal versions. Defaulting does not happen
    38  		// on the client.
    39  		config.NegotiatedSerializer = &serializer.WithoutConversionCodecFactory{CodecFactory: scheme.Codecs}
    40  	}
    41  	return rest.SetKubernetesDefaults(config)
    42  }